Skip to content

Commit

Permalink
Making use of PhpDocNodeTraverser (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jul 1, 2021
1 parent 2bc0f4c commit 37d04ea
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 52 deletions.
34 changes: 17 additions & 17 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@
"symfony/http-kernel": "^5.3",
"symfony/process": "^5.3",
"symfony/yaml": "^5.3",
"symplify/astral": "^9.3.22",
"symplify/autowire-array-parameter": "^9.3.22",
"symplify/composer-json-manipulator": "^9.3.22",
"symplify/console-color-diff": "^9.3.22",
"symplify/package-builder": "^9.3.22",
"symplify/rule-doc-generator-contracts": "^9.3.22",
"symplify/simple-php-doc-parser": "^9.3.22",
"symplify/skipper": "^9.3.22",
"symplify/smart-file-system": "^9.3.22",
"symplify/symfony-php-config": "^9.3.22",
"symplify/astral": "^9.4.3",
"symplify/autowire-array-parameter": "^9.4.3",
"symplify/composer-json-manipulator": "^9.4.3",
"symplify/console-color-diff": "^9.4.3",
"symplify/package-builder": "^9.4.3",
"symplify/rule-doc-generator-contracts": "^9.4.3",
"symplify/simple-php-doc-parser": "^9.4.3",
"symplify/skipper": "^9.4.3",
"symplify/smart-file-system": "^9.4.3",
"symplify/symfony-php-config": "^9.4.3",
"tracy/tracy": "^2.8",
"webmozart/assert": "^1.10"
},
Expand All @@ -56,13 +56,13 @@
"phpunit/phpunit": "^9.5",
"rector/phpstan-rules": "^0.3.3",
"rector/rector-generator": "^0.1.7",
"symplify/coding-standard": "^9.3.22",
"symplify/easy-ci": "^9.3.22",
"symplify/easy-coding-standard": "^9.3.22",
"symplify/easy-testing": "^9.3.22",
"symplify/phpstan-extensions": "^9.3.22",
"symplify/phpstan-rules": "^9.3.22",
"symplify/rule-doc-generator": "^9.3.22",
"symplify/coding-standard": "^9.4.3",
"symplify/easy-ci": "^9.4.3",
"symplify/easy-coding-standard": "^9.4.1",
"symplify/easy-testing": "^9.4.3",
"symplify/phpstan-extensions": "^9.4.3",
"symplify/phpstan-rules": "^9.4.3",
"symplify/rule-doc-generator": "^9.4.3",
"timeweb/phpstan-enum": "^2.3"
},
"replace": {
Expand Down
34 changes: 4 additions & 30 deletions packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
use Rector\BetterPhpDocParser\Annotation\AnnotationNaming;
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\BetterPhpDocParser\PhpDoc\SpacelessPhpDocTagNode;
use Rector\BetterPhpDocParser\PhpDocNodeFinder\DoctrineAnnotationMatcher;
use Rector\BetterPhpDocParser\PhpDocNodeVisitor\ChangedPhpDocNodeVisitor;
use Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator;
use Rector\BetterPhpDocParser\ValueObject\PhpDoc\DoctrineAnnotation\CurlyListNode;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareUnionTypeNode;
use Rector\ChangesReporting\Collector\RectorChangeCollector;
use Rector\Core\Configuration\CurrentNodeProvider;
Expand Down Expand Up @@ -65,7 +65,8 @@ public function __construct(
private \PhpParser\Node $node,
private AnnotationNaming $annotationNaming,
private CurrentNodeProvider $currentNodeProvider,
private RectorChangeCollector $rectorChangeCollector
private RectorChangeCollector $rectorChangeCollector,
private DoctrineAnnotationMatcher $doctrineAnnotationMatcher
) {
$this->originalPhpDocNode = clone $phpDocNode;

Expand Down Expand Up @@ -541,15 +542,6 @@ private function resolveNameForPhpDocTagValueNode(PhpDocTagValueNode $phpDocTagV
throw new NotImplementedYetException($phpDocTagValueNode::class);
}

private function isFnmatch(string $currentValue, string $desiredValue): bool
{
if (! \str_contains($desiredValue, '*')) {
return false;
}

return fnmatch($desiredValue, $currentValue, FNM_NOESCAPE);
}

/**
* @return DoctrineAnnotationTagValueNode[]
*/
Expand All @@ -559,32 +551,14 @@ private function filterDoctrineTagValuesNodesINcludingNested(string $desiredClas

$doctrineTagValueNodes = $this->getDoctrineTagValueNodesNestedIncluded();
foreach ($doctrineTagValueNodes as $doctrineTagValueNode) {
if ($this->isMatchingDesiredClass($doctrineTagValueNode, $desiredClass)) {
if ($this->doctrineAnnotationMatcher->matches($doctrineTagValueNode, $desiredClass)) {
$desiredDoctrineTagValueNodes[] = $doctrineTagValueNode;
}
}

return $desiredDoctrineTagValueNodes;
}

private function isMatchingDesiredClass(
DoctrineAnnotationTagValueNode $doctrineAnnotationTagValueNode,
string $desiredClass
): bool {
if ($doctrineAnnotationTagValueNode->hasClassName($desiredClass)) {
return true;
}

$identifierTypeNode = $doctrineAnnotationTagValueNode->identifierTypeNode;
if ($this->isFnmatch($identifierTypeNode->name, $desiredClass)) {
return true;
}

// FQN check
$resolvedClass = $identifierTypeNode->getAttribute(PhpDocAttributeKey::RESOLVED_CLASS);
return is_string($resolvedClass) && $this->isFnmatch($resolvedClass, $desiredClass);
}

/**
* @return DoctrineAnnotationTagValueNode[]
*/
Expand Down
12 changes: 7 additions & 5 deletions packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use PHPStan\PhpDocParser\Lexer\Lexer;
use Rector\BetterPhpDocParser\Annotation\AnnotationNaming;
use Rector\BetterPhpDocParser\Contract\PhpDocNodeFactoryInterface;
use Rector\BetterPhpDocParser\PhpDocNodeFinder\DoctrineAnnotationMatcher;
use Rector\BetterPhpDocParser\PhpDocNodeMapper;
use Rector\BetterPhpDocParser\PhpDocParser\BetterPhpDocParser;
use Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator;
Expand All @@ -34,7 +34,8 @@ public function __construct(
private BetterPhpDocParser $betterPhpDocParser,
private StaticTypeMapper $staticTypeMapper,
private AnnotationNaming $annotationNaming,
private RectorChangeCollector $rectorChangeCollector
private RectorChangeCollector $rectorChangeCollector,
private DoctrineAnnotationMatcher $doctrineAnnotationMatcher
) {
}

Expand All @@ -61,7 +62,7 @@ public function createFromNode(Node $node): ?PhpDocInfo
return $this->phpDocInfosByObjectHash[$objectHash];
}

/** needed for @see PhpDocNodeFactoryInterface */
/** @see \Rector\BetterPhpDocParser\PhpDocParser\DoctrineAnnotationDecorator::decorate() */
$this->currentNodeProvider->setNode($node);

$docComment = $node->getDocComment();
Expand Down Expand Up @@ -91,7 +92,7 @@ public function createFromNode(Node $node): ?PhpDocInfo

public function createEmpty(Node $node): PhpDocInfo
{
/** needed for @see PhpDocNodeFactoryInterface */
/** @see \Rector\BetterPhpDocParser\PhpDocParser\DoctrineAnnotationDecorator::decorate() */
$this->currentNodeProvider->setNode($node);

$phpDocNode = new PhpDocNode([]);
Expand Down Expand Up @@ -136,7 +137,8 @@ private function createFromPhpDocNode(
$node,
$this->annotationNaming,
$this->currentNodeProvider,
$this->rectorChangeCollector
$this->rectorChangeCollector,
$this->doctrineAnnotationMatcher
);

$node->setAttribute(AttributeKey::PHP_DOC_INFO, $phpDocInfo);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Rector\BetterPhpDocParser\PhpDocNodeFinder;

use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;

final class DoctrineAnnotationMatcher
{
public function matches(
DoctrineAnnotationTagValueNode $doctrineAnnotationTagValueNode,
string $desiredClass
): bool {
if ($doctrineAnnotationTagValueNode->hasClassName($desiredClass)) {
return true;
}

// fnmatching class?
if (! str_contains($desiredClass, '*')) {
return false;
}

$identifierTypeNode = $doctrineAnnotationTagValueNode->identifierTypeNode;
if ($this->isFnmatch($identifierTypeNode->name, $desiredClass)) {
return true;
}

// FQN check
$resolvedClass = $identifierTypeNode->getAttribute(PhpDocAttributeKey::RESOLVED_CLASS);
return is_string($resolvedClass) && $this->isFnmatch($resolvedClass, $desiredClass);
}

private function isFnmatch(string $currentValue, string $desiredValue): bool
{
return fnmatch($desiredValue, $currentValue, FNM_NOESCAPE);
}
}

0 comments on commit 37d04ea

Please sign in to comment.