Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/virtual-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
5 changes: 5 additions & 0 deletions src/Node/ClassPropertyNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,9 @@ public function getHooks(): array
return $this->originalNode->hooks;
}

public function isVirtual(): bool
{
return $this->classReflection->getNativeProperty($this->name)->isVirtual()->yes();
}

}
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->isVirtual()) {
if ($node->getDefault() !== null) {
return [
RuleErrorBuilder::message('Virtual hooked properties cannot have a default value.')
->nonIgnorable()
->identifier('property.virtualDefault')
->build(),
];
}
}

return [];
}

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

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

$this->analyse([__DIR__ . '/data/virtual-hooked-properties.php'], [
[
'Virtual hooked properties cannot have a default value.',
17,
],
]);
}

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

namespace VirtualHookedProperties;

class HelloWorld
{
public string $firstName = 'John' {
get => $this->firstName;
set => $this->firstName = $value;
}

public string $middleName {
get => $this->middleName;
set => $this->middleName = $value;
}

public string $lastName = 'Doe' {
get => 'Smith';
}

public string $maidenName = 'Brown';
}
Loading