Skip to content

Commit a8616d5

Browse files
Support SimultaneousTraverser
1 parent 6c44887 commit a8616d5

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

src/Parser/TypeTraverserInstanceofVisitor.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@
66
use PhpParser\Node;
77
use PhpParser\NodeVisitorAbstract;
88
use PHPStan\DependencyInjection\AutowiredService;
9+
use function in_array;
910

1011
#[AutowiredService]
1112
final class TypeTraverserInstanceofVisitor extends NodeVisitorAbstract
1213
{
1314

1415
public const ATTRIBUTE_NAME = 'insideTypeTraverserMap';
1516

17+
private const TYPE_TRAVERSER_CLASSES = [
18+
'phpstan\\type\\typetraverser',
19+
'phpstan\\type\\simultaneoustypetraverser',
20+
];
21+
1622
private int $depth = 0;
1723

1824
#[Override]
@@ -33,7 +39,7 @@ public function enterNode(Node $node): ?Node
3339
if (
3440
$node instanceof Node\Expr\StaticCall
3541
&& $node->class instanceof Node\Name
36-
&& $node->class->toLowerString() === 'phpstan\\type\\typetraverser'
42+
&& in_array($node->class->toLowerString(), self::TYPE_TRAVERSER_CLASSES, true)
3743
&& $node->name instanceof Node\Identifier
3844
&& $node->name->toLowerString() === 'map'
3945
) {
@@ -49,7 +55,7 @@ public function leaveNode(Node $node): ?Node
4955
if (
5056
$node instanceof Node\Expr\StaticCall
5157
&& $node->class instanceof Node\Name
52-
&& $node->class->toLowerString() === 'phpstan\\type\\typetraverser'
58+
&& in_array($node->class->toLowerString(), self::TYPE_TRAVERSER_CLASSES, true)
5359
&& $node->name instanceof Node\Identifier
5460
&& $node->name->toLowerString() === 'map'
5561
) {

tests/PHPStan/Rules/Api/ApiInstanceofTypeRuleTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,22 @@ public function testRule(): void
2222
$this->analyse([__DIR__ . '/data/instanceof-type.php'], [
2323
[
2424
'Doing instanceof PHPStan\Type\TypeWithClassName is error-prone and deprecated. Use Type::getObjectClassNames() or Type::getObjectClassReflections() instead.',
25-
20,
25+
21,
2626
$tipText,
2727
],
2828
[
2929
'Doing instanceof phpstan\type\typewithclassname is error-prone and deprecated. Use Type::getObjectClassNames() or Type::getObjectClassReflections() instead.',
30-
24,
30+
25,
3131
$tipText,
3232
],
3333
[
3434
'Doing instanceof PHPStan\Type\TypeWithClassName is error-prone and deprecated. Use Type::getObjectClassNames() or Type::getObjectClassReflections() instead.',
35-
36,
35+
37,
3636
$tipText,
3737
],
3838
[
3939
'Doing instanceof PHPStan\Type\Generic\GenericObjectType is error-prone and deprecated.',
40-
40,
40+
41,
4141
$tipText,
4242
],
4343
]);

tests/PHPStan/Rules/Api/data/instanceof-type.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ApiInstanceofType;
44

55
use PHPStan\Type\Generic\GenericObjectType;
6+
use PHPStan\Type\SimultaneousTypeTraverser;
67
use PHPStan\Type\Type;
78
use PHPStan\Type\TypeTraverser;
89
use PHPStan\Type\TypeWithClassName;
@@ -40,6 +41,15 @@ public function doFoo($a, Type $type)
4041
if ($a instanceof GenericObjectType) {
4142

4243
}
44+
45+
$type = SimultaneousTypeTraverser::map($type, $type, function (Type $left, Type $right, callable $traverse): Type {
46+
if ($left instanceof TypeWithClassName) {
47+
return $left;
48+
}
49+
50+
return $traverse($left, $right);
51+
});
52+
4353
}
4454

4555
}

0 commit comments

Comments
 (0)