Skip to content

Commit

Permalink
[Php71] Skip defer() function on RemoveExtraParametersRector (#3211)
Browse files Browse the repository at this point in the history
* [Php71] Skip defer() function on RemoveExtraParametersRector

* Fixed 🎉

* final touch: move to private method to reuse validate

* final touch: move to private method to reuse validate

* Fix

* Final touch: move phpstan.phar check on RemoveExtraParametersReflection
  • Loading branch information
samsonasik committed Dec 17, 2022
1 parent 7d5d1ce commit 14e60b0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

// no namespace on purpose to demonstrate the issue
function defer(string $foo, callable $callback): void
{
$callback($foo);
}

defer("foo", static function ($bar) {
echo $bar;
});
16 changes: 16 additions & 0 deletions rules/Php71/Rector/FuncCall/RemoveExtraParametersRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($this->shouldSkipFunctionReflection($functionLikeReflection)) {
return null;
}

$numberOfArguments = count($node->getRawArgs());
if ($numberOfArguments <= $maximumAllowedParameterCount) {
return null;
Expand All @@ -99,6 +103,18 @@ public function refactor(Node $node): ?Node
return $node;
}

private function shouldSkipFunctionReflection(MethodReflection|FunctionReflection $reflection): bool
{
if ($reflection instanceof FunctionReflection) {
$fileName = (string) $reflection->getFileName();
if (str_contains($fileName, 'phpstan.phar')) {
return true;
}
}

return false;
}

private function shouldSkip(FuncCall | MethodCall | StaticCall $call): bool
{
if ($call->args === []) {
Expand Down

0 comments on commit 14e60b0

Please sign in to comment.