Skip to content
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
@@ -0,0 +1,37 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\FuncCall\FunctionFirstClassCallableRector\Fixture\NamespacedFunctionShadow;

function trim(string $value): string
{
return 'shadowed';
}

final class NamespacedFunctionShadow
{
public function run(array $data)
{
return array_map('trim', $data);
}
}

?>
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\FuncCall\FunctionFirstClassCallableRector\Fixture\NamespacedFunctionShadow;

function trim(string $value): string
{
return 'shadowed';
}

final class NamespacedFunctionShadow
{
public function run(array $data)
{
return array_map(\trim(...), $data);
}
}

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

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

use function Rector\Tests\CodingStyle\Rector\FuncCall\FunctionFirstClassCallableRector\Source\strtoupper;

final class UseFunctionImport
{
public function run(array $data)
{
return array_map('strtoupper', $data);
}
}

?>
-----
<?php

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

use function Rector\Tests\CodingStyle\Rector\FuncCall\FunctionFirstClassCallableRector\Source\strtoupper;

final class UseFunctionImport
{
public function run(array $data)
{
return array_map(\strtoupper(...), $data);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\VariadicPlaceholder;
use PHPStan\Reflection\ReflectionProvider;
use Rector\Naming\Naming\UseImportsResolver;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;
Expand All @@ -25,6 +29,12 @@
*/
final class FunctionFirstClassCallableRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly ReflectionProvider $reflectionProvider,
private readonly UseImportsResolver $useImportsResolver
) {
}

public function getRuleDefinition(): RuleDefinition
{
// see RFC https://wiki.php.net/rfc/first_class_callable_syntax
Expand Down Expand Up @@ -106,7 +116,10 @@ public function refactor(Node $node): ?FuncCall

$node->args[$key] = new Arg(
new FuncCall(
$isFullyQualified || str_contains($callableName, '\\')
$isFullyQualified || str_contains($callableName, '\\') || $this->isShadowedFunction(
$node,
$callableName
)
? new FullyQualified($callableName)
: new Name($callableName),
[new VariadicPlaceholder()]
Expand All @@ -119,6 +132,35 @@ public function refactor(Node $node): ?FuncCall
return $hasChanged ? $node : null;
}

/**
* A string callable always resolves to the global function,
* while a bare name resolves to a namespaced or imported function first
*/
private function isShadowedFunction(FuncCall $funcCall, string $functionName): bool
{
$namespaceName = ScopeFetcher::fetch($funcCall)->getNamespace();
if ($namespaceName !== null && $this->reflectionProvider->hasFunction(
new FullyQualified($namespaceName . '\\' . $functionName),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nodeNameResolver->getName() should already resolve the correct name

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you PR this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will try

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null
)) {
return true;
}

foreach ($this->useImportsResolver->resolve() as $use) {
foreach ($use->uses as $useItem) {
if ($use->type !== Use_::TYPE_FUNCTION && $useItem->type !== Use_::TYPE_FUNCTION) {
continue;
}

if ($useItem->getAlias()->toLowerString() === strtolower($functionName)) {
return true;
}
}
}

return false;
}

public function provideMinPhpVersion(): int
{
return PhpVersion::PHP_81;
Expand Down
Loading