Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add support for DenormalizerInterface::denormalize #54

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
lookyman merged 2 commits into phpstan:master from tyx:feature/denormalizer-interface
Oct 6, 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
7 changes: 6 additions & 1 deletion extension.neon
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions ...erInterfaceDynamicReturnTypeExtension.php → .../SerializerDynamicReturnTypeExtension.php
100644 → 100755
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/Symfony/NeonTest.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Type/Symfony/ExtensionTestCase.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ protected function processFile(
true,
true,
true,
[]
[],
true
);
$resolver->setAnalysedFiles([$fileHelper->normalizePath($file)]);

Expand Down
49 changes: 49 additions & 0 deletions tests/Type/Symfony/SerializerDynamicReturnTypeExtensionTest.php
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Symfony;

use Iterator;

final class SerializerDynamicReturnTypeExtensionTest extends ExtensionTestCase
{

/**
* @dataProvider getContentProvider
*/
public function testSerializerInterface(string $expression, string $type): void
{
$this->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<Bar>'];
yield ['$third', 'array<array<Bar>>'];
}

}
View file Open in desktop

This file was deleted.

9 changes: 9 additions & 0 deletions tests/Type/Symfony/denormalizer.php
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types = 1);

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

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

die;

AltStyle によって変換されたページ (->オリジナル) /