Skip to content

Commit

Permalink
[CodingStyle] Fix ArrayCallableToMethodCallFactory for non-object typ…
Browse files Browse the repository at this point in the history
…es (#548)

Co-authored-by: Zing <zingimmick@outlook.com>
  • Loading branch information
TomasVotruba and zingimmick committed Jul 30, 2021
1 parent a152283 commit 62cca3b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\FuncCall\CallUserFuncToMethodCallRector\Fixture;

final class SkipStringVariable
{
public function run()
{
$classname = 'some';
call_user_func([$classname, 'method']);
$result = call_user_func(['some', 'method'], $args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ namespace Rector\Tests\CodingStyle\Rector\FuncCall\CallUserFuncToMethodCallRecto

final class SomeClass
{
private $property;

public function __construct(\stdClass $property)
{
$this->property = $property;
}

public function run()
{
$result = \call_user_func([$this->property, 'method'], $args);
Expand All @@ -18,6 +25,13 @@ namespace Rector\Tests\CodingStyle\Rector\FuncCall\CallUserFuncToMethodCallRecto

final class SomeClass
{
private $property;

public function __construct(\stdClass $property)
{
$this->property = $property;
}

public function run()
{
$result = $this->property->method($args);
Expand Down
12 changes: 12 additions & 0 deletions rules/CodingStyle/NodeFactory/ArrayCallableToMethodCallFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\String_;
use PHPStan\Type\TypeWithClassName;
use Rector\NodeTypeResolver\NodeTypeResolver;

final class ArrayCallableToMethodCallFactory
{
public function __construct(
private NodeTypeResolver $nodeTypeResolver
) {
}

public function create(Array_ $array): ?MethodCall
{
if (count($array->items) !== 2) {
Expand All @@ -38,6 +45,11 @@ public function create(Array_ $array): ?MethodCall
return null;
}

$firstItemType = $this->nodeTypeResolver->resolve($firstItem->value);
if (! $firstItemType instanceof TypeWithClassName) {
return null;
}

$string = $secondItem->value;
$methodName = $string->value;

Expand Down

0 comments on commit 62cca3b

Please sign in to comment.