-
Notifications
You must be signed in to change notification settings - Fork 18
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
Detect override of deprecated property #90
Open
Khartir
wants to merge
5
commits into
phpstan:1.1.x
Choose a base branch
from
Khartir:overridden-property
base: 1.1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cf66c3d
detect override of deprecated property
Khartir 0a68ae1
detect override of deprecated method
Khartir db87881
detect override of deprecated constant
Khartir 2ab5276
handle deprected methods in traits
Khartir b924bc4
remove unnecessary null check
Khartir 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 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 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 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 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,73 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Deprecations; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\ClassConst; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use function sprintf; | ||
|
||
/** | ||
* @implements Rule<ClassConst> | ||
*/ | ||
class OverrideDeprecatedConstantRule implements Rule | ||
{ | ||
|
||
/** @var DeprecatedScopeHelper */ | ||
private $deprecatedScopeHelper; | ||
|
||
public function __construct(DeprecatedScopeHelper $deprecatedScopeHelper) | ||
{ | ||
$this->deprecatedScopeHelper = $deprecatedScopeHelper; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return ClassConst::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) { | ||
return []; | ||
} | ||
|
||
if (!$scope->isInClass()) { | ||
return []; | ||
} | ||
|
||
if ($node->isPrivate()) { | ||
return []; | ||
} | ||
|
||
$class = $scope->getClassReflection(); | ||
|
||
$parents = $class->getParents(); | ||
|
||
$name = (string) $node->consts[0]->name; | ||
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 should go through all the constants, not just the first one. |
||
|
||
foreach ($parents as $parent) { | ||
if (!$parent->hasConstant($name)) { | ||
continue; | ||
} | ||
|
||
$parentConst = $parent->getConstant($name); | ||
|
||
if (!$parentConst->isDeprecated()->yes()) { | ||
return []; | ||
} | ||
|
||
return [RuleErrorBuilder::message(sprintf( | ||
'Class %s overrides deprecated const %s of class %s.', | ||
$class->getName(), | ||
$name, | ||
$parent->getName() | ||
))->identifier('constant.deprecated')->build()]; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
} |
This file contains 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,88 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Deprecations; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use function sprintf; | ||
|
||
/** | ||
* @implements Rule<ClassMethod> | ||
*/ | ||
class OverrideDeprecatedMethodRule implements Rule | ||
{ | ||
|
||
/** @var DeprecatedScopeHelper */ | ||
private $deprecatedScopeHelper; | ||
|
||
public function __construct(DeprecatedScopeHelper $deprecatedScopeHelper) | ||
{ | ||
$this->deprecatedScopeHelper = $deprecatedScopeHelper; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return ClassMethod::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) { | ||
return []; | ||
} | ||
|
||
if (!$scope->isInClass()) { | ||
return []; | ||
} | ||
|
||
if ($node->isPrivate()) { | ||
return []; | ||
} | ||
|
||
$class = $scope->getClassReflection(); | ||
|
||
$ancestors = $class->getAncestors(); | ||
|
||
$methodName = (string) $node->name; | ||
|
||
$method = $class->getMethod($methodName, $scope); | ||
|
||
if ($method->isDeprecated()->no()) { | ||
return []; | ||
} | ||
|
||
foreach ($ancestors as $ancestor) { | ||
if ($ancestor === $class) { | ||
continue; | ||
} | ||
|
||
if ($ancestor->isTrait()) { | ||
continue; | ||
} | ||
|
||
if (!$ancestor->hasMethod($methodName)) { | ||
continue; | ||
} | ||
|
||
$ancestorMethod = $ancestor->getMethod($methodName, $scope); | ||
|
||
if (!$ancestorMethod->isDeprecated()->yes()) { | ||
return []; | ||
} | ||
|
||
return [RuleErrorBuilder::message(sprintf( | ||
'Class %s overrides deprecated method %s of %s %s.', | ||
$class->getName(), | ||
$methodName, | ||
$ancestor->isInterface() ? 'interface' : 'class', | ||
$ancestor->getName() | ||
))->identifier('method.deprecated')->build()]; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
} |
This file contains 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,79 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Deprecations; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Property; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use function sprintf; | ||
|
||
/** | ||
* @implements Rule<Property> | ||
*/ | ||
class OverrideDeprecatedPropertyRule implements Rule | ||
{ | ||
|
||
/** @var DeprecatedScopeHelper */ | ||
private $deprecatedScopeHelper; | ||
|
||
public function __construct(DeprecatedScopeHelper $deprecatedScopeHelper) | ||
{ | ||
$this->deprecatedScopeHelper = $deprecatedScopeHelper; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Property::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) { | ||
return []; | ||
} | ||
|
||
if (!$scope->isInClass()) { | ||
return []; | ||
} | ||
|
||
if ($node->isPrivate()) { | ||
return []; | ||
} | ||
|
||
$class = $scope->getClassReflection(); | ||
|
||
$parents = $class->getParents(); | ||
|
||
$propertyName = (string) $node->props[0]->name; | ||
Khartir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
$property = $class->getProperty($propertyName, $scope); | ||
|
||
if ($property->isDeprecated()->no()) { | ||
return []; | ||
} | ||
|
||
foreach ($parents as $parent) { | ||
if (!$parent->hasProperty($propertyName)) { | ||
continue; | ||
} | ||
|
||
$parentProperty = $parent->getProperty($propertyName, $scope); | ||
|
||
if (!$parentProperty->isDeprecated()->yes()) { | ||
return []; | ||
} | ||
|
||
return [RuleErrorBuilder::message(sprintf( | ||
'Class %s overrides deprecated property %s of class %s.', | ||
$class->getName(), | ||
$propertyName, | ||
$parent->getName() | ||
))->identifier('property.deprecated')->build()]; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
tests/Rules/Deprecations/OverrideDeprecatedConstantRuleTest.php
This file contains 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,32 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Deprecations; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<OverrideDeprecatedConstantRule> | ||
*/ | ||
class OverrideDeprecatedConstantRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new OverrideDeprecatedConstantRule(new DeprecatedScopeHelper([new DefaultDeprecatedScopeResolver()])); | ||
} | ||
|
||
public function testDeprecatedConstantOverride(): void | ||
{ | ||
$this->analyse( | ||
[__DIR__ . '/data/override-deprecated-constant.php'], | ||
[ | ||
[ | ||
'Class OverrideDeprecatedConstant\Child overrides deprecated const DEPRECATED of class OverrideDeprecatedConstant\Ancestor.', | ||
20, | ||
], | ||
] | ||
); | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
tests/Rules/Deprecations/OverrideDeprecatedMethodRuleTest.php
This file contains 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,44 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Deprecations; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<OverrideDeprecatedMethodRule> | ||
*/ | ||
class OverrideDeprecatedMethodRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new OverrideDeprecatedMethodRule(new DeprecatedScopeHelper([new DefaultDeprecatedScopeResolver()])); | ||
} | ||
|
||
public function testDeprecatedMethodOverride(): void | ||
{ | ||
$this->analyse( | ||
[__DIR__ . '/data/override-deprecated-method.php'], | ||
[ | ||
[ | ||
'Class OverrideDeprecatedMethod\Child overrides deprecated method deprecatedMethod of class OverrideDeprecatedMethod\Ancestor.', | ||
49, | ||
], | ||
[ | ||
'Class OverrideDeprecatedMethod\Child overrides deprecated method deprecatedInInterface of interface OverrideDeprecatedMethod\Deprecated.', | ||
61, | ||
], | ||
[ | ||
'Class OverrideDeprecatedMethod\Child overrides deprecated method deprecatedInTrait of class OverrideDeprecatedMethod\Ancestor.', | ||
64, | ||
], | ||
[ | ||
'Class OverrideDeprecatedMethod\GrandChild overrides deprecated method deprecatedInChild of class OverrideDeprecatedMethod\Child.', | ||
73, | ||
], | ||
] | ||
); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
tests/Rules/Deprecations/OverrideDeprecatedPropertyRuleTest.php
This file contains 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,32 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Deprecations; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<OverrideDeprecatedPropertyRule> | ||
*/ | ||
class OverrideDeprecatedPropertyRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new OverrideDeprecatedPropertyRule(new DeprecatedScopeHelper([new DefaultDeprecatedScopeResolver()])); | ||
} | ||
|
||
public function testDeprecatedPropertyOverride(): void | ||
{ | ||
$this->analyse( | ||
[__DIR__ . '/data/override-deprecated-property.php'], | ||
[ | ||
[ | ||
'Class OverrideDeprecatedProperty\Child overrides deprecated property deprecatedProperty of class OverrideDeprecatedProperty\Ancestor.', | ||
25, | ||
], | ||
] | ||
); | ||
} | ||
|
||
} |
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.
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.
You shouldn't go through all the parents. This would break the
@deprecated
/@not-deprecated
behaviour. Please read about it and write a test: https://phpstan.org/writing-php-code/phpdocs-basics#deprecationsIt's sufficient to ask for the property in the first parent class if there's one.
You should also look into interfaces.