Skip to content

Commit d6ea162

Browse files
enumagondrejmirtes
authored andcommitted
Fix Envelope::all() return type
1 parent 7210072 commit d6ea162

File tree

5 files changed

+14
-15
lines changed

5 files changed

+14
-15
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"require": {
1616
"php": "^7.2 || ^8.0",
1717
"ext-simplexml": "*",
18-
"phpstan/phpstan": "^1.8.2"
18+
"phpstan/phpstan": "^1.9.1"
1919
},
2020
"conflict": {
2121
"symfony/framework-bundle": "<3.0"

src/Type/Symfony/EnvelopeReturnTypeExtension.php

+9-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
use PhpParser\Node\Expr\MethodCall;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\MethodReflection;
8+
use PHPStan\Type\Accessory\AccessoryArrayListType;
89
use PHPStan\Type\ArrayType;
910
use PHPStan\Type\Constant\ConstantStringType;
1011
use PHPStan\Type\DynamicMethodReturnTypeExtension;
11-
use PHPStan\Type\MixedType;
12+
use PHPStan\Type\Generic\GenericClassStringType;
13+
use PHPStan\Type\IntegerType;
1214
use PHPStan\Type\ObjectType;
1315
use PHPStan\Type\Type;
1416
use function count;
@@ -33,15 +35,18 @@ public function getTypeFromMethodCall(
3335
): Type
3436
{
3537
if (count($methodCall->getArgs()) === 0) {
36-
return new ArrayType(new MixedType(), new ArrayType(new MixedType(), new ObjectType('Symfony\Component\Messenger\Stamp\StampInterface')));
38+
return new ArrayType(
39+
new GenericClassStringType(new ObjectType('Symfony\Component\Messenger\Stamp\StampInterface')),
40+
AccessoryArrayListType::intersectWith(new ArrayType(new IntegerType(), new ObjectType('Symfony\Component\Messenger\Stamp\StampInterface')))
41+
);
3742
}
3843

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

44-
return new ArrayType(new MixedType(), new ObjectType($argType->getValue()));
49+
return AccessoryArrayListType::intersectWith(new ArrayType(new IntegerType(), new ObjectType($argType->getValue())));
4550
}
4651

4752
}

stubs/Symfony/Component/Messenger/Envelope.stub

-7
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,4 @@ final class Envelope
1414
public function last(string $stampFqcn): ?StampInterface
1515
{
1616
}
17-
18-
/**
19-
* @return ($stampFqcn is null ? array<string, list<StampInterface>> : list<StampInterface>)
20-
*/
21-
public function all(?string $stampFqcn = null): array
22-
{
23-
}
2417
}

tests/Type/Symfony/ExtensionTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public static function getAdditionalConfigFiles(): array
7676
return [
7777
__DIR__ . '/../../../extension.neon',
7878
__DIR__ . '/extension-test.neon',
79+
'phar://' . __DIR__ . '/../../../vendor/phpstan/phpstan/phpstan.phar/conf/bleedingEdge.neon',
7980
];
8081
}
8182

tests/Type/Symfony/data/envelope_all.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

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

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

0 commit comments

Comments
 (0)