Skip to content

Commit

Permalink
Template types aren't subtractable
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 6, 2020
1 parent cc1c23e commit 0a1e2d3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 95 deletions.
46 changes: 8 additions & 38 deletions src/Type/Generic/TemplateMixedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
use PHPStan\Type\CompoundType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
Expand All @@ -35,11 +33,10 @@ public function __construct(
TemplateTypeScope $scope,
TemplateTypeStrategy $templateTypeStrategy,
TemplateTypeVariance $templateTypeVariance,
string $name,
?Type $subtractedType = null
string $name
)
{
parent::__construct(true, $subtractedType);
parent::__construct(true);

$this->scope = $scope;
$this->strategy = $templateTypeStrategy;
Expand Down Expand Up @@ -82,7 +79,7 @@ public function equals(Type $type): bool
public function getBound(): Type
{
if ($this->bound === null) {
$this->bound = new MixedType(true, $this->getSubtractedType());
$this->bound = new MixedType(true);
}
return $this->bound;
}
Expand Down Expand Up @@ -172,8 +169,7 @@ public function toArgument(): TemplateType
$this->scope,
new TemplateTypeArgumentStrategy(),
$this->variance,
$this->name,
$this->getSubtractedType()
$this->name
);
}

Expand All @@ -184,42 +180,17 @@ public function isValidVariance(Type $a, Type $b): bool

public function subtract(Type $type): Type
{
if ($type instanceof self) {
return new NeverType();
}
if ($this->getSubtractedType() !== null) {
$type = TypeCombinator::union($this->getSubtractedType(), $type);
}

return new self(
$this->scope,
$this->strategy,
$this->variance,
$this->name,
$type
);
return $this;
}

public function getTypeWithoutSubtractedType(): Type
{
return new self(
$this->scope,
$this->strategy,
$this->variance,
$this->name,
null
);
return $this;
}

public function changeSubtractedType(?Type $subtractedType): Type
{
return new self(
$this->scope,
$this->strategy,
$this->variance,
$this->name,
$subtractedType
);
return $this;
}

public function getVariance(): TemplateTypeVariance
Expand All @@ -237,8 +208,7 @@ public static function __set_state(array $properties): Type
$properties['scope'],
$properties['strategy'],
$properties['variance'],
$properties['name'],
$properties['subtractedType']
$properties['name']
);
}

Expand Down
32 changes: 16 additions & 16 deletions src/Type/Generic/TemplateObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,16 @@ public function __construct(
TemplateTypeStrategy $templateTypeStrategy,
TemplateTypeVariance $templateTypeVariance,
string $name,
string $class,
?Type $subtractedType = null
string $class
)
{
parent::__construct($class, $subtractedType);
parent::__construct($class);

$this->scope = $scope;
$this->strategy = $templateTypeStrategy;
$this->variance = $templateTypeVariance;
$this->name = $name;
$this->bound = new ObjectType($class, $subtractedType);
$this->bound = new ObjectType($class);
}

public function getName(): string
Expand Down Expand Up @@ -179,8 +178,7 @@ public function toArgument(): TemplateType
new TemplateTypeArgumentStrategy(),
TemplateTypeVariance::createInvariant(),
$this->name,
$this->getClassName(),
$this->getSubtractedType()
$this->getClassName()
);
}

Expand All @@ -189,16 +187,19 @@ public function isValidVariance(Type $a, Type $b): bool
return $this->variance->isValidVariance($a, $b);
}

public function subtract(Type $type): Type
{
return $this;
}

public function getTypeWithoutSubtractedType(): Type
{
return $this;
}

public function changeSubtractedType(?Type $subtractedType): Type
{
return new self(
$this->scope,
$this->strategy,
$this->variance,
$this->name,
$this->getClassName(),
$subtractedType
);
return $this;
}

public function getVariance(): TemplateTypeVariance
Expand All @@ -217,8 +218,7 @@ public static function __set_state(array $properties): Type
$properties['strategy'],
$properties['variance'],
$properties['name'],
$properties['className'],
$properties['subtractedType']
$properties['className']
);
}

Expand Down
50 changes: 10 additions & 40 deletions src/Type/Generic/TemplateObjectWithoutClassType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
use PHPStan\TrinaryLogic;
use PHPStan\Type\CompoundType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\NeverType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
Expand All @@ -35,11 +33,10 @@ public function __construct(
TemplateTypeScope $scope,
TemplateTypeStrategy $templateTypeStrategy,
TemplateTypeVariance $templateTypeVariance,
string $name,
?Type $subtractedType = null
string $name
)
{
parent::__construct($subtractedType);
parent::__construct();

$this->scope = $scope;
$this->strategy = $templateTypeStrategy;
Expand All @@ -60,7 +57,7 @@ public function getScope(): TemplateTypeScope
public function getBound(): Type
{
if ($this->bound === null) {
$this->bound = new ObjectWithoutClassType($this->getSubtractedType());
$this->bound = new ObjectWithoutClassType();
}
return $this->bound;
}
Expand Down Expand Up @@ -95,8 +92,7 @@ public function toArgument(): TemplateType
$this->scope,
new TemplateTypeArgumentStrategy(),
$this->variance,
$this->name,
$this->getSubtractedType()
$this->name
);
}

Expand All @@ -105,44 +101,19 @@ public function isValidVariance(Type $a, Type $b): bool
return $this->variance->isValidVariance($a, $b);
}

public function getTypeWithoutSubtractedType(): Type
public function subtract(Type $type): Type
{
return new self(
$this->scope,
$this->strategy,
$this->variance,
$this->name,
null
);
return $this;
}

public function subtract(Type $type): Type
public function getTypeWithoutSubtractedType(): Type
{
if ($type instanceof self) {
return new NeverType();
}
if ($this->getSubtractedType() !== null) {
$type = TypeCombinator::union($this->getSubtractedType(), $type);
}

return new self(
$this->scope,
$this->strategy,
$this->variance,
$this->name,
$type
);
return $this;
}

public function changeSubtractedType(?Type $subtractedType): Type
{
return new self(
$this->scope,
$this->strategy,
$this->variance,
$this->name,
$subtractedType
);
return $this;
}

public function equals(Type $type): bool
Expand Down Expand Up @@ -241,8 +212,7 @@ public static function __set_state(array $properties): Type
$properties['scope'],
$properties['strategy'],
$properties['variance'],
$properties['name'],
$properties['subtractedType']
$properties['name']
);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/data/generics.php
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,7 @@ function testGenericObjectWithoutClassType2($a)
return $a;
}

assertType('T of object~stdClass (function PHPStan\Generics\FunctionsAssertType\testGenericObjectWithoutClassType2(), argument)', $b);
assertType('T of object (function PHPStan\Generics\FunctionsAssertType\testGenericObjectWithoutClassType2(), argument)', $b);

return $a;
}
Expand Down

0 comments on commit 0a1e2d3

Please sign in to comment.