1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Fix #4264 - prevent crash when analysing file with duplicate classes

This commit is contained in:
Matt Brown 2020-10-01 15:07:25 -04:00
parent c23406fe36
commit 6ad5e1c013
2 changed files with 33 additions and 1 deletions

View File

@ -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,
@ -285,7 +290,8 @@ class ReflectorVisitor extends PhpParser\NodeVisitorAbstract implements PhpParse
}
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_
@ -575,6 +581,10 @@ class ReflectorVisitor extends PhpParser\NodeVisitorAbstract implements PhpParse
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');
}

View File

@ -599,6 +599,28 @@ class IncludeTest extends TestCase
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',
],
],
];
}