Skip to content

Add support for Serializer::deserialize #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"slevomat/coding-standard": "^4.5.2",
"phpstan/phpstan-phpunit": "^0.11",
"symfony/framework-bundle": "^3.0 || ^4.0",
"squizlabs/php_codesniffer": "^3.3.2"
"squizlabs/php_codesniffer": "^3.3.2",
"symfony/serializer": "^3|^4"
},
"conflict": {
"symfony/framework-bundle": "<3.0"
Expand Down
5 changes: 5 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,8 @@ services:
-
class: PHPStan\Type\Symfony\HeaderBagDynamicReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]

# SerializerInterface::deserialize() return type
-
class: PHPStan\Type\Symfony\SerializerInterfaceDynamicReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
45 changes: 45 additions & 0 deletions src/Type/Symfony/SerializerInterfaceDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Symfony;

use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;

class SerializerInterfaceDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
{

public function getClass(): string
{
return 'Symfony\Component\Serializer\SerializerInterface';
}

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === 'deserialize';
}

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
{
$argType = $scope->getType($methodCall->args[1]->value);
if (!$argType instanceof ConstantStringType) {
return new MixedType();
}

$objectName = $argType->getValue();

if (substr($objectName, -2) === '[]') {
// The key type is determined by the data
return new ArrayType(new MixedType(false), new ObjectType(substr($objectName, 0, -2)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part sould be recursive as you can define types like this Foo[][] and the denormalizer will handle them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right of course, #35

}

return new ObjectType($objectName);
}

}
2 changes: 1 addition & 1 deletion tests/Symfony/NeonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testExtensionNeon(): void
], $container->getParameters());

self::assertCount(2, $container->findByTag('phpstan.rules.rule'));
self::assertCount(5, $container->findByTag('phpstan.broker.dynamicMethodReturnTypeExtension'));
self::assertCount(6, $container->findByTag('phpstan.broker.dynamicMethodReturnTypeExtension'));
self::assertCount(3, $container->findByTag('phpstan.typeSpecifier.methodTypeSpecifyingExtension'));
self::assertInstanceOf(ServiceMap::class, $container->getByType(ServiceMap::class));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Symfony;

use Iterator;

final class SerializerInterfaceDynamicReturnTypeExtensionTest extends ExtensionTestCase
{

/**
* @dataProvider getContentProvider
*/
public function testGetContent(string $expression, string $type): void
{
$this->processFile(
__DIR__ . '/serializer.php',
$expression,
$type,
new SerializerInterfaceDynamicReturnTypeExtension()
);
}

public function getContentProvider(): Iterator
{
yield ['$first', 'Bar'];
yield ['$second', 'array<Bar>'];
}

}
8 changes: 8 additions & 0 deletions tests/Type/Symfony/serializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

$serializer = new \Symfony\Component\Serializer\Serializer();

$first = $serializer->deserialize('bar', 'Bar', 'format');
$second = $serializer->deserialize('bar', 'Bar[]', 'format');

die;