Skip to content

Commit 263d66c

Browse files
paxalondrejmirtes
authored andcommitted
Listen to InClassMethodNode
1 parent 7833bc7 commit 263d66c

File tree

5 files changed

+116
-113
lines changed

5 files changed

+116
-113
lines changed

rules.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ rules:
99
- PHPStan\Rules\Deprecations\InheritanceOfDeprecatedClassRule
1010
- PHPStan\Rules\Deprecations\InheritanceOfDeprecatedInterfaceRule
1111
- PHPStan\Rules\Deprecations\InstantiationOfDeprecatedClassRule
12-
- PHPStan\Rules\Deprecations\TypeHintDeprecatedInFunctionSignatureRule
12+
- PHPStan\Rules\Deprecations\TypeHintDeprecatedInClassMethodSignatureRule
1313
- PHPStan\Rules\Deprecations\UsageOfDeprecatedTraitRule
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Deprecations;
4+
5+
use PhpParser\Node;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Broker\Broker;
8+
use PHPStan\Node\InClassMethodNode;
9+
use PHPStan\Reflection\MethodReflection;
10+
use PHPStan\Reflection\ParameterReflection;
11+
use PHPStan\Reflection\ParametersAcceptorSelector;
12+
13+
class TypeHintDeprecatedInClassMethodSignatureRule implements \PHPStan\Rules\Rule
14+
{
15+
16+
/** @var Broker */
17+
private $broker;
18+
19+
public function __construct(Broker $broker)
20+
{
21+
$this->broker = $broker;
22+
}
23+
24+
public function getNodeType(): string
25+
{
26+
return InClassMethodNode::class;
27+
}
28+
29+
/**
30+
* @param InClassMethodNode $node
31+
* @param \PHPStan\Analyser\Scope $scope
32+
* @return string[] errors
33+
*/
34+
public function processNode(Node $node, Scope $scope): array
35+
{
36+
/** @var MethodReflection $method */
37+
$method = $scope->getFunction();
38+
if (!$method instanceof MethodReflection) {
39+
throw new \PHPStan\ShouldNotHappenException();
40+
}
41+
$methodSignature = ParametersAcceptorSelector::selectSingle($method->getVariants());
42+
43+
$errorLists = [];
44+
foreach ($methodSignature->getParameters() as $i => $parameter) {
45+
/** @var ParameterReflection $parameter */
46+
$errorLists[] = $this->checkClasses(
47+
$parameter->getType()->getReferencedClasses(),
48+
'argument #' . $i
49+
);
50+
}
51+
52+
$errorLists[] = $this->checkClasses(
53+
$methodSignature->getReturnType()->getReferencedClasses(),
54+
'return type'
55+
);
56+
57+
return array_merge(...$errorLists);
58+
}
59+
60+
/**
61+
* @param string[] $referencedClasses
62+
* @param string $where
63+
* @return string[]
64+
*/
65+
private function checkClasses(array $referencedClasses, string $where): array
66+
{
67+
$errors = [];
68+
foreach ($referencedClasses as $referencedClass) {
69+
try {
70+
$class = $this->broker->getClass($referencedClass);
71+
} catch (\PHPStan\Broker\ClassNotFoundException $e) {
72+
continue;
73+
}
74+
75+
if (!$class->isDeprecated()) {
76+
continue;
77+
}
78+
79+
$classDescription = null;
80+
if (method_exists($class, 'getDeprecatedDescription')) {
81+
$classDescription = $class->getDeprecatedDescription();
82+
}
83+
84+
if ($classDescription === null) {
85+
$errors[] = sprintf(
86+
'Usage of deprecated class %s in %s.',
87+
$referencedClass,
88+
$where
89+
);
90+
} else {
91+
$errors[] = sprintf(
92+
"Usage of deprecated class %s in %s:\n%s",
93+
$referencedClass,
94+
$where,
95+
$classDescription
96+
);
97+
}
98+
}
99+
100+
return $errors;
101+
}
102+
103+
}

src/Rules/Deprecations/TypeHintDeprecatedInFunctionSignatureRule.php

Lines changed: 0 additions & 106 deletions
This file was deleted.

tests/Rules/Deprecations/TypeHintDeprecatedInFunctionSignatureRuleTest.php renamed to tests/Rules/Deprecations/TypeHintDeprecatedInClassMethodSignatureRuleTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace PHPStan\Rules\Deprecations;
44

5-
class TypeHintDeprecatedInFunctionSignatureRuleTest extends \PHPStan\Testing\RuleTestCase
5+
class TypeHintDeprecatedInClassMethodSignatureRuleTest extends \PHPStan\Testing\RuleTestCase
66
{
77

88
protected function getRule(): \PHPStan\Rules\Rule
99
{
1010
$broker = $this->createBroker();
1111

12-
return new TypeHintDeprecatedInFunctionSignatureRule($broker);
12+
return new TypeHintDeprecatedInClassMethodSignatureRule($broker);
1313
}
1414

1515
public function test(): void
@@ -18,9 +18,10 @@ public function test(): void
1818
$this->analyse(
1919
[__DIR__ . '/data/typehint-deprecated-class.php'],
2020
[
21-
['Usage of deprecated class TypeHintDeprecatedInFunctionSignature\DeprecatedProperty in argument #0.', 10],
22-
['Usage of deprecated class TypeHintDeprecatedInFunctionSignature\DeprecatedProperty in argument #1.', 10],
23-
['Usage of deprecated class TypeHintDeprecatedInFunctionSignature\DeprecatedProperty in return type.', 10],
21+
['Usage of deprecated class TypeHintDeprecatedInFunctionSignature\DeprecatedProperty in argument #0.', 13],
22+
['Usage of deprecated class TypeHintDeprecatedInFunctionSignature\DeprecatedProperty in argument #1.', 13],
23+
['Usage of deprecated class TypeHintDeprecatedInFunctionSignature\DeprecatedProperty in argument #5.', 13],
24+
['Usage of deprecated class TypeHintDeprecatedInFunctionSignature\DeprecatedProperty in return type.', 13],
2425
]
2526
);
2627
}

tests/Rules/Deprecations/data/typehint-deprecated-class.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,24 @@ class Foo
77

88
private $properties;
99

10+
/**
11+
* @param DeprecatedProperty $property6
12+
*/
1013
public function setProperties(
1114
DeprecatedProperty $property,
1215
?DeprecatedProperty $property2,
1316
$property3,
1417
Property $property4,
15-
string $property5
18+
string $property5,
19+
$property6
1620
): DeprecatedProperty {
1721
$this->properties = [
1822
$property,
1923
$property2,
2024
$property3,
2125
$property4,
2226
$property5,
27+
$property6
2328
];
2429

2530
return $property;

0 commit comments

Comments
 (0)