Skip to content
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
wants to merge 5 commits into
base: 1.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
],
"require": {
"php": "^7.2 || ^8.0",
"phpstan/phpstan": "^1.10.3"
"phpstan/phpstan": "^1.10.24"
},
"require-dev": {
"php-parallel-lint/php-parallel-lint": "^1.2",
Expand Down
14 changes: 14 additions & 0 deletions rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ services:
class: PHPStan\Rules\Deprecations\DefaultDeprecatedScopeResolver
tags:
- phpstan.deprecations.deprecatedScopeResolver
-
class: PHPStan\Rules\Deprecations\OverrideDeprecatedPropertyRule
-
class: PHPStan\Rules\Deprecations\OverrideDeprecatedMethodRule
-
class: PHPStan\Rules\Deprecations\OverrideDeprecatedConstantRule

rules:
- PHPStan\Rules\Deprecations\AccessDeprecatedPropertyRule
Expand All @@ -33,3 +39,11 @@ rules:
- PHPStan\Rules\Deprecations\TypeHintDeprecatedInFunctionSignatureRule
- PHPStan\Rules\Deprecations\UsageOfDeprecatedCastRule
- PHPStan\Rules\Deprecations\UsageOfDeprecatedTraitRule

conditionalTags:
PHPStan\Rules\Deprecations\OverrideDeprecatedPropertyRule:
phpstan.rules.rule: %featureToggles.bleedingEdge%
PHPStan\Rules\Deprecations\OverrideDeprecatedMethodRule:
phpstan.rules.rule: %featureToggles.bleedingEdge%
PHPStan\Rules\Deprecations\OverrideDeprecatedConstantRule:
phpstan.rules.rule: %featureToggles.bleedingEdge%
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if ($node->extends === null) {
return [];
}

$interfaceName = isset($node->namespacedName)
? (string) $node->namespacedName
: (string) $node->name;
Expand Down
73 changes: 73 additions & 0 deletions src/Rules/Deprecations/OverrideDeprecatedConstantRule.php
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();
Copy link
Member

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#deprecations

It's sufficient to ask for the property in the first parent class if there's one.

You should also look into interfaces.


$name = (string) $node->consts[0]->name;
Copy link
Member

Choose a reason for hiding this comment

The 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 [];
}

}
88 changes: 88 additions & 0 deletions src/Rules/Deprecations/OverrideDeprecatedMethodRule.php
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 [];
}

}
79 changes: 79 additions & 0 deletions src/Rules/Deprecations/OverrideDeprecatedPropertyRule.php
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 tests/Rules/Deprecations/OverrideDeprecatedConstantRuleTest.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<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 tests/Rules/Deprecations/OverrideDeprecatedMethodRuleTest.php
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 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(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,
],
]
);
}

}
Loading