Skip to content

Commit a4d4a19

Browse files
committed
Revert "move helpers to base class"
This reverts commit f7e5c88.
1 parent f772374 commit a4d4a19

File tree

3 files changed

+63
-32
lines changed

3 files changed

+63
-32
lines changed

src/Rules/Deprecations/ConfigEntityConfigExportRule.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
7+
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
8+
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
79
use PHPStan\Reflection\ClassReflection;
810
use PHPStan\ShouldNotHappenException;
911
use function preg_match;
@@ -21,7 +23,7 @@ protected function doProcessNode(ClassReflection $reflection, Node\Stmt\Class_ $
2123
$phpDoc = $reflection->getResolvedPhpDoc();
2224
// Plugins should always be annotated, but maybe this class is missing its
2325
// annotation since it swaps an existing one.
24-
if ($phpDoc === null || !$this->isAnnotated($phpDoc, '@ConfigEntityType')) {
26+
if ($phpDoc === null || !$this->isAnnotated($phpDoc)) {
2527
return [];
2628
}
2729
$hasMatch = preg_match('/config_export\s?=\s?{/', $phpDoc->getPhpDocString());
@@ -35,4 +37,16 @@ protected function doProcessNode(ClassReflection $reflection, Node\Stmt\Class_ $
3537
}
3638
return [];
3739
}
40+
41+
private function isAnnotated(ResolvedPhpDocBlock $phpDoc): bool
42+
{
43+
foreach ($phpDoc->getPhpDocNodes() as $docNode) {
44+
foreach ($docNode->children as $childNode) {
45+
if (($childNode instanceof PhpDocTagNode) && $childNode->name === '@ConfigEntityType') {
46+
return true;
47+
}
48+
}
49+
}
50+
return false;
51+
}
3852
}

src/Rules/Deprecations/DeprecatedAnnotationsRuleBase.php

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,9 @@
44

55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
7-
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
8-
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
97
use PHPStan\Reflection\ClassReflection;
108
use PHPStan\Reflection\ReflectionProvider;
119
use PHPStan\Rules\Rule;
12-
use ReflectionAttribute;
13-
use ReflectionClass;
14-
use ReflectionException;
1510

1611
/**
1712
* @implements Rule<Node\Stmt\Class_>
@@ -69,28 +64,4 @@ public function processNode(Node $node, Scope $scope): array
6964

7065
return $this->doProcessNode($reflection, $node, $scope);
7166
}
72-
73-
protected function isAnnotated(ResolvedPhpDocBlock $phpDoc, string $annotationId): bool
74-
{
75-
foreach ($phpDoc->getPhpDocNodes() as $docNode) {
76-
foreach ($docNode->children as $childNode) {
77-
if (($childNode instanceof PhpDocTagNode) && $childNode->name === $annotationId) {
78-
return true;
79-
}
80-
}
81-
}
82-
return false;
83-
}
84-
85-
protected function getPluginAttribute(ClassReflection $reflection, string $attributeName): ?ReflectionAttribute
86-
{
87-
try {
88-
$nativeReflection = new ReflectionClass($reflection->getName());
89-
$attribute = $nativeReflection->getAttributes($attributeName);
90-
} catch (ReflectionException) {
91-
return null;
92-
}
93-
94-
return $attribute[0] ?? null;
95-
}
9667
}

src/Rules/Deprecations/FieldTypeCategoryRule.php

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
use PhpParser\Node;
1010
use PHPStan\Analyser\Scope;
1111
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
12+
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
1213
use PHPStan\Reflection\ClassReflection;
1314
use ReflectionAttribute;
15+
use ReflectionClass;
16+
use ReflectionException;
1417
use function array_key_exists;
1518
use function preg_match;
1619

@@ -39,12 +42,12 @@ protected function doProcessNode(ClassReflection $reflection, Node\Stmt\Class_ $
3942

4043
$phpDoc = $reflection->getResolvedPhpDoc();
4144
if ($phpDoc instanceof ResolvedPhpDocBlock) {
42-
if ($this->isAnnotated($phpDoc, '@FieldType') && preg_match('/category\s?=\s?@Translation/', $phpDoc->getPhpDocString()) === 1) {
45+
if ($this->hasFieldTypeAnnotation($phpDoc) && preg_match('/category\s?=\s?@Translation/', $phpDoc->getPhpDocString()) === 1) {
4346
$errors[] = self::DEPRECATION_MESSAGE;
4447
}
4548
}
4649

47-
$fieldTypeAttributes = $this->getPluginAttribute($reflection, FieldType::class);
50+
$fieldTypeAttributes = $this->getFieldTypeAttributes($reflection);
4851
if ($fieldTypeAttributes instanceof ReflectionAttribute) {
4952
$arguments = $fieldTypeAttributes->getArguments();
5053
if (array_key_exists('category', $arguments) && $arguments['category'] instanceof TranslatableMarkup) {
@@ -54,4 +57,47 @@ protected function doProcessNode(ClassReflection $reflection, Node\Stmt\Class_ $
5457

5558
return $errors;
5659
}
60+
61+
/**
62+
* Checks whether a PHP doc block contains a field type annotation.
63+
*
64+
* @param \PHPStan\PhpDoc\ResolvedPhpDocBlock $phpDoc
65+
* The PHP doc block object.
66+
*
67+
* @return bool
68+
* True if it does, otherwise false.
69+
*/
70+
private function hasFieldTypeAnnotation(ResolvedPhpDocBlock $phpDoc): bool
71+
{
72+
foreach ($phpDoc->getPhpDocNodes() as $docNode) {
73+
foreach ($docNode->children as $childNode) {
74+
if (($childNode instanceof PhpDocTagNode) && $childNode->name === '@FieldType') {
75+
return true;
76+
}
77+
}
78+
}
79+
80+
return false;
81+
}
82+
83+
/**
84+
* Checks whether a given class has a field type attribute.
85+
*
86+
* @param \PHPStan\Reflection\ClassReflection $reflection
87+
* The class reflection object.
88+
*
89+
* @return ReflectionAttribute|null
90+
* The attribute, or null.
91+
*/
92+
private function getFieldTypeAttributes(ClassReflection $reflection): ?ReflectionAttribute
93+
{
94+
try {
95+
$nativeReflection = new ReflectionClass($reflection->getName());
96+
$attribute = $nativeReflection->getAttributes(FieldType::class);
97+
} catch (ReflectionException $e) {
98+
return null;
99+
}
100+
101+
return $attribute[0] ?? null;
102+
}
57103
}

0 commit comments

Comments
 (0)