Skip to content

Commit 9a6fdb0

Browse files
authored
Convert Doctrine annotations from phpdoc-parser nodes, remove StaticDoctrineAnnotationParser (#8881)
1 parent 8323e93 commit 9a6fdb0

21 files changed

Lines changed: 240 additions & 1288 deletions

File tree

‎phpstan.neon‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,6 @@ parameters:
300300
message: '#Function "property_exists\(\)" cannot be used/left in the code#'
301301
paths:
302302
- src/NodeNameResolver/NodeNameResolver.php
303-
- src/BetterPhpDocParser/NodeDecorator/DoctrineAnnotationDecorator.php
304303

305304
-
306305
identifier: symplify.forbiddenFuncCall
@@ -361,8 +360,3 @@ parameters:
361360
paths:
362361
- tests/Issues/
363362
- tests/PhpParser/NodeTraverser/StopTraverseOnTypeChange/Class_
364-
365-
# known types
366-
-
367-
identifier: varTag.nativeType
368-
path: src/BetterPhpDocParser/PhpDocParser/StaticDoctrineAnnotationParser.php

‎rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Fixture/keep_single_quoted.php.inc‎

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

‎rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Fixture/keep_single_quoted2.php.inc‎

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

‎rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Fixture/keep_single_quoted_key_value.php.inc‎

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture;
4+
5+
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\GenericAnnotation;
6+
7+
/**
8+
* @GenericAnnotation("sample", itemOperations='yes')
9+
*/
10+
final class SkipSingleQuotedKeyValue
11+
{
12+
}
13+
14+
?>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture;
4+
5+
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\GenericSingleImplicitAnnotation;
6+
7+
/**
8+
* @GenericSingleImplicitAnnotation('/')
9+
*/
10+
final class SkipSingleQuotedValue
11+
{
12+
}
13+
14+
?>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture;
4+
5+
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\GenericSingleImplicitAnnotation;
6+
7+
/**
8+
* @GenericSingleImplicitAnnotation('"foo"')
9+
*/
10+
final class SkipSingleQuotedWithDoubleQuotes
11+
{
12+
}
13+
14+
?>

‎rules/Php80/NodeAnalyzer/PromotedPropertyCandidateResolver.php‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
use PhpParser\Node\Stmt\ClassMethod;
1515
use PhpParser\Node\Stmt\Expression;
1616
use PhpParser\Node\Stmt\Property;
17+
use PHPStan\PhpDocParser\Ast\PhpDoc\InvalidTagValueNode;
1718
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
1819
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
20+
use Rector\BetterPhpDocParser\PhpDocParser\ClassAnnotationMatcher;
1921
use Rector\Enum\ClassName;
2022
use Rector\NodeAnalyzer\PropertyFetchAnalyzer;
2123
use Rector\NodeNameResolver\NodeNameResolver;
@@ -31,7 +33,8 @@ public function __construct(
3133
private NodeComparator $nodeComparator,
3234
private PropertyFetchAnalyzer $propertyFetchAnalyzer,
3335
private PhpDocInfoFactory $phpDocInfoFactory,
34-
private PhpAttributeAnalyzer $phpAttributeAnalyzer
36+
private PhpAttributeAnalyzer $phpAttributeAnalyzer,
37+
private ClassAnnotationMatcher $classAnnotationMatcher
3538
) {
3639
}
3740

@@ -79,13 +82,33 @@ public function resolveFromClass(
7982
private function hasModelTypeCheck(Class_|Property $node, string $modelType): bool
8083
{
8184
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
82-
if ($phpDocInfo instanceof PhpDocInfo && $phpDocInfo->hasByAnnotationClass($modelType)) {
85+
if ($phpDocInfo instanceof PhpDocInfo && ($phpDocInfo->hasByAnnotationClass(
86+
$modelType
87+
) || $this->hasInvalidAnnotation($phpDocInfo, $node, $modelType))) {
8388
return true;
8489
}
8590

8691
return $this->phpAttributeAnalyzer->hasPhpAttribute($node, $modelType);
8792
}
8893

94+
/**
95+
* An annotation with arguments that are not valid, e.g. @JMS\Type('string)
96+
*/
97+
private function hasInvalidAnnotation(PhpDocInfo $phpDocInfo, Class_|Property $node, string $modelType): bool
98+
{
99+
foreach ($phpDocInfo->getPhpDocNode()->getTags() as $phpDocTagNode) {
100+
if (! $phpDocTagNode->value instanceof InvalidTagValueNode) {
101+
continue;
102+
}
103+
104+
if ($this->classAnnotationMatcher->resolveTagFullyQualifiedName($phpDocTagNode->name, $node) === $modelType) {
105+
return true;
106+
}
107+
}
108+
109+
return false;
110+
}
111+
89112
private function matchPropertyPromotionCandidate(
90113
Property $property,
91114
ClassMethod $constructClassMethod,

0 commit comments

Comments
 (0)