-
-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Making use of PhpDocNodeTraverser (#348)
- Loading branch information
1 parent
2bc0f4c
commit 37d04ea
Showing
4 changed files
with
67 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/BetterPhpDocParser/PhpDocNodeFinder/DoctrineAnnotationMatcher.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |