Skip to content

Generic variance #4

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

Merged
merged 1 commit into from
Nov 6, 2019
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
14 changes: 13 additions & 1 deletion src/PhpDoc/PhpDocNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use PHPStan\Type\ArrayType;
use PHPStan\Type\Generic\TemplateTypeFactory;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -263,7 +264,18 @@ private function resolveTemplateTags(PhpDocNode $phpDocNode, NameScope $nameScop
foreach ($phpDocNode->getTemplateTagValues($tagName) as $tagValue) {
$resolved[$tagValue->name] = new TemplateTag(
$tagValue->name,
$tagValue->bound !== null ? $this->typeNodeResolver->resolve($tagValue->bound, $nameScope) : new MixedType()
$tagValue->bound !== null ? $this->typeNodeResolver->resolve($tagValue->bound, $nameScope) : new MixedType(),
TemplateTypeVariance::createInvariant()
);
}
}

foreach (['@template-covariant', '@psalm-template-covariant', '@phpstan-template-covariant'] as $tagName) {
foreach ($phpDocNode->getTemplateTagValues($tagName) as $tagValue) {
$resolved[$tagValue->name] = new TemplateTag(
$tagValue->name,
$tagValue->bound !== null ? $this->typeNodeResolver->resolve($tagValue->bound, $nameScope) : new MixedType(),
TemplateTypeVariance::createCovariant()
);
}
}
Expand Down
15 changes: 13 additions & 2 deletions src/PhpDoc/Tag/TemplateTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\PhpDoc\Tag;

use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\Type;

class TemplateTag
Expand All @@ -13,10 +14,14 @@ class TemplateTag
/** @var \PHPStan\Type\Type */
private $bound;

public function __construct(string $name, Type $bound)
/** @var TemplateTypeVariance */
private $variance;

public function __construct(string $name, Type $bound, TemplateTypeVariance $variance)
{
$this->name = $name;
$this->bound = $bound;
$this->variance = $variance;
}

public function getName(): string
Expand All @@ -29,6 +34,11 @@ public function getBound(): Type
return $this->bound;
}

public function getVariance(): TemplateTypeVariance
{
return $this->variance;
}

/**
* @param mixed[] $properties
* @return self
Expand All @@ -37,7 +47,8 @@ public static function __set_state(array $properties): self
{
return new self(
$properties['name'],
$properties['bound']
$properties['bound'],
$properties['variance']
);
}

Expand Down
29 changes: 20 additions & 9 deletions src/Type/Generic/GenericObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use PHPStan\Type\CompoundType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
Expand Down Expand Up @@ -116,17 +115,24 @@ private function isSuperTypeOfInternal(Type $type, bool $acceptsContext): Trinar
return TrinaryLogic::createNo();
}

foreach ($this->types as $i => $t) {
$classReflection = $this->getClassReflection();
if ($classReflection === null) {
return $nakedSuperTypeOf;
}

$typeList = $classReflection->typeMapToList($classReflection->getTemplateTypeMap());

foreach ($typeList as $i => $templateType) {
if (!isset($ancestor->types[$i])) {
throw new \PHPStan\ShouldNotHappenException();
}
if (!$t->equals($ancestor->types[$i])) {
if ($t instanceof MixedType) {
continue;
}
if ($ancestor->types[$i] instanceof MixedType) {
continue;
}
if (!isset($this->types[$i])) {
throw new \PHPStan\ShouldNotHappenException();
}
if (!$templateType instanceof TemplateType) {
throw new \PHPStan\ShouldNotHappenException();
}
if (!$templateType->isValidVariance($this->types[$i], $ancestor->types[$i])) {
return TrinaryLogic::createNo();
}
}
Expand Down Expand Up @@ -218,6 +224,11 @@ public function traverse(callable $cb): Type
return $this;
}

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

/**
* @param mixed[] $properties
* @return Type
Expand Down
15 changes: 15 additions & 0 deletions src/Type/Generic/TemplateMixedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ final class TemplateMixedType extends MixedType implements TemplateType
/** @var TemplateTypeStrategy */
private $strategy;

/** @var TemplateTypeVariance */
private $variance;

public function __construct(
TemplateTypeScope $scope,
TemplateTypeStrategy $templateTypeStrategy,
TemplateTypeVariance $templateTypeVariance,
string $name,
?Type $subtractedType = null
)
Expand All @@ -36,6 +40,7 @@ public function __construct(

$this->scope = $scope;
$this->strategy = $templateTypeStrategy;
$this->variance = $templateTypeVariance;
$this->name = $name;
}

Expand Down Expand Up @@ -143,11 +148,17 @@ public function toArgument(): TemplateType
return new self(
$this->scope,
new TemplateTypeArgumentStrategy(),
$this->variance,
$this->name,
$this->getSubtractedType()
);
}

public function isValidVariance(Type $a, Type $b): bool
{
return $this->variance->isValidVariance($a, $b);
}

public function subtract(Type $type): Type
{
if ($type instanceof self) {
Expand All @@ -160,6 +171,7 @@ public function subtract(Type $type): Type
return new self(
$this->scope,
$this->strategy,
$this->variance,
$this->name,
$type
);
Expand All @@ -170,6 +182,7 @@ public function getTypeWithoutSubtractedType(): Type
return new self(
$this->scope,
$this->strategy,
$this->variance,
$this->name,
null
);
Expand All @@ -180,6 +193,7 @@ public function changeSubtractedType(?Type $subtractedType): Type
return new self(
$this->scope,
$this->strategy,
$this->variance,
$this->name,
$subtractedType
);
Expand All @@ -194,6 +208,7 @@ public static function __set_state(array $properties): Type
return new self(
$properties['scope'],
$properties['strategy'],
$properties['variance'],
$properties['name'],
$properties['subtractedType']
);
Expand Down
34 changes: 10 additions & 24 deletions src/Type/Generic/TemplateObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PHPStan\Type\ObjectType;
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 @@ -28,9 +27,13 @@ final class TemplateObjectType extends ObjectType implements TemplateType
/** @var ObjectType */
private $bound;

/** @var TemplateTypeVariance */
private $variance;

public function __construct(
TemplateTypeScope $scope,
TemplateTypeStrategy $templateTypeStrategy,
TemplateTypeVariance $templateTypeVariance,
string $name,
string $class,
?Type $subtractedType = null
Expand All @@ -40,6 +43,7 @@ public function __construct(

$this->scope = $scope;
$this->strategy = $templateTypeStrategy;
$this->variance = $templateTypeVariance;
$this->name = $name;
$this->bound = new ObjectType($class, $subtractedType);
}
Expand Down Expand Up @@ -163,43 +167,24 @@ public function toArgument(): TemplateType
return new self(
$this->scope,
new TemplateTypeArgumentStrategy(),
TemplateTypeVariance::createInvariant(),
$this->name,
$this->getClassName(),
$this->getSubtractedType()
);
}

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

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

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

public function changeSubtractedType(?Type $subtractedType): Type
{
return new self(
$this->scope,
$this->strategy,
$this->variance,
$this->name,
$this->getClassName(),
$subtractedType
Expand All @@ -215,6 +200,7 @@ public static function __set_state(array $properties): Type
return new self(
$properties['scope'],
$properties['strategy'],
$properties['variance'],
$properties['name'],
$properties['className'],
$properties['subtractedType']
Expand Down
2 changes: 2 additions & 0 deletions src/Type/Generic/TemplateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ public function toArgument(): TemplateType;

public function isArgument(): bool;

public function isValidVariance(Type $a, Type $b): bool;

}
8 changes: 4 additions & 4 deletions src/Type/Generic/TemplateTypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@
final class TemplateTypeFactory
{

public static function create(TemplateTypeScope $scope, string $name, ?Type $bound): Type
public static function create(TemplateTypeScope $scope, string $name, ?Type $bound, TemplateTypeVariance $variance): Type
{
$strategy = new TemplateTypeParameterStrategy();

if ($bound instanceof ObjectType) {
return new TemplateObjectType($scope, $strategy, $name, $bound->getClassName());
return new TemplateObjectType($scope, $strategy, $variance, $name, $bound->getClassName());
}

if ($bound === null || get_class($bound) === MixedType::class) {
return new TemplateMixedType($scope, $strategy, $name);
return new TemplateMixedType($scope, $strategy, $variance, $name);
}

return new ErrorType();
}

public static function fromTemplateTag(TemplateTypeScope $scope, TemplateTag $tag): Type
{
return self::create($scope, $tag->getName(), $tag->getBound());
return self::create($scope, $tag->getName(), $tag->getBound(), $tag->getVariance());
}

}
Loading