Skip to content

Commit bb9888a

Browse files
jakubtobiaszondrejmirtes
authored andcommitted
Prevent setting a default value for a virtual property
1 parent b82230a commit bb9888a

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ lint:
9090
--exclude tests/PHPStan/Rules/Properties/data/hooked-properties-without-bodies-in-class.php \
9191
--exclude tests/PHPStan/Rules/Properties/data/readonly-property-hooks.php \
9292
--exclude tests/PHPStan/Rules/Properties/data/readonly-property-hooks-in-interface.php \
93+
--exclude tests/PHPStan/Rules/Properties/data/virtual-hooked-properties.php \
9394
--exclude tests/PHPStan/Rules/Classes/data/bug-12281.php \
9495
--exclude tests/PHPStan/Rules/Traits/data/bug-12281.php \
9596
--exclude tests/PHPStan/Rules/Classes/data/invalid-hooked-properties.php \

src/Node/ClassPropertyNode.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,9 @@ public function getHooks(): array
160160
return $this->originalNode->hooks;
161161
}
162162

163+
public function isVirtual(): bool
164+
{
165+
return $this->classReflection->getNativeProperty($this->name)->isVirtual()->yes();
166+
}
167+
163168
}

src/Rules/Properties/PropertyInClassRule.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@ public function processNode(Node $node, Scope $scope): array
9292
}
9393
}
9494

95+
if ($node->isVirtual()) {
96+
if ($node->getDefault() !== null) {
97+
return [
98+
RuleErrorBuilder::message('Virtual hooked properties cannot have a default value.')
99+
->nonIgnorable()
100+
->identifier('property.virtualDefault')
101+
->build(),
102+
];
103+
}
104+
}
105+
95106
return [];
96107
}
97108

tests/PHPStan/Rules/Properties/PropertyInClassRuleTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,18 @@ public function testPhp84AndReadonlyHookedProperties(): void
181181
]);
182182
}
183183

184+
public function testPhp84AndVirtualHookedProperties(): void
185+
{
186+
if (PHP_VERSION_ID < 80400) {
187+
$this->markTestSkipped('Test requires PHP 8.4 or later.');
188+
}
189+
190+
$this->analyse([__DIR__ . '/data/virtual-hooked-properties.php'], [
191+
[
192+
'Virtual hooked properties cannot have a default value.',
193+
17,
194+
],
195+
]);
196+
}
197+
184198
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace VirtualHookedProperties;
4+
5+
class HelloWorld
6+
{
7+
public string $firstName = 'John' {
8+
get => $this->firstName;
9+
set => $this->firstName = $value;
10+
}
11+
12+
public string $middleName {
13+
get => $this->middleName;
14+
set => $this->middleName = $value;
15+
}
16+
17+
public string $lastName = 'Doe' {
18+
get => 'Smith';
19+
}
20+
21+
public string $maidenName = 'Brown';
22+
}

0 commit comments

Comments
 (0)