Skip to content

Commit f7e5c88

Browse files
committed
move helpers to base class
1 parent 671929a commit f7e5c88

File tree

4 files changed

+33
-64
lines changed

4 files changed

+33
-64
lines changed

extension.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ parameters:
1616
scanFiles:
1717
- stubs/Twig/functions.stub
1818
drupal:
19-
drupal_root: ~
19+
drupal_root:
2020
bleedingEdge:
2121
checkDeprecatedHooksInApiFiles: false
2222
checkCoreDeprecatedHooksInApiFiles: false

src/Rules/Deprecations/ConfigEntityConfigExportRule.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
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\ShouldNotHappenException;
119
use function preg_match;
@@ -23,7 +21,7 @@ protected function doProcessNode(ClassReflection $reflection, Node\Stmt\Class_ $
2321
$phpDoc = $reflection->getResolvedPhpDoc();
2422
// Plugins should always be annotated, but maybe this class is missing its
2523
// annotation since it swaps an existing one.
26-
if ($phpDoc === null || !$this->isAnnotated($phpDoc)) {
24+
if ($phpDoc === null || !$this->isAnnotated($phpDoc, '@ConfigEntityType')) {
2725
return [];
2826
}
2927
$hasMatch = preg_match('/config_export\s?=\s?{/', $phpDoc->getPhpDocString());
@@ -37,16 +35,4 @@ protected function doProcessNode(ClassReflection $reflection, Node\Stmt\Class_ $
3735
}
3836
return [];
3937
}
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-
}
5238
}

src/Rules/Deprecations/DeprecatedAnnotationsRuleBase.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
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\Reflection\ReflectionProvider;
911
use PHPStan\Rules\Rule;
12+
use ReflectionAttribute;
13+
use ReflectionClass;
14+
use ReflectionException;
1015

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

6570
return $this->doProcessNode($reflection, $node, $scope);
6671
}
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+
}
6796
}

src/Rules/Deprecations/FieldTypeCategoryRule.php

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

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

4340
$phpDoc = $reflection->getResolvedPhpDoc();
4441
if ($phpDoc instanceof ResolvedPhpDocBlock) {
45-
if ($this->hasFieldTypeAnnotation($phpDoc) && preg_match('/category\s?=\s?@Translation/', $phpDoc->getPhpDocString()) === 1) {
42+
if ($this->isAnnotated($phpDoc, '@FieldType') && preg_match('/category\s?=\s?@Translation/', $phpDoc->getPhpDocString()) === 1) {
4643
$errors[] = self::DEPRECATION_MESSAGE;
4744
}
4845
}
4946

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

5855
return $errors;
5956
}
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-
}
10357
}

0 commit comments

Comments
 (0)