-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from enumag/feature/messenger
Add EnvelopeReturnTypeExtension for symfony/messenger
- Loading branch information
Showing
7 changed files
with
110 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?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; | ||
|
||
final class EnvelopeReturnTypeExtension implements DynamicMethodReturnTypeExtension | ||
{ | ||
|
||
public function getClass(): string | ||
{ | ||
return 'Symfony\Component\Messenger\Envelope'; | ||
} | ||
|
||
public function isMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return $methodReflection->getName() === 'all'; | ||
} | ||
|
||
public function getTypeFromMethodCall( | ||
MethodReflection $methodReflection, | ||
MethodCall $methodCall, | ||
Scope $scope | ||
): Type | ||
{ | ||
if (count($methodCall->args) === 0) { | ||
return new ArrayType(new MixedType(), new ArrayType(new MixedType(), new ObjectType('Symfony\Component\Messenger\Stamp\StampInterface'))); | ||
} | ||
|
||
$argType = $scope->getType($methodCall->args[0]->value); | ||
if (!$argType instanceof ConstantStringType) { | ||
return new ArrayType(new MixedType(), new ObjectType('Symfony\Component\Messenger\Stamp\StampInterface')); | ||
} | ||
|
||
return new ArrayType(new MixedType(), new ObjectType($argType->getValue())); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Symfony; | ||
|
||
use Iterator; | ||
use Symfony\Component\Messenger\Stamp\ReceivedStamp; | ||
use Symfony\Component\Messenger\Stamp\StampInterface; | ||
|
||
final class EnvelopeReturnTypeExtensionTest extends ExtensionTestCase | ||
{ | ||
|
||
/** | ||
* @dataProvider getProvider | ||
*/ | ||
public function testAll(string $expression, string $type): void | ||
{ | ||
$this->processFile( | ||
__DIR__ . '/envelope_all.php', | ||
$expression, | ||
$type, | ||
new EnvelopeReturnTypeExtension() | ||
); | ||
} | ||
|
||
public function getProvider(): Iterator | ||
{ | ||
yield ['$test1', 'array<' . ReceivedStamp::class . '>']; | ||
yield ['$test2', 'array<' . StampInterface::class . '>']; | ||
yield ['$test3', 'array<array<' . StampInterface::class . '>>']; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
$envelope = new \Symfony\Component\Messenger\Envelope(new stdClass()); | ||
|
||
$test1 = $envelope->all(\Symfony\Component\Messenger\Stamp\ReceivedStamp::class); | ||
$test2 = $envelope->all(random_bytes(1)); | ||
$test3 = $envelope->all(); | ||
|
||
die; |