Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
36 changes: 36 additions & 0 deletions src/Rules/Variables/UnsetRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Properties\PropertyReflectionFinder;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\VerbosityLevel;
Expand All @@ -17,6 +18,12 @@
final class UnsetRule implements Rule
{

public function __construct(
private PropertyReflectionFinder $propertyReflectionFinder,
)
{
}

public function getNodeType(): string
{
return Node\Stmt\Unset_::class;
Expand Down Expand Up @@ -69,6 +76,35 @@ 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

$foundPropertyReflection = $this->propertyReflectionFinder->findPropertyReflectionFromNode($node, $scope);
if ($foundPropertyReflection === null) {
return null;
}

$propertyReflection = $foundPropertyReflection->getNativeReflection();
if ($propertyReflection === null) {
return null;
}

if ($propertyReflection->isReadOnly() || $propertyReflection->isReadOnlyByPhpDoc()) {
$type = $scope->getType($node->var);
Copy link
Member

Choose a reason for hiding this comment

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

You don't need to do this, just ask for property declaringClass getDisplayName.


return RuleErrorBuilder::message(
sprintf(
'Cannot unset %s property %s of %s.',
Copy link
Member

Choose a reason for hiding this comment

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

Usually we do Foo::$name format.

%s of %s sounds like an ancient text: Yennefer of Vengerberg 😀

$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
33 changes: 32 additions & 1 deletion tests/PHPStan/Rules/Variables/UnsetRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Rules\Variables;

use PHPStan\Rules\Properties\PropertyReflectionFinder;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;

Expand All @@ -13,7 +14,7 @@ class UnsetRuleTest extends RuleTestCase

protected function getRule(): Rule
{
return new UnsetRule();
return new UnsetRule(self::getContainer()->getByType(PropertyReflectionFinder::class));
}

public function testUnsetRule(): void
Expand Down Expand Up @@ -91,4 +92,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