Skip to content

Commit 9a5714c

Browse files
Parse custom tags (#317)
* parsing custom docblocks
1 parent 5acb686 commit 9a5714c

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/Analyzer/NameResolver.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,18 @@ public function enterNode(Node $node)
147147
$node->type = $type;
148148
break;
149149
}
150+
151+
if (null === $node->type) {
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;
157+
158+
break;
159+
}
160+
}
161+
}
150162
} elseif ($node instanceof Stmt\Const_) {
151163
foreach ($node->consts as $const) {
152164
$this->addNamespacedName($const);

tests/Unit/Analyzer/FileVisitorTest.php

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

614614
$this->assertCount(1, $violations);
615615
}
616+
617+
public function test_it_parse_custom_tags_in_docblocks(): void
618+
{
619+
$code = <<< 'EOF'
620+
<?php
621+
namespace MyProject\AppBundle\Application;
622+
use Symfony\Component\Validator\Constraints as Assert;
623+
class ApplicationLevelDto
624+
{
625+
/**
626+
* @Assert\NotBlank
627+
*/
628+
public $foo;
629+
630+
}
631+
EOF;
632+
633+
/** @var FileParser $fp */
634+
$fp = FileParserFactory::createFileParser(TargetPhpVersion::create('8.1'));
635+
$fp->parse($code, 'relativePathName');
636+
637+
$cd = $fp->getClassDescriptions();
638+
639+
$violations = new Violations();
640+
641+
$notHaveDependencyOutsideNamespace = new DependsOnlyOnTheseNamespaces('MyProject\AppBundle\Application');
642+
$notHaveDependencyOutsideNamespace->evaluate($cd[0], $violations, 'we want to add this rule for our software');
643+
644+
$this->assertCount(1, $violations);
645+
}
616646
}

0 commit comments

Comments
 (0)