Skip to content

Commit

Permalink
Fix Envelope::all() return type
Browse files Browse the repository at this point in the history
  • Loading branch information
enumag authored and ondrejmirtes committed Nov 4, 2022
1 parent 7210072 commit d6ea162
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 15 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"require": {
"php": "^7.2 || ^8.0",
"ext-simplexml": "*",
"phpstan/phpstan": "^1.8.2"
"phpstan/phpstan": "^1.9.1"
},
"conflict": {
"symfony/framework-bundle": "<3.0"
Expand Down
13 changes: 9 additions & 4 deletions src/Type/Symfony/EnvelopeReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\MixedType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use function count;
Expand All @@ -33,15 +35,18 @@ public function getTypeFromMethodCall(
): Type
{
if (count($methodCall->getArgs()) === 0) {
return new ArrayType(new MixedType(), new ArrayType(new MixedType(), new ObjectType('Symfony\Component\Messenger\Stamp\StampInterface')));
return new ArrayType(
new GenericClassStringType(new ObjectType('Symfony\Component\Messenger\Stamp\StampInterface')),
AccessoryArrayListType::intersectWith(new ArrayType(new IntegerType(), new ObjectType('Symfony\Component\Messenger\Stamp\StampInterface')))
);
}

$argType = $scope->getType($methodCall->getArgs()[0]->value);
if (!$argType instanceof ConstantStringType) {
return new ArrayType(new MixedType(), new ObjectType('Symfony\Component\Messenger\Stamp\StampInterface'));
return AccessoryArrayListType::intersectWith(new ArrayType(new IntegerType(), new ObjectType('Symfony\Component\Messenger\Stamp\StampInterface')));
}

return new ArrayType(new MixedType(), new ObjectType($argType->getValue()));
return AccessoryArrayListType::intersectWith(new ArrayType(new IntegerType(), new ObjectType($argType->getValue())));
}

}
7 changes: 0 additions & 7 deletions stubs/Symfony/Component/Messenger/Envelope.stub
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,4 @@ final class Envelope
public function last(string $stampFqcn): ?StampInterface
{
}

/**
* @return ($stampFqcn is null ? array<string, list<StampInterface>> : list<StampInterface>)
*/
public function all(?string $stampFqcn = null): array
{
}
}
1 change: 1 addition & 0 deletions tests/Type/Symfony/ExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public static function getAdditionalConfigFiles(): array
return [
__DIR__ . '/../../../extension.neon',
__DIR__ . '/extension-test.neon',
'phar://' . __DIR__ . '/../../../vendor/phpstan/phpstan/phpstan.phar/conf/bleedingEdge.neon',
];
}

Expand Down
6 changes: 3 additions & 3 deletions tests/Type/Symfony/data/envelope_all.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

$envelope = new \Symfony\Component\Messenger\Envelope(new stdClass());

assertType('array<Symfony\Component\Messenger\Stamp\ReceivedStamp>', $envelope->all(\Symfony\Component\Messenger\Stamp\ReceivedStamp::class));
assertType('array<Symfony\Component\Messenger\Stamp\StampInterface>', $envelope->all(random_bytes(1)));
assertType('array<array<Symfony\Component\Messenger\Stamp\StampInterface>>', $envelope->all());
assertType('list<Symfony\Component\Messenger\Stamp\ReceivedStamp>', $envelope->all(\Symfony\Component\Messenger\Stamp\ReceivedStamp::class));
assertType('list<Symfony\Component\Messenger\Stamp\StampInterface>', $envelope->all(random_bytes(1)));
assertType('array<class-string<Symfony\Component\Messenger\Stamp\StampInterface>, list<Symfony\Component\Messenger\Stamp\StampInterface>>', $envelope->all());

0 comments on commit d6ea162

Please sign in to comment.