Skip to content
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
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 SkipArrayCallalbesFqn
{
public function run()
{
$array = [3, 2, 1];

usort($array, [SkipArrayCallalbesFqn::class, 'sort']);

return $array;
}

private function sort($a, $b)
{
return $a <=> $b;
}
}
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 SkipArrayCallalbesSelf
{
public function run()
{
$array = [3, 2, 1];

usort($array, [self::class, 'sort']);

return $array;
}

private function sort($a, $b)
{
return $a <=> $b;
}
}
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 SkipArrayCallalbesStatic
{
public function run()
{
$array = [3, 2, 1];

usort($array, [static::class, 'sort']);

return $array;
}

private function sort($a, $b)
{
return $a <=> $b;
}
}
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 SkipArrayCallalbesThis
{
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,7 +20,11 @@ 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',

__DIR__ . '/Fixture/skip_array_callables_this.php.inc',
__DIR__ . '/Fixture/skip_array_callables_self.php.inc',
__DIR__ . '/Fixture/skip_array_callables_static.php.inc',
__DIR__ . '/Fixture/skip_array_callables_fqn.php.inc',
]);
}

Expand Down
37 changes: 32 additions & 5 deletions src/NodeContainer/ParsedNodesByType.php
Original file line number Diff line number Diff line change
Expand Up @@ -615,11 +615,8 @@ private function matchArrayCallableClassAndMethod(Array_ $array): ?array
return null;
}

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

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

Expand All @@ -637,6 +634,36 @@ private function matchArrayCallableClassAndMethod(Array_ $array): ?array
$methodName = $string->value;
$className = $array->getAttribute(AttributeKey::CLASS_NAME);

if ($className === null) {
return null;
}

return [$className, $methodName];
}

private function isThisVariable(Node $node): bool
{
// $this
if ($node instanceof Variable && $this->nameResolver->isName($node, 'this')) {
return true;
}

if ($node instanceof ClassConstFetch) {
if (! $this->nameResolver->isName($node->name, 'class')) {
return false;
}

// self::class, static::class
if ($this->nameResolver->isNames($node->class, ['self', 'static'])) {
return true;
}

/** @var string $className */
$className = $node->getAttribute(AttributeKey::CLASS_NAME);

return $this->nameResolver->isName($node->class, $className);
}

return false;
}
}