get_inst__om_dbform(array( "obj" => "some_class", "idx" => 123 )); CODE_SAMPLE , // <<<'CODE_SAMPLE' // /** // * @param class-string $obj // * @param ?int|"]" $idx L'ID de l'élément à instancier, ou "]" pour un nouvel élément // * @return ?T // */ // function get_inst__om_dbform($obj = null, $idx = null) { // // method body // } // // Usage // $instance = $this->get_inst__om_dbform(\some_class::class, 123); // CODE_SAMPLE ), ] ); } public function getNodeTypes(): array { return [ // Function_::class, // ClassMethod::class, MethodCall::class, ]; } public function configure(array $configuration): void { // No configuration needed } public function provideMinPhpVersion(): int { return \Rector\ValueObject\PhpVersion::PHP_80; } public function refactor(Node $node): ?Node { // if ($node instanceof Function_ || $node instanceof ClassMethod) { // return $this->refactorMethodSignature($node); // } if ($node instanceof MethodCall) { return $this->refactorMethodCall($node); } return null; } // private function refactorMethodSignature(Node $node): ?Node // { // if (!$this->isGetInstOmDbformMethod($node)) { // return null; // } // // Change method signature with default null values // $node->params = [ // new Param( // new Variable('obj'), // $this->nodeFactory->createNull() // ), // new Param( // new Variable('idx'), // $this->nodeFactory->createNull() // ), // ]; // // Update method body to use direct parameters instead of array // $this->updateMethodBody($node); // return $node; // } private function refactorMethodCall(MethodCall $node): Node { if (!$this->isName($node->name, 'get_inst__om_dbform')) { return $node; } $args = $node->getArgs(); if (empty($args)) { return $node; } $firstArg = $args[0]; if (!$firstArg->value instanceof Array_) { return $node; } $arrayItems = $firstArg->value->items; $objValue = null; $idxValue = null; // Extract obj and idx values from array foreach ($arrayItems as $item) { if ($item instanceof ArrayItem && $item->key instanceof String_) { if ($item->key->value === 'obj') { $objValue = $item->value; } elseif ($item->key->value === 'idx') { $idxValue = $item->value; } } } if ($objValue === null) { return $node; } // Create new method call with direct parameters // Convert string literals to class-string where possible $objArg = $this->convertToStringLiteralOrClassString($objValue); $newArgs = [ new Arg($objArg), new Arg($idxValue ?? $this->nodeFactory->createNull()) ]; $node->args = $newArgs; return $node; } // private function isGetInstOmDbformMethod(Node $node): bool // { // if ($node instanceof Function_) { // return $this->isName($node, 'get_inst__om_dbform'); // } // if ($node instanceof ClassMethod) { // return $this->isName($node, 'get_inst__om_dbform'); // } // return false; // } // private function updateMethodBody(Node $node): void // { // if ($node->stmts === null) { // return; // } // // Update variable references in the method body // $node->stmts = array_filter($node->stmts, function (Node $node) { // if ( // $node instanceof Node\Stmt\Expression && $node->expr instanceof Node\Expr\Assign // && $node->expr->var instanceof Node\Expr\Variable // && $this->isName($node->expr->var, 'method_args') // ) { // return false; // } // // Find and replace the array-based parameter extraction logic // if ($node instanceof Node\Stmt\Foreach_ && $node->expr instanceof Node\Expr\Variable && $this->isName($node->expr, 'method_args')) { // return false; // } // return true; // }); // } /** * Convert string literals to ::class constants where the class exists */ private function convertToStringLiteralOrClassString(Node $node): Node { // If it's a string literal, check if it's a valid class name if ($node instanceof String_) { $className = $node->value; // Check if the class exists if ($this->isValidClassName($className)) { // Transform to ::class constant return new ClassConstFetch( new Name('\\' . $className), new Identifier('class') ); } } // If it's a variable or other expression, keep as is return $node; } /** * Check if a string represents a valid class name that exists */ private function isValidClassName(string $className): bool { // Inclusion comme dans openMairie $obj = $className; if (!file_exists(__DIR__ ."/../obj/".$obj.".class.php") && file_exists(__DIR__ ."/../core/obj/".$obj.".class.php")) { return true; } elseif (!file_exists("__DIR__ ../../obj/".$obj.".class.php") && file_exists(__DIR__ ."/../gen/obj/".$obj.".class.php")) { return true; } elseif (file_exists(__DIR__ ."/../obj/".$obj.".class.php")) { return true; } // Check if class exists if (class_exists($className)) { return true; } // Check if class exists with namespace prefix if (class_exists('\\' . $className)) { return true; } // Check common patterns in the codebase $commonPatterns = [ 'om_' . $className, $className . '_core', $className . '_gen', $className . '_custom', ]; foreach ($commonPatterns as $pattern) { if (class_exists($pattern) || class_exists('\\' . $pattern)) { return true; } } return false; } }