-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for union type as template type bound
- Loading branch information
1 parent
dbf5eef
commit ac7b49e
Showing
8 changed files
with
187 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Generic; | ||
|
||
use PHPStan\Type\Type; | ||
use PHPStan\Type\BenevolentUnionType; | ||
|
||
final class TemplateBenevolentUnionType extends BenevolentUnionType implements TemplateType | ||
{ | ||
|
||
use TemplateTypeTrait; | ||
|
||
/** | ||
* @param Type[] $types | ||
*/ | ||
public function __construct( | ||
TemplateTypeScope $scope, | ||
TemplateTypeStrategy $templateTypeStrategy, | ||
TemplateTypeVariance $templateTypeVariance, | ||
array $types, | ||
string $name | ||
) | ||
{ | ||
parent::__construct($types); | ||
|
||
$this->scope = $scope; | ||
$this->strategy = $templateTypeStrategy; | ||
$this->variance = $templateTypeVariance; | ||
$this->name = $name; | ||
$this->bound = new BenevolentUnionType($types); | ||
} | ||
|
||
public function toArgument(): TemplateType | ||
{ | ||
return new self( | ||
$this->scope, | ||
new TemplateTypeArgumentStrategy(), | ||
$this->variance, | ||
$this->getTypes(), | ||
$this->name | ||
); | ||
} | ||
|
||
/** | ||
* @param mixed[] $properties | ||
* @return Type | ||
*/ | ||
public static function __set_state(array $properties): Type | ||
{ | ||
return new self( | ||
$properties['scope'], | ||
$properties['strategy'], | ||
$properties['variance'], | ||
$properties['types'], | ||
$properties['name'] | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Generic; | ||
|
||
use PHPStan\Type\Type; | ||
use PHPStan\Type\UnionType; | ||
|
||
final class TemplateUnionType extends UnionType implements TemplateType | ||
{ | ||
|
||
use TemplateTypeTrait; | ||
|
||
/** | ||
* @param Type[] $types | ||
*/ | ||
public function __construct( | ||
TemplateTypeScope $scope, | ||
TemplateTypeStrategy $templateTypeStrategy, | ||
TemplateTypeVariance $templateTypeVariance, | ||
array $types, | ||
string $name | ||
) | ||
{ | ||
parent::__construct($types); | ||
|
||
$this->scope = $scope; | ||
$this->strategy = $templateTypeStrategy; | ||
$this->variance = $templateTypeVariance; | ||
$this->name = $name; | ||
$this->bound = new UnionType($types); | ||
} | ||
|
||
public function toArgument(): TemplateType | ||
{ | ||
return new self( | ||
$this->scope, | ||
new TemplateTypeArgumentStrategy(), | ||
$this->variance, | ||
$this->getTypes(), | ||
$this->name | ||
); | ||
} | ||
|
||
/** | ||
* @param mixed[] $properties | ||
* @return Type | ||
*/ | ||
public static function __set_state(array $properties): Type | ||
{ | ||
return new self( | ||
$properties['scope'], | ||
$properties['strategy'], | ||
$properties['variance'], | ||
$properties['types'], | ||
$properties['name'] | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Bug3769; | ||
|
||
use function PHPStan\Analyser\assertType; | ||
|
||
/** | ||
* @template K of array-key | ||
* @param array<K, int> $in | ||
* @return array<K, string> | ||
*/ | ||
function stringValues(array $in): array { | ||
assertType('array<K of (int|string) (function Bug3769\stringValues(), argument), int>', $in); | ||
return array_map(fn (int $int): string => (string) $int, $in); | ||
} | ||
|
||
/** | ||
* @param array<int, int> $foo | ||
* @param array<string, int> $bar | ||
* @param array<int> $baz | ||
*/ | ||
function foo( | ||
array $foo, | ||
array $bar, | ||
array $baz | ||
): void { | ||
assertType('array<int, string>', stringValues($foo)); | ||
assertType('array<string, string>', stringValues($bar)); | ||
assertType('array<string>', stringValues($baz)); | ||
}; | ||
|
||
/** | ||
* @template T of \stdClass|\Exception | ||
* @param T $foo | ||
*/ | ||
function fooUnion($foo): void { | ||
assertType('T of Exception|stdClass (function Bug3769\fooUnion(), argument)', $foo); | ||
} |