Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply Rector on itself #2531

Merged
merged 2 commits into from
Dec 30, 2019
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
Expand Up @@ -10,6 +10,7 @@
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\ClosureUse;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
Expand All @@ -19,6 +20,7 @@
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Type\ObjectType;
use Rector\Exception\ShouldNotHappenException;
use Rector\NodeContainer\ParsedNodesByType;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
Expand Down Expand Up @@ -132,7 +134,15 @@ private function shouldSkipArray(Array_ $array): bool
}

// can be totally empty in case of "[, $value]"
return $array->items[0] === null;
if ($array->items[0] === null) {
return true;
}

if ($array->items[1] === null) {
return true;
}

return $this->isCallbackAtFunctionName($array, 'register_shutdown_function');
}

/**
Expand All @@ -141,6 +151,10 @@ private function shouldSkipArray(Array_ $array): bool
private function matchCallableMethod(Expr $objectExpr, String_ $methodExpr): ?ClassMethod
{
$methodName = $this->getValue($methodExpr);
if (! is_string($methodName)) {
throw new ShouldNotHappenException();
}

$objectType = $this->getObjectType($objectExpr);

if ($objectType instanceof ObjectType) {
Expand Down Expand Up @@ -236,4 +250,19 @@ private function copyParams(array $params): array

return $newParams;
}

private function isCallbackAtFunctionName(Array_ $array, string $functionName): bool
{
$parentNode = $array->getAttribute(AttributeKey::PARENT_NODE);
if (! $parentNode instanceof Arg) {
return false;
}

$parentParentNode = $parentNode->getAttribute(AttributeKey::PARENT_NODE);
if (! $parentParentNode instanceof FuncCall) {
return false;
}

return $this->isName($parentParentNode, $functionName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\CodeQuality\Tests\Rector\Array_\CallableThisArrayToAnonymousFunctionRector\Fixture;

class SkipErrorHandlerShutdown
{
public function run()
{
register_shutdown_function([$this, 'shutdown_function']);
}

public function shutdown_function()
{
}
}