Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions src/Psalm/Internal/Type/TypeCombiner.php
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,11 @@ private static function scrapeTypeProperties(
}

if (isset($combination->named_object_types[$type_key])) {
if (!$type->is_static) {
$combination->named_object_types[$type_key]
= $combination->named_object_types[$type_key]->setIsStatic(false);
}

return null;
}

Expand Down
24 changes: 24 additions & 0 deletions tests/ReturnTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2694,6 +2694,20 @@ public function get(int|string $x): int {
'ignored_issues' => ['RedundantCondition'],
'php_version' => '8.0',
],
'unionOfThisAndStaticStaysStatic' => [
'code' => '<?php
class A {
/** @return static */
public function me() {
return $this;
}

/** @return static */
public function pick(bool $f) {
return $f ? $this : $this->me();
}
}',
],
];
}

Expand Down Expand Up @@ -3949,6 +3963,16 @@ function f($x): int {
'error_message' => 'InvalidReturnType',
'error_levels' => ['RedundantConditionGivenDocblockType'],
],
'unionOfThisAndOtherInstanceIsNotStatic' => [
'code' => '<?php
class A {
/** @return static */
public function pick(A $other, bool $f) {
return $f ? $this : $other;
}
}',
'error_message' => 'LessSpecificReturnStatement',
],
];
}
}
12 changes: 12 additions & 0 deletions tests/TypeCombinationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
use Psalm\Type;
use Psalm\Type\Atomic;
use Psalm\Type\Atomic\TNamedObject;

use function array_reverse;

Expand Down Expand Up @@ -43,6 +44,17 @@ public function testValidTypeCombination(string $expected, array $types): void
);
}

public function testStaticIsKeptOnlyWhenEveryCombinedObjectIsStatic(): void
{
$static = new TNamedObject('Exception', true);
$plain = new TNamedObject('Exception');

$this->assertSame('Exception', TypeCombiner::combine([$static, $plain])->getId());
$this->assertSame('Exception', TypeCombiner::combine([$plain, $static])->getId());
$this->assertSame('Exception', TypeCombiner::combine([$static, $plain, $static])->getId());
$this->assertSame('Exception&static', TypeCombiner::combine([$static, $static])->getId());
}

/**
* @psalm-pure
*/
Expand Down
Loading