Skip to content
Closed
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
2 changes: 0 additions & 2 deletions bin/rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
use Rector\Bootstrap\AutoloadFileParameterResolver;
use Rector\Bootstrap\RectorConfigsResolver;
use Rector\ChangesReporting\Output\JsonOutputFormatter;
use Rector\Config\RectorConfig;
use Rector\Configuration\Option;
use Rector\Console\Style\SymfonyStyleFactory;
use Rector\DependencyInjection\LazyContainerFactory;
use Rector\DependencyInjection\RectorContainerFactory;
use Rector\NodeTypeResolver\DependencyInjection\PHPStanServicesFactory;
use Rector\Util\Reflection\PrivatesAccessor;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
Expand Down
1 change: 1 addition & 0 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
])
->withSkip([
'*/Source/*',
'*/SourcePhp81/*',
'*/Fixture/*',
'*/Expected/*',

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

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

final class SkipUsedByReflectionAsArrayCallable
{
public function run(): int
{
$reflectionMethod = new \ReflectionMethod(...[$this, 'methodInjection']);

return $reflectionMethod->getNumberOfParameters();
}

private function methodInjection(\stdClass $contextArgument): void
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
namespace Rector\DeadCode\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
Expand All @@ -17,7 +20,10 @@
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\DeadCode\NodeCollector\UnusedParameterResolver;
use Rector\NodeCollector\NodeAnalyzer\ArrayCallableMethodMatcher;
use Rector\NodeCollector\ValueObject\ArrayCallable;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -33,6 +39,7 @@ public function __construct(
private readonly DocBlockUpdater $docBlockUpdater,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly ArrayCallableMethodMatcher $arrayCallableMethodMatcher,
) {
}

Expand Down Expand Up @@ -91,6 +98,11 @@ public function refactor(Node $node): ?Node
continue;
}

// method used via reflection, eg: new ReflectionMethod(...[$this, 'method'])
if ($this->isUsedByReflectionMethod($node, (string) $this->getName($classMethod))) {
continue;
}

// early remove callers
if (! $this->removeCallerArgs($node, $classMethod, $unusedParameters)) {
continue;
Expand Down Expand Up @@ -120,6 +132,45 @@ public function refactor(Node $node): ?Node
return null;
}

private function isUsedByReflectionMethod(Class_ $class, string $methodName): bool
{
$scope = ScopeFetcher::fetch($class);

$reflectionMethodUsage = $this->betterNodeFinder->findFirst(
$class,
function (Node $subNode) use ($class, $scope, $methodName): bool {
if (! $subNode instanceof New_ || ! $subNode->class instanceof Name) {
return false;
}

if (! $this->isName($subNode->class, 'ReflectionMethod')) {
return false;
}

if ($subNode->isFirstClassCallable()) {
return false;
}

foreach ($subNode->getArgs() as $arg) {
if (! $arg->value instanceof Array_) {
continue;
}

$arrayCallable = $this->arrayCallableMethodMatcher->match($arg->value, $scope, $methodName);
if ($arrayCallable instanceof ArrayCallable
&& $this->isName($class, $arrayCallable->getClass())
&& strcasecmp($arrayCallable->getMethod(), $methodName) === 0) {
return true;
}
}

return false;
}
);

return $reflectionMethodUsage instanceof Node;
}

/**
* @param Param[] $unusedParameters
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Autoloading/BootstrapFilesIncluder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace Rector\Autoloading;

use PHPStan\DependencyInjection\Container;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\Exception\ShouldNotHappenException;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SplFileInfo;
use Webmozart\Assert\Assert;
use PHPStan\DependencyInjection\Container;

/**
* @see \Rector\Tests\Autoloading\BootstrapFilesIncluderTest
Expand Down
Loading