Skip to content

Commit 7e08f0c

Browse files
committed
Detect hooked properties outside of constructor
1 parent 8224420 commit 7e08f0c

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ lint:
8787
--exclude tests/PHPStan/Rules/Properties/data/non-abstract-hooked-properties-in-class.php \
8888
--exclude tests/PHPStan/Rules/Properties/data/hooked-properties-in-class.php \
8989
--exclude tests/PHPStan/Rules/Properties/data/hooked-properties-without-bodies-in-class.php \
90+
--exclude tests/PHPStan/Rules/Classes/data/invalid-hooked-properties.php \
9091
src tests
9192

9293
cs:

src/Rules/Classes/InvalidPromotedPropertiesRule.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ public function processNode(Node $node, Scope $scope): array
3131
$hasPromotedProperties = false;
3232

3333
foreach ($node->getParams() as $param) {
34-
if ($param->flags === 0) {
34+
if ($param->flags !== 0) {
35+
$hasPromotedProperties = true;
36+
break;
37+
}
38+
39+
if ($param->hooks === []) {
3540
continue;
3641
}
3742

tests/PHPStan/Rules/Classes/InvalidPromotedPropertiesRuleTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,19 @@ public function testBug9577(): void
104104
$this->analyse([__DIR__ . '/data/bug-9577.php'], []);
105105
}
106106

107+
public function testHooks(): void
108+
{
109+
if (PHP_VERSION_ID < 80100) {
110+
$this->markTestSkipped('Test requires PHP 8.4.');
111+
}
112+
113+
$this->phpVersion = 80100;
114+
$this->analyse([__DIR__ . '/data/invalid-hooked-properties.php'], [
115+
[
116+
'Promoted properties can be in constructor only.',
117+
9,
118+
],
119+
]);
120+
}
121+
107122
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace InvalidHookedProperties;
4+
5+
use DateTimeImmutable;
6+
7+
class HelloWorld
8+
{
9+
public function sayHello(DateTimeImmutable $date { get; }): void
10+
{
11+
12+
}
13+
}
14+
15+
class ValidPromotedProperty
16+
{
17+
public function __construct(DateTimeImmutable $date { get {} })
18+
{
19+
20+
}
21+
}

0 commit comments

Comments
 (0)