Skip to content

Commit

Permalink
Revert "Use narrowed conditional type if/else types for subtype checks"
Browse files Browse the repository at this point in the history
This reverts commit 4b978ba.
  • Loading branch information
mvorisek committed Mar 8, 2024
1 parent 166a040 commit bd91139
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 97 deletions.
85 changes: 23 additions & 62 deletions src/Type/ConditionalType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ final class ConditionalType implements CompoundType, LateResolvableType
use LateResolvableTypeTrait;
use NonGeneralizableTypeTrait;

private ?Type $normalizedIf = null;

private ?Type $normalizedElse = null;

private ?Type $subjectWithTargetIntersectedType = null;

private ?Type $subjectWithTargetRemovedType = null;

public function __construct(
private Type $subject,
private Type $target,
Expand Down Expand Up @@ -121,33 +113,37 @@ protected function getResult(): Type
{
$isSuperType = $this->target->isSuperTypeOf($this->subject);

$intersectedType = TypeCombinator::intersect($this->subject, $this->target);
$removedType = TypeCombinator::remove($this->subject, $this->target);

$yesType = fn () => TypeTraverser::map(
!$this->negated ? $this->if : $this->else,
fn (Type $type, callable $traverse) => $type === $this->subject ? (!$this->negated ? $intersectedType : $removedType) : $traverse($type),
);
$noType = fn () => TypeTraverser::map(
!$this->negated ? $this->else : $this->if,
fn (Type $type, callable $traverse) => $type === $this->subject ? (!$this->negated ? $removedType : $intersectedType) : $traverse($type),
);

if ($isSuperType->yes()) {
return !$this->negated ? $this->getNormalizedIf() : $this->getNormalizedElse();
return $yesType();
}

if ($isSuperType->no()) {
return !$this->negated ? $this->getNormalizedElse() : $this->getNormalizedIf();
return $noType();
}

return TypeCombinator::union(
$this->getNormalizedIf(),
$this->getNormalizedElse(),
);
return TypeCombinator::union($yesType(), $noType());
}

public function traverse(callable $cb): Type
{
$subject = $cb($this->subject);
$target = $cb($this->target);
$if = $cb($this->getNormalizedIf());
$else = $cb($this->getNormalizedElse());

if (
$this->subject === $subject
&& $this->target === $target
&& $this->getNormalizedIf() === $if
&& $this->getNormalizedElse() === $else
) {
$if = $cb($this->if);
$else = $cb($this->else);

if ($this->subject === $subject && $this->target === $target && $this->if === $if && $this->else === $else) {
return $this;
}

Expand All @@ -162,15 +158,10 @@ public function traverseSimultaneously(Type $right, callable $cb): Type

$subject = $cb($this->subject, $right->subject);
$target = $cb($this->target, $right->target);
$if = $cb($this->getNormalizedIf(), $right->getNormalizedIf());
$else = $cb($this->getNormalizedElse(), $right->getNormalizedElse());

if (
$this->subject === $subject
&& $this->target === $target
&& $this->getNormalizedIf() === $if
&& $this->getNormalizedElse() === $else
) {
$if = $cb($this->if, $right->if);
$else = $cb($this->else, $right->else);

if ($this->subject === $subject && $this->target === $target && $this->if === $if && $this->else === $else) {
return $this;
}

Expand Down Expand Up @@ -202,34 +193,4 @@ public static function __set_state(array $properties): Type
);
}

private function getNormalizedIf(): Type
{
return $this->normalizedIf ??= TypeTraverser::map(
$this->if,
fn (Type $type, callable $traverse) => $type === $this->subject
? (!$this->negated ? $this->getSubjectWithTargetIntersectedType() : $this->getSubjectWithTargetRemovedType())
: $traverse($type),
);
}

private function getNormalizedElse(): Type
{
return $this->normalizedElse ??= TypeTraverser::map(
$this->else,
fn (Type $type, callable $traverse) => $type === $this->subject
? (!$this->negated ? $this->getSubjectWithTargetRemovedType() : $this->getSubjectWithTargetIntersectedType())
: $traverse($type),
);
}

private function getSubjectWithTargetIntersectedType(): Type
{
return $this->subjectWithTargetIntersectedType ??= TypeCombinator::intersect($this->subject, $this->target);
}

private function getSubjectWithTargetRemovedType(): Type
{
return $this->subjectWithTargetRemovedType ??= TypeCombinator::remove($this->subject, $this->target);
}

}
5 changes: 0 additions & 5 deletions tests/PHPStan/Rules/PhpDoc/IncompatiblePhpDocTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,11 +424,6 @@ public function testGenericCallables(): void
]);
}

public function testBug10622(): void
{
$this->analyse([__DIR__ . '/data/bug-10622.php'], []);
}

public function testBug10622B(): void
{
$this->analyse([__DIR__ . '/data/bug-10622b.php'], []);
Expand Down
30 changes: 0 additions & 30 deletions tests/PHPStan/Rules/PhpDoc/data/bug-10622.php

This file was deleted.

0 comments on commit bd91139

Please sign in to comment.