fzaninotto/Faker

Undefined property with phpstan

Opened this issue · 4 comments

Access to an undefined property Faker\Generator::$tollFreePhoneNumber.

@NekouSama

The challenge here is that Faker\Generator provides quite a lot of class-level annotations for formatters (such as the tollFreeNumber() provider), but the formatter is not implemented for every locale.

Running

$ git grep -iw tollFreeNumber

on current master yields

readme.md:931:echo $faker->tollFreeNumber; // "0800 123 456"
readme.md:991:echo $faker->tollFreeNumber; // 0800 555 5555
src/Faker/Provider/el_GR/PhoneNumber.php:81:    public static function tollFreeNumber()
src/Faker/Provider/en_NZ/PhoneNumber.php:71:    public static function tollFreeNumber()
src/Faker/Provider/en_ZA/PhoneNumber.php:94:    public function tollFreeNumber()
test/Faker/Provider/en_ZA/PhoneNumberTest.php:39:            $number = $this->faker->tollFreeNumber;

If we added this to Faker\Generator, and someone created an instance of Faker\Generator for, let's say, the locale de_DE, then an attempt to use the tollFreeNumber formatter would fail.

When I create a file containing this code in ~/Sites/fzaninotto/faker/.notes/de.php

<?php

require_once __DIR__ . '/../vendor/autoload.php';

$faker = \Faker\Factory::create('de_DE');

var_dump($faker->tollFreeNumber);

and run it, it fails with

PHP Fatal error:  Uncaught InvalidArgumentException: Unknown formatter "tollFreeNumber" in /Users/am/Sites/fzaninotto/Faker/src/Faker/Generator.php:244
Stack trace:
#0 /Users/am/Sites/fzaninotto/Faker/src/Faker/Generator.php(224): Faker\Generator->getFormatter('tollFreeNumber')
#1 /Users/am/Sites/fzaninotto/Faker/src/Faker/Generator.php(270): Faker\Generator->format('tollFreeNumber')
#2 /Users/am/Sites/fzaninotto/Faker/.notes/de.php(7): Faker\Generator->__get('tollFreeNumber')
#3 {main}
  thrown in /Users/am/Sites/fzaninotto/Faker/src/Faker/Generator.php on line 244

Fatal error: Uncaught InvalidArgumentException: Unknown formatter "tollFreeNumber" in /Users/am/Sites/fzaninotto/Faker/src/Faker/Generator.php:244
Stack trace:
#0 /Users/am/Sites/fzaninotto/Faker/src/Faker/Generator.php(224): Faker\Generator->getFormatter('tollFreeNumber')
#1 /Users/am/Sites/fzaninotto/Faker/src/Faker/Generator.php(270): Faker\Generator->format('tollFreeNumber')
#2 /Users/am/Sites/fzaninotto/Faker/.notes/de.php(7): Faker\Generator->__get('tollFreeNumber')
#3 {main}
  thrown in /Users/am/Sites/fzaninotto/Faker/src/Faker/Generator.php on line 244

I can only think of a phpstan extension to solve this 🤔
Or you have to ignore those errors, which seems not a fine way to go.

Well you will never close this gap due the fact it is not implemented for all the locales so i guess ignoring it would be the best solution for now. But we should consider these things for future versions i guess