mirror of
https://github.com/rectorphp/rector.git
synced 2024-07-03 08:03:30 +00:00
Merge pull request #2311 from jeroensmit/class-constant-with-trait
[DeadCode] Class constant with trait
This commit is contained in:
commit
288946da41
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\DeadCode\Tests\Rector\ClassConst\RemoveUnusedPrivateConstantRector\UsedInTrait;
|
||||
|
||||
final class KeepStaticConstant
|
||||
{
|
||||
use aTrait;
|
||||
private const SOME_CONSTANT = 5;
|
||||
|
||||
}
|
||||
|
||||
trait aTrait {
|
||||
public function foo(){
|
||||
return self::SOME_CONSTANT;
|
||||
}
|
||||
}
|
|
@ -217,6 +217,7 @@ parameters:
|
|||
- '#Parameter \#2 \$versionedSets of method Rector\\Bootstrap\\SetOptionResolver\:\:createSetListInString\(\) expects array<array<string\>\>, array<array<string\>\|string\> given#'
|
||||
- '#Parameter \#1 \$node of method Rector\\Rector\\AbstractRector\:\:removeNode\(\) expects PhpParser\\Node, PhpParser\\Node\|null given#'
|
||||
- '#Cannot access property \$props on PhpParser\\Node\|null#'
|
||||
- '#Parameter \#1 \$nodes of method Rector\\PhpParser\\Node\\BetterNodeFinder\:\:find\(\) expects array<PhpParser\\Node\>\|PhpParser\\Node, array<int, PhpParser\\Node\\Stmt\\Class_\|PhpParser\\Node\\Stmt\\Trait_\|null\> given#'
|
||||
|
||||
# test
|
||||
- '#Class Rector\\DynamicTypeAnalysis\\Tests\\Rector\\ClassMethod\\AddArgumentTypeWithProbeDataRector\\Fixture\\SomeClass not found#'
|
||||
|
|
|
@ -7,6 +7,7 @@ namespace Rector\PhpParser\Node\Manipulator;
|
|||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\ClassConstFetch;
|
||||
use PhpParser\Node\Stmt\ClassConst;
|
||||
use Rector\NodeContainer\ParsedNodesByType;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\PhpParser\Node\Resolver\NameResolver;
|
||||
|
@ -29,14 +30,28 @@ final class ClassConstManipulator
|
|||
*/
|
||||
private $betterStandardPrinter;
|
||||
|
||||
/**
|
||||
* @var ParsedNodesByType
|
||||
*/
|
||||
private $parsedNodesByType;
|
||||
|
||||
/**
|
||||
* @var ClassManipulator
|
||||
*/
|
||||
private $classManipulator;
|
||||
|
||||
public function __construct(
|
||||
NameResolver $nameResolver,
|
||||
BetterNodeFinder $betterNodeFinder,
|
||||
BetterStandardPrinter $betterStandardPrinter
|
||||
BetterStandardPrinter $betterStandardPrinter,
|
||||
ParsedNodesByType $parsedNodesByType,
|
||||
ClassManipulator $classManipulator
|
||||
) {
|
||||
$this->nameResolver = $nameResolver;
|
||||
$this->betterNodeFinder = $betterNodeFinder;
|
||||
$this->betterStandardPrinter = $betterStandardPrinter;
|
||||
$this->parsedNodesByType = $parsedNodesByType;
|
||||
$this->classManipulator = $classManipulator;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -44,12 +59,21 @@ final class ClassConstManipulator
|
|||
*/
|
||||
public function getAllClassConstFetch(ClassConst $classConst): array
|
||||
{
|
||||
/** @var Node\Stmt\Class_|null $classNode */
|
||||
$classNode = $classConst->getAttribute(AttributeKey::CLASS_NODE);
|
||||
if ($classNode === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->betterNodeFinder->find($classNode, function (Node $node) use ($classConst): bool {
|
||||
$searchInNodes = [$classNode];
|
||||
foreach ($this->classManipulator->getUsedTraits($classNode) as $trait) {
|
||||
$trait_ = $this->parsedNodesByType->findTrait((string) $trait);
|
||||
if ($trait !== null) {
|
||||
$searchInNodes[] = $trait_;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->betterNodeFinder->find($searchInNodes, function (Node $node) use ($classConst): bool {
|
||||
// itself
|
||||
if ($this->betterStandardPrinter->areNodesEqual($node, $classConst)) {
|
||||
return false;
|
||||
|
|
Loading…
Reference in New Issue
Block a user