Skip to content

Commit

Permalink
Fix #4264 - prevent crash when analysing file with duplicate classes
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Oct 7, 2020
1 parent 52ffe7d commit 8375890
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Psalm/Internal/PhpVisitor/ReflectorVisitor.php
Expand Up @@ -146,6 +146,11 @@ class ReflectorVisitor extends PhpParser\NodeVisitorAbstract implements PhpParse
*/
private $classlike_type_aliases = [];

/**
* @var array<int, bool>
*/
private $bad_classes = [];

public function __construct(
Codebase $codebase,
FileStorage $file_storage,
Expand Down Expand Up @@ -285,7 +290,8 @@ public function enterNode(PhpParser\Node $node): ?int
}

if ($this->registerClassLike($node) === false) {
return PhpParser\NodeTraverser::STOP_TRAVERSAL;
$this->bad_classes[\spl_object_id($node)] = true;
return PhpParser\NodeTraverser::DONT_TRAVERSE_CHILDREN;
}
} elseif (($node instanceof PhpParser\Node\Expr\New_
|| $node instanceof PhpParser\Node\Expr\Instanceof_
Expand Down Expand Up @@ -575,6 +581,10 @@ public function leaveNode(PhpParser\Node $node)
return null;
}

if (isset($this->bad_classes[\spl_object_id($node)])) {
return null;
}

if (!$this->fq_classlike_names) {
throw new \LogicException('$this->fq_classlike_names should not be empty');
}
Expand Down
22 changes: 22 additions & 0 deletions tests/IncludeTest.php
Expand Up @@ -599,6 +599,28 @@ function bar(): void {}
getcwd() . DIRECTORY_SEPARATOR . 'file2.php',
],
],
'noCrash' => [
'files' => [
getcwd() . DIRECTORY_SEPARATOR . 'classes.php' => '<?php
// one.php
if (true) {
class One {}
}
else {
class One {}
}
class Two {}',
getcwd() . DIRECTORY_SEPARATOR . 'user.php' => '<?php
include("classes.php");
new Two();',
],
'files_to_check' => [
getcwd() . DIRECTORY_SEPARATOR . 'user.php',
],
],
];
}

Expand Down

0 comments on commit 8375890

Please sign in to comment.