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
5 changes: 5 additions & 0 deletions src/Type/NeverType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Enum\EnumCaseObjectType;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
use PHPStan\Type\Traits\NonGenericTypeTrait;
use PHPStan\Type\Traits\NonRemoveableTypeTrait;
Expand Down Expand Up @@ -81,6 +82,10 @@ public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
return IsSuperTypeOfResult::createYes();
}

if ($type instanceof TemplateType) {
return IsSuperTypeOfResult::createMaybe();
}

return IsSuperTypeOfResult::createNo();
}

Expand Down
6 changes: 6 additions & 0 deletions src/Type/NonAcceptingNeverType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PHPStan\Type;

use PHPStan\Type\Generic\TemplateType;

/** @api */
class NonAcceptingNeverType extends NeverType
{
Expand All @@ -21,6 +23,10 @@ public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
return IsSuperTypeOfResult::createMaybe();
}

if ($type instanceof TemplateType) {
return IsSuperTypeOfResult::createMaybe();
}

return IsSuperTypeOfResult::createNo();
}

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

namespace Bug10938;

use function PHPStan\Testing\assertType;

/**
* @template TKey
* @template TValue
* @extends \IteratorAggregate<TKey, TValue>
*/
interface Collection extends \IteratorAggregate
{
/**
* @return (TValue is never ? true : bool)
*/
function isEmpty(): bool;
}

/** @param Collection<never, never> $c */
function emptyCollection(Collection $c): void {
assertType('true', $c->isEmpty());
}

/** @param Collection<int, string> $c */
function nonEmptyCollection(Collection $c): void {
assertType('bool', $c->isEmpty());
}
29 changes: 29 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-9634.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types = 1);

namespace Bug9634;

use function PHPStan\Testing\assertType;

/** @template T */
interface Option {
/** @return self<never> */
static function none(): self;

/** @return T */
function unwrap(): mixed;

/**
* @return (T is never ? false : bool)
*/
function isSome(): bool;
}

/** @param Option<never> $o */
function f(Option $o): void {
assertType('false', $o->isSome());
}

/** @param Option<int> $o */
function g(Option $o): void {
assertType('bool', $o->isSome());
}
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/MethodConditionalReturnTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,14 @@ public function testBug11939(): void
$this->analyse([__DIR__ . '/data/bug-11939.php'], []);
}

public function testBug9634(): void
{
$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-9634.php'], []);
}

public function testBug10938(): void
{
$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-10938.php'], []);
}

}
24 changes: 24 additions & 0 deletions tests/PHPStan/Type/TemplateTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,30 @@ public static function dataIsSuperTypeOf(): array
TrinaryLogic::createMaybe(),
TrinaryLogic::createYes(),
],
23 => [
$templateType('T', null),
new NeverType(),
TrinaryLogic::createYes(), // T isSuperTypeTo *NEVER* - never is bottom type
TrinaryLogic::createMaybe(), // *NEVER* isSuperTypeTo T - T could be never
],
24 => [
$templateType('T', null),
new NonAcceptingNeverType(),
TrinaryLogic::createYes(), // T isSuperTypeTo never - never is bottom type
TrinaryLogic::createMaybe(), // never isSuperTypeTo T - T could be never
],
25 => [
$templateType('T', new ObjectType('DateTime')),
new NeverType(),
TrinaryLogic::createYes(), // (T of DateTime) isSuperTypeTo *NEVER* - never is bottom type
TrinaryLogic::createMaybe(), // *NEVER* isSuperTypeTo (T of DateTime) - T could be never
],
26 => [
$templateType('T', new ObjectType('DateTime')),
new NonAcceptingNeverType(),
TrinaryLogic::createYes(), // (T of DateTime) isSuperTypeTo never - never is bottom type
TrinaryLogic::createMaybe(), // never isSuperTypeTo (T of DateTime) - T could be never
],
];
}

Expand Down
Loading