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
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ lint:
--exclude tests/PHPStan/Rules/Properties/data/non-abstract-hooked-properties-in-class.php \
--exclude tests/PHPStan/Rules/Properties/data/hooked-properties-in-class.php \
--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/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
9 changes: 9 additions & 0 deletions src/Rules/Properties/PropertiesInInterfaceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ public function processNode(Node $node, Scope $scope): array
];
}

if ($node->isReadOnly()) {
return [
RuleErrorBuilder::message('Interfaces cannot include readonly hooked properties.')
->nonIgnorable()
->identifier('property.readOnlyInInterface')
->build(),
];
}

if ($this->hasAnyHookBody($node)) {
return [
RuleErrorBuilder::message('Interfaces cannot include property hooks with bodies.')
Expand Down
17 changes: 12 additions & 5 deletions src/Rules/Properties/PropertyInClassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,7 @@ public function processNode(Node $node, Scope $scope): array
->build(),
];
}

return [];
}

if (!$this->doAllHooksHaveBody($node)) {
} elseif (!$this->doAllHooksHaveBody($node)) {
return [
RuleErrorBuilder::message('Non-abstract properties cannot include hooks without bodies.')
->nonIgnorable()
Expand All @@ -85,6 +81,17 @@ public function processNode(Node $node, Scope $scope): array
];
}

if ($node->isReadOnly()) {
if ($node->hasHooks()) {
return [
RuleErrorBuilder::message('Hooked properties cannot be readonly.')
->nonIgnorable()
->identifier('property.hookReadOnly')
->build(),
];
}
}

return [];
}

Expand Down
22 changes: 22 additions & 0 deletions tests/PHPStan/Rules/Properties/PropertiesInInterfaceRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,26 @@ public function testPhp84AndPropertyHooksWithBodiesInInterface(): void
]);
}

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

$this->analyse([__DIR__ . '/data/readonly-property-hooks-in-interface.php'], [
[
'Interfaces cannot include readonly hooked properties.',
7,
],
[
'Interfaces cannot include readonly hooked properties.',
9,
],
[
'Interfaces cannot include readonly hooked properties.',
11,
],
]);
}

}
26 changes: 26 additions & 0 deletions tests/PHPStan/Rules/Properties/PropertyInClassRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,30 @@ public function testPhp84AndAbstractHookedPropertiesWithBodies(): void
]);
}

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

$this->analyse([__DIR__ . '/data/readonly-property-hooks.php'], [
[
'Hooked properties cannot be readonly.',
7,
],
[
'Hooked properties cannot be readonly.',
12,
],
[
'Hooked properties cannot be readonly.',
14,
],
[
'Hooked properties cannot be readonly.',
19,
],
]);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AbstractPerson
class PromotedHookedPropertyWithoutVisibility
{

public function __construct(mixed $test { get; })
public function __construct(public mixed $test { get; })
{

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types=1);

namespace ReadonlyPropertyHooksInInterface;

interface HelloWorld
{
public readonly string $firstName { get; set; }

public readonly string $middleName { get; }

public readonly string $lastName { set; }
}
20 changes: 20 additions & 0 deletions tests/PHPStan/Rules/Properties/data/readonly-property-hooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types=1);

namespace ReadonlyPropertyHooks;

class HelloWorld
{
public readonly string $firstName {
get => $this->firstName;
set => $this->firstName;
}

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

public readonly string $lastName { set => $this->lastName; }
}

abstract class HiWorld
{
public abstract readonly string $firstName { get { return 'jake'; } set; }
}
Loading