Skip to content

Commit

Permalink
Fix @template name clash from different scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 19, 2021
1 parent 3c43c54 commit 527269a
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Type/Generic/GenericObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,20 +187,32 @@ public function getClassReflection(): ?ClassReflection
public function getProperty(string $propertyName, ClassMemberAccessAnswerer $scope): PropertyReflection
{
$reflection = parent::getProperty($propertyName, $scope);
$ancestor = $this->getAncestorWithClassName($reflection->getDeclaringClass()->getName());
if ($ancestor === null) {
$classReflection = $reflection->getDeclaringClass();
} else {
$classReflection = $ancestor->getClassReflection();
}

return new ResolvedPropertyReflection(
$reflection,
$this->getClassReflection()->getActiveTemplateTypeMap()
$classReflection->getActiveTemplateTypeMap()
);
}

public function getMethod(string $methodName, ClassMemberAccessAnswerer $scope): MethodReflection
{
$reflection = parent::getMethod($methodName, $scope);
$ancestor = $this->getAncestorWithClassName($reflection->getDeclaringClass()->getName());
if ($ancestor === null) {
$classReflection = $reflection->getDeclaringClass();
} else {
$classReflection = $ancestor->getClassReflection();
}

return new ResolvedMethodReflection(
$reflection,
$this->getClassReflection()->getActiveTemplateTypeMap()
$classReflection->getActiveTemplateTypeMap()
);
}

Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10837,6 +10837,11 @@ public function dataBug4558(): array
return $this->gatherAssertTypes(__DIR__ . '/data/bug-4558.php');
}

public function dataBug4557(): array
{
return $this->gatherAssertTypes(__DIR__ . '/data/bug-4557.php');
}

/**
* @param string $file
* @return array<string, mixed[]>
Expand Down Expand Up @@ -11066,6 +11071,7 @@ private function gatherAssertTypes(string $file): array
* @dataProvider dataBug1801
* @dataProvider dataBug2927
* @dataProvider dataBug4558
* @dataProvider dataBug4557
* @param string $assertType
* @param string $file
* @param mixed ...$args
Expand Down
66 changes: 66 additions & 0 deletions tests/PHPStan/Analyser/data/bug-4557.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Bug4557;

use function PHPStan\Analyser\assertType;

class Lorem
{
}

class Ipsum extends Lorem
{
}

interface MockObject
{
}

class Foo
{

/**
* @template T
* @param class-string<T> $class
* @return T&MockObject
*/
public function createMock($class)
{
}

}

/**
* @template T of Lorem
*/
class Bar extends Foo
{

public function doBar(): void
{
$mock = $this->createMock(\stdClass::class);
assertType('Bug4557\\MockObject&stdClass', $mock);
}

/** @return T */
public function doBaz()
{

}

}

class Baz
{

/**
* @param Bar<Lorem> $barLorem
* @param Bar<Ipsum> $barIpsum
*/
public function doFoo(Bar $barLorem, Bar $barIpsum): void
{
assertType('Bug4557\\Lorem', $barLorem->doBaz());
assertType('Bug4557\\Ipsum', $barIpsum->doBaz());
}

}
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1818,4 +1818,12 @@ public function testBug3534(): void
$this->analyse([__DIR__ . '/data/bug-3534.php'], []);
}

public function testBug4557(): void
{
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->analyse([__DIR__ . '/../../Analyser/data/bug-4557.php'], []);
}

}

0 comments on commit 527269a

Please sign in to comment.