Skip to content

Commit

Permalink
Add handling an attempt to declare a hooked property static
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubtobiasz committed Feb 5, 2025
1 parent da5754c commit 8d4b70a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Rules/Properties/PropertyInClassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ public function processNode(Node $node, Scope $scope): array
}
}

if ($node->isStatic()) {
if ($node->hasHooks()) {
return [
RuleErrorBuilder::message('Hooked properties cannot be static.')
->nonIgnorable()
->identifier('property.hookStatic')
->build(),
];
}
}

return [];
}

Expand Down
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Properties/PropertyInClassRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,22 @@ public function testPhp84AndReadonlyHookedProperties(): void
]);
}

public function testPhp84AndStaticHookedProperties(): void
{
if (PHP_VERSION_ID < 80400) {
$this->markTestSkipped('Test requires PHP 8.4 or later.');
}

$this->analyse([__DIR__ . '/data/static-hooked-properties.php'], [
[
'Hooked properties cannot be static.',
7,
],
[
'Hooked properties cannot be static.',
15,
],
]);
}

}
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Properties/data/static-hooked-properties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);

namespace StaticHookedProperties;

class HelloWorld
{
public static string $foo {
get => $this->foo;
set => $this->foo = $value;
}
}

abstract class HiWorld
{
public static string $foo {
get => 'dummy';
}
}

0 comments on commit 8d4b70a

Please sign in to comment.