Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement: Implement InstanceofStrategy
- Loading branch information
1 parent
c368680
commit d8350d8
Showing
5 changed files
with
66 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Icanhazstring\Composer\Unused\Parser\PHP\Strategy; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ClassConstFetch; | ||
|
||
class InstanceofStrategy implements ParseStrategyInterface | ||
{ | ||
public function meetsCriteria(Node $node): bool | ||
{ | ||
if (!$node instanceof Node\Expr\Instanceof_) { | ||
return false; | ||
} | ||
|
||
if (!$node->class instanceof Node\Name) { | ||
return false; | ||
} | ||
|
||
return $node->class->isFullyQualified() || $node->class->isQualified(); | ||
} | ||
|
||
/** | ||
* @param Node&ClassConstFetch $node | ||
* @return array<string> | ||
*/ | ||
public function extractNamespaces(Node $node): array | ||
{ | ||
/** @var Node\Name $class */ | ||
$class = $node->class; | ||
|
||
return [ | ||
$class->toString(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
use Foo\Bar; | ||
use Baz\Qux; | ||
|
||
|
||
if ($foo instanceof Bar\Baz) { | ||
// usage should be detected because class is a name | ||
} | ||
|
||
$baz = 'Qux\Quux'; | ||
|
||
if ($bar instanceof $baz) { | ||
// usage should not be detected because class is a variable expression | ||
} |