Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix supertype checks for conditional types #1287

Merged
merged 1 commit into from
May 5, 2022
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
11 changes: 11 additions & 0 deletions src/Type/ConditionalType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Type;

use PHPStan\TrinaryLogic;
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\Traits\LateResolvableTypeTrait;
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
Expand Down Expand Up @@ -40,6 +41,16 @@ public function isNegated(): bool
return $this->negated;
}

public function isSuperTypeOf(Type $type): TrinaryLogic
{
if ($type instanceof self) {
return $this->if->isSuperTypeOf($type->if)
->and($this->else->isSuperTypeOf($type->else));
}

return $this->isSuperTypeOfDefault($type);
}

public function getReferencedClasses(): array
{
return array_merge(
Expand Down
11 changes: 11 additions & 0 deletions src/Type/ConditionalTypeForParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Type;

use PHPStan\TrinaryLogic;
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\Traits\LateResolvableTypeTrait;
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
Expand Down Expand Up @@ -62,6 +63,16 @@ public function toConditional(Type $subject): Type
);
}

public function isSuperTypeOf(Type $type): TrinaryLogic
{
if ($type instanceof self) {
return $this->if->isSuperTypeOf($type->if)
->and($this->else->isSuperTypeOf($type->else));
}

return $this->isSuperTypeOfDefault($type);
}

public function getReferencedClasses(): array
{
return array_merge(
Expand Down
13 changes: 12 additions & 1 deletion src/Type/Traits/LateResolvableTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPStan\Type\BooleanType;
use PHPStan\Type\CompoundType;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;

trait LateResolvableTypeTrait
Expand All @@ -26,7 +27,17 @@ public function accepts(Type $type, bool $strictTypes): TrinaryLogic

public function isSuperTypeOf(Type $type): TrinaryLogic
{
return $this->getResult()->isSuperTypeOf($type);
return $this->isSuperTypeOfDefault($type);
}

private function isSuperTypeOfDefault(Type $type): TrinaryLogic
{
if ($type instanceof NeverType) {
return TrinaryLogic::createYes();
}

return $this->getResult()->isSuperTypeOf($type)
->and(TrinaryLogic::createMaybe());
}

public function canAccessProperties(): TrinaryLogic
Expand Down
19 changes: 19 additions & 0 deletions tests/PHPStan/Analyser/data/conditional-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,25 @@ public function testMaybeNever(): void
assertType('*NEVER*', $this->maybeNever(1));
assertType('void', $this->maybeNever(2));
}

/**
* @return ($if is true ? mixed : null)|false
*/
abstract public function lateConditional1(bool $if);

/**
* @return ($if is true ? mixed : null)|($if is true ? null : mixed)|false
*/
abstract public function lateConditional2(bool $if);

public function testLateConditional(): void
{
assertType('mixed', $this->lateConditional1(true));
assertType('false|null', $this->lateConditional1(false));

assertType('mixed', $this->lateConditional2(true));
assertType('mixed', $this->lateConditional2(false));
}
}

class ParentClassToInherit
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Rules/Methods/MethodSignatureRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public function testOverridenMethodWithConditionalReturnType(): void
$this->reportStatic = true;
$this->analyse([__DIR__ . '/data/overriden-method-with-conditional-return-type.php'], [
[
'Return type (($p is int ? stdClass : string)) of method OverridenMethodWithConditionalReturnType\Bar2::doFoo() should be covariant with return type (($p is int ? int : string)) of method OverridenMethodWithConditionalReturnType\Foo::doFoo()',
'Return type (($p is int ? stdClass : string)) of method OverridenMethodWithConditionalReturnType\Bar2::doFoo() should be compatible with return type (($p is int ? int : string)) of method OverridenMethodWithConditionalReturnType\Foo::doFoo()',
37,
],
]);
Expand Down