1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Check for indirect circular dependencies with stubbed parents.

This commit is contained in:
AndrolGenhald 2021-12-14 17:07:49 -06:00
parent 9a064c04cf
commit 3510f55e31
3 changed files with 24 additions and 14 deletions

View File

@ -20,8 +20,10 @@ use function array_filter;
use function array_intersect_key;
use function array_keys;
use function array_merge;
use function array_splice;
use function count;
use function in_array;
use function key;
use function reset;
use function strlen;
use function strpos;
@ -251,18 +253,26 @@ class Populator
if (isset($this->invalid_class_storages[$fq_classlike_name_lc])) {
foreach ($this->invalid_class_storages[$fq_classlike_name_lc] as $dependency) {
if (isset($dependency->dependent_classlikes[$fq_classlike_name_lc])) {
if ($dependency->location) {
IssueBuffer::maybeAdd(
new CircularReference(
'Circular reference discovered when loading ' . $dependency->name,
$dependency->location
)
);
}
// Dependencies may not be fully set yet, so we have to loop through dependencies of dependencies
$dependencies = [strtolower($dependency->name) => true];
do {
$current_dependency_name = key(array_splice($dependencies, 0, 1)); // Key shift
$current_dependency = $storage_provider->get($current_dependency_name);
$dependencies += $current_dependency->dependent_classlikes;
continue;
}
if (isset($current_dependency->dependent_classlikes[$fq_classlike_name_lc])) {
if ($dependency->location) {
IssueBuffer::maybeAdd(
new CircularReference(
'Circular reference discovered when loading ' . $dependency->name,
$dependency->location
)
);
}
continue 2;
}
} while (!empty($dependencies));
$dependency->populated = false;
unset($dependency->invalid_dependencies[$fq_classlike_name_lc]);

View File

@ -281,9 +281,7 @@ class StubTest extends TestCase
$this->addFile(
$file_path,
'<?php
class Foo extends Bar
{
}
class Foo extends Baz {}
'
);

View File

@ -1,3 +1,5 @@
<?php
class Bar extends Foo {}
class Baz extends Bar {}