Skip to content

Commit

Permalink
Fix scope namespace when entering trait
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 7, 2022
1 parent 838e98e commit 7430930
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
[],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,9 @@ public function testBug3576(): void
]);
}

public function testBug7952(): void
{
$this->analyse([__DIR__ . '/data/bug-7952.php'], []);
}

}
29 changes: 29 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-7952.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Bug7952A {

function clean(): void
{
echo 'cleaned';
}

trait FooTrait
{
public function doClean(): void
{
clean();
}
}

}

namespace Bug7952A\Sub {

use Bug7952A\FooTrait;

class FooClass
{
use FooTrait;
}

}

0 comments on commit 7430930

Please sign in to comment.