Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/PhpDoc/PhpDocInheritanceResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPStan\Type\FileTypeMapper;
use function array_key_exists;
use function count;
use function explode;
use function is_bool;
use function strtolower;

Expand Down Expand Up @@ -81,10 +82,12 @@ public function resolvePhpDocForMethod(
array $currentPositionalParameterNames,
): ?ResolvedPhpDocBlock
{
$methodNameForLookup = $this->resolveOriginalMethodName($declaringClass, $methodName);

$parent = $declaringClass->getParentClass();
if ($parent !== null) {
if ($parent->hasNativeMethod($methodName)) {
$parentMethod = $parent->getNativeMethod($methodName);
if ($parent->hasNativeMethod($methodNameForLookup)) {
$parentMethod = $parent->getNativeMethod($methodNameForLookup);
if (!$parentMethod->isPrivate() && $parentMethod->getResolvedPhpDoc() !== null) {
if ($parentMethod->getName() !== '__construct' || !$parentMethod->getDeclaringClass()->isBuiltin()) {
return $this->resolveMethodPhpDocFromParentClass($parentMethod, $parentMethod->getResolvedPhpDoc(), $declaringClass, $parent, $currentResolvedPhpDoc, $currentPositionalParameterNames);
Expand All @@ -94,11 +97,11 @@ public function resolvePhpDocForMethod(
}

foreach ($declaringClass->getImmediateInterfaces() as $interface) {
if (!$interface->hasNativeMethod($methodName)) {
if (!$interface->hasNativeMethod($methodNameForLookup)) {
continue;
}

$interfaceMethod = $interface->getNativeMethod($methodName);
$interfaceMethod = $interface->getNativeMethod($methodNameForLookup);
if ($interfaceMethod->isPrivate()) {
continue;
}
Expand All @@ -110,11 +113,11 @@ public function resolvePhpDocForMethod(
}

foreach ($declaringClass->getTraits() as $trait) {
if (!$trait->hasNativeMethod($methodName)) {
if (!$trait->hasNativeMethod($methodNameForLookup)) {
continue;
}

$traitMethod = $trait->getNativeMethod($methodName);
$traitMethod = $trait->getNativeMethod($methodNameForLookup);
if ($traitMethod->getDocComment() === null) {
continue;
}
Expand All @@ -135,7 +138,7 @@ public function resolvePhpDocForMethod(
$declaringClass->getFileName(),
$declaringClass->getName(),
$trait->getName(),
$methodName,
$methodNameForLookup,
$traitMethod->getDocComment(),
);

Expand Down Expand Up @@ -210,6 +213,19 @@ private static function remapParameterNames(
return $parameterNameMapping;
}

private function resolveOriginalMethodName(ClassReflection $declaringClass, string $methodName): string
{
$traitAliases = $declaringClass->getNativeReflection()->getTraitAliases();
if (array_key_exists($methodName, $traitAliases)) {
$parts = explode('::', $traitAliases[$methodName]);
if (count($parts) === 2) {
return $parts[1];
}
}

return $methodName;
}

private function resolveConstantPhpDocFromParentClass(
ClassReflection $declaringClass,
ClassReflection $parent,
Expand Down
28 changes: 28 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-9733.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

namespace Bug9733;

use function PHPStan\Testing\assertType;

abstract class Base
{
/**
* @param int[] $array
*/
abstract public function test(array $array): void;
}

trait MyTrait
{
public function test(array $array): void
{
assertType('array<int>', $array);
}
}

class Concrete extends Base
{
use MyTrait {
test as test2;
}
}
Loading