Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DeadCode] Keep array method call in RemoveUnusedPrivateMethodRector #1716

Merged
merged 1 commit into from
Jul 9, 2019
Merged
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
1 change: 1 addition & 0 deletions ecs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ parameters:

Symplify\CodingStandard\Sniffs\CleanCode\CognitiveComplexitySniff:
# tough logic
- 'src/NodeContainer/ParsedNodesByType.php'
- 'packages/PHPStan/src/Rector/Node/RemoveNonExistingVarAnnotationRector.php'
- 'packages/Architecture/src/Rector/Class_/ConstructorInjectionToActionInjectionRector.php'
- 'src/PhpParser/Node/Commander/NodeRemovingCommander.php'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Rector\CodeQuality\Tests\Rector\If_\ExplicitBoolCompareRector\Fixture;

final class String_
final class ExplicitString
{
public function run(string $item)
{
Expand All @@ -22,7 +22,7 @@ final class String_

namespace Rector\CodeQuality\Tests\Rector\If_\ExplicitBoolCompareRector\Fixture;

final class String_
final class ExplicitString
{
public function run(string $item)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Trait_;
use Rector\NodeContainer\ParsedNodesByType;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand Down Expand Up @@ -70,37 +70,43 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
/** @var ClassLike|null $classNode */
$classNode = $node->getAttribute(AttributeKey::CLASS_NODE);
if ($classNode === null) {
if ($this->shouldSkip($node)) {
return null;
}

// unreliable to detect trait method usage
if ($classNode instanceof Trait_) {
$classMethodCalls = $this->parsedNodesByType->findClassMethodCalls($node);
if ($classMethodCalls !== []) {
return null;
}

if ($classNode instanceof Class_ && $classNode->isAnonymous()) {
return null;
}
$this->removeNode($node);

// skips interfaces by default too
if (! $node->isPrivate()) {
return null;
return $node;
}

private function shouldSkip(ClassMethod $classMethod): bool
{
/** @var Class_|Interface_|Trait_|null $classNode */
$classNode = $classMethod->getAttribute(AttributeKey::CLASS_NODE);
if ($classNode === null) {
return true;
}

if ($this->isName($node, '__*')) {
return null;
// unreliable to detect trait, interface doesn't make sense
if ($classNode instanceof Trait_ || $classNode instanceof Interface_) {
return true;
}

$classMethodCalls = $this->parsedNodesByType->findClassMethodCalls($node);
if ($classMethodCalls !== []) {
return null;
if ($classNode->isAnonymous()) {
return true;
}

$this->removeNode($node);
// skips interfaces by default too
if (! $classMethod->isPrivate()) {
return true;
}

return $node;
// skip magic methods - @see https://www.php.net/manual/en/language.oop5.magic.php
return $this->isName($classMethod, '__*');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Rector\DeadCode\Tests\Rector\ClassMethod\RemoveUnusedPrivateMethodRector\Fixture;

final class SkipArrayCallalbes
{
public function run()
{
$array = [3, 2, 1];

usort($array, [$this, 'sort']);

return $array;
}

private function sort($a, $b)
{
return $a <=> $b;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function test(): void
__DIR__ . '/Fixture/keep_used_method.php.inc',
__DIR__ . '/Fixture/keep_used_method_static.php.inc',
__DIR__ . '/Fixture/skip_anonymous_class.php.inc',
__DIR__ . '/Fixture/skip_array_callables.php.inc',
]);
}

Expand Down
72 changes: 68 additions & 4 deletions src/NodeContainer/ParsedNodesByType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
Expand Down Expand Up @@ -43,6 +44,8 @@ final class ParsedNodesByType
New_::class,
StaticCall::class,
MethodCall::class,
// for array callable - [$this, 'someCall']
Array_::class,
];

/**
Expand Down Expand Up @@ -81,10 +84,16 @@ final class ParsedNodesByType
private $simpleParsedNodesByType = [];

/**
* @var mixed[]
* @var MethodCall[][][]|StaticCall[][][]
*/
private $methodsCallsByTypeAndMethod = [];

/**
* E.g. [$this, 'someLocalMethod']
* @var Array_[][][]
*/
private $arrayCallablesByTypeAndMethod = [];

public function __construct(NameResolver $nameResolver)
{
$this->nameResolver = $nameResolver;
Expand Down Expand Up @@ -388,6 +397,22 @@ public function collect(Node $node): void
return;
}

// array callable - [$this, 'someCall']
if ($node instanceof Array_) {
$arrayCallableClassAndMethod = $this->matchArrayCallableClassAndMethod($node);
if ($arrayCallableClassAndMethod === null) {
return;
}

[$className, $methodName] = $arrayCallableClassAndMethod;
if (! method_exists($className, $methodName)) {
return;
}

$this->arrayCallablesByTypeAndMethod[$className][$methodName][] = $node;
return;
}

if ($node instanceof MethodCall || $node instanceof StaticCall) {
$this->addCall($node);
return;
Expand All @@ -399,7 +424,7 @@ public function collect(Node $node): void
}

/**
* @return MethodCall[]
* @return MethodCall[]|StaticCall[]|Array_[]
*/
public function findClassMethodCalls(ClassMethod $classMethod): array
{
Expand All @@ -413,11 +438,11 @@ public function findClassMethodCalls(ClassMethod $classMethod): array
return [];
}

return $this->methodsCallsByTypeAndMethod[$className][$methodName] ?? [];
return $this->methodsCallsByTypeAndMethod[$className][$methodName] ?? $this->arrayCallablesByTypeAndMethod[$className][$methodName] ?? [];
}

/**
* @return MethodCall[]|StaticCall[]
* @return MethodCall[][]|StaticCall[][]
*/
public function findMethodCallsOnClass(string $className): array
{
Expand Down Expand Up @@ -575,4 +600,43 @@ private function addCall(Node $node): void

$this->methodsCallsByTypeAndMethod[$className][$methodName][] = $node;
}

/**
* Matches array like: "[$this, 'methodName']" → ['ClassName', 'methodName']
* @return string[]|null
*/
private function matchArrayCallableClassAndMethod(Array_ $array): ?array
{
if (count($array->items) !== 2) {
return null;
}

if ($array->items[0] === null) {
return null;
}

if (! $array->items[0]->value instanceof Variable) {
return null;
}

if (! $this->nameResolver->isName($array->items[0]->value, 'this')) {
return null;
}

if ($array->items[1] === null) {
return null;
}

if (! $array->items[1]->value instanceof Node\Scalar\String_) {
return null;
}

/** @var Node\Scalar\String_ $string */
$string = $array->items[1]->value;

$methodName = $string->value;
$className = $array->getAttribute(AttributeKey::CLASS_NAME);

return [$className, $methodName];
}
}