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,20 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\StmtsAwareInterface\MoveInnerFunctionToTopLevelRector\Fixture;

function SkipRedefineNativeFunction(): void
{
function strlen(): int
{
return 1;
}

echo strlen();
}

function bar()
{
echo strlen('abc');
}

bar();
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
namespace Rector\CodeQuality\Rector\StmtsAwareInterface;

use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Function_;
use PHPStan\Reflection\Native\NativeFunctionReflection;
use PHPStan\Reflection\ReflectionProvider;
use Rector\PhpParser\Enum\NodeGroup;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -16,6 +19,12 @@
*/
final class MoveInnerFunctionToTopLevelRector extends AbstractRector
{
public function __construct(
private readonly ReflectionProvider $reflectionProvider
) {

}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down Expand Up @@ -126,6 +135,7 @@ private function extractInnerFunctions(Function_ $outerFunction, array $siblingS
*/
private function hasSiblingFunctionOfSameName(Function_ $innerFunction, array $siblingStmts): bool
{
$innerFunctionName = new Name($innerFunction->name->toString());
foreach ($siblingStmts as $siblingStmt) {
if (! $siblingStmt instanceof Function_) {
continue;
Expand All @@ -134,6 +144,13 @@ private function hasSiblingFunctionOfSameName(Function_ $innerFunction, array $s
if ($this->nodeNameResolver->areNamesEqual($siblingStmt, $innerFunction)) {
return true;
}

if ($this->reflectionProvider->hasFunction($innerFunctionName, null)) {
$functionReflection = $this->reflectionProvider->getFunction($innerFunctionName, null);
if ($functionReflection instanceof NativeFunctionReflection) {
return true;
}
}
}

return false;
Expand Down
Loading