From 4073030cacbd8148790acf1e5927435e354ded72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20BARRAY?= Date: Mon, 8 Jul 2019 14:06:11 +0200 Subject: [PATCH 1/2] Add support for DenormalizerInterface::denormalize --- extension.neon | 7 ++- ... SerializerDynamicReturnTypeExtension.php} | 18 +++++-- tests/Symfony/NeonTest.php | 2 +- ...rializerDynamicReturnTypeExtensionTest.php | 49 +++++++++++++++++++ ...nterfaceDynamicReturnTypeExtensionTest.php | 30 ------------ tests/Type/Symfony/denormalizer.php | 9 ++++ 6 files changed, 80 insertions(+), 35 deletions(-) rename src/Type/Symfony/{SerializerInterfaceDynamicReturnTypeExtension.php => SerializerDynamicReturnTypeExtension.php} (75%) mode change 100644 => 100755 create mode 100644 tests/Type/Symfony/SerializerDynamicReturnTypeExtensionTest.php delete mode 100644 tests/Type/Symfony/SerializerInterfaceDynamicReturnTypeExtensionTest.php create mode 100644 tests/Type/Symfony/denormalizer.php diff --git a/extension.neon b/extension.neon index 70249360..ee0fec8a 100644 --- a/extension.neon +++ b/extension.neon @@ -66,7 +66,12 @@ services: # SerializerInterface::deserialize() return type - - factory: PHPStan\Type\Symfony\SerializerInterfaceDynamicReturnTypeExtension + factory: PHPStan\Type\Symfony\SerializerDynamicReturnTypeExtension(Symfony\Component\Serializer\SerializerInterface, deserialize) + tags: [phpstan.broker.dynamicMethodReturnTypeExtension] + + # DenormalizerInterface::denormalize() return type + - + factory: PHPStan\Type\Symfony\SerializerDynamicReturnTypeExtension(Symfony\Component\Serializer\Normalizer\DenormalizerInterface, denormalize) tags: [phpstan.broker.dynamicMethodReturnTypeExtension] # Envelope::all() return type diff --git a/src/Type/Symfony/SerializerInterfaceDynamicReturnTypeExtension.php b/src/Type/Symfony/SerializerDynamicReturnTypeExtension.php old mode 100644 new mode 100755 similarity index 75% rename from src/Type/Symfony/SerializerInterfaceDynamicReturnTypeExtension.php rename to src/Type/Symfony/SerializerDynamicReturnTypeExtension.php index b6f7e33b..66c1cc44 --- a/src/Type/Symfony/SerializerInterfaceDynamicReturnTypeExtension.php +++ b/src/Type/Symfony/SerializerDynamicReturnTypeExtension.php @@ -12,17 +12,29 @@ use PHPStan\Type\ObjectType; use PHPStan\Type\Type; -class SerializerInterfaceDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension +class SerializerDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension { + /** @var string */ + private $class; + + /** @var string */ + private $method; + + public function __construct(string $class, string $method) + { + $this->class = $class; + $this->method = $method; + } + public function getClass(): string { - return 'Symfony\Component\Serializer\SerializerInterface'; + return $this->class; } public function isMethodSupported(MethodReflection $methodReflection): bool { - return $methodReflection->getName() === 'deserialize'; + return $methodReflection->getName() === $this->method; } public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type diff --git a/tests/Symfony/NeonTest.php b/tests/Symfony/NeonTest.php index dfc57b4c..1b22ac55 100644 --- a/tests/Symfony/NeonTest.php +++ b/tests/Symfony/NeonTest.php @@ -46,7 +46,7 @@ public function testExtensionNeon(): void ], $parameters); self::assertCount(6, $container->findByTag('phpstan.rules.rule')); - self::assertCount(11, $container->findByTag('phpstan.broker.dynamicMethodReturnTypeExtension')); + self::assertCount(12, $container->findByTag('phpstan.broker.dynamicMethodReturnTypeExtension')); self::assertCount(5, $container->findByTag('phpstan.typeSpecifier.methodTypeSpecifyingExtension')); self::assertInstanceOf(ServiceMap::class, $container->getByType(ServiceMap::class)); } diff --git a/tests/Type/Symfony/SerializerDynamicReturnTypeExtensionTest.php b/tests/Type/Symfony/SerializerDynamicReturnTypeExtensionTest.php new file mode 100644 index 00000000..f4863a73 --- /dev/null +++ b/tests/Type/Symfony/SerializerDynamicReturnTypeExtensionTest.php @@ -0,0 +1,49 @@ +processFile( + __DIR__ . '/serializer.php', + $expression, + $type, + new SerializerDynamicReturnTypeExtension( + 'Symfony\Component\Serializer\SerializerInterface', + 'deserialize' + ) + ); + } + + /** + * @dataProvider getContentProvider + */ + public function testDenormalizerInterface(string $expression, string $type): void + { + $this->processFile( + __DIR__ . '/denormalizer.php', + $expression, + $type, + new SerializerDynamicReturnTypeExtension( + 'Symfony\Component\Serializer\Normalizer\DenormalizerInterface', + 'denormalize' + ) + ); + } + + public function getContentProvider(): Iterator + { + yield ['$first', 'Bar']; + yield ['$second', 'array']; + yield ['$third', 'array>']; + } + +} diff --git a/tests/Type/Symfony/SerializerInterfaceDynamicReturnTypeExtensionTest.php b/tests/Type/Symfony/SerializerInterfaceDynamicReturnTypeExtensionTest.php deleted file mode 100644 index fad62b3b..00000000 --- a/tests/Type/Symfony/SerializerInterfaceDynamicReturnTypeExtensionTest.php +++ /dev/null @@ -1,30 +0,0 @@ -processFile( - __DIR__ . '/serializer.php', - $expression, - $type, - new SerializerInterfaceDynamicReturnTypeExtension() - ); - } - - public function getContentProvider(): Iterator - { - yield ['$first', 'Bar']; - yield ['$second', 'array']; - yield ['$third', 'array>']; - } - -} diff --git a/tests/Type/Symfony/denormalizer.php b/tests/Type/Symfony/denormalizer.php new file mode 100644 index 00000000..95d56676 --- /dev/null +++ b/tests/Type/Symfony/denormalizer.php @@ -0,0 +1,9 @@ +denormalize('bar', 'Bar', 'format'); +$second = $serializer->denormalize('bar', 'Bar[]', 'format'); +$third = $serializer->denormalize('bar', 'Bar[][]', 'format'); + +die; From 7d34a1376a6c1ea18ba8fd3427fb49c5518edf53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20BARRAY?= Date: Mon, 8 Jul 2019 16:41:49 +0200 Subject: [PATCH 2/2] Fix to follow latest change in PHPStan core --- tests/Type/Symfony/ExtensionTestCase.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Type/Symfony/ExtensionTestCase.php b/tests/Type/Symfony/ExtensionTestCase.php index a1cef2bf..c66936d3 100644 --- a/tests/Type/Symfony/ExtensionTestCase.php +++ b/tests/Type/Symfony/ExtensionTestCase.php @@ -52,7 +52,8 @@ protected function processFile( true, true, true, - [] + [], + true ); $resolver->setAnalysedFiles([$fileHelper->normalizePath($file)]);