-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
detect override of deprecated property
- Loading branch information
Showing
4 changed files
with
109 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?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 function sprintf; | ||
|
||
/** | ||
* @implements Rule<Property> | ||
*/ | ||
class OverrideDeprecatedPropertyRule implements Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Property::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) { | ||
return []; | ||
} | ||
|
||
if (!$scope->isInClass()) { | ||
return []; | ||
} | ||
|
||
$class = $scope->getClassReflection(); | ||
|
||
$parents = $class->getParents(); | ||
|
||
$propertyName = (string) $node->props[0]->name; | ||
|
||
foreach ($parents as $parent) { | ||
if (!$parent->hasProperty($propertyName)) { | ||
continue; | ||
} | ||
|
||
$parentProperty = $parent->getProperty($propertyName, $scope); | ||
|
||
if (!$parentProperty->isDeprecated()->yes()) { | ||
return []; | ||
} | ||
|
||
return [sprintf( | ||
'Class %s overrides deprecated property %s of class %s.', | ||
$class->getName(), | ||
$propertyName, | ||
$parent->getName() | ||
)]; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
} |
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(); | ||
} | ||
|
||
public function testDeprecatedMethodCall(): void | ||
{ | ||
$this->analyse( | ||
[__DIR__ . '/data/override-deprecated-property.php'], | ||
[ | ||
[ | ||
'Class OverrideDeprecatedProperty\Child overrides deprecated property foo of class OverrideDeprecatedProperty\Foo.', | ||
15, | ||
], | ||
] | ||
); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
tests/Rules/Deprecations/data/override-deprecated-property.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,16 @@ | ||
<?php | ||
|
||
namespace OverrideDeprecatedProperty; | ||
|
||
class Foo | ||
{ | ||
/** | ||
* @deprecated | ||
*/ | ||
public $foo; | ||
} | ||
|
||
class Child extends Foo | ||
{ | ||
public $foo; | ||
} |