From d7cdc4555100939ceb1070f810358a7a5c8e511e Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 7 Oct 2026 14:05:50 +0200 Subject: [PATCH] [CodingStyle] Fix NewlineBeforeNewAssignSetRector false positive on local var and $this property name collision --- ...ip_this_property_local_var_collision.php.inc | 17 +++++++++++++++++ .../NewlineBeforeNewAssignSetRector.php | 4 +++- 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 rules-tests/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector/Fixture/skip_this_property_local_var_collision.php.inc diff --git a/rules-tests/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector/Fixture/skip_this_property_local_var_collision.php.inc b/rules-tests/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector/Fixture/skip_this_property_local_var_collision.php.inc new file mode 100644 index 00000000000..1db725ee1ac --- /dev/null +++ b/rules-tests/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector/Fixture/skip_this_property_local_var_collision.php.inc @@ -0,0 +1,17 @@ +query = \is_array($query) ? $query : []; + $this->another = 'string'; + } +} diff --git a/rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php b/rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php index 02fa6f23b6f..e0ec2c22ee6 100644 --- a/rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php +++ b/rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php @@ -145,8 +145,10 @@ private function resolveCurrentStmtVariableName(Stmt $stmt): ?string $nodeVar = $nodeVar->var; } while ($nodeVar instanceof PropertyFetch); + // $this->property, namespace it to avoid collision with a local variable of the same name if ($this->getName($nodeVar) === 'this') { - $nodeVar = $previous; + $propertyName = $this->getName($previous); + return $propertyName === null ? null : 'this->' . $propertyName; } }