Skip to content

Commit 379d638

Browse files
committed
fix(renaming): Ensure scope attribute is set for variable nodes
- Add a check to ensure the scope attribute is set on nodes. - If the scope is not an instance of Scope, fetch and set it from the parent node. - This change improves the handling of node attributes in the renaming process.
1 parent a40a6d3 commit 379d638

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@
377377
"rector:process-clear-cache": "@rector:process --clear-cache",
378378
"rector:process-clear-cache-dry-run": "@rector:process-clear-cache --dry-run",
379379
"rector:process-dry-run": "@rector:process --dry-run",
380-
"rector:process-only": "@rector:process-clear-cache tests.php --only=Guanguans\\RectorRules\\Rector\\FuncCall\\RenameAppFunctionToResolveFunctionRector",
380+
"rector:process-only": "@rector:process-clear-cache tests.php --only=Guanguans\\RectorRules\\Rector\\Name\\RenameToConventionalCaseNameRector",
381381
"rector:process-only-dry-run": "@rector:process-only --dry-run",
382382
"rule-doc-generator": [
383383
"@putenv:php",

rector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
// ->withoutParallel()
7272
->withParallel()
7373
// ->withImportNames(importDocBlockNames: false, importShortClasses: false)
74-
->withImportNames(true, false, false)
74+
// ->withImportNames(true, false, false)
7575
// ->withImportNames(importNames: false)
7676
// ->withEditorUrl()
7777
->withFluentCallNewLine()

src/Rector/Name/RenameToConventionalCaseNameRector.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ public function getNodeTypes(): array
184184
*
185185
* @param \PhpParser\Node\Expr\FuncCall|\PhpParser\Node\Expr\Variable|\PhpParser\Node\Identifier|\PhpParser\Node\Name $node
186186
*
187+
* @throws \Rector\Exception\ShouldNotHappenException
188+
*
187189
* @noinspection BadExceptionsProcessingInspection
188190
*/
189191
public function refactor(Node $node): ?Node
@@ -376,6 +378,8 @@ class Foo{public int $propertyName;}
376378
* @see \Rector\Renaming\Collector\RenamedNameCollector
377379
*
378380
* @param \PhpParser\Node\Expr\FuncCall|\PhpParser\Node\Expr\Variable|\PhpParser\Node\Identifier|\PhpParser\Node\Name $node
381+
*
382+
* @throws \Rector\Exception\ShouldNotHappenException
379383
*/
380384
private function rename(Node $node, callable $renamer): ?Node
381385
{
@@ -404,21 +408,26 @@ private function rename(Node $node, callable $renamer): ?Node
404408
}
405409

406410
/**
411+
* ```
412+
* function func_name($var_name): void {}
413+
* ```
407414
* In the `\PhpParser\Node\FunctionLike` parameter,
408415
* the `\PhpParser\Node\Expr\Variable` node does not have a scope attribute,
409-
* and even if it is renamed, `rector` will report an error.
410-
*
411-
* "System error: "Node "PhpParser\Node\Expr\Variable" with is missing scope required for scope refresh"
416+
* and even if it is renamed, `rector` will report an error:
412417
* ```
413-
* function func_name($var_name): void {}
418+
* System error: "Node "PhpParser\Node\Expr\Variable" with is missing scope required for scope refresh
414419
* ```.
420+
*
421+
* So we fetch from parent node to set the scope attribute.
415422
*/
416423
if ($node instanceof Variable && !$node->getAttribute(AttributeKey::SCOPE) instanceof Scope) {
417-
throw new RectorErrorException(
418-
$this,
419-
"The variable name [$node->name] cannot be renamed to [$newName] because of missing scope.",
420-
$node->getAttributes()
421-
);
424+
// throw new RectorErrorException(
425+
// $this,
426+
// "The variable name [$node->name] cannot be renamed to [$newName] because of missing scope.",
427+
// $node->getAttributes()
428+
// );
429+
$parent = $node->getAttribute('parent');
430+
$parent instanceof Node and $node->setAttribute(AttributeKey::SCOPE, ScopeFetcher::fetch($parent));
422431
}
423432

424433
$node->name = $newName;
@@ -757,7 +766,7 @@ private function wrapRenamer(callable $renamer, Node $node): \Closure
757766
}
758767

759768
// if the name is all uppercase letters, convert it to lowercase letters.
760-
if (ctype_upper(preg_replace('/[^a-zA-Z]/', '', $name))) {
769+
if (preg_match('/^[A-Z_]+$/', $name)) {
761770
return Str::lower($name);
762771
}
763772

tests/Rector/Name/RenameToConventionalCaseNameRector/Fixture/fixture_variable_scope.php.inc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
/**
66
* In the `\PhpParser\Node\FunctionLike` parameter,
7-
* the `\PhpParser\Node\Expr\Variable` node does not have a scope attribute,
8-
* and even if it is renamed, `rector` will report an error.
7+
* the `\PhpParser\Node\Expr\Variable` node does not have a scope attribute.
98
*/
109
function func_name(string $var_name): string
1110
{
@@ -22,10 +21,9 @@ function func_name(string $var_name): string
2221

2322
/**
2423
* In the `\PhpParser\Node\FunctionLike` parameter,
25-
* the `\PhpParser\Node\Expr\Variable` node does not have a scope attribute,
26-
* and even if it is renamed, `rector` will report an error.
24+
* the `\PhpParser\Node\Expr\Variable` node does not have a scope attribute.
2725
*/
28-
function func_name(string $var_name): string
26+
function func_name(string $varName): string
2927
{
3028
$newVarName = $varName;
3129

0 commit comments

Comments
 (0)