-
Notifications
You must be signed in to change notification settings - Fork 530
Hooked properties cannot be unset() #3842
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1b77886
Hooked properties cannot be unset()
staabm 6171ebe
more consitent error message
staabm 19e1ff9
use $propertyReflection->isHooked()
staabm 3f82a55
Cannot unset property which might get hooked in subclass.
staabm 28a1696
fix version dependent errors
staabm 4e25c3e
Update src/Rules/Variables/UnsetRule.php
ondrejmirtes b93676f
Update UnsetRule.php
staabm 149b829
Update UnsetRuleTest.php
staabm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Rules\IdentifierRuleError; | ||
use PHPStan\Rules\Properties\PropertyReflectionFinder; | ||
use PHPStan\Rules\Rule; | ||
|
@@ -20,6 +21,7 @@ final class UnsetRule implements Rule | |
|
||
public function __construct( | ||
private PropertyReflectionFinder $propertyReflectionFinder, | ||
private PhpVersion $phpVersion, | ||
) | ||
{ | ||
} | ||
|
@@ -103,6 +105,36 @@ private function canBeUnset(Node $node, Scope $scope): ?IdentifierRuleError | |
->identifier($propertyReflection->isReadOnly() ? 'unset.readOnlyProperty' : 'unset.readOnlyPropertyByPhpDoc') | ||
->build(); | ||
} | ||
|
||
if ($propertyReflection->isHooked()) { | ||
return RuleErrorBuilder::message( | ||
sprintf( | ||
'Cannot unset hooked %s::$%s property.', | ||
$propertyReflection->getDeclaringClass()->getDisplayName(), | ||
$foundPropertyReflection->getName(), | ||
), | ||
) | ||
->line($node->getStartLine()) | ||
->identifier('unset.hookedProperty') | ||
->build(); | ||
} elseif ($this->phpVersion->supportsPropertyHooks()) { | ||
if ( | ||
!$propertyReflection->isPrivate() | ||
&& !$propertyReflection->isFinal()->yes() | ||
&& !$propertyReflection->getDeclaringClass()->isFinal() | ||
) { | ||
Comment on lines
+121
to
+125
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. I also though about adding public function isHookedInSubclass(): TrinaryLogic {
if (
!$this->isPrivate()
&& !$this->isFinal()->yes()
&& !$this->getDeclaringClass()->isFinal()
) {
return TrinaryLogic::createMaybe();
}
return TrinaryLogic::createNo();
} to 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. Not needed right now :) I'd have to this about that more. |
||
return RuleErrorBuilder::message( | ||
sprintf( | ||
'Cannot unset %s::$%s property which might get hooked in subclass.', | ||
ondrejmirtes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$propertyReflection->getDeclaringClass()->getDisplayName(), | ||
$foundPropertyReflection->getName(), | ||
), | ||
) | ||
->line($node->getStartLine()) | ||
->identifier('unset.maybeHookedProperty') | ||
staabm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
->build(); | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
tests/PHPStan/Rules/Variables/data/unset-hooked-property.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php // lint >= 8.4 | ||
|
||
namespace UnsetHookedProperty; | ||
|
||
function doUnset(Foo $foo, User $user, NonFinalClass $nonFinalClass, FinalClass $finalClass): void { | ||
unset($user->name); | ||
unset($user->fullName); | ||
|
||
unset($foo->ii); | ||
unset($foo->iii); | ||
|
||
unset($nonFinalClass->publicFinalProperty); | ||
unset($nonFinalClass->publicProperty); | ||
|
||
unset($finalClass->publicFinalProperty); | ||
unset($finalClass->publicProperty); | ||
} | ||
|
||
class User | ||
{ | ||
public string $name { | ||
set { | ||
if (strlen($value) === 0) { | ||
throw new \ValueError("Name must be non-empty"); | ||
} | ||
$this->name = $value; | ||
} | ||
} | ||
|
||
public string $fullName { | ||
get { | ||
return "Yennefer of Vengerberg"; | ||
} | ||
} | ||
|
||
public function __construct(string $name) { | ||
$this->name = $name; | ||
} | ||
} | ||
|
||
abstract class Foo | ||
{ | ||
abstract protected int $ii { get; } | ||
|
||
abstract public int $iii { get; } | ||
} | ||
|
||
class NonFinalClass { | ||
private string $privateProperty; | ||
public string $publicProperty; | ||
final public string $publicFinalProperty; | ||
|
||
function doFoo() { | ||
unset($this->privateProperty); | ||
} | ||
} | ||
|
||
final class FinalClass { | ||
private string $privateProperty; | ||
public string $publicProperty; | ||
final public string $publicFinalProperty; | ||
|
||
function doFoo() { | ||
unset($this->privateProperty); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.