Skip to content

Commit bafb280

Browse files
authored
[CodeQuality] Skip native function on MoveInnerFunctionToTopLevelRector (#8044)
* [CodeQuality] Skip native function on MoveInnerFunctionToTopLevelRector * eol
1 parent daeacc0 commit bafb280

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\StmtsAwareInterface\MoveInnerFunctionToTopLevelRector\Fixture;
4+
5+
function SkipRedefineNativeFunction(): void
6+
{
7+
function strlen(): int
8+
{
9+
return 1;
10+
}
11+
12+
echo strlen();
13+
}
14+
15+
function bar()
16+
{
17+
echo strlen('abc');
18+
}
19+
20+
bar();

‎rules/CodeQuality/Rector/StmtsAwareInterface/MoveInnerFunctionToTopLevelRector.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
namespace Rector\CodeQuality\Rector\StmtsAwareInterface;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\Name;
89
use PhpParser\Node\Stmt\Function_;
10+
use PHPStan\Reflection\Native\NativeFunctionReflection;
11+
use PHPStan\Reflection\ReflectionProvider;
912
use Rector\PhpParser\Enum\NodeGroup;
1013
use Rector\Rector\AbstractRector;
1114
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -16,6 +19,12 @@
1619
*/
1720
final class MoveInnerFunctionToTopLevelRector extends AbstractRector
1821
{
22+
public function __construct(
23+
private readonly ReflectionProvider $reflectionProvider
24+
) {
25+
26+
}
27+
1928
public function getRuleDefinition(): RuleDefinition
2029
{
2130
return new RuleDefinition(
@@ -126,6 +135,7 @@ private function extractInnerFunctions(Function_ $outerFunction, array $siblingS
126135
*/
127136
private function hasSiblingFunctionOfSameName(Function_ $innerFunction, array $siblingStmts): bool
128137
{
138+
$innerFunctionName = new Name($innerFunction->name->toString());
129139
foreach ($siblingStmts as $siblingStmt) {
130140
if (! $siblingStmt instanceof Function_) {
131141
continue;
@@ -134,6 +144,13 @@ private function hasSiblingFunctionOfSameName(Function_ $innerFunction, array $s
134144
if ($this->nodeNameResolver->areNamesEqual($siblingStmt, $innerFunction)) {
135145
return true;
136146
}
147+
148+
if ($this->reflectionProvider->hasFunction($innerFunctionName, null)) {
149+
$functionReflection = $this->reflectionProvider->getFunction($innerFunctionName, null);
150+
if ($functionReflection instanceof NativeFunctionReflection) {
151+
return true;
152+
}
153+
}
137154
}
138155

139156
return false;

0 commit comments

Comments
 (0)