Skip to content

Commit

Permalink
detect override of deprecated property
Browse files Browse the repository at this point in the history
  • Loading branch information
Khartir committed Mar 17, 2023
1 parent a22b36b commit a5d3011
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
1 change: 1 addition & 0 deletions rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ rules:
- PHPStan\Rules\Deprecations\InheritanceOfDeprecatedClassRule
- PHPStan\Rules\Deprecations\InheritanceOfDeprecatedInterfaceRule
- PHPStan\Rules\Deprecations\InstantiationOfDeprecatedClassRule
- PHPStan\Rules\Deprecations\OverrideDeprecatedPropertyRule
- PHPStan\Rules\Deprecations\TypeHintDeprecatedInClassMethodSignatureRule
- PHPStan\Rules\Deprecations\TypeHintDeprecatedInClosureSignatureRule
- PHPStan\Rules\Deprecations\TypeHintDeprecatedInFunctionSignatureRule
Expand Down
60 changes: 60 additions & 0 deletions src/Rules/Deprecations/OverrideDeprecatedPropertyRule.php
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 tests/Rules/Deprecations/OverrideDeprecatedPropertyRuleTest.php
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 tests/Rules/Deprecations/data/override-deprecated-property.php
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;
}

0 comments on commit a5d3011

Please sign in to comment.