Skip to content

Commit

Permalink
[NodeCollector] Add ArrayCallableDynamicMethod value object (#951)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Oct 3, 2021
1 parent c784110 commit 6bfe013
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Rector\Core\PhpParser\Node\Value\ValueResolver;
use Rector\Core\ValueObject\MethodName;
use Rector\NodeCollector\ValueObject\ArrayCallable;
use Rector\NodeCollector\ValueObject\ArrayCallableDynamicMethod;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\NodeTypeResolver;
Expand All @@ -35,11 +36,11 @@ public function __construct(

/**
* Matches array like: "[$this, 'methodName']" → ['ClassName', 'methodName']
* Returns back value $array when unknown method of callable used, eg: [$this, $other]
* Returns ArrayCallableDynamicMethod object when unknown method of callable used, eg: [$this, $other]
* @see https://github.com/rectorphp/rector-src/pull/908
* @see https://github.com/rectorphp/rector-src/pull/909
*/
public function match(Array_ $array): null | Array_ | ArrayCallable
public function match(Array_ $array): null | ArrayCallableDynamicMethod | ArrayCallable
{
$arrayItems = $array->items;
if (count($arrayItems) !== 2) {
Expand All @@ -66,15 +67,17 @@ public function match(Array_ $array): null | Array_ | ArrayCallable
}

$values = $this->valueResolver->getValue($array);
$className = $calleeType->getClassName();
$secondItemValue = $items[1]->value;

if ($values === []) {
return $array;
return new ArrayCallableDynamicMethod($firstItemValue, $className, $secondItemValue);
}

if ($this->shouldSkipAssociativeArray($values)) {
return null;
}

$secondItemValue = $items[1]->value;
if (! $secondItemValue instanceof String_) {
return null;
}
Expand All @@ -83,9 +86,7 @@ public function match(Array_ $array): null | Array_ | ArrayCallable
return null;
}

$className = $calleeType->getClassName();
$methodName = $secondItemValue->value;

if ($methodName === MethodName::CONSTRUCT) {
return null;
}
Expand Down
32 changes: 32 additions & 0 deletions packages/NodeCollector/ValueObject/ArrayCallableDynamicMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Rector\NodeCollector\ValueObject;

use PhpParser\Node\Expr;

final class ArrayCallableDynamicMethod
{
public function __construct(
private Expr $callerExpr,
private string $class,
private Expr $method
) {
}

public function getClass(): string
{
return $this->class;
}

public function getMethod(): Expr
{
return $this->method;
}

public function getCallerExpr(): Expr
{
return $this->callerExpr;
}
}
5 changes: 3 additions & 2 deletions rules/DeadCode/NodeAnalyzer/IsClassMethodUsedAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Rector\Core\PhpParser\Node\Value\ValueResolver;
use Rector\NodeCollector\NodeAnalyzer\ArrayCallableMethodMatcher;
use Rector\NodeCollector\ValueObject\ArrayCallable;
use Rector\NodeCollector\ValueObject\ArrayCallableDynamicMethod;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;

Expand Down Expand Up @@ -118,7 +119,7 @@ private function isClassMethodCalledInLocalArrayCall(Class_ $class, ClassMethod
}

$arrayCallable = $this->arrayCallableMethodMatcher->match($array);
if ($arrayCallable instanceof Array_) {
if ($arrayCallable instanceof ArrayCallableDynamicMethod) {
return true;
}

Expand All @@ -136,7 +137,7 @@ private function isClassMethodCalledInLocalArrayCall(Class_ $class, ClassMethod
return false;
}

private function shouldSkipArrayCallable(Class_ $class, null | Array_ | ArrayCallable $arrayCallable): bool
private function shouldSkipArrayCallable(Class_ $class, null | ArrayCallable $arrayCallable): bool
{
if (! $arrayCallable instanceof ArrayCallable) {
return true;
Expand Down

0 comments on commit 6bfe013

Please sign in to comment.