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

Prevent infinite loops when analysing private functions that call each other

This commit is contained in:
Matt Brown 2018-01-24 13:11:23 -05:00
parent 0018dee5e3
commit 72a4f148ff
3 changed files with 43 additions and 2 deletions

View File

@ -1228,9 +1228,19 @@ class CallChecker
) {
$method_id = $fq_class_name . '::' . strtolower($method_name);
$declaring_method_id = MethodChecker::getDeclaringMethodId($project_checker, $method_id);
$declaring_method_id = (string) MethodChecker::getDeclaringMethodId($project_checker, $method_id);
$method_storage = $codebase->getMethodStorage((string)$declaring_method_id);
if (isset($context->initialized_methods[$declaring_method_id])) {
return;
}
if ($context->initialized_methods === null) {
$context->initialized_methods = [];
}
$context->initialized_methods[$declaring_method_id] = true;
$method_storage = $codebase->getMethodStorage($declaring_method_id);
$class_checker = $source->getSource();

View File

@ -112,6 +112,13 @@ class Context
*/
public $collect_initializations = false;
/**
* Stored to prevent re-analysing methods when checking for initialised properties
*
* @var array<string, bool>|null
*/
public $initialized_methods = null;
/**
* @var array<string, Type\Union>
*/

View File

@ -1143,6 +1143,30 @@ class PropertyTypeTest extends TestCase
$a->foo = new B;',
'error_message' => 'ImplicitToStringCast',
],
'noInfiniteLoop' => [
'<?php
class A {
/** @var string */
public $foo;
public function __construct() {
$this->doThing();
}
private function doThing(): void {
if (rand(0, 1)) {
$this->doOtherThing();
}
}
private function doOtherThing(): void {
if (rand(0, 1)) {
$this->doThing();
}
}
}',
'error_message' => 'PropertyNotSetInConstructor',
],
];
}
}