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 70f0241
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ lint:
--exclude tests/PHPStan/Rules/Properties/data/hooked-properties-without-bodies-in-class.php \
--exclude tests/PHPStan/Rules/Properties/data/readonly-property-hooks.php \
--exclude tests/PHPStan/Rules/Properties/data/readonly-property-hooks-in-interface.php \
--exclude tests/PHPStan/Rules/Properties/data/static-hooked-properties.php \
--exclude tests/PHPStan/Rules/Classes/data/bug-12281.php \
--exclude tests/PHPStan/Rules/Traits/data/bug-12281.php \
--exclude tests/PHPStan/Rules/Classes/data/invalid-hooked-properties.php \
Expand Down
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 70f0241

Please sign in to comment.