From 1d248a4d7af4c71aff2455a967fa1424f5fa5824 Mon Sep 17 00:00:00 2001 From: Olivier Dolbeau Date: Thu, 30 Nov 2023 01:36:12 +0100 Subject: [PATCH] Bump phpstan level --- Makefile | 4 +- phpstan-baseline.neon | 6 + phpstan.neon | 10 +- .../Compiler/FormTwigTemplateCompilerPass.php | 11 +- .../PhoneNumberToArrayTransformer.php | 20 +- .../PhoneNumberToStringTransformer.php | 29 +-- src/Form/Type/PhoneNumberType.php | 12 +- .../Normalizer/PhoneNumberNormalizer.php | 45 ++--- src/Templating/Helper/PhoneNumberHelper.php | 14 +- src/Validator/Constraints/PhoneNumber.php | 32 +++- .../Constraints/PhoneNumberValidator.php | 25 +-- .../FormTwigTemplateCompilerPassTest.php | 12 +- .../DependencyInjection/ConfigurationTest.php | 10 +- .../MisdPhoneNumberExtensionTest.php | 15 +- .../DBAL/Types/PhoneNumberTypeTest.php | 43 ++--- .../PhoneNumberToArrayTransformerTest.php | 172 +++++++++-------- .../PhoneNumberToStringTransformerTest.php | 55 +++--- tests/Form/Type/PhoneNumberTypeTest.php | 180 +++++++++--------- .../Normalizer/PhoneNumberNormalizerTest.php | 12 +- .../Helper/PhoneNumberHelperTest.php | 36 ++-- .../Validator/Constraints/PhoneNumberTest.php | 46 ++--- .../Constraints/PhoneNumberValidatorTest.php | 132 ++++++------- tools/phpstan/composer.json | 3 +- 23 files changed, 462 insertions(+), 462 deletions(-) create mode 100644 phpstan-baseline.neon diff --git a/Makefile b/Makefile index bac31515..e8e19de5 100644 --- a/Makefile +++ b/Makefile @@ -14,11 +14,11 @@ help: cs-lint: ## Verify check styles composer install --working-dir=tools/php-cs-fixer - tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --dry-run --diff --config=.php_cs.dist.php + tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --dry-run --diff cs-fix: ## Apply Check styles composer install --working-dir=tools/php-cs-fixer - tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --diff --config=.php_cs.dist.php + tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --diff phpstan: ## Run PHPStan composer install --working-dir=tools/phpstan diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon new file mode 100644 index 00000000..b0ad9c1d --- /dev/null +++ b/phpstan-baseline.neon @@ -0,0 +1,6 @@ +parameters: + ignoreErrors: + - + message: "#^Parameter \\#2 \\$regionCallingFrom of method libphonenumber\\\\PhoneNumberUtil\\:\\:formatOutOfCountryCallingNumber\\(\\) expects string, string\\|null given\\.$#" + count: 1 + path: src/Templating/Helper/PhoneNumberHelper.php diff --git a/phpstan.neon b/phpstan.neon index 85410572..c17ff6e2 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,6 +1,12 @@ +includes: + - phpstan-baseline.neon + - tools/phpstan/vendor/jangregor/phpstan-prophecy/extension.neon + parameters: - level: 1 + level: 8 paths: - src - tests - inferPrivatePropertyTypeFromConstructor: true + + ignoreErrors: + - '#Call to an undefined method Symfony\\Component\\Config\\Definition\\Builder\\NodeDefinition::children\(\).#' diff --git a/src/DependencyInjection/Compiler/FormTwigTemplateCompilerPass.php b/src/DependencyInjection/Compiler/FormTwigTemplateCompilerPass.php index a35323e2..1db1b024 100644 --- a/src/DependencyInjection/Compiler/FormTwigTemplateCompilerPass.php +++ b/src/DependencyInjection/Compiler/FormTwigTemplateCompilerPass.php @@ -19,10 +19,10 @@ */ class FormTwigTemplateCompilerPass implements CompilerPassInterface { - private $phoneNumberLayout = '@MisdPhoneNumber/Form/phone_number.html.twig'; - private $phoneNumberBootstrapLayout = '@MisdPhoneNumber/Form/phone_number_bootstrap.html.twig'; - private $phoneNumberBootstrap4Layout = '@MisdPhoneNumber/Form/phone_number_bootstrap_4.html.twig'; - private $phoneNumberBootstrap5Layout = '@MisdPhoneNumber/Form/phone_number_bootstrap_5.html.twig'; + private string $phoneNumberLayout = '@MisdPhoneNumber/Form/phone_number.html.twig'; + private string $phoneNumberBootstrapLayout = '@MisdPhoneNumber/Form/phone_number_bootstrap.html.twig'; + private string $phoneNumberBootstrap4Layout = '@MisdPhoneNumber/Form/phone_number_bootstrap_4.html.twig'; + private string $phoneNumberBootstrap5Layout = '@MisdPhoneNumber/Form/phone_number_bootstrap_5.html.twig'; public function process(ContainerBuilder $container): void { @@ -30,6 +30,9 @@ public function process(ContainerBuilder $container): void return; } + /** + * @var string[] $parameter + */ $parameter = $container->getParameter('twig.form.resources'); if (\in_array($this->phoneNumberLayout, $parameter)) { diff --git a/src/Form/DataTransformer/PhoneNumberToArrayTransformer.php b/src/Form/DataTransformer/PhoneNumberToArrayTransformer.php index 6c73586c..58ae71d9 100644 --- a/src/Form/DataTransformer/PhoneNumberToArrayTransformer.php +++ b/src/Form/DataTransformer/PhoneNumberToArrayTransformer.php @@ -19,30 +19,33 @@ use Symfony\Component\Form\Exception\TransformationFailedException; /** - * Phone number to array transformer. + * @implements DataTransformerInterface */ class PhoneNumberToArrayTransformer implements DataTransformerInterface { /** - * @var array + * @var string[] */ - private $countryChoices; + private array $countryChoices; /** - * Constructor. + * @param string[] $countryChoices */ public function __construct(array $countryChoices) { $this->countryChoices = $countryChoices; } - public function transform($value): array + /** + * @return array{country: string, number: string} + */ + public function transform(mixed $value): array { if (null === $value) { return ['country' => '', 'number' => '']; } - if (false === $value instanceof PhoneNumber) { + if (!$value instanceof PhoneNumber) { throw new TransformationFailedException('Expected a \libphonenumber\PhoneNumber.'); } @@ -53,12 +56,12 @@ public function transform($value): array } return [ - 'country' => $util->getRegionCodeForNumber($value), + 'country' => (string) $util->getRegionCodeForNumber($value), 'number' => $util->format($value, PhoneNumberFormat::NATIONAL), ]; } - public function reverseTransform($value): ?PhoneNumber + public function reverseTransform(mixed $value): ?PhoneNumber { if (!$value) { return null; @@ -68,6 +71,7 @@ public function reverseTransform($value): ?PhoneNumber throw new TransformationFailedException('Expected an array.'); } + /* @phpstan-ignore-next-line */ if ('' === trim($value['number'] ?? '')) { return null; } diff --git a/src/Form/DataTransformer/PhoneNumberToStringTransformer.php b/src/Form/DataTransformer/PhoneNumberToStringTransformer.php index 44fbc09e..13ce7059 100644 --- a/src/Form/DataTransformer/PhoneNumberToStringTransformer.php +++ b/src/Form/DataTransformer/PhoneNumberToStringTransformer.php @@ -19,33 +19,16 @@ use Symfony\Component\Form\Exception\TransformationFailedException; /** - * Phone number to string transformer. + * @implements DataTransformerInterface */ class PhoneNumberToStringTransformer implements DataTransformerInterface { - /** - * Default region code. - * - * @var string - */ - private $defaultRegion; + private string $defaultRegion; + private int $format; - /** - * Display format. - * - * @var int - */ - private $format; - - /** - * Constructor. - * - * @param string $defaultRegion default region code - * @param int $format display format - */ public function __construct( - $defaultRegion = PhoneNumberUtil::UNKNOWN_REGION, - $format = PhoneNumberFormat::INTERNATIONAL + string $defaultRegion = PhoneNumberUtil::UNKNOWN_REGION, + int $format = PhoneNumberFormat::INTERNATIONAL ) { $this->defaultRegion = $defaultRegion; $this->format = $format; @@ -57,7 +40,7 @@ public function transform($value): string return ''; } - if (false === $value instanceof PhoneNumber) { + if (!$value instanceof PhoneNumber) { throw new TransformationFailedException('Expected a \libphonenumber\PhoneNumber.'); } diff --git a/src/Form/Type/PhoneNumberType.php b/src/Form/Type/PhoneNumberType.php index aa498d8a..42bab611 100644 --- a/src/Form/Type/PhoneNumberType.php +++ b/src/Form/Type/PhoneNumberType.php @@ -67,10 +67,20 @@ public function buildForm(FormBuilderInterface $builder, array $options): void continue; } - $label = $this->formatDisplayChoice($options['country_display_type'], $intlCountries[$regionCode], $regionCode, $countryCode, $options['country_display_emoji_flag']); + $label = $this->formatDisplayChoice( + $options['country_display_type'], + $intlCountries[$regionCode], + (string) $regionCode, + (string) $countryCode, + $options['country_display_emoji_flag'] + ); + $countryChoices[$label] = $regionCode; } + /** + * @var string[] $transformerChoices + */ $transformerChoices = array_values($countryChoices); $countryOptions = array_replace([ diff --git a/src/Serializer/Normalizer/PhoneNumberNormalizer.php b/src/Serializer/Normalizer/PhoneNumberNormalizer.php index 2c79e6de..a251dd71 100644 --- a/src/Serializer/Normalizer/PhoneNumberNormalizer.php +++ b/src/Serializer/Normalizer/PhoneNumberNormalizer.php @@ -25,35 +25,16 @@ */ class PhoneNumberNormalizer implements NormalizerInterface, DenormalizerInterface { - /** - * Region code. - * - * @var string - */ - private $region; - - /** - * Display format. - * - * @var int - */ - private $format; - - /** - * Display format. - * - * @var PhoneNumberUtil - */ - private $phoneNumberUtil; + private PhoneNumberUtil $phoneNumberUtil; + private string $region; + private int $format; /** - * Constructor. - * * @param PhoneNumberUtil $phoneNumberUtil phone number utility * @param string $region region code * @param int $format display format */ - public function __construct(PhoneNumberUtil $phoneNumberUtil, $region = PhoneNumberUtil::UNKNOWN_REGION, $format = PhoneNumberFormat::E164) + public function __construct(PhoneNumberUtil $phoneNumberUtil, string $region = PhoneNumberUtil::UNKNOWN_REGION, int $format = PhoneNumberFormat::E164) { $this->phoneNumberUtil = $phoneNumberUtil; $this->region = $region; @@ -61,22 +42,29 @@ public function __construct(PhoneNumberUtil $phoneNumberUtil, $region = PhoneNum } /** + * @param array $context + * * @throws InvalidArgumentException */ - public function normalize($object, $format = null, array $context = []): string + public function normalize(mixed $object, string $format = null, array $context = []): string { return $this->phoneNumberUtil->format($object, $this->format); } - public function supportsNormalization($data, $format = null, array $context = []): bool + /** + * @param array $context + */ + public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool { return $data instanceof PhoneNumber; } /** + * @param array $context + * * @throws UnexpectedValueException */ - public function denormalize($data, $type, $format = null, array $context = []): ?PhoneNumber + public function denormalize(mixed $data, string $type, string $format = null, array $context = []): ?PhoneNumber { if (null === $data) { return null; @@ -89,7 +77,10 @@ public function denormalize($data, $type, $format = null, array $context = []): } } - public function supportsDenormalization($data, $type, $format = null, array $context = []): bool + /** + * @param array $context + */ + public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool { return PhoneNumber::class === $type && \is_string($data); } diff --git a/src/Templating/Helper/PhoneNumberHelper.php b/src/Templating/Helper/PhoneNumberHelper.php index d9cc5e52..67f05bd4 100644 --- a/src/Templating/Helper/PhoneNumberHelper.php +++ b/src/Templating/Helper/PhoneNumberHelper.php @@ -22,18 +22,8 @@ */ class PhoneNumberHelper { - /** - * Phone number utility. - * - * @var PhoneNumberUtil - */ - protected $phoneNumberUtil; + protected PhoneNumberUtil $phoneNumberUtil; - /** - * Constructor. - * - * @param PhoneNumberUtil $phoneNumberUtil phone number utility - */ public function __construct(PhoneNumberUtil $phoneNumberUtil) { $this->phoneNumberUtil = $phoneNumberUtil; @@ -62,7 +52,7 @@ public function format(PhoneNumber|string $phoneNumber, string|int $format = Pho * @param PhoneNumber|string $phoneNumber phone number * @param string|null $regionCode The ISO 3166-1 alpha-2 country code */ - public function formatOutOfCountryCallingNumber($phoneNumber, $regionCode): string + public function formatOutOfCountryCallingNumber(PhoneNumber|string $phoneNumber, ?string $regionCode): string { $phoneNumber = $this->getPhoneNumber($phoneNumber); diff --git a/src/Validator/Constraints/PhoneNumber.php b/src/Validator/Constraints/PhoneNumber.php index c8bf9df9..3019d2ea 100644 --- a/src/Validator/Constraints/PhoneNumber.php +++ b/src/Validator/Constraints/PhoneNumber.php @@ -44,22 +44,28 @@ class PhoneNumber extends Constraint /** * @deprecated since PhoneNumberBundle 3.6, use const ERROR_NAMES instead + * + * @var string[] */ - protected static $errorNames = self::ERROR_NAMES; + protected static array $errorNames = self::ERROR_NAMES; - public $message; - public $type = self::ANY; - public $defaultRegion; - public $regionPath; - public $format; + public ?string $message = null; + /** + * @var string|string[] + */ + public string|array $type = self::ANY; + public ?string $defaultRegion = null; + public ?string $regionPath = null; + public ?int $format = null; /** - * @param int|array|null $format Specify the format (\libphonenumber\PhoneNumberFormat::*) - * or options (an associative array) - * @param string|array|null $type + * @param int|array|null $format Specify the format (\libphonenumber\PhoneNumberFormat::*) + * or options (an associative array) + * @param string|string[]|null $type + * @param array $options */ #[HasNamedArguments] - public function __construct($format = null, $type = null, string $defaultRegion = null, string $regionPath = null, string $message = null, array $groups = null, $payload = null, array $options = []) + public function __construct(int|array $format = null, string|array $type = null, string $defaultRegion = null, string $regionPath = null, string $message = null, array $groups = null, $payload = null, array $options = []) { if (\is_array($format)) { @trigger_error('Usage of the argument $format to specify options is deprecated and will be removed in 4.0. Use "$option" argument instead.', \E_USER_DEPRECATED); @@ -89,6 +95,9 @@ public function getType(): ?string return reset($types); } + /** + * @return string[] + */ public function getTypes(): array { if (\is_array($this->type)) { @@ -114,6 +123,9 @@ public function getMessage(): string return 'This value is not a valid phone number.'; } + /** + * @return string[] + */ public function getTypeNames(): array { $types = \is_array($this->type) ? $this->type : [$this->type]; diff --git a/src/Validator/Constraints/PhoneNumberValidator.php b/src/Validator/Constraints/PhoneNumberValidator.php index 1a5beebf..27dbfea1 100644 --- a/src/Validator/Constraints/PhoneNumberValidator.php +++ b/src/Validator/Constraints/PhoneNumberValidator.php @@ -31,25 +31,10 @@ */ class PhoneNumberValidator extends ConstraintValidator { - /** - * @var PhoneNumberUtil - */ - private $phoneUtil; - - /** - * @var string - */ - private $defaultRegion; - - /** - * @var PropertyAccessorInterface - */ - private $propertyAccessor; - - /** - * @var int - */ - private $format; + private PhoneNumberUtil $phoneUtil; + private string $defaultRegion; + private ?PropertyAccessorInterface $propertyAccessor = null; + private int $format; public function __construct(PhoneNumberUtil $phoneUtil = null, string $defaultRegion = PhoneNumberUtil::UNKNOWN_REGION, int $format = PhoneNumberFormat::INTERNATIONAL) { @@ -58,7 +43,7 @@ public function __construct(PhoneNumberUtil $phoneUtil = null, string $defaultRe $this->format = $format; } - public function validate($value, Constraint $constraint): void + public function validate(mixed $value, Constraint $constraint): void { if (!$constraint instanceof PhoneNumberConstraint) { return; diff --git a/tests/DependencyInjection/Compiler/FormTwigTemplateCompilerPassTest.php b/tests/DependencyInjection/Compiler/FormTwigTemplateCompilerPassTest.php index 1a6608f9..bbc9d181 100644 --- a/tests/DependencyInjection/Compiler/FormTwigTemplateCompilerPassTest.php +++ b/tests/DependencyInjection/Compiler/FormTwigTemplateCompilerPassTest.php @@ -18,7 +18,7 @@ public function testItDoesNothingWhenWithIsNotEnabled(): void $this->assertFalse($container->hasParameter('twig.form.resources')); } - public function testItDoesNothingIfThePhoneNumberTwigIsAlreadyConfigured() + public function testItDoesNothingIfThePhoneNumberTwigIsAlreadyConfigured(): void { $subject = new FormTwigTemplateCompilerPass(); $container = new ContainerBuilder(); @@ -36,7 +36,7 @@ public function testItDoesNothingIfThePhoneNumberTwigIsAlreadyConfigured() /** * @dataProvider themesProvider */ - public function testItAddsPhoneTemplatesAccordingToSymfonyTemplates($sfTemplate, $phoneTemplate) + public function testItAddsPhoneTemplatesAccordingToSymfonyTemplates(string $sfTemplate, string $phoneTemplate): void { $subject = new FormTwigTemplateCompilerPass(); $container = new ContainerBuilder(); @@ -45,9 +45,15 @@ public function testItAddsPhoneTemplatesAccordingToSymfonyTemplates($sfTemplate, $subject->process($container); - $this->assertTrue(\in_array($phoneTemplate, $container->getParameter('twig.form.resources'), true)); + $resources = $container->getParameter('twig.form.resources'); + + $this->assertTrue(\is_array($resources)); + $this->assertTrue(\in_array($phoneTemplate, $resources, true)); } + /** + * @return iterable + */ public function themesProvider(): iterable { yield 'Bootstrap 5 horizontal' => [ diff --git a/tests/DependencyInjection/ConfigurationTest.php b/tests/DependencyInjection/ConfigurationTest.php index dd0c5673..68f76dbf 100644 --- a/tests/DependencyInjection/ConfigurationTest.php +++ b/tests/DependencyInjection/ConfigurationTest.php @@ -20,8 +20,11 @@ class ConfigurationTest extends TestCase { /** * @dataProvider configurationDataProvider + * + * @param array $configs + * @param array $expected */ - public function testConfiguration(array $configs, array $expected) + public function testConfiguration(array $configs, array $expected): void { $processor = new Processor(); $result = $processor->processConfiguration(new Configuration(), $configs); @@ -29,7 +32,10 @@ public function testConfiguration(array $configs, array $expected) $this->assertSame($expected, $result); } - public function configurationDataProvider() + /** + * @return iterable, array}> + */ + public function configurationDataProvider(): iterable { yield [[], [ 'twig' => [ diff --git a/tests/DependencyInjection/MisdPhoneNumberExtensionTest.php b/tests/DependencyInjection/MisdPhoneNumberExtensionTest.php index 3502ea38..e7e70a02 100644 --- a/tests/DependencyInjection/MisdPhoneNumberExtensionTest.php +++ b/tests/DependencyInjection/MisdPhoneNumberExtensionTest.php @@ -23,12 +23,9 @@ */ class MisdPhoneNumberExtensionTest extends TestCase { - /** - * @var TaggedContainerInterface - */ - protected $container; + private TaggedContainerInterface $container; - public function testLoad() + public function testLoad(): void { $extension = new MisdPhoneNumberExtension(); $this->container = new ContainerBuilder(); @@ -61,7 +58,7 @@ public function testLoad() $this->assertSame(PhoneNumberUtil::UNKNOWN_REGION, $this->container->getParameter('misd_phone_number.validator.default_region')); } - public function testDisabledServices() + public function testDisabledServices(): void { $extension = new MisdPhoneNumberExtension(); $this->container = new ContainerBuilder(); @@ -82,7 +79,7 @@ public function testDisabledServices() $this->assertFalse($this->container->has('Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumberValidator')); } - public function testValidatorParameters() + public function testValidatorParameters(): void { $extension = new MisdPhoneNumberExtension(); $this->container = new ContainerBuilder(); @@ -99,7 +96,7 @@ public function testValidatorParameters() $this->assertSame(0, $this->container->getParameter('misd_phone_number.validator.format')); } - public function testNormalizerParameters() + public function testNormalizerParameters(): void { $extension = new MisdPhoneNumberExtension(); $this->container = new ContainerBuilder(); @@ -116,7 +113,7 @@ public function testNormalizerParameters() $this->assertSame(PhoneNumberFormat::INTERNATIONAL, $this->container->getParameter('misd_phone_number.serializer.format')); } - public function testValidatorDefaultRegionUppercase() + public function testValidatorDefaultRegionUppercase(): void { $extension = new MisdPhoneNumberExtension(); $this->container = new ContainerBuilder(); diff --git a/tests/Doctrine/DBAL/Types/PhoneNumberTypeTest.php b/tests/Doctrine/DBAL/Types/PhoneNumberTypeTest.php index 4a45779d..c7548624 100644 --- a/tests/Doctrine/DBAL/Types/PhoneNumberTypeTest.php +++ b/tests/Doctrine/DBAL/Types/PhoneNumberTypeTest.php @@ -19,6 +19,7 @@ use Misd\PhoneNumberBundle\Doctrine\DBAL\Types\PhoneNumberType; use PHPUnit\Framework\TestCase; use Prophecy\PhpUnit\ProphecyTrait; +use Prophecy\Prophecy\ObjectProphecy; /** * Phone number type test. @@ -28,23 +29,15 @@ class PhoneNumberTypeTest extends TestCase use ProphecyTrait; /** - * @var AbstractPlatform + * @var ObjectProphecy */ - protected $platform; - - /** - * @var PhoneNumberType - */ - protected $type; - - /** - * @var PhoneNumberUtil - */ - protected $phoneNumberUtil; + private ObjectProphecy $platform; + private Type $type; + private PhoneNumberUtil $phoneNumberUtil; public static function setUpBeforeClass(): void { - Type::addType('phone_number', 'Misd\PhoneNumberBundle\Doctrine\DBAL\Types\PhoneNumberType'); + Type::addType('phone_number', PhoneNumberType::class); } protected function setUp(): void @@ -62,17 +55,17 @@ protected function setUp(): void $this->phoneNumberUtil = PhoneNumberUtil::getInstance(); } - public function testInstanceOf() + public function testInstanceOf(): void { - $this->assertInstanceOf('Doctrine\DBAL\Types\Type', $this->type); + $this->assertInstanceOf(Type::class, $this->type); } - public function testGetName() + public function testGetName(): void { $this->assertSame('phone_number', $this->type->getName()); } - public function testGetSQLDeclaration() + public function testGetSQLDeclaration(): void { if (method_exists(AbstractPlatform::class, 'getVarcharTypeDeclarationSQL')) { // DBAL < 4 @@ -84,31 +77,31 @@ public function testGetSQLDeclaration() $this->assertSame('DUMMYVARCHAR()', $this->type->getSQLDeclaration([], $this->platform->reveal())); } - public function testConvertToDatabaseValueWithNull() + public function testConvertToDatabaseValueWithNull(): void { $this->assertNull($this->type->convertToDatabaseValue(null, $this->platform->reveal())); } - public function testConvertToDatabaseValueWithPhoneNumber() + public function testConvertToDatabaseValueWithPhoneNumber(): void { $phoneNumber = $this->phoneNumberUtil->parse('+441234567890', PhoneNumberUtil::UNKNOWN_REGION); $this->assertSame('+441234567890', $this->type->convertToDatabaseValue($phoneNumber, $this->platform->reveal())); } - public function testConvertToDatabaseValueFailure() + public function testConvertToDatabaseValueFailure(): void { $this->expectException(ConversionException::class); $this->type->convertToDatabaseValue('foo', $this->platform->reveal()); } - public function testConvertToPHPValueWithNull() + public function testConvertToPHPValueWithNull(): void { $this->assertNull($this->type->convertToPHPValue(null, $this->platform->reveal())); } - public function testConvertToPHPValueWithPhoneNumber() + public function testConvertToPHPValueWithPhoneNumber(): void { $phoneNumber = $this->type->convertToPHPValue('+441234567890', $this->platform->reveal()); @@ -116,7 +109,7 @@ public function testConvertToPHPValueWithPhoneNumber() $this->assertSame('+441234567890', $this->phoneNumberUtil->format($phoneNumber, PhoneNumberFormat::E164)); } - public function testConvertToPHPValueWithAPhoneNumberInstance() + public function testConvertToPHPValueWithAPhoneNumberInstance(): void { $expectedPhoneNumber = $this->phoneNumberUtil->parse('+441234567890', PhoneNumberUtil::UNKNOWN_REGION); @@ -125,14 +118,14 @@ public function testConvertToPHPValueWithAPhoneNumberInstance() $this->assertEquals($expectedPhoneNumber, $phoneNumber); } - public function testConvertToPHPValueFailure() + public function testConvertToPHPValueFailure(): void { $this->expectException(ConversionException::class); $this->type->convertToPHPValue('foo', $this->platform->reveal()); } - public function testRequiresSQLCommentHint() + public function testRequiresSQLCommentHint(): void { $this->assertTrue($this->type->requiresSQLCommentHint($this->platform->reveal())); } diff --git a/tests/Form/DataTransformer/PhoneNumberToArrayTransformerTest.php b/tests/Form/DataTransformer/PhoneNumberToArrayTransformerTest.php index 95412ea4..72853dca 100644 --- a/tests/Form/DataTransformer/PhoneNumberToArrayTransformerTest.php +++ b/tests/Form/DataTransformer/PhoneNumberToArrayTransformerTest.php @@ -1,5 +1,7 @@ phoneNumberUtil = PhoneNumberUtil::getInstance(); } - public function testConstructor() + public function testConstructor(): void { $transformer = new PhoneNumberToArrayTransformer([]); @@ -45,8 +44,12 @@ public function testConstructor() /** * @dataProvider transformProvider + * + * @param string[] $countryChoices + * @param array{country: string, number: string}|null $actual + * @param array{country: string, number: string}|string $expected */ - public function testTransform(array $countryChoices, $actual, $expected) + public function testTransform(array $countryChoices, ?array $actual, array|string $expected): void { $transformer = new PhoneNumberToArrayTransformer($countryChoices); @@ -60,6 +63,7 @@ public function testTransform(array $countryChoices, $actual, $expected) } try { + /* @phpstan-ignore-next-line */ $transformed = $transformer->transform($phoneNumber); } catch (TransformationFailedException $e) { $transformed = self::TRANSFORMATION_FAILED; @@ -72,47 +76,52 @@ public function testTransform(array $countryChoices, $actual, $expected) * 0 => Country choices * 1 => Actual value * 2 => Expected result. + * + * @return iterable */ - public function transformProvider() + public function transformProvider(): iterable { - return [ - [ - ['GB'], - null, - ['country' => '', 'number' => ''], - ], - [ - ['GB'], - ['country' => 'GB', 'number' => '01234567890'], - ['country' => 'GB', 'number' => '01234 567890'], - ], - [// Wrong country code, but matching country exists. - ['GB', 'JE'], - ['country' => 'JE', 'number' => '01234567890'], - ['country' => 'GB', 'number' => '01234 567890'], - ], - [// Wrong country code, but matching country exists. - ['GB', 'JE'], - ['country' => 'JE', 'number' => '+441234567890'], - ['country' => 'GB', 'number' => '01234 567890'], - ], - [// Country code not in list. - ['US'], - ['country' => 'GB', 'number' => '01234567890'], - self::TRANSFORMATION_FAILED, - ], - [ - ['US'], - ['country' => 'GB', 'number' => 'foo'], - self::TRANSFORMATION_FAILED, - ], + yield [ + ['GB'], + null, + ['country' => '', 'number' => ''], + ]; + yield [ + ['GB'], + ['country' => 'GB', 'number' => '01234567890'], + ['country' => 'GB', 'number' => '01234 567890'], + ]; + // Wrong country code, but matching country exists. + yield [ + ['GB', 'JE'], + ['country' => 'JE', 'number' => '01234567890'], + ['country' => 'GB', 'number' => '01234 567890'], + ]; + // Wrong country code, but matching country exists. + yield [ + ['GB', 'JE'], + ['country' => 'JE', 'number' => '+441234567890'], + ['country' => 'GB', 'number' => '01234 567890'], + ]; + // Country code not in list. + yield [ + ['US'], + ['country' => 'GB', 'number' => '01234567890'], + self::TRANSFORMATION_FAILED, + ]; + yield [ + ['US'], + ['country' => 'GB', 'number' => 'foo'], + self::TRANSFORMATION_FAILED, ]; } /** * @dataProvider reverseTransformProvider + * + * @param string[] $countryChoices */ - public function testReverseTransform(array $countryChoices, $actual, $expected) + public function testReverseTransform(array $countryChoices, mixed $actual, ?string $expected): void { $transformer = new PhoneNumberToArrayTransformer($countryChoices); @@ -133,50 +142,51 @@ public function testReverseTransform(array $countryChoices, $actual, $expected) * 0 => Country choices * 1 => Actual value * 2 => Expected result. + * + * @return iterable */ - public function reverseTransformProvider() + public function reverseTransformProvider(): iterable { - return [ - [ - ['GB'], - null, - null, - ], - [ - ['GB'], - 'foo', - self::TRANSFORMATION_FAILED, - ], - [ - ['GB'], - ['country' => '', 'number' => ''], - null, - ], - [ - ['GB'], - ['country' => 'GB', 'number' => ''], - null, - ], - [ - ['GB'], - ['country' => '', 'number' => 'foo'], - self::TRANSFORMATION_FAILED, - ], - [ - ['GB'], - ['country' => 'GB', 'number' => '01234 567890'], - '+441234567890', - ], - [ - ['GB'], - ['country' => 'GB', 'number' => '+44 1234 567890'], - '+441234567890', - ], - [// Country code not in list. - ['US'], - ['country' => 'GB', 'number' => '+44 1234 567890'], - self::TRANSFORMATION_FAILED, - ], + yield [ + ['GB'], + null, + null, + ]; + yield [ + ['GB'], + 'foo', + self::TRANSFORMATION_FAILED, + ]; + yield [ + ['GB'], + ['country' => '', 'number' => ''], + null, + ]; + yield [ + ['GB'], + ['country' => 'GB', 'number' => ''], + null, + ]; + yield [ + ['GB'], + ['country' => '', 'number' => 'foo'], + self::TRANSFORMATION_FAILED, + ]; + yield [ + ['GB'], + ['country' => 'GB', 'number' => '01234 567890'], + '+441234567890', + ]; + yield [ + ['GB'], + ['country' => 'GB', 'number' => '+44 1234 567890'], + '+441234567890', + ]; + // Country code not in list. + yield [ + ['US'], + ['country' => 'GB', 'number' => '+44 1234 567890'], + self::TRANSFORMATION_FAILED, ]; } } diff --git a/tests/Form/DataTransformer/PhoneNumberToStringTransformerTest.php b/tests/Form/DataTransformer/PhoneNumberToStringTransformerTest.php index ecd3ce6e..64acebea 100644 --- a/tests/Form/DataTransformer/PhoneNumberToStringTransformerTest.php +++ b/tests/Form/DataTransformer/PhoneNumberToStringTransformerTest.php @@ -1,5 +1,7 @@ phoneNumberUtil = PhoneNumberUtil::getInstance(); } - public function testConstructor() + public function testConstructor(): void { $transformer = new PhoneNumberToStringTransformer(); @@ -46,18 +45,20 @@ public function testConstructor() /** * @dataProvider transformProvider */ - public function testTransform($defaultRegion, $format, $actual, $expected) + public function testTransform(string $defaultRegion, int $format, ?string $actual, string $expected): void { $transformer = new PhoneNumberToStringTransformer($defaultRegion, $format); $phoneNumberUtil = PhoneNumberUtil::getInstance(); try { + /* @phpstan-ignore-next-line */ $phoneNumber = $phoneNumberUtil->parse($actual, $defaultRegion); } catch (NumberParseException $e) { $phoneNumber = $actual; } try { + /* @phpstan-ignore-next-line */ $transformed = $transformer->transform($phoneNumber); } catch (TransformationFailedException $e) { $transformed = self::TRANSFORMATION_FAILED; @@ -71,27 +72,27 @@ public function testTransform($defaultRegion, $format, $actual, $expected) * 1 => Format * 2 => Actual value * 3 => Expected result. + * + * @return iterable */ - public function transformProvider() + public function transformProvider(): iterable { - return [ - [PhoneNumberUtil::UNKNOWN_REGION, PhoneNumberFormat::INTERNATIONAL, null, ''], - [PhoneNumberUtil::UNKNOWN_REGION, PhoneNumberFormat::NATIONAL, 'foo', self::TRANSFORMATION_FAILED], - [PhoneNumberUtil::UNKNOWN_REGION, PhoneNumberFormat::NATIONAL, '0', self::TRANSFORMATION_FAILED], - [ - PhoneNumberUtil::UNKNOWN_REGION, - PhoneNumberFormat::INTERNATIONAL, - '+441234567890', - '+44 1234 567890', - ], - ['GB', PhoneNumberFormat::NATIONAL, '01234567890', '01234 567890'], + yield [PhoneNumberUtil::UNKNOWN_REGION, PhoneNumberFormat::INTERNATIONAL, null, '']; + yield [PhoneNumberUtil::UNKNOWN_REGION, PhoneNumberFormat::NATIONAL, 'foo', self::TRANSFORMATION_FAILED]; + yield [PhoneNumberUtil::UNKNOWN_REGION, PhoneNumberFormat::NATIONAL, '0', self::TRANSFORMATION_FAILED]; + yield [ + PhoneNumberUtil::UNKNOWN_REGION, + PhoneNumberFormat::INTERNATIONAL, + '+441234567890', + '+44 1234 567890', ]; + yield ['GB', PhoneNumberFormat::NATIONAL, '01234567890', '01234 567890']; } /** * @dataProvider reverseTransformProvider */ - public function testReverseTransform($defaultRegion, $actual, $expected) + public function testReverseTransform(string $defaultRegion, ?string $actual, ?string $expected): void { $transformer = new PhoneNumberToStringTransformer($defaultRegion); @@ -112,15 +113,15 @@ public function testReverseTransform($defaultRegion, $actual, $expected) * 0 => Default region * 1 => Actual value * 2 => Expected result. + * + * @return iterable */ - public function reverseTransformProvider() + public function reverseTransformProvider(): iterable { - return [ - [PhoneNumberUtil::UNKNOWN_REGION, null, null], - [PhoneNumberUtil::UNKNOWN_REGION, 'foo', self::TRANSFORMATION_FAILED], - [PhoneNumberUtil::UNKNOWN_REGION, '0', self::TRANSFORMATION_FAILED], - [PhoneNumberUtil::UNKNOWN_REGION, '+44 1234 567890', '+441234567890'], - ['GB', '01234 567890', '+441234567890'], - ]; + yield [PhoneNumberUtil::UNKNOWN_REGION, null, null]; + yield [PhoneNumberUtil::UNKNOWN_REGION, 'foo', self::TRANSFORMATION_FAILED]; + yield [PhoneNumberUtil::UNKNOWN_REGION, '0', self::TRANSFORMATION_FAILED]; + yield [PhoneNumberUtil::UNKNOWN_REGION, '+44 1234 567890', '+441234567890']; + yield ['GB', '01234 567890', '+441234567890']; } } diff --git a/tests/Form/Type/PhoneNumberTypeTest.php b/tests/Form/Type/PhoneNumberTypeTest.php index 0ab1971b..6bf1eed1 100644 --- a/tests/Form/Type/PhoneNumberTypeTest.php +++ b/tests/Form/Type/PhoneNumberTypeTest.php @@ -26,10 +26,7 @@ */ class PhoneNumberTypeTest extends TestCase { - /** - * @var FormFactoryInterface - */ - protected $factory; + protected FormFactoryInterface $factory; protected function setUp(): void { @@ -40,8 +37,10 @@ protected function setUp(): void /** * @dataProvider singleFieldProvider + * + * @param array $options */ - public function testSingleField($input, $options, $output) + public function testSingleField(string $input, array $options, string $output): void { $form = $this->factory->create(PhoneNumberType::class, null, $options); @@ -63,25 +62,28 @@ public function testSingleField($input, $options, $output) * 0 => Input * 1 => Options * 2 => Output. + * + * @return iterable, string}> */ - public function singleFieldProvider() + public function singleFieldProvider(): iterable { - return [ - ['+441234567890', [], '+44 1234 567890'], - ['+44 1234 567890', ['format' => PhoneNumberFormat::NATIONAL], '+44 1234 567890'], - ['+44 1234 567890', ['default_region' => 'GB', 'format' => PhoneNumberFormat::NATIONAL], '01234 567890'], - ['+1 650-253-0000', ['default_region' => 'GB', 'format' => PhoneNumberFormat::NATIONAL], '00 1 650-253-0000'], - ['01234 567890', ['default_region' => 'GB'], '+44 1234 567890'], - ['', [], ''], - ]; + yield ['+441234567890', [], '+44 1234 567890']; + yield ['+44 1234 567890', ['format' => PhoneNumberFormat::NATIONAL], '+44 1234 567890']; + yield ['+44 1234 567890', ['default_region' => 'GB', 'format' => PhoneNumberFormat::NATIONAL], '01234 567890']; + yield ['+1 650-253-0000', ['default_region' => 'GB', 'format' => PhoneNumberFormat::NATIONAL], '00 1 650-253-0000']; + yield ['01234 567890', ['default_region' => 'GB'], '+44 1234 567890']; + yield ['', [], '']; } /** * @dataProvider countryChoiceValuesProvider + * + * @param array $input + * @param array $output */ - public function testCountryChoiceValues($input, $options, $output) + public function testCountryChoiceValues(array $input, array $output): void { - $options['widget'] = PhoneNumberType::WIDGET_COUNTRY_CHOICE; + $options = ['widget' => PhoneNumberType::WIDGET_COUNTRY_CHOICE]; $form = $this->factory->create(PhoneNumberType::class, null, $options); $form->submit($input); @@ -102,22 +104,25 @@ public function testCountryChoiceValues($input, $options, $output) * 0 => Input * 1 => Options * 2 => Output. + * + * @return iterable, array}> */ - public function countryChoiceValuesProvider() + public function countryChoiceValuesProvider(): iterable { - return [ - [['country' => 'GB', 'number' => '01234 567890'], [], ['country' => 'GB', 'number' => '01234 567890']], - [['country' => 'GB', 'number' => '+44 1234 567890'], [], ['country' => 'GB', 'number' => '01234 567890']], - [['country' => 'GB', 'number' => '1234 567890'], [], ['country' => 'GB', 'number' => '01234 567890']], - [['country' => 'GB', 'number' => '+1 650-253-0000'], [], ['country' => 'US', 'number' => '(650) 253-0000']], - [['country' => '', 'number' => ''], [], ['country' => '', 'number' => '']], - ]; + yield [['country' => 'GB', 'number' => '01234 567890'], ['country' => 'GB', 'number' => '01234 567890']]; + yield [['country' => 'GB', 'number' => '+44 1234 567890'], ['country' => 'GB', 'number' => '01234 567890']]; + yield [['country' => 'GB', 'number' => '1234 567890'], ['country' => 'GB', 'number' => '01234 567890']]; + yield [['country' => 'GB', 'number' => '+1 650-253-0000'], ['country' => 'US', 'number' => '(650) 253-0000']]; + yield [['country' => '', 'number' => ''], ['country' => '', 'number' => '']]; } /** * @dataProvider countryChoiceChoicesProvider + * + * @param string[] $choices + * @param ChoiceView[] $expectedChoices */ - public function testCountryChoiceChoices(array $choices, $expectedChoicesCount, array $expectedChoices) + public function testCountryChoiceChoices(array $choices, int $expectedChoicesCount, array $expectedChoices): void { IntlTestHelper::requireIntl($this); @@ -140,42 +145,44 @@ public function testCountryChoiceChoices(array $choices, $expectedChoicesCount, * 0 => Choices * 1 => Expected choices count * 2 => Expected choices. + * + * @return iterable */ - public function countryChoiceChoicesProvider() + public function countryChoiceChoicesProvider(): iterable { - return [ + yield [ + [], + // 3 regions have an already used label "TA", "AC" and XK + // @see https://fr.wikipedia.org/wiki/ISO_3166-2#cite_note-UPU-1 + 242, [ - [], - // 3 regions have an already used label "TA", "AC" and XK - // @see https://fr.wikipedia.org/wiki/ISO_3166-2#cite_note-UPU-1 - 242, - [ - $this->createChoiceView('United Kingdom (+44)', 'GB'), - ], + $this->createChoiceView('United Kingdom (+44)', 'GB'), ], + ]; + yield [ + ['GB', 'US'], + 2, [ - ['GB', 'US'], - 2, - [ - $this->createChoiceView('United Kingdom (+44)', 'GB'), - $this->createChoiceView('United States (+1)', 'US'), - ], + $this->createChoiceView('United Kingdom (+44)', 'GB'), + $this->createChoiceView('United States (+1)', 'US'), ], + ]; + yield [ + ['GB', 'US', PhoneNumberUtil::UNKNOWN_REGION], + 2, [ - ['GB', 'US', PhoneNumberUtil::UNKNOWN_REGION], - 2, - [ - $this->createChoiceView('United Kingdom (+44)', 'GB'), - $this->createChoiceView('United States (+1)', 'US'), - ], + $this->createChoiceView('United Kingdom (+44)', 'GB'), + $this->createChoiceView('United States (+1)', 'US'), ], ]; } /** * @dataProvider countryChoiceFormatProvider + * + * @param ChoiceView[] $expectedChoices */ - public function testCountryChoiceFormat(string $displayType, bool $displayEmojiFlag, array $expectedChoices) + public function testCountryChoiceFormat(string $displayType, bool $displayEmojiFlag, array $expectedChoices): void { $options['widget'] = PhoneNumberType::WIDGET_COUNTRY_CHOICE; $options['country_display_type'] = $displayType; @@ -194,37 +201,37 @@ public function testCountryChoiceFormat(string $displayType, bool $displayEmojiF * 0 => Display type * 1 => Display emoji flag * 2 => Expected choices. + * + * @return iterable */ - public function countryChoiceFormatProvider() + public function countryChoiceFormatProvider(): iterable { - return [ + yield [ + PhoneNumberType::DISPLAY_COUNTRY_FULL, + false, [ - PhoneNumberType::DISPLAY_COUNTRY_FULL, - false, - [ - $this->createChoiceView('United Kingdom (+44)', 'GB'), - ], + $this->createChoiceView('United Kingdom (+44)', 'GB'), ], + ]; + yield [ + PhoneNumberType::DISPLAY_COUNTRY_SHORT, + false, [ - PhoneNumberType::DISPLAY_COUNTRY_SHORT, - false, - [ - $this->createChoiceView('GB +44', 'GB'), - ], + $this->createChoiceView('GB +44', 'GB'), ], + ]; + yield [ + PhoneNumberType::DISPLAY_COUNTRY_FULL, + true, [ - PhoneNumberType::DISPLAY_COUNTRY_FULL, - true, - [ - $this->createChoiceView('🇬🇧 United Kingdom (+44)', 'GB'), - ], + $this->createChoiceView('🇬🇧 United Kingdom (+44)', 'GB'), ], + ]; + yield [ + PhoneNumberType::DISPLAY_COUNTRY_SHORT, + true, [ - PhoneNumberType::DISPLAY_COUNTRY_SHORT, - true, - [ - $this->createChoiceView('🇬🇧 GB +44', 'GB'), - ], + $this->createChoiceView('🇬🇧 GB +44', 'GB'), ], ]; } @@ -232,7 +239,7 @@ public function countryChoiceFormatProvider() /** * @dataProvider countryChoicePlaceholderProvider */ - public function testCountryChoicePlaceholder($placeholder, $expectedPlaceholder) + public function testCountryChoicePlaceholder(?string $placeholder, ?string $expectedPlaceholder): void { IntlTestHelper::requireIntl($this); $form = $this->factory->create(PhoneNumberType::class, null, ['widget' => PhoneNumberType::WIDGET_COUNTRY_CHOICE, 'country_placeholder' => $placeholder]); @@ -246,26 +253,17 @@ public function testCountryChoicePlaceholder($placeholder, $expectedPlaceholder) * 0 => Filled * 1 => not filled * 2 => empty. + * + * @return iterable */ - public function countryChoicePlaceholderProvider() + public function countryChoicePlaceholderProvider(): iterable { - return [ - [ - 'Choose a country', - 'Choose a country', - ], - [ - null, - null, - ], - [ - '', - '', - ], - ]; + yield ['Choose a country', 'Choose a country']; + yield [null, null]; + yield ['', '']; } - public function testCountryChoiceTranslations() + public function testCountryChoiceTranslations(): void { IntlTestHelper::requireFullIntl($this); \Locale::setDefault('fr'); @@ -279,14 +277,14 @@ public function testCountryChoiceTranslations() $this->assertFalse($view['country']->vars['choice_translation_domain']); } - public function testInvalidWidget() + public function testInvalidWidget(): void { $this->expectException(InvalidOptionsException::class); $this->factory->create(PhoneNumberType::class, null, ['widget' => 'foo']); } - public function testGetNameAndBlockPrefixAreTel() + public function testGetNameAndBlockPrefixAreTel(): void { $type = new PhoneNumberType(); @@ -294,7 +292,7 @@ public function testGetNameAndBlockPrefixAreTel() $this->assertSame($type->getBlockPrefix(), $type->getName()); } - public function testCountryChoiceCountryOptions() + public function testCountryChoiceCountryOptions(): void { $form = $this->factory->create(PhoneNumberType::class, null, [ 'widget' => PhoneNumberType::WIDGET_COUNTRY_CHOICE, @@ -309,7 +307,7 @@ public function testCountryChoiceCountryOptions() $this->assertEquals(['class' => 'custom-select-class'], $view['country']->vars['attr']); } - public function testCountryChoiceNumberOptions() + public function testCountryChoiceNumberOptions(): void { $form = $this->factory->create(PhoneNumberType::class, null, [ 'widget' => PhoneNumberType::WIDGET_COUNTRY_CHOICE, @@ -324,7 +322,7 @@ public function testCountryChoiceNumberOptions() $this->assertEquals(['placeholder' => '000 000'], $view['number']->vars['attr']); } - private function createChoiceView($label, $code) + private function createChoiceView(string $label, string $code): ChoiceView { return new ChoiceView($code, $code, $label); } diff --git a/tests/Serializer/Normalizer/PhoneNumberNormalizerTest.php b/tests/Serializer/Normalizer/PhoneNumberNormalizerTest.php index 20abdebe..a14816ec 100644 --- a/tests/Serializer/Normalizer/PhoneNumberNormalizerTest.php +++ b/tests/Serializer/Normalizer/PhoneNumberNormalizerTest.php @@ -36,7 +36,7 @@ protected function setUp(): void } } - public function testSupportNormalization() + public function testSupportNormalization(): void { $normalizer = new PhoneNumberNormalizer($this->prophesize(PhoneNumberUtil::class)->reveal()); @@ -44,7 +44,7 @@ public function testSupportNormalization() $this->assertFalse($normalizer->supportsNormalization(new \stdClass())); } - public function testNormalize() + public function testNormalize(): void { $phoneNumber = new PhoneNumber(); $phoneNumber->setRawInput('+33193166989'); @@ -57,7 +57,7 @@ public function testNormalize() $this->assertEquals('+33193166989', $normalizer->normalize($phoneNumber)); } - public function testSupportDenormalization() + public function testSupportDenormalization(): void { $normalizer = new PhoneNumberNormalizer($this->prophesize(PhoneNumberUtil::class)->reveal()); @@ -66,7 +66,7 @@ public function testSupportDenormalization() $this->assertFalse($normalizer->supportsDenormalization('+33193166989', 'stdClass')); } - public function testDenormalize() + public function testDenormalize(): void { $phoneNumber = new PhoneNumber(); $phoneNumber->setRawInput('+33193166989'); @@ -79,7 +79,7 @@ public function testDenormalize() $this->assertSame($phoneNumber, $normalizer->denormalize('+33193166989', 'libphonenumber\PhoneNumber')); } - public function testItDenormalizeNullToNull() + public function testItDenormalizeNullToNull(): void { $phoneNumberUtil = $this->prophesize(PhoneNumberUtil::class); $phoneNumberUtil->parse(Argument::cetera())->shouldNotBeCalled(); @@ -89,7 +89,7 @@ public function testItDenormalizeNullToNull() $this->assertNull($normalizer->denormalize(null, 'libphonenumber\PhoneNumber')); } - public function testInvalidDateThrowException() + public function testInvalidDateThrowException(): void { $this->expectException(UnexpectedValueException::class); diff --git a/tests/Templating/Helper/PhoneNumberHelperTest.php b/tests/Templating/Helper/PhoneNumberHelperTest.php index 3730a7ec..62611e3f 100644 --- a/tests/Templating/Helper/PhoneNumberHelperTest.php +++ b/tests/Templating/Helper/PhoneNumberHelperTest.php @@ -18,6 +18,7 @@ use Misd\PhoneNumberBundle\Templating\Helper\PhoneNumberHelper; use PHPUnit\Framework\TestCase; use Prophecy\PhpUnit\ProphecyTrait; +use Prophecy\Prophecy\ObjectProphecy; /** * Phone number templating helper test. @@ -26,8 +27,11 @@ class PhoneNumberHelperTest extends TestCase { use ProphecyTrait; - protected $phoneNumberUtil; - protected $helper; + /** + * @var ObjectProphecy + */ + protected ObjectProphecy $phoneNumberUtil; + protected PhoneNumberHelper $helper; protected function setUp(): void { @@ -38,7 +42,7 @@ protected function setUp(): void /** * @dataProvider processProvider */ - public function testProcess($format, $expectedFormat) + public function testProcess(int|string $format, int $expectedFormat): void { $phoneNumber = $this->prophesize(PhoneNumber::class); $this->phoneNumberUtil @@ -52,14 +56,16 @@ public function testProcess($format, $expectedFormat) /** * 0 => Format * 1 => Expected format. + * + * @return iterable */ - public function processProvider() + public function processProvider(): iterable { yield [PhoneNumberFormat::NATIONAL, PhoneNumberFormat::NATIONAL]; yield ['NATIONAL', PhoneNumberFormat::NATIONAL]; } - public function testProcessInvalidArgumentException() + public function testProcessInvalidArgumentException(): void { $this->expectException(InvalidArgumentException::class); @@ -71,7 +77,7 @@ public function testProcessInvalidArgumentException() /** * @dataProvider formatOutOfCountryCallingNumberProvider */ - public function testFormatOutOfCountryCallingNumber($phoneNumber, $defaultRegion, $regionCode, $expectedResult) + public function testFormatOutOfCountryCallingNumber(string $phoneNumber, string $defaultRegion, ?string $regionCode, string $expectedResult): void { $phoneNumberUtil = PhoneNumberUtil::getInstance(); $helper = new PhoneNumberHelper($phoneNumberUtil); @@ -81,27 +87,13 @@ public function testFormatOutOfCountryCallingNumber($phoneNumber, $defaultRegion $this->assertSame($expectedResult, $helper->formatOutOfCountryCallingNumber($phoneNumber, $regionCode)); } - public function testFormatAcceptString() - { - $phoneNumberUtil = PhoneNumberUtil::getInstance(); - $helper = new PhoneNumberHelper($phoneNumberUtil); - $result = $helper->format('+37122222222'); - $this->assertEquals('+371 22 222 222', $result); - } - - public function formatOutOfCountryCallingNumberAcceptString() - { - $phoneNumberUtil = PhoneNumberUtil::getInstance(); - $helper = new PhoneNumberHelper($phoneNumberUtil); - $result = $helper->formatOutOfCountryCallingNumber('+37122222222', 'LV'); - $this->assertEquals('+371 22 222 222', $result); - } - /** * 0 => The phone number. * 1 => Phone number default region. * 2 => Country calling from. * 3 => Expected format. + * + * @return iterable */ public function formatOutOfCountryCallingNumberProvider() { diff --git a/tests/Validator/Constraints/PhoneNumberTest.php b/tests/Validator/Constraints/PhoneNumberTest.php index 50d3ec36..c06b2964 100644 --- a/tests/Validator/Constraints/PhoneNumberTest.php +++ b/tests/Validator/Constraints/PhoneNumberTest.php @@ -20,7 +20,7 @@ */ class PhoneNumberTest extends TestCase { - public function testProperties() + public function testProperties(): void { $phoneNumber = new PhoneNumber(); @@ -32,8 +32,10 @@ public function testProperties() /** * @dataProvider messageProvider + * + * @param string|string[]|null $type */ - public function testMessage($message, $type, $format, $expectedMessage) + public function testMessage(?string $message, string|array|null $type, ?int $format, string $expectedMessage): void { $phoneNumber = new PhoneNumber($format, $type, null, null, $message); $this->assertSame($expectedMessage, $phoneNumber->getMessage()); @@ -45,27 +47,27 @@ public function testMessage($message, $type, $format, $expectedMessage) * 1 => Type (optional) * 2 => Format (optional) * 3 => Expected message. + * + * @return iterable */ - public function messageProvider() + public function messageProvider(): iterable { - return [ - [null, null, null, 'This value is not a valid phone number.'], - [null, 'fixed_line', null, 'This value is not a valid fixed-line number.'], - [null, 'mobile', null, 'This value is not a valid mobile number.'], - [null, 'pager', null, 'This value is not a valid pager number.'], - [null, 'personal_number', null, 'This value is not a valid personal number.'], - [null, 'premium_rate', null, 'This value is not a valid premium-rate number.'], - [null, 'shared_cost', null, 'This value is not a valid shared-cost number.'], - [null, 'toll_free', null, 'This value is not a valid toll-free number.'], - [null, 'uan', null, 'This value is not a valid UAN.'], - [null, 'voip', null, 'This value is not a valid VoIP number.'], - [null, 'voicemail', null, 'This value is not a valid voicemail access number.'], - [null, ['fixed_line', 'voip'], null, 'This value is not a valid phone number.'], - [null, ['uan', 'fixed_line'], null, 'This value is not a valid phone number.'], - ['foo', null, null, 'foo'], - ['foo', 'fixed_line', null, 'foo'], - ['foo', 'mobile', null, 'foo'], - [null, null, PhoneNumberFormat::E164, 'This value is not a valid phone number.'], - ]; + yield [null, null, null, 'This value is not a valid phone number.']; + yield [null, 'fixed_line', null, 'This value is not a valid fixed-line number.']; + yield [null, 'mobile', null, 'This value is not a valid mobile number.']; + yield [null, 'pager', null, 'This value is not a valid pager number.']; + yield [null, 'personal_number', null, 'This value is not a valid personal number.']; + yield [null, 'premium_rate', null, 'This value is not a valid premium-rate number.']; + yield [null, 'shared_cost', null, 'This value is not a valid shared-cost number.']; + yield [null, 'toll_free', null, 'This value is not a valid toll-free number.']; + yield [null, 'uan', null, 'This value is not a valid UAN.']; + yield [null, 'voip', null, 'This value is not a valid VoIP number.']; + yield [null, 'voicemail', null, 'This value is not a valid voicemail access number.']; + yield [null, ['fixed_line', 'voip'], null, 'This value is not a valid phone number.']; + yield [null, ['uan', 'fixed_line'], null, 'This value is not a valid phone number.']; + yield ['foo', null, null, 'foo']; + yield ['foo', 'fixed_line', null, 'foo']; + yield ['foo', 'mobile', null, 'foo']; + yield [null, null, PhoneNumberFormat::E164, 'This value is not a valid phone number.']; } } diff --git a/tests/Validator/Constraints/PhoneNumberValidatorTest.php b/tests/Validator/Constraints/PhoneNumberValidatorTest.php index 7d38e8f1..8f6369ff 100644 --- a/tests/Validator/Constraints/PhoneNumberValidatorTest.php +++ b/tests/Validator/Constraints/PhoneNumberValidatorTest.php @@ -11,6 +11,7 @@ namespace Misd\PhoneNumberBundle\Tests\Validator\Constraints; +use libphonenumber\PhoneNumber as LibPhoneNumber; use libphonenumber\PhoneNumberFormat; use libphonenumber\PhoneNumberUtil; use Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber; @@ -29,15 +30,8 @@ */ class PhoneNumberValidatorTest extends TestCase { - /** - * @var \Symfony\Component\Validator\Context\ExecutionContextInterface|MockObject - */ - protected $context; - - /** - * @var \Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumberValidator - */ - protected $validator; + private ExecutionContextInterface&MockObject $context; + private PhoneNumberValidator $validator; protected function setUp(): void { @@ -51,9 +45,17 @@ protected function setUp(): void /** * @dataProvider validateProvider + * + * @param string[]|string|null $type */ - public function testValidate($value, $violates, $type = null, $defaultRegion = null, $regionPath = null, $format = null) - { + public function testValidate( + string|LibPhoneNumber|null $value, + bool $violates, + array|string $type = null, + string $defaultRegion = null, + string $regionPath = null, + int $format = null + ): void { $constraint = new PhoneNumber($format, $type, $defaultRegion, $regionPath); if (true === $violates) { @@ -84,7 +86,7 @@ public function testValidate($value, $violates, $type = null, $defaultRegion = n /** * @requires PHP 8 */ - public function testValidateFromAttribute() + public function testValidateFromAttribute(): void { $classMetadata = new ClassMetadata(PhoneNumberDummy::class); if (class_exists(AnnotationLoader::class)) { @@ -108,64 +110,64 @@ public function testValidateFromAttribute() * 2 => Type (optional) * 3 => Default region (optional). * 4 => Region Path (optional). + * + * @return iterable */ - public function validateProvider() + public function validateProvider(): iterable { - return [ - [null, false], - ['', false], - [PhoneNumberUtil::getInstance()->parse('+441234567890', PhoneNumberUtil::UNKNOWN_REGION), false], - [PhoneNumberUtil::getInstance()->parse('+441234567890', PhoneNumberUtil::UNKNOWN_REGION), false, 'fixed_line'], - [PhoneNumberUtil::getInstance()->parse('+441234567890', PhoneNumberUtil::UNKNOWN_REGION), true, 'mobile'], - [PhoneNumberUtil::getInstance()->parse('+441234567890', PhoneNumberUtil::UNKNOWN_REGION), false, ['fixed_line', 'mobile']], - [PhoneNumberUtil::getInstance()->parse('+44123456789', PhoneNumberUtil::UNKNOWN_REGION), true], - ['+441234567890', false], - ['+441234567890', false, 'fixed_line'], - ['+441234567890', true, 'mobile'], - ['+441234567890', false, ['mobile', 'fixed_line']], - ['+441234567890', true, ['mobile', 'voip']], - ['+44123456789', true], - ['+44123456789', true, 'mobile'], - ['+12015555555', false], - ['+12015555555', false, 'fixed_line'], - ['+12015555555', false, 'mobile'], - ['+12015555555', false, ['mobile', 'fixed_line']], - ['+12015555555', true, ['pager', 'voip', 'uan']], - ['+447640123456', false, 'pager'], - ['+441234567890', true, 'pager'], - ['+447012345678', false, 'personal_number'], - ['+441234567890', true, 'personal_number'], - ['+449012345678', false, 'premium_rate'], - ['+441234567890', true, 'premium_rate'], - ['+441234567890', true, 'shared_cost'], - ['+448001234567', false, 'toll_free'], - ['+441234567890', true, 'toll_free'], - ['+445512345678', false, 'uan'], - ['+441234567890', true, 'uan'], - ['+445612345678', false, 'voip'], - ['+441234567890', true, 'voip'], - ['+41860123456789', false, 'voicemail'], - ['+441234567890', true, 'voicemail'], - ['2015555555', false, null, 'US'], - ['2015555555', false, 'fixed_line', 'US'], - ['2015555555', false, 'mobile', 'US'], - ['01234 567890', false, null, 'GB'], - ['foo', true], - ['+441234567890', true, 'mobile', null, 'regionPath'], - ['+33606060606', false, 'mobile', null, 'regionPath'], - ['+33606060606', false, 'mobile', null, null, PhoneNumberFormat::E164], - ['2015555555', true, null, null, null, PhoneNumberFormat::E164], - ]; + yield [null, false]; + yield ['', false]; + yield [PhoneNumberUtil::getInstance()->parse('+441234567890', PhoneNumberUtil::UNKNOWN_REGION), false]; + yield [PhoneNumberUtil::getInstance()->parse('+441234567890', PhoneNumberUtil::UNKNOWN_REGION), false, 'fixed_line']; + yield [PhoneNumberUtil::getInstance()->parse('+441234567890', PhoneNumberUtil::UNKNOWN_REGION), true, 'mobile']; + yield [PhoneNumberUtil::getInstance()->parse('+441234567890', PhoneNumberUtil::UNKNOWN_REGION), false, ['fixed_line', 'mobile']]; + yield [PhoneNumberUtil::getInstance()->parse('+44123456789', PhoneNumberUtil::UNKNOWN_REGION), true]; + yield ['+441234567890', false]; + yield ['+441234567890', false, 'fixed_line']; + yield ['+441234567890', true, 'mobile']; + yield ['+441234567890', false, ['mobile', 'fixed_line']]; + yield ['+441234567890', true, ['mobile', 'voip']]; + yield ['+44123456789', true]; + yield ['+44123456789', true, 'mobile']; + yield ['+12015555555', false]; + yield ['+12015555555', false, 'fixed_line']; + yield ['+12015555555', false, 'mobile']; + yield ['+12015555555', false, ['mobile', 'fixed_line']]; + yield ['+12015555555', true, ['pager', 'voip', 'uan']]; + yield ['+447640123456', false, 'pager']; + yield ['+441234567890', true, 'pager']; + yield ['+447012345678', false, 'personal_number']; + yield ['+441234567890', true, 'personal_number']; + yield ['+449012345678', false, 'premium_rate']; + yield ['+441234567890', true, 'premium_rate']; + yield ['+441234567890', true, 'shared_cost']; + yield ['+448001234567', false, 'toll_free']; + yield ['+441234567890', true, 'toll_free']; + yield ['+445512345678', false, 'uan']; + yield ['+441234567890', true, 'uan']; + yield ['+445612345678', false, 'voip']; + yield ['+441234567890', true, 'voip']; + yield ['+41860123456789', false, 'voicemail']; + yield ['+441234567890', true, 'voicemail']; + yield ['2015555555', false, null, 'US']; + yield ['2015555555', false, 'fixed_line', 'US']; + yield ['2015555555', false, 'mobile', 'US']; + yield ['01234 567890', false, null, 'GB']; + yield ['foo', true]; + yield ['+441234567890', true, 'mobile', null, 'regionPath']; + yield ['+33606060606', false, 'mobile', null, 'regionPath']; + yield ['+33606060606', false, 'mobile', null, null, PhoneNumberFormat::E164]; + yield ['2015555555', true, null, null, null, PhoneNumberFormat::E164]; } - public function testValidateThrowsUnexpectedTypeExceptionOnBadValue() + public function testValidateThrowsUnexpectedTypeExceptionOnBadValue(): void { $this->expectException(UnexpectedTypeException::class); $this->validator->validate($this, new PhoneNumber()); } - protected function createValidator() + protected function createValidator(): PhoneNumberValidator { return new PhoneNumberValidator(PhoneNumberUtil::getInstance()); } @@ -173,16 +175,18 @@ protected function createValidator() class Foo { - public $regionPath = 'GB'; + public string $regionPath = 'GB'; } class PhoneNumberDummy { #[PhoneNumber(type: [PhoneNumber::MOBILE], defaultRegion: 'FR')] - private $phoneNumber1; + /* @phpstan-ignore-next-line */ + private PhoneNumber $phoneNumber1; #[PhoneNumber(regionPath: 'regionPath')] - private $phoneNumber2; + /* @phpstan-ignore-next-line */ + private PhoneNumber $phoneNumber2; - public $regionPath = 'GB'; + public string $regionPath = 'GB'; } diff --git a/tools/phpstan/composer.json b/tools/phpstan/composer.json index 1b3d323d..ba6a7e87 100644 --- a/tools/phpstan/composer.json +++ b/tools/phpstan/composer.json @@ -1,5 +1,6 @@ { "require": { - "phpstan/phpstan": "^1.10.46" + "phpstan/phpstan": "^1.10.46", + "jangregor/phpstan-prophecy": "^1.0" } }