Skip to content
Merged
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
17 changes: 17 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7025,6 +7025,23 @@ private function getPhpDocReturnType(ResolvedPhpDocBlock $resolvedPhpDoc, Type $
return $phpDocReturnType;
}

if ($phpDocReturnType instanceof UnionType) {
$types = [];
foreach ($phpDocReturnType->getTypes() as $innerType) {
if (!$nativeReturnType->isSuperTypeOf($innerType)->yes()) {
continue;
}

$types[] = $innerType;
}

if (count($types) === 0) {
return null;
}

return TypeCombinator::union(...$types);
}

return null;
}

Expand Down
24 changes: 23 additions & 1 deletion src/Reflection/Php/PhpClassReflectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypehintHelper;
use PHPStan\Type\UnionType;
use function array_key_exists;
use function array_keys;
use function array_map;
Expand Down Expand Up @@ -1241,10 +1242,31 @@ private function getPhpDocReturnType(ClassReflection $phpDocBlockClassReflection
TemplateTypeVariance::createCovariant(),
);

if ($returnTag->isExplicit() || $nativeReturnType->isSuperTypeOf($phpDocReturnType)->yes()) {
if ($returnTag->isExplicit()) {
return $phpDocReturnType;
}

if ($nativeReturnType->isSuperTypeOf($phpDocReturnType)->yes()) {
return $phpDocReturnType;
}

if ($phpDocReturnType instanceof UnionType) {
$types = [];
foreach ($phpDocReturnType->getTypes() as $innerType) {
if (!$nativeReturnType->isSuperTypeOf($innerType)->yes()) {
continue;
}

$types[] = $innerType;
}

if (count($types) === 0) {
return null;
}

return TypeCombinator::union(...$types);
}

return null;
}

Expand Down
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ private static function findTestFiles(): iterable
yield __DIR__ . '/../Rules/Arrays/data/slevomat-foreach-unset-bug.php';
yield __DIR__ . '/../Rules/Arrays/data/slevomat-foreach-array-key-exists-bug.php';

yield __DIR__ . '/../Rules/Methods/data/inherit-phpdoc-return-type-with-narrower-native-return-type.php';

if (PHP_VERSION_ID >= 80000) {
yield __DIR__ . '/../Rules/Comparison/data/bug-7898.php';
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php // lint >= 8.0

namespace InheritPhpDocReturnTypeWithNarrowerNativeReturnTypePhp8;

use function PHPStan\Testing\assertType;

class Foo
{

/**
* @return array<string>|positive-int|null
*/
public function doFoo(): array|int|null
{

}

}

class Bar extends Foo
{

public function doFoo(): array|int
{

}

}

function (Bar $bar): void {
assertType('array<string>|int<1, max>', $bar->doFoo());
};
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,9 @@ public function testBug9657(): void
$this->analyse([__DIR__ . '/data/bug-9657.php'], []);
}

public function testInheritPhpDocReturnTypeWithNarrowerNativeReturnType(): void
{
$this->analyse([__DIR__ . '/data/inherit-phpdoc-return-type-with-narrower-native-return-type.php'], []);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace InheritPhpDocReturnTypeWithNarrowerNativeReturnType;

use function PHPStan\Testing\assertType;

class Foo
{

/**
* @return array<string>|null
*/
public function doFoo(): ?array
{

}

}

class Bar extends Foo
{

public function doFoo(): array
{

}

}

function (Bar $bar): void {
assertType('array<string>', $bar->doFoo());
};
Loading