55namespace Rector \CodeQuality \Rector \StmtsAwareInterface ;
66
77use PhpParser \Node ;
8+ use PhpParser \Node \Name ;
89use PhpParser \Node \Stmt \Function_ ;
10+ use PHPStan \Reflection \Native \NativeFunctionReflection ;
11+ use PHPStan \Reflection \ReflectionProvider ;
912use Rector \PhpParser \Enum \NodeGroup ;
1013use Rector \Rector \AbstractRector ;
1114use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
1619 */
1720final 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