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 74a8692
Showing
5 changed files
with
84 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,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Icanhazstring\Composer\Unused\Parser\PHP\Strategy; | ||
|
||
use PhpParser\Node; | ||
|
||
final 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&Node\Expr\Instanceof_ $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,33 @@ | ||
<?php | ||
|
||
namespace Foo\Bar { | ||
class Baz {} | ||
class Qux {} | ||
class Quz {} | ||
class Corge {} | ||
} | ||
|
||
namespace { | ||
|
||
// // https://github.com/composer-unused/composer-unused/pull/100#discussion_r506167242 | ||
// | ||
// use Foo\Bar; | ||
// | ||
// if ($foo instanceof Bar\Baz) { | ||
// // usage should be detected because class is a name, relative to imported namespace | ||
// } | ||
|
||
if ($foo instanceof Foo\Bar\Qux) { | ||
// usage should be detected because class is a fully-qualified name used in the root namespace | ||
} | ||
|
||
if ($foo instanceof \Foo\Bar\Quz) { | ||
// usage should be detected because class is a fully-qualified name relative to the root namespace | ||
} | ||
|
||
$name = 'Foo\Bar\Corge'; | ||
|
||
if ($foo instanceof $name) { | ||
// usage should not be detected because class is a variable expression | ||
} | ||
} |