Skip to content

Commit

Permalink
Overriding property - when overriding readable property, the property…
Browse files Browse the repository at this point in the history
… has to be readable (same for writable)
  • Loading branch information
ondrejmirtes committed Feb 13, 2025
1 parent d3909c7 commit 924a7a2
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/Rules/Properties/OverridingPropertyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,32 @@ public function processNode(Node $node, Scope $scope): array
}
}

$propertyReflection = $classReflection->getNativeProperty($node->getName());
if ($this->phpVersion->supportsPropertyHooks()) {
if ($prototype->isReadable()) {
if (!$propertyReflection->isReadable()) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Property %s::$%s overriding readable property %s::$%s also has to be readable.',
$classReflection->getDisplayName(),
$node->getName(),
$prototype->getDeclaringClass()->getDisplayName(),
$node->getName(),
))->identifier('property.notReadable')->nonIgnorable()->build();
}
}
if ($prototype->isWritable()) {
if (!$propertyReflection->isWritable()) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Property %s::$%s overriding writable property %s::$%s also has to be writable.',
$classReflection->getDisplayName(),
$node->getName(),
$prototype->getDeclaringClass()->getDisplayName(),
$node->getName(),
))->identifier('property.notWritable')->nonIgnorable()->build();
}
}
}

if ($prototype->isPublic()) {
if (!$node->isPublic()) {
$errors[] = RuleErrorBuilder::message(sprintf(
Expand Down Expand Up @@ -198,7 +224,6 @@ public function processNode(Node $node, Scope $scope): array
return $errors;
}

$propertyReflection = $classReflection->getNativeProperty($node->getName());
if ($prototype->getReadableType()->equals($propertyReflection->getReadableType())) {
return $errors;
}
Expand Down
12 changes: 12 additions & 0 deletions tests/PHPStan/Rules/Properties/OverridingPropertyRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,18 @@ public function testPropertyPrototypeFromInterface(): void
'Type string of property Bug12466\Bar::$a is not the same as type int of overridden property Bug12466\Foo::$a.',
15,
],
[
'Property Bug12466\TestMoreProps::$a overriding writable property Bug12466\MoreProps::$a also has to be writable.',
34,
],
[
'Property Bug12466\TestMoreProps::$b overriding readable property Bug12466\MoreProps::$b also has to be readable.',
41,
],
[
'Property Bug12466\TestMoreProps::$c overriding writable property Bug12466\MoreProps::$c also has to be writable.',
48,
],
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,40 @@ class Bar implements Foo
public string $a;

}

interface MoreProps
{

public int $a { get; set; }

public int $b { get; }

public int $c { set; }

}

class TestMoreProps implements MoreProps
{

// not writable
public int $a {
get {
return 1;
}
}

// not readable
public int $b {
set {
$this->a = 1;
}
}

// not writable
public int $c {
get {
return 1;
}
}

}

0 comments on commit 924a7a2

Please sign in to comment.