1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Fix issue capturing method mutations in traits

This commit is contained in:
Matthew Brown 2017-06-12 22:51:39 -04:00
parent 72e1221a2f
commit 152d1512f1
2 changed files with 43 additions and 5 deletions

View File

@ -908,11 +908,11 @@ class ProjectChecker
$file_checker = $this->getVisitedFileCheckerForClassLike($fq_class_name);
$declaring_method_id = (string)MethodChecker::getDeclaringMethodId($original_method_id);
list($declaring_fq_class_name) = explode('::', $declaring_method_id);
$appearing_method_id = (string)MethodChecker::getAppearingMethodId($original_method_id);
list($appearing_fq_class_name) = explode('::', $appearing_method_id);
if (strtolower($declaring_fq_class_name) !== strtolower($fq_class_name)) {
$file_checker = $this->getVisitedFileCheckerForClassLike($declaring_fq_class_name);
if (strtolower($appearing_fq_class_name) !== strtolower($fq_class_name)) {
$file_checker = $this->getVisitedFileCheckerForClassLike($appearing_fq_class_name);
}
$file_checker->analyze(false, true);
@ -922,7 +922,7 @@ class ProjectChecker
$this_context->vars_in_scope['$this'] = Type::parseString($fq_class_name);
}
$file_checker->getMethodMutations($declaring_method_id, $this_context);
$file_checker->getMethodMutations($appearing_method_id, $this_context);
}
/**

View File

@ -136,4 +136,42 @@ class MethodMutationTest extends TestCase
$this->assertSame('Foo', (string)$method_context->vars_in_scope['$this->foo']);
}
/**
* @return void
*/
public function testTraitMethod()
{
$this->project_checker->registerFile(
'somefile.php',
'<?php
class Foo { }
trait T {
private function setFoo() : void {
$this->foo = new Foo();
}
}
class FooController {
use T;
/** @var Foo|null */
public $foo;
public function __construct() {
$this->setFoo();
}
}'
);
$file_checker = new FileChecker('somefile.php', $this->project_checker);
$context = new Context();
$file_checker->visit($context);
$file_checker->analyze(false, true);
$method_context = new Context();
$this->project_checker->getMethodMutations('FooController::__construct', $method_context);
$this->assertSame('Foo', (string)$method_context->vars_in_scope['$this->foo']);
}
}