From 152d1512f16ee9fe91ba4561a43543994873d089 Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Mon, 12 Jun 2017 22:51:39 -0400 Subject: [PATCH] Fix issue capturing method mutations in traits --- src/Psalm/Checker/ProjectChecker.php | 10 ++++---- tests/MethodMutationTest.php | 38 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/Psalm/Checker/ProjectChecker.php b/src/Psalm/Checker/ProjectChecker.php index a8310dad8..351592408 100644 --- a/src/Psalm/Checker/ProjectChecker.php +++ b/src/Psalm/Checker/ProjectChecker.php @@ -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); } /** diff --git a/tests/MethodMutationTest.php b/tests/MethodMutationTest.php index 5dbb19394..8aa2f0191 100644 --- a/tests/MethodMutationTest.php +++ b/tests/MethodMutationTest.php @@ -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', + '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']); + } }