Skip to content

Commit ba6db34

Browse files
paxalondrejmirtes
authored andcommitted
Introduce DeprecatedClassHelper
1 parent a5f7b4d commit ba6db34

File tree

4 files changed

+81
-62
lines changed

4 files changed

+81
-62
lines changed

rules.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
services:
2+
-
3+
class: PHPStan\Rules\Deprecations\DeprecatedClassHelper
4+
15
rules:
26
- PHPStan\Rules\Deprecations\AccessDeprecatedPropertyRule
37
- PHPStan\Rules\Deprecations\AccessDeprecatedStaticPropertyRule
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Deprecations;
4+
5+
use PHPStan\Broker\Broker;
6+
use PHPStan\Reflection\ClassReflection;
7+
8+
class DeprecatedClassHelper
9+
{
10+
11+
/** @var Broker */
12+
private $broker;
13+
14+
public function __construct(Broker $broker)
15+
{
16+
$this->broker = $broker;
17+
}
18+
19+
public function getClassType(ClassReflection $class): string
20+
{
21+
if ($class->isInterface()) {
22+
return 'interface';
23+
}
24+
25+
return 'class';
26+
}
27+
28+
public function getClassDeprecationDescription(ClassReflection $class): string
29+
{
30+
$description = $class->getDeprecatedDescription();
31+
if ($description === null) {
32+
return '.';
33+
}
34+
35+
return sprintf(":\n%s", $description);
36+
}
37+
38+
/**
39+
* @param string[] $referencedClasses
40+
* @return ClassReflection[]
41+
*/
42+
public function filterDeprecatedClasses(array $referencedClasses): array
43+
{
44+
$deprecatedClasses = [];
45+
foreach ($referencedClasses as $referencedClass) {
46+
try {
47+
$class = $this->broker->getClass($referencedClass);
48+
} catch (\PHPStan\Broker\ClassNotFoundException $e) {
49+
continue;
50+
}
51+
52+
if (!$class->isDeprecated()) {
53+
continue;
54+
}
55+
56+
$deprecatedClasses[] = $class;
57+
}
58+
59+
return $deprecatedClasses;
60+
}
61+
62+
}

src/Rules/Deprecations/TypeHintDeprecatedInClassMethodSignatureRule.php

Lines changed: 14 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,19 @@
44

55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
7-
use PHPStan\Broker\Broker;
87
use PHPStan\Node\InClassMethodNode;
9-
use PHPStan\Reflection\ClassReflection;
108
use PHPStan\Reflection\MethodReflection;
11-
use PHPStan\Reflection\ParameterReflection;
129
use PHPStan\Reflection\ParametersAcceptorSelector;
1310

1411
class TypeHintDeprecatedInClassMethodSignatureRule implements \PHPStan\Rules\Rule
1512
{
1613

17-
/** @var Broker */
18-
private $broker;
14+
/** @var DeprecatedClassHelper */
15+
private $deprecatedClassHelper;
1916

20-
public function __construct(Broker $broker)
17+
public function __construct(DeprecatedClassHelper $deprecatedClassHelper)
2118
{
22-
$this->broker = $broker;
19+
$this->deprecatedClassHelper = $deprecatedClassHelper;
2320
}
2421

2522
public function getNodeType(): string
@@ -47,98 +44,54 @@ public function processNode(Node $node, Scope $scope): array
4744

4845
$errors = [];
4946
foreach ($methodSignature->getParameters() as $i => $parameter) {
50-
/** @var ParameterReflection $parameter */
51-
$deprecatedClasses = $this->filterDeprecatedClasses($parameter->getType()->getReferencedClasses());
47+
$deprecatedClasses = $this->deprecatedClassHelper->filterDeprecatedClasses($parameter->getType()->getReferencedClasses());
5248
foreach ($deprecatedClasses as $deprecatedClass) {
5349
if ($method->getDeclaringClass()->isAnonymous()) {
5450
$errors[] = sprintf(
5551
'Parameter $%s of method %s() in anonymous class has typehint with deprecated %s %s%s',
5652
$parameter->getName(),
5753
$method->getName(),
58-
$this->getClassType($deprecatedClass),
54+
$this->deprecatedClassHelper->getClassType($deprecatedClass),
5955
$deprecatedClass->getName(),
60-
$this->getClassDeprecationDescription($deprecatedClass)
56+
$this->deprecatedClassHelper->getClassDeprecationDescription($deprecatedClass)
6157
);
6258
} else {
6359
$errors[] = sprintf(
6460
'Parameter $%s of method %s::%s() has typehint with deprecated %s %s%s',
6561
$parameter->getName(),
6662
$method->getDeclaringClass()->getName(),
6763
$method->getName(),
68-
$this->getClassType($deprecatedClass),
64+
$this->deprecatedClassHelper->getClassType($deprecatedClass),
6965
$deprecatedClass->getName(),
70-
$this->getClassDeprecationDescription($deprecatedClass)
66+
$this->deprecatedClassHelper->getClassDeprecationDescription($deprecatedClass)
7167
);
7268
}
7369
}
7470
}
7571

76-
$deprecatedClasses = $this->filterDeprecatedClasses($methodSignature->getReturnType()->getReferencedClasses());
72+
$deprecatedClasses = $this->deprecatedClassHelper->filterDeprecatedClasses($methodSignature->getReturnType()->getReferencedClasses());
7773
foreach ($deprecatedClasses as $deprecatedClass) {
7874
if ($method->getDeclaringClass()->isAnonymous()) {
7975
$errors[] = sprintf(
8076
'Return type of method %s() in anonymous class has typehint with deprecated %s %s%s',
8177
$method->getName(),
82-
$this->getClassType($deprecatedClass),
78+
$this->deprecatedClassHelper->getClassType($deprecatedClass),
8379
$deprecatedClass->getName(),
84-
$this->getClassDeprecationDescription($deprecatedClass)
80+
$this->deprecatedClassHelper->getClassDeprecationDescription($deprecatedClass)
8581
);
8682
} else {
8783
$errors[] = sprintf(
8884
'Return type of method %s::%s() has typehint with deprecated %s %s%s',
8985
$method->getDeclaringClass()->getName(),
9086
$method->getName(),
91-
$this->getClassType($deprecatedClass),
87+
$this->deprecatedClassHelper->getClassType($deprecatedClass),
9288
$deprecatedClass->getName(),
93-
$this->getClassDeprecationDescription($deprecatedClass)
89+
$this->deprecatedClassHelper->getClassDeprecationDescription($deprecatedClass)
9490
);
9591
}
9692
}
9793

9894
return $errors;
9995
}
10096

101-
private function getClassType(ClassReflection $class): string
102-
{
103-
if ($class->isInterface()) {
104-
return 'interface';
105-
}
106-
107-
return 'class';
108-
}
109-
110-
private function getClassDeprecationDescription(ClassReflection $class): string
111-
{
112-
$description = $class->getDeprecatedDescription();
113-
if ($description === null) {
114-
return '.';
115-
}
116-
117-
return sprintf(":\n%s", $description);
118-
}
119-
120-
/**
121-
* @param string[] $referencedClasses
122-
* @return ClassReflection[]
123-
*/
124-
private function filterDeprecatedClasses(array $referencedClasses): array
125-
{
126-
$deprecatedClasses = [];
127-
foreach ($referencedClasses as $referencedClass) {
128-
try {
129-
$class = $this->broker->getClass($referencedClass);
130-
} catch (\PHPStan\Broker\ClassNotFoundException $e) {
131-
continue;
132-
}
133-
134-
if (!$class->isDeprecated()) {
135-
continue;
136-
}
137-
138-
$deprecatedClasses[] = $class;
139-
}
140-
141-
return $deprecatedClasses;
142-
}
143-
14497
}

tests/Rules/Deprecations/TypeHintDeprecatedInClassMethodSignatureRuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ protected function getRule(): \PHPStan\Rules\Rule
99
{
1010
$broker = $this->createBroker();
1111

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

1515
public function test(): void

0 commit comments

Comments
 (0)