From b091141d4c22511ec2ea8fd3205e15cbd7eca1f6 Mon Sep 17 00:00:00 2001 From: Jesper Noordsij Date: Thu, 6 Mar 2025 16:20:38 +0100 Subject: [PATCH 1/4] Add incorrect non static access of static property to AccessPropertiesRuleTest --- .../Properties/AccessPropertiesRuleTest.php | 10 ++++++++++ .../PHPStan/Rules/Properties/data/bug-12692.php | 17 +++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 tests/PHPStan/Rules/Properties/data/bug-12692.php diff --git a/tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php b/tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php index f1e6d16f50..fc8708e8c1 100644 --- a/tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php +++ b/tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php @@ -344,6 +344,16 @@ public function testAccessPropertiesOnThisOnly(): void ); } + public function testBug12692(): void + { + $this->checkThisOnly = false; + $this->checkUnionTypes = false; + $this->checkDynamicProperties = false; + $this->analyse([__DIR__ . '/data/bug-12692.php'], [ + // This should not be empty! + ]); + } + public function testAccessPropertiesAfterIsNullInBooleanOr(): void { $this->checkThisOnly = false; diff --git a/tests/PHPStan/Rules/Properties/data/bug-12692.php b/tests/PHPStan/Rules/Properties/data/bug-12692.php new file mode 100644 index 0000000000..237f71e684 --- /dev/null +++ b/tests/PHPStan/Rules/Properties/data/bug-12692.php @@ -0,0 +1,17 @@ +static; + } + +} From 26fedb5abc5461f4029f344128d4c07c3aec249b Mon Sep 17 00:00:00 2001 From: Jesper Noordsij Date: Thu, 6 Mar 2025 16:21:07 +0100 Subject: [PATCH 2/4] Report non static access of static properties --- src/Rules/Properties/AccessPropertiesCheck.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Rules/Properties/AccessPropertiesCheck.php b/src/Rules/Properties/AccessPropertiesCheck.php index 023cc16756..815447c83a 100644 --- a/src/Rules/Properties/AccessPropertiesCheck.php +++ b/src/Rules/Properties/AccessPropertiesCheck.php @@ -160,6 +160,16 @@ private function processSingleProperty(Scope $scope, PropertyFetch $node, string } $propertyReflection = $type->getProperty($name, $scope); + if ($propertyReflection->isStatic()) { + return [ + RuleErrorBuilder::message(sprintf( + 'Non static access to static property %s::$%s.', + $propertyReflection->getDeclaringClass()->getDisplayName(), + $name, + ))->identifier('property.nonStaticAccess')->build(), + ]; + } + if ($write) { if ($scope->canWriteProperty($propertyReflection)) { return []; From d3d8c793e563a99e0d150154633c0674e8c8a0f1 Mon Sep 17 00:00:00 2001 From: Jesper Noordsij Date: Thu, 6 Mar 2025 16:23:09 +0100 Subject: [PATCH 3/4] Update tests to account for incorrect non static access usage --- .../PHPStan/Rules/Properties/AccessPropertiesRuleTest.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php b/tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php index fc8708e8c1..020a7101a3 100644 --- a/tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php +++ b/tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php @@ -349,9 +349,10 @@ public function testBug12692(): void $this->checkThisOnly = false; $this->checkUnionTypes = false; $this->checkDynamicProperties = false; - $this->analyse([__DIR__ . '/data/bug-12692.php'], [ - // This should not be empty! - ]); + $this->analyse([__DIR__ . '/data/bug-12692.php'], [[ + 'Non static access to static property Bug12692\Foo::$static.', + 14, + ]]); } public function testAccessPropertiesAfterIsNullInBooleanOr(): void From e4acdfa6762fec90fa93f9f945866c78f00ec609 Mon Sep 17 00:00:00 2001 From: Jesper Noordsij Date: Sun, 9 Mar 2025 17:57:33 +0100 Subject: [PATCH 4/4] Update non-static access rule naming and message MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ondřej Mirtes --- src/Rules/Properties/AccessPropertiesCheck.php | 4 ++-- tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Rules/Properties/AccessPropertiesCheck.php b/src/Rules/Properties/AccessPropertiesCheck.php index 815447c83a..f1a70365a7 100644 --- a/src/Rules/Properties/AccessPropertiesCheck.php +++ b/src/Rules/Properties/AccessPropertiesCheck.php @@ -163,10 +163,10 @@ private function processSingleProperty(Scope $scope, PropertyFetch $node, string if ($propertyReflection->isStatic()) { return [ RuleErrorBuilder::message(sprintf( - 'Non static access to static property %s::$%s.', + 'Non-static access to static property %s::$%s.', $propertyReflection->getDeclaringClass()->getDisplayName(), $name, - ))->identifier('property.nonStaticAccess')->build(), + ))->identifier('staticProperty.nonStaticAccess')->build(), ]; } diff --git a/tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php b/tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php index 020a7101a3..e6aa299b75 100644 --- a/tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php +++ b/tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php @@ -350,7 +350,7 @@ public function testBug12692(): void $this->checkUnionTypes = false; $this->checkDynamicProperties = false; $this->analyse([__DIR__ . '/data/bug-12692.php'], [[ - 'Non static access to static property Bug12692\Foo::$static.', + 'Non-static access to static property Bug12692\Foo::$static.', 14, ]]); }