-
Notifications
You must be signed in to change notification settings - Fork 530
Readonly properties cannot be unset() #3827
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
Changes from 5 commits
f26ae2f
c07127c
ca308ab
aa238e2
c4c7b8c
71ff1f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -17,6 +18,12 @@ | |
final class UnsetRule implements Rule | ||
{ | ||
|
||
public function __construct( | ||
private PropertyReflectionFinder $propertyReflectionFinder, | ||
) | ||
{ | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Stmt\Unset_::class; | ||
|
@@ -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 | ||
) { | ||
$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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
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 | ||
{ | ||
} | ||
|
There was a problem hiding this comment.
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:
Scope::getPropertyReflection()
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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