Skip to content

Commit 0d65be6

Browse files
committed
Fix FileVisitor issues with wrong class detection
It seems the php parser is using phpdocs and deciding that something like /** * @return array<int, string|int> */ is a class with FQCN `My\Namespace\array<int, string|int>` which then ofc fails to create a FullyQualifiedClassName.
1 parent 89bfe5f commit 0d65be6

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

src/Analyzer/FileVisitor.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ private function handleTypedProperty(Node $node): void
228228
return;
229229
}
230230

231+
if (FullyQualifiedClassName::isNotAValidFqcn($type->toString())) {
232+
return;
233+
}
234+
231235
try {
232236
$this->classDescriptionBuilder
233237
->addDependency(new ClassDependency($type->toString(), $node->getLine()));
@@ -288,10 +292,17 @@ private function handleReturnTypeDependency(Node $node): void
288292
{
289293
if ($node instanceof Node\Stmt\ClassMethod) {
290294
$returnType = $node->returnType;
291-
if ($returnType instanceof Node\Name\FullyQualified) {
292-
$this->classDescriptionBuilder
293-
->addDependency(new ClassDependency($returnType->toString(), $returnType->getLine()));
295+
296+
if (!$returnType instanceof Node\Name\FullyQualified) {
297+
return;
294298
}
299+
300+
if (FullyQualifiedClassName::isNotAValidFqcn($returnType->toString())) {
301+
return;
302+
}
303+
304+
$this->classDescriptionBuilder
305+
->addDependency(new ClassDependency($returnType->toString(), $returnType->getLine()));
295306
}
296307
}
297308

@@ -300,10 +311,16 @@ private function handleAttributeNode(Node $node): void
300311
if ($node instanceof Node\Attribute) {
301312
$nodeName = $node->name;
302313

303-
if ($nodeName instanceof Node\Name\FullyQualified) {
304-
$this->classDescriptionBuilder
305-
->addAttribute($node->name->toString(), $node->getLine());
314+
if (!$nodeName instanceof Node\Name\FullyQualified) {
315+
return;
316+
}
317+
318+
if (FullyQualifiedClassName::isNotAValidFqcn($nodeName->toString())) {
319+
return;
306320
}
321+
322+
$this->classDescriptionBuilder
323+
->addAttribute($nodeName->toString(), $node->getLine());
307324
}
308325
}
309326

@@ -337,6 +354,10 @@ private function addParamDependency(Node\Param $node): void
337354
return;
338355
}
339356

357+
if (FullyQualifiedClassName::isNotAValidFqcn($type->toString())) {
358+
return;
359+
}
360+
340361
$this->classDescriptionBuilder
341362
->addDependency(new ClassDependency($type->toString(), $node->getLine()));
342363
}

src/Analyzer/FullyQualifiedClassName.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function toString(): string
2727

2828
public function classMatches(string $pattern): bool
2929
{
30-
if ($this->isNotAValidPattern($pattern)) {
30+
if (self::isNotAValidFqcn($pattern)) {
3131
throw new InvalidPatternException("'$pattern' is not a valid class or namespace pattern. Regex are not allowed, only * and ? wildcard.");
3232
}
3333

@@ -36,7 +36,7 @@ public function classMatches(string $pattern): bool
3636

3737
public function matches(string $pattern): bool
3838
{
39-
if ($this->isNotAValidPattern($pattern)) {
39+
if (self::isNotAValidFqcn($pattern)) {
4040
throw new InvalidPatternException("'$pattern' is not a valid class or namespace pattern. Regex are not allowed, only * and ? wildcard.");
4141
}
4242

@@ -69,12 +69,12 @@ public static function fromString(string $fqcn): self
6969
return new self(new PatternString($fqcn), new PatternString($namespace), new PatternString($className));
7070
}
7171

72-
public function isNotAValidPattern(string $pattern): bool
72+
public static function isNotAValidFqcn(string $fqcn): bool
7373
{
7474
$validClassNameCharacters = '[a-zA-Z0-9_\x80-\xff]';
7575
$or = '|';
7676
$backslash = '\\\\';
7777

78-
return 0 === preg_match('/^('.$validClassNameCharacters.$or.$backslash.$or.'\*'.$or.'\?)*$/', $pattern);
78+
return 0 === preg_match('/^('.$validClassNameCharacters.$or.$backslash.$or.'\*'.$or.'\?)*$/', $fqcn);
7979
}
8080
}

0 commit comments

Comments
 (0)