diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index ec32dd512c..0a89803f83 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -116,8 +116,11 @@ use function array_keys; use function array_map; use function array_pop; +use function array_slice; use function count; +use function explode; use function get_class; +use function implode; use function in_array; use function is_string; use function ltrim; @@ -2486,12 +2489,18 @@ public function enterClass(ClassReflection $classReflection): self public function enterTrait(ClassReflection $traitReflection): self { + $namespace = null; + $traitName = $traitReflection->getName(); + $traitNameParts = explode('\\', $traitName); + if (count($traitNameParts) > 1) { + $namespace = implode('\\', array_slice($traitNameParts, 0, -1)); + } return $this->scopeFactory->create( $this->context->enterTrait($traitReflection), $this->isDeclareStrictTypes(), $this->constantTypes, $this->getFunction(), - $this->getNamespace(), + $namespace, $this->getVariableTypes(), $this->moreSpecificTypes, [], diff --git a/tests/PHPStan/Rules/Functions/CallToNonExistentFunctionRuleTest.php b/tests/PHPStan/Rules/Functions/CallToNonExistentFunctionRuleTest.php index c09200dad1..29b5d1662b 100644 --- a/tests/PHPStan/Rules/Functions/CallToNonExistentFunctionRuleTest.php +++ b/tests/PHPStan/Rules/Functions/CallToNonExistentFunctionRuleTest.php @@ -157,4 +157,9 @@ public function testBug3576(): void ]); } + public function testBug7952(): void + { + $this->analyse([__DIR__ . '/data/bug-7952.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Functions/data/bug-7952.php b/tests/PHPStan/Rules/Functions/data/bug-7952.php new file mode 100644 index 0000000000..69dced87b1 --- /dev/null +++ b/tests/PHPStan/Rules/Functions/data/bug-7952.php @@ -0,0 +1,29 @@ +