Skip to content

Commit 4794631

Browse files
committedOct 6, 2019
Merge branch 'feature/denormalizer-interface' of git://github.com/tyx/phpstan-symfony into tyx-feature/denormalizer-interface
2 parents 36c8cd7 + 7d34a13 commit 4794631

7 files changed

+82
-36
lines changed
 

Diff for: ‎extension.neon

+6-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ services:
6666

6767
# SerializerInterface::deserialize() return type
6868
-
69-
factory: PHPStan\Type\Symfony\SerializerInterfaceDynamicReturnTypeExtension
69+
factory: PHPStan\Type\Symfony\SerializerDynamicReturnTypeExtension(Symfony\Component\Serializer\SerializerInterface, deserialize)
70+
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
71+
72+
# DenormalizerInterface::denormalize() return type
73+
-
74+
factory: PHPStan\Type\Symfony\SerializerDynamicReturnTypeExtension(Symfony\Component\Serializer\Normalizer\DenormalizerInterface, denormalize)
7075
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
7176

7277
# Envelope::all() return type

Diff for: ‎src/Type/Symfony/SerializerInterfaceDynamicReturnTypeExtension.php renamed to ‎src/Type/Symfony/SerializerDynamicReturnTypeExtension.php

+15-3
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,29 @@
1212
use PHPStan\Type\ObjectType;
1313
use PHPStan\Type\Type;
1414

15-
class SerializerInterfaceDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
15+
class SerializerDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
1616
{
1717

18+
/** @var string */
19+
private $class;
20+
21+
/** @var string */
22+
private $method;
23+
24+
public function __construct(string $class, string $method)
25+
{
26+
$this->class = $class;
27+
$this->method = $method;
28+
}
29+
1830
public function getClass(): string
1931
{
20-
return 'Symfony\Component\Serializer\SerializerInterface';
32+
return $this->class;
2133
}
2234

2335
public function isMethodSupported(MethodReflection $methodReflection): bool
2436
{
25-
return $methodReflection->getName() === 'deserialize';
37+
return $methodReflection->getName() === $this->method;
2638
}
2739

2840
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type

Diff for: ‎tests/Symfony/NeonTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function testExtensionNeon(): void
4646
], $parameters);
4747

4848
self::assertCount(6, $container->findByTag('phpstan.rules.rule'));
49-
self::assertCount(11, $container->findByTag('phpstan.broker.dynamicMethodReturnTypeExtension'));
49+
self::assertCount(12, $container->findByTag('phpstan.broker.dynamicMethodReturnTypeExtension'));
5050
self::assertCount(5, $container->findByTag('phpstan.typeSpecifier.methodTypeSpecifyingExtension'));
5151
self::assertInstanceOf(ServiceMap::class, $container->getByType(ServiceMap::class));
5252
}

Diff for: ‎tests/Type/Symfony/ExtensionTestCase.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ protected function processFile(
5252
true,
5353
true,
5454
true,
55-
[]
55+
[],
56+
true
5657
);
5758
$resolver->setAnalysedFiles([$fileHelper->normalizePath($file)]);
5859

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Symfony;
4+
5+
use Iterator;
6+
7+
final class SerializerDynamicReturnTypeExtensionTest extends ExtensionTestCase
8+
{
9+
10+
/**
11+
* @dataProvider getContentProvider
12+
*/
13+
public function testSerializerInterface(string $expression, string $type): void
14+
{
15+
$this->processFile(
16+
__DIR__ . '/serializer.php',
17+
$expression,
18+
$type,
19+
new SerializerDynamicReturnTypeExtension(
20+
'Symfony\Component\Serializer\SerializerInterface',
21+
'deserialize'
22+
)
23+
);
24+
}
25+
26+
/**
27+
* @dataProvider getContentProvider
28+
*/
29+
public function testDenormalizerInterface(string $expression, string $type): void
30+
{
31+
$this->processFile(
32+
__DIR__ . '/denormalizer.php',
33+
$expression,
34+
$type,
35+
new SerializerDynamicReturnTypeExtension(
36+
'Symfony\Component\Serializer\Normalizer\DenormalizerInterface',
37+
'denormalize'
38+
)
39+
);
40+
}
41+
42+
public function getContentProvider(): Iterator
43+
{
44+
yield ['$first', 'Bar'];
45+
yield ['$second', 'array<Bar>'];
46+
yield ['$third', 'array<array<Bar>>'];
47+
}
48+
49+
}

Diff for: ‎tests/Type/Symfony/SerializerInterfaceDynamicReturnTypeExtensionTest.php

-30
This file was deleted.

Diff for: ‎tests/Type/Symfony/denormalizer.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types = 1);
2+
3+
$serializer = new \Symfony\Component\Serializer\Serializer();
4+
5+
$first = $serializer->denormalize('bar', 'Bar', 'format');
6+
$second = $serializer->denormalize('bar', 'Bar[]', 'format');
7+
$third = $serializer->denormalize('bar', 'Bar[][]', 'format');
8+
9+
die;

0 commit comments

Comments
 (0)