Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hooked properties cannot be both final and private #3830

Open
wants to merge 2 commits into
base: 2.1.x
Choose a base branch
from
Open
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 @@ -101,6 +101,7 @@ lint:
--exclude tests/PHPStan/Rules/Properties/data/existing-classes-property-hooks.php \
--exclude tests/PHPStan/Rules/Properties/data/set-property-hook-parameter.php \
--exclude tests/PHPStan/Rules/Properties/data/overriding-final-property.php \
--exclude tests/PHPStan/Rules/Properties/data/private-final-property-hooks.php \
src tests

cs:
Expand Down
5 changes: 5 additions & 0 deletions src/Node/ClassPropertyNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public function isPrivate(): bool
return (bool) ($this->flags & Modifiers::PRIVATE);
}

public function isFinal(): bool
{
return (bool) ($this->flags & Modifiers::FINAL);
}

public function isStatic(): bool
{
return (bool) ($this->flags & Modifiers::STATIC);
Expand Down
26 changes: 26 additions & 0 deletions src/Rules/Properties/PropertyInClassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,32 @@ public function processNode(Node $node, Scope $scope): array
];
}

if ($node->isPrivate()) {
if ($node->hasHooks()) {
if ($node->isFinal()) {
return [
RuleErrorBuilder::message('Hooked properties cannot be both final and private.')
->nonIgnorable()
->identifier('property.abstractPrivate')
->build(),
];
}

foreach ($node->getHooks() as $hook) {
if (!$hook->isFinal()) {
continue;
}

return [
RuleErrorBuilder::message('Hooked properties cannot be both final and private.')
->nonIgnorable()
->identifier('property.abstractPrivate')
->build(),
];
}
}
}

if ($node->isReadOnly()) {
if ($node->hasHooks()) {
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 @@ -213,4 +213,22 @@ public function testPhp84AndStaticHookedProperties(): void
]);
}

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

$this->analyse([__DIR__ . '/data/private-final-property-hooks.php'], [
[
'Hooked properties cannot be both final and private.',
7,
],
[
'Hooked properties cannot be both final and private.',
11,
],
]);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php // lint >= 8.4

namespace PrivateFinalHook;

final class User
{
final private string $privatePropGet = 'mailto: example.org' {
get => 'private:' . $this->privatePropGet;
}

private string $privateSet = 'mailto: example.org' {
final set => 'private:' . $this->privateSet;
}

protected string $protected = 'mailto: example.org' {
final get => 'protected:' . $this->protected;
}

public string $public = 'mailto: example.org' {
final get => 'public:' . $this->public;
}

private string $email = 'mailto: example.org' {
get => 'mailto:' . $this->email;
}

function doFoo(): void
{
$u = new User;
var_dump($u->private);
var_dump($u->protected);
var_dump($u->public);
var_dump($u->email);
}
}
Loading