Skip to content

Commit c55bc8b

Browse files
Add a configuration to skip DocBlock Custom Tag detection (#336)
1 parent 1f8418e commit c55bc8b

File tree

5 files changed

+62
-10
lines changed

5 files changed

+62
-10
lines changed

src/Analyzer/FileParserFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
class FileParserFactory
1111
{
12-
public static function createFileParser(TargetPhpVersion $targetPhpVersion): FileParser
12+
public static function createFileParser(TargetPhpVersion $targetPhpVersion, $parseDocBlockCustomTags = true): FileParser
1313
{
1414
return new FileParser(
1515
new NodeTraverser(),
1616
new FileVisitor(ClassDescriptionBuilder::create()),
17-
new NameResolver(),
17+
new NameResolver(null, ['parseDocBlockCustomTags' => $parseDocBlockCustomTags]),
1818
$targetPhpVersion
1919
);
2020
}

src/Analyzer/NameResolver.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class NameResolver extends NodeVisitorAbstract
3131
/** @var bool Whether to replace resolved nodes in place, or to add resolvedNode attributes */
3232
protected $replaceNodes;
3333

34+
/** @var bool Whether to parse DocBlock Custom Tags */
35+
protected $parseDocBlockCustomTags;
36+
3437
/**
3538
* Constructs a name resolution visitor.
3639
*
@@ -40,6 +43,7 @@ class NameResolver extends NodeVisitorAbstract
4043
* * replaceNodes (default true): Resolved names are replaced in-place. Otherwise, a
4144
* resolvedName attribute is added. (Names that cannot be statically resolved receive a
4245
* namespacedName attribute, as usual.)
46+
* * parseDocBlockCustomTags (default true): Whether to parse DocBlock Custom Tags.
4347
*
4448
* @param ErrorHandler|null $errorHandler Error handler
4549
* @param array $options Options
@@ -49,6 +53,7 @@ public function __construct(ErrorHandler $errorHandler = null, array $options =
4953
$this->nameContext = new NameContext($errorHandler ?? new ErrorHandler\Throwing());
5054
$this->preserveOriginalNames = $options['preserveOriginalNames'] ?? false;
5155
$this->replaceNodes = $options['replaceNodes'] ?? true;
56+
$this->parseDocBlockCustomTags = $options['parseDocBlockCustomTags'] ?? true;
5257
}
5358

5459
/**
@@ -148,14 +153,16 @@ public function enterNode(Node $node)
148153
break;
149154
}
150155

151-
if (!($node->type instanceof FullyQualified)) {
152-
foreach ($phpDocNode->getTags() as $tagValue) {
153-
if ('@' === $tagValue->name[0] && false === strpos($tagValue->name, '@var')) {
154-
$customTag = str_replace('@', '', $tagValue->name);
155-
$type = $this->resolveName(new Node\Name($customTag), Use_::TYPE_NORMAL);
156-
$node->type = $type;
156+
if ($this->parseDocBlockCustomTags) {
157+
if (!($node->type instanceof FullyQualified)) {
158+
foreach ($phpDocNode->getTags() as $tagValue) {
159+
if ('@' === $tagValue->name[0] && false === strpos($tagValue->name, '@var')) {
160+
$customTag = str_replace('@', '', $tagValue->name);
161+
$type = $this->resolveName(new Node\Name($customTag), Use_::TYPE_NORMAL);
162+
$node->type = $type;
157163

158-
break;
164+
break;
165+
}
159166
}
160167
}
161168
}

src/CLI/Config.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ class Config
1313
private $classSetRules;
1414
/** @var bool */
1515
private $runOnlyARule;
16+
/** @var bool */
17+
private $parseDocBlockCustomTags;
1618

1719
public function __construct()
1820
{
1921
$this->classSetRules = [];
2022
$this->runOnlyARule = false;
23+
$this->parseDocBlockCustomTags = true;
2124
}
2225

2326
public function add(ClassSet $classSet, ArchRule ...$rules): self
@@ -46,4 +49,16 @@ public function getClassSetRules(): array
4649
{
4750
return $this->classSetRules;
4851
}
52+
53+
public function setParseDocBlockCustomTags($parseDocBlockCustomTags): self
54+
{
55+
$this->parseDocBlockCustomTags = $parseDocBlockCustomTags;
56+
57+
return $this;
58+
}
59+
60+
public function getParseDocBlockCustomTags(): bool
61+
{
62+
return $this->parseDocBlockCustomTags;
63+
}
4964
}

src/CLI/Runner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function __construct(bool $stopOnFailure = false)
3131
public function run(Config $config, Progress $progress, TargetPhpVersion $targetPhpVersion): void
3232
{
3333
/** @var FileParser $fileParser */
34-
$fileParser = FileParserFactory::createFileParser($targetPhpVersion);
34+
$fileParser = FileParserFactory::createFileParser($targetPhpVersion, $config->getParseDocBlockCustomTags());
3535

3636
/** @var ClassSetRules $classSetRule */
3737
foreach ($config->getClassSetRules() as $classSetRule) {

tests/Unit/Analyzer/FileVisitorTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,4 +704,34 @@ class ApplicationLevelDto
704704

705705
$this->assertCount(1, $violations);
706706
}
707+
708+
public function test_it_skip_custom_tags_in_docblocks_the_option_parse_doc_block_custom_tags_is_false(): void
709+
{
710+
$code = <<< 'EOF'
711+
<?php
712+
namespace MyProject\AppBundle\Application;
713+
use Symfony\Component\Validator\Constraints as Assert;
714+
class ApplicationLevelDto
715+
{
716+
/**
717+
* @Assert\NotBlank
718+
*/
719+
public $foo;
720+
721+
}
722+
EOF;
723+
724+
/** @var FileParser $fp */
725+
$fp = FileParserFactory::createFileParser(TargetPhpVersion::create('8.1'), false);
726+
$fp->parse($code, 'relativePathName');
727+
728+
$cd = $fp->getClassDescriptions();
729+
730+
$violations = new Violations();
731+
732+
$notHaveDependencyOutsideNamespace = new DependsOnlyOnTheseNamespaces('MyProject\AppBundle\Application');
733+
$notHaveDependencyOutsideNamespace->evaluate($cd[0], $violations, 'we want to add this rule for our software');
734+
735+
$this->assertCount(0, $violations);
736+
}
707737
}

0 commit comments

Comments
 (0)