Skip to content

Commit d4619ca

Browse files
committed
Add EnvelopeReturnTypeExtension for symfony/messenger
1 parent 649c258 commit d4619ca

File tree

4 files changed

+90
-1
lines changed

4 files changed

+90
-1
lines changed

Diff for: composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"phpstan/phpstan-phpunit": "^0.11",
3434
"symfony/framework-bundle": "^3.0 || ^4.0",
3535
"squizlabs/php_codesniffer": "^3.3.2",
36-
"symfony/serializer": "^3|^4"
36+
"symfony/serializer": "^3|^4",
37+
"symfony/messenger": "^4.2"
3738
},
3839
"conflict": {
3940
"symfony/framework-bundle": "<3.0"

Diff for: src/Type/Symfony/EnvelopeReturnTypeExtension.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Symfony;
4+
5+
use PhpParser\Node\Expr\MethodCall;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Reflection\MethodReflection;
8+
use PHPStan\Type\ArrayType;
9+
use PHPStan\Type\Constant\ConstantStringType;
10+
use PHPStan\Type\DynamicMethodReturnTypeExtension;
11+
use PHPStan\Type\MixedType;
12+
use PHPStan\Type\ObjectType;
13+
use PHPStan\Type\Type;
14+
use Symfony\Component\Messenger\Stamp\StampInterface;
15+
16+
final class EnvelopeReturnTypeExtension implements DynamicMethodReturnTypeExtension
17+
{
18+
19+
public function getClass(): string
20+
{
21+
return 'Symfony\Component\Messenger\Envelope';
22+
}
23+
24+
public function isMethodSupported(MethodReflection $methodReflection): bool
25+
{
26+
return $methodReflection->getName() === 'all';
27+
}
28+
29+
public function getTypeFromMethodCall(
30+
MethodReflection $methodReflection,
31+
MethodCall $methodCall,
32+
Scope $scope
33+
): Type
34+
{
35+
if (count($methodCall->args) === 0) {
36+
return new ArrayType(new MixedType(), new ArrayType(new MixedType(), new ObjectType(StampInterface::class)));
37+
}
38+
39+
$argType = $scope->getType($methodCall->args[0]->value);
40+
if (!$argType instanceof ConstantStringType) {
41+
return new ArrayType(new MixedType(), new ObjectType(StampInterface::class));
42+
}
43+
44+
return new ArrayType(new MixedType(), new ObjectType($argType->getValue()));
45+
}
46+
47+
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Symfony;
4+
5+
use Iterator;
6+
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
7+
use Symfony\Component\Messenger\Stamp\StampInterface;
8+
9+
final class EnvelopeReturnTypeExtensionTest extends ExtensionTestCase
10+
{
11+
12+
/**
13+
* @dataProvider getProvider
14+
*/
15+
public function testAll(string $expression, string $type): void
16+
{
17+
$this->processFile(
18+
__DIR__ . '/envelope_all.php',
19+
$expression,
20+
$type,
21+
new EnvelopeReturnTypeExtension()
22+
);
23+
}
24+
25+
public function getProvider(): Iterator
26+
{
27+
yield ['$test1', 'array<' . ReceivedStamp::class . '>'];
28+
yield ['$test2', 'array<' . StampInterface::class . '>'];
29+
yield ['$test3', 'array<array<' . StampInterface::class . '>>'];
30+
}
31+
32+
}

Diff for: tests/Type/Symfony/envelope_all.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types = 1);
2+
3+
$envelope = new \Symfony\Component\Messenger\Envelope(new stdClass());
4+
5+
$test1 = $envelope->all(\Symfony\Component\Messenger\Stamp\ReceivedStamp::class);
6+
$test2 = $envelope->all(random_bytes(1));
7+
$test3 = $envelope->all();
8+
9+
die;

0 commit comments

Comments
 (0)