Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
25 changes: 25 additions & 0 deletions src/Rules/Variables/UnsetRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,31 @@ private function canBeUnset(Node $node, Scope $scope): ?IdentifierRuleError
}

return $this->canBeUnset($node->var, $scope);
} elseif (
$node instanceof Node\Expr\PropertyFetch
&& $node->name instanceof Node\Identifier
) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have better ways of getting a property reflection from PropertyFetch. Two actually:

  1. PropertyReflectionFinder. FoundPropertyReflection will even tell you if it's a native one or not.
  2. Scope::getPropertyReflection()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh nice.

I was already searching how to know about property hooks so I can implement the error for unsetting a hooked property.

will be a followup PR

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote about property hooks here phpstan/phpstan#12337

$type = $scope->getType($node->var);
foreach ($type->getObjectClassReflections() as $classReflection) {
if (!$classReflection->hasNativeProperty($node->name->name)) {
continue;
}

$propertyReflection = $classReflection->getNativeProperty($node->name->name);
if ($propertyReflection->isReadOnly() || $propertyReflection->isReadOnlyByPhpDoc()) {
return RuleErrorBuilder::message(
sprintf(
'Cannot unset %s property %s of %s.',
$propertyReflection->isReadOnly() ? 'readonly' : '@readonly',
$node->name->name,
$type->describe(VerbosityLevel::value()),
),
)
->line($node->getStartLine())
->identifier($propertyReflection->isReadOnly() ? 'unset.readOnlyProperty' : 'unset.readOnlyPropertyByPhpDoc')
->build();
}
}
}

return null;
Expand Down
30 changes: 30 additions & 0 deletions tests/PHPStan/Rules/Variables/UnsetRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,34 @@ public function testBug4565(): void
$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-4565.php'], []);
}

public function testBug12421(): void
{
$this->analyse([__DIR__ . '/data/bug-12421.php'], [
[
'Cannot unset readonly property y of Bug12421\NativeReadonlyClass.',
11,
],
[
'Cannot unset readonly property y of Bug12421\NativeReadonlyProperty.',
15,
],
[
'Cannot unset @readonly property y of Bug12421\PhpdocReadonlyClass.',
19,
],
[
'Cannot unset @readonly property y of Bug12421\PhpdocReadonlyProperty.',
23,
],
[
'Cannot unset @readonly property y of Bug12421\PhpdocImmutableClass.',
27,
],
[
'Cannot unset readonly property y of Bug12421\NativeReadonlyPropertySubClass.',
34,
],
]);
}

}
108 changes: 108 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-12421.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php // lint >= 8.2

namespace Bug12421;

function doFoo() {
$x = new RegularProperty();
unset($x->y);
var_dump($x->y);

$x = new NativeReadonlyClass();
unset($x->y);
var_dump($x->y);

$x = new NativeReadonlyProperty();
unset($x->y);
var_dump($x->y);

$x = new PhpdocReadonlyClass();
unset($x->y);
var_dump($x->y);

$x = new PhpdocReadonlyProperty();
unset($x->y);
var_dump($x->y);

$x = new PhpdocImmutableClass();
unset($x->y);
var_dump($x->y);

$x = new \stdClass();
unset($x->y);

$x = new NativeReadonlyPropertySubClass();
unset($x->y);
var_dump($x->y);
}

readonly class NativeReadonlyClass
{
public Y $y;

public function __construct()
{
$this->y = new Y();
}
}

class NativeReadonlyProperty
{
public readonly Y $y;

public function __construct()
{
$this->y = new Y();
}
}

/** @readonly */
class PhpdocReadonlyClass
{
public Y $y;

public function __construct()
{
$this->y = new Y();
}
}

class PhpdocReadonlyProperty
{
/** @readonly */
public Y $y;

public function __construct()
{
$this->y = new Y();
}
}

/** @immutable */
class PhpdocImmutableClass
{
public Y $y;

public function __construct()
{
$this->y = new Y();
}
}

class RegularProperty
{
public Y $y;

public function __construct()
{
$this->y = new Y();
}
}

class NativeReadonlyPropertySubClass extends NativeReadonlyProperty
{
}

class Y
{
}

Loading