Skip to content

Commit

Permalink
[DeadCode] Handle crash on has first class callable on RemoveUnusedCo…
Browse files Browse the repository at this point in the history
…nstructorParamRector (#3015)

* [DeadCode] Handle has first class callable on RemoveUnusedConstructorParamRector

* Fixed 🎉

* final touch: more fixture
  • Loading branch information
samsonasik committed Oct 26, 2022
1 parent d82ef17 commit b46d5f5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

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

final class HasFirstClassCallable
{
private $hey;

public function __construct($hey, $man)
{
$this->hey = $hey;

$this->execute(...);
}

private function execute()
{
}
}

?>
-----
<?php

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

final class HasFirstClassCallable
{
private $hey;

public function __construct($hey)
{
$this->hey = $hey;

$this->execute(...);
}

private function execute()
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

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

final class SkipUsedByExecFirstClassCallable
{
private $hey;

public function __construct($hey, $man)
{
$this->hey = $hey;

$this->execute(...)($man);
}

private function execute($man)
{
echo $man;
}
}
4 changes: 4 additions & 0 deletions src/NodeAnalyzer/ParamAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ private function isVariableInClosureUses(Closure $closure, Variable $variable):
private function isUsedAsArg(Node $node, Param $param): bool
{
if ($node instanceof New_ || $node instanceof CallLike) {
if ($node->isFirstClassCallable()) {
return false;
}

foreach ($node->getArgs() as $arg) {
if ($this->nodeComparator->areNodesEqual($param->var, $arg->value)) {
return true;
Expand Down

0 comments on commit b46d5f5

Please sign in to comment.