Skip to content

Commit 9376332

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 c09490a commit 9376332

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/Analyzer/FileVisitor.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@ private function handleTypedProperty(Node $node): void
247247
return;
248248
}
249249

250+
if (FullyQualifiedClassName::isNotAValidFqcn($type->toString())) {
251+
return;
252+
}
253+
250254
$this->classDescriptionBuilder
251255
->addDependency(new ClassDependency($type->toString(), $node->getLine()));
252256
}
@@ -314,6 +318,10 @@ private function handleReturnTypeDependency(Node $node): void
314318
return;
315319
}
316320

321+
if (FullyQualifiedClassName::isNotAValidFqcn($returnType->toString())) {
322+
return;
323+
}
324+
317325
$this->classDescriptionBuilder
318326
->addDependency(new ClassDependency($returnType->toString(), $returnType->getLine()));
319327
}
@@ -330,6 +338,10 @@ private function handleAttributeNode(Node $node): void
330338
return;
331339
}
332340

341+
if (FullyQualifiedClassName::isNotAValidFqcn($nodeName->toString())) {
342+
return;
343+
}
344+
333345
$this->classDescriptionBuilder
334346
->addAttribute($node->name->toString(), $node->getLine());
335347
}
@@ -346,6 +358,10 @@ private function addParamDependency(Node\Param $node): void
346358
return;
347359
}
348360

361+
if (FullyQualifiedClassName::isNotAValidFqcn($type->toString())) {
362+
return;
363+
}
364+
349365
$this->classDescriptionBuilder
350366
->addDependency(new ClassDependency($type->toString(), $node->getLine()));
351367
}

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)