-
Notifications
You must be signed in to change notification settings - Fork 94
Add EnvelopeReturnTypeExtension for symfony/messenger #44
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?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; | ||
use Symfony\Component\Messenger\Stamp\StampInterface; | ||
|
||
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(StampInterface::class))); | ||
} | ||
|
||
$argType = $scope->getType($methodCall->args[0]->value); | ||
if (!$argType instanceof ConstantStringType) { | ||
return new ArrayType(new MixedType(), new ObjectType(StampInterface::class)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ...and here... |
||
} | ||
|
||
return new ArrayType(new MixedType(), new ObjectType($argType->getValue())); | ||
} | ||
|
||
} |
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 . '>>']; | ||
} | ||
|
||
} |
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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any better way to pass a "non-constant-string" in this test? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this is fine. |
||
$test3 = $envelope->all(); | ||
|
||
die; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
StampInterface
needs to be a string here, so that composer require checker doesn't complain. I know, not ideal.