-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
149 additions
and
86 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace ShipMonk\PHPStan\DeadCode\Transformer; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Const_; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Stmt\ClassConst; | ||
use PhpParser\Node\Stmt\ClassLike; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\Node\Stmt\Namespace_; | ||
use PhpParser\NodeTraverser; | ||
use PhpParser\NodeVisitorAbstract; | ||
use function array_fill_keys; | ||
use function ltrim; | ||
|
||
class RemoveClassMemberVisitor extends NodeVisitorAbstract | ||
{ | ||
|
||
private string $currentNamespace = ''; | ||
|
||
private string $currentClass = ''; | ||
|
||
/** | ||
* @var array<string, true> | ||
*/ | ||
private array $deadMethods; | ||
|
||
/** | ||
* @var array<string, true> | ||
*/ | ||
private array $deadConstants; | ||
|
||
/** | ||
* @param list<string> $deadMethods | ||
* @param list<string> $deadConstants | ||
*/ | ||
public function __construct(array $deadMethods, array $deadConstants) | ||
{ | ||
$this->deadMethods = array_fill_keys($deadMethods, true); | ||
$this->deadConstants = array_fill_keys($deadConstants, true); | ||
} | ||
|
||
public function enterNode(Node $node): ?Node | ||
{ | ||
if ($node instanceof Namespace_ && $node->name !== null) { | ||
$this->currentNamespace = $node->name->toString(); | ||
|
||
} elseif ($node instanceof ClassLike && $node->name !== null) { | ||
$this->currentClass = $node->name->name; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function leaveNode(Node $node): ?int | ||
{ | ||
if ($node instanceof ClassMethod) { | ||
$methodKey = $this->getNamespacedName($node->name); | ||
|
||
if (isset($this->deadMethods[$methodKey])) { | ||
return NodeTraverser::REMOVE_NODE; | ||
} | ||
} | ||
|
||
if ($node instanceof ClassConst) { | ||
$allDead = true; | ||
|
||
foreach ($node->consts as $const) { | ||
$constKey = $this->getNamespacedName($const->name); | ||
|
||
if (!isset($this->deadConstants[$constKey])) { | ||
$allDead = false; | ||
break; | ||
} | ||
} | ||
|
||
if ($allDead) { | ||
return NodeTraverser::REMOVE_NODE; | ||
} | ||
} | ||
|
||
if ($node instanceof Const_) { | ||
$constKey = $this->getNamespacedName($node->name); | ||
|
||
if (isset($this->deadConstants[$constKey])) { | ||
return NodeTraverser::REMOVE_NODE; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @param Name|Identifier $name | ||
*/ | ||
private function getNamespacedName(Node $name): string | ||
{ | ||
return ltrim($this->currentNamespace . '\\' . $this->currentClass, '\\') . '::' . $name->name; | ||
} | ||
|
||
} |
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 was deleted.
Oops, something went wrong.
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
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