Skip to content
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
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
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
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
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
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>>'];
}

}

This file was deleted.

9 changes: 9 additions & 0 deletions tests/Type/Symfony/denormalizer.php
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;