Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Parser/TypeTraverserInstanceofVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@
use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;
use PHPStan\DependencyInjection\AutowiredService;
use function in_array;

#[AutowiredService]
final class TypeTraverserInstanceofVisitor extends NodeVisitorAbstract
{

public const ATTRIBUTE_NAME = 'insideTypeTraverserMap';

private const TYPE_TRAVERSER_CLASSES = [
'phpstan\\type\\typetraverser',
'phpstan\\type\\simultaneoustypetraverser',
];

private int $depth = 0;

#[Override]
Expand All @@ -33,7 +39,7 @@ public function enterNode(Node $node): ?Node
if (
$node instanceof Node\Expr\StaticCall
&& $node->class instanceof Node\Name
&& $node->class->toLowerString() === 'phpstan\\type\\typetraverser'
&& in_array($node->class->toLowerString(), self::TYPE_TRAVERSER_CLASSES, true)
&& $node->name instanceof Node\Identifier
&& $node->name->toLowerString() === 'map'
) {
Expand All @@ -49,7 +55,7 @@ public function leaveNode(Node $node): ?Node
if (
$node instanceof Node\Expr\StaticCall
&& $node->class instanceof Node\Name
&& $node->class->toLowerString() === 'phpstan\\type\\typetraverser'
&& in_array($node->class->toLowerString(), self::TYPE_TRAVERSER_CLASSES, true)
&& $node->name instanceof Node\Identifier
&& $node->name->toLowerString() === 'map'
) {
Expand Down
8 changes: 4 additions & 4 deletions tests/PHPStan/Rules/Api/ApiInstanceofTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ public function testRule(): void
$this->analyse([__DIR__ . '/data/instanceof-type.php'], [
[
'Doing instanceof PHPStan\Type\TypeWithClassName is error-prone and deprecated. Use Type::getObjectClassNames() or Type::getObjectClassReflections() instead.',
20,
21,
$tipText,
],
[
'Doing instanceof phpstan\type\typewithclassname is error-prone and deprecated. Use Type::getObjectClassNames() or Type::getObjectClassReflections() instead.',
24,
25,
$tipText,
],
[
'Doing instanceof PHPStan\Type\TypeWithClassName is error-prone and deprecated. Use Type::getObjectClassNames() or Type::getObjectClassReflections() instead.',
36,
37,
$tipText,
],
[
'Doing instanceof PHPStan\Type\Generic\GenericObjectType is error-prone and deprecated.',
40,
41,
$tipText,
],
]);
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/Api/data/instanceof-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ApiInstanceofType;

use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\SimultaneousTypeTraverser;
use PHPStan\Type\Type;
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\TypeWithClassName;
Expand Down Expand Up @@ -40,6 +41,15 @@ public function doFoo($a, Type $type)
if ($a instanceof GenericObjectType) {

}

$type = SimultaneousTypeTraverser::map($type, $type, function (Type $left, Type $right, callable $traverse): Type {
if ($left instanceof TypeWithClassName) {
return $left;
}

return $traverse($left, $right);
});

}

}
Loading