Skip to content
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
24 changes: 24 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,18 @@ parameters:
count: 3
path: src/Type/Generic/TemplateConstantArrayType.php

-
rawMessage: Doing instanceof PHPStan\Type\IntersectionType is error-prone and deprecated.
identifier: phpstanApi.instanceofType
count: 3
path: src/Type/Generic/TemplateConstantFloatType.php

-
rawMessage: 'Method PHPStan\Type\Generic\TemplateConstantFloatType::toPhpDocNode() should return PHPStan\PhpDocParser\Ast\Type\ConstTypeNode but returns PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode.'
identifier: return.type
count: 1
path: src/Type/Generic/TemplateConstantFloatType.php

-
rawMessage: Doing instanceof PHPStan\Type\IntersectionType is error-prone and deprecated.
identifier: phpstanApi.instanceofType
Expand All @@ -1255,6 +1267,12 @@ parameters:
count: 3
path: src/Type/Generic/TemplateConstantStringType.php

-
rawMessage: Doing instanceof PHPStan\Type\IntersectionType is error-prone and deprecated.
identifier: phpstanApi.instanceofType
count: 3
path: src/Type/Generic/TemplateFloatRangeType.php

-
rawMessage: Doing instanceof PHPStan\Type\IntersectionType is error-prone and deprecated.
identifier: phpstanApi.instanceofType
Expand All @@ -1267,6 +1285,12 @@ parameters:
count: 3
path: src/Type/Generic/TemplateGenericObjectType.php

-
rawMessage: Doing instanceof PHPStan\Type\IntersectionType is error-prone and deprecated.
identifier: phpstanApi.instanceofType
count: 3
path: src/Type/Generic/TemplateIntegerRangeType.php

-
rawMessage: Doing instanceof PHPStan\Type\IntersectionType is error-prone and deprecated.
identifier: phpstanApi.instanceofType
Expand Down
6 changes: 6 additions & 0 deletions src/Rules/Generics/TemplateTypeCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantFloatType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\FloatRangeType;
use PHPStan\Type\FloatType;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\Generic\TemplateTypeScope;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\IterableType;
Expand Down Expand Up @@ -143,7 +146,10 @@ public function check(
&& $boundTypeClass !== StringType::class
&& $boundTypeClass !== ConstantIntegerType::class
&& $boundTypeClass !== IntegerType::class
&& $boundTypeClass !== IntegerRangeType::class
&& $boundTypeClass !== ConstantFloatType::class
&& $boundTypeClass !== FloatType::class
&& $boundTypeClass !== FloatRangeType::class
&& $boundTypeClass !== BooleanType::class
&& $boundTypeClass !== ObjectWithoutClassType::class
&& $boundTypeClass !== ObjectType::class
Expand Down
6 changes: 6 additions & 0 deletions src/Type/Combinator/FiniteTypesRemoveRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Type\Combinator;

use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
Expand All @@ -27,6 +28,11 @@ public function tryRemove(Type $fromType, Type $typeToRemove): ?Type
return null;
}

// a template of several values stands for an unknown part of them, not for all of them
if ($typeToRemove instanceof TemplateType && count($finiteTypesToRemove) > 1) {
return null;
}

$result = [];
foreach ($fromFiniteTypes as $finiteType) {
foreach ($finiteTypesToRemove as $finiteTypeToRemove) {
Expand Down
12 changes: 11 additions & 1 deletion src/Type/FloatRangeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantFloatType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Generic\TemplateFloatRangeType;
use function array_map;
use function get_class;
use function is_finite;
Expand Down Expand Up @@ -49,7 +50,7 @@
class FloatRangeType extends FloatType implements CompoundType
{

private function __construct(private float $min, private float $max)
protected function __construct(private float $min, private float $max)
{
parent::__construct();
}
Expand Down Expand Up @@ -484,6 +485,11 @@ public function toAbsoluteNumber(): Type
*/
public function tryIntersect(Type $otherType): ?Type
{
// a template stands for an unknown part of its range, not for all of it
if ($otherType instanceof TemplateFloatRangeType) {
return null;
}

$bounds = self::getOrdinalBounds($otherType);
if ($bounds !== null) {
[$otherMin, $otherMax] = $bounds;
Expand Down Expand Up @@ -515,6 +521,10 @@ public function tryRemove(Type $typeToRemove): ?Type
return new NeverType();
}

if ($typeToRemove instanceof TemplateFloatRangeType) {
return null;
}

$bounds = self::getOrdinalBounds($typeToRemove);
if ($bounds === null) {
// NAN is in no range
Expand Down
40 changes: 40 additions & 0 deletions src/Type/Generic/TemplateConstantFloatType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Generic;

use PHPStan\Turbo\ShadowedByTurboExtension;
use PHPStan\Type\Constant\ConstantFloatType;
use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
use PHPStan\Type\Type;

/** @api */
#[ShadowedByTurboExtension(implementation: __DIR__ . '/../../../turbo-ext/src/TemplateConstantFloatType.cpp')]
final class TemplateConstantFloatType extends ConstantFloatType implements TemplateType
{

/** @use TemplateTypeTrait<ConstantFloatType> */
use TemplateTypeTrait;
use UndecidedComparisonCompoundTypeTrait;

/**
* @param non-empty-string $name
*/
public function __construct(
TemplateTypeScope $scope,
TemplateTypeStrategy $templateTypeStrategy,
TemplateTypeVariance $templateTypeVariance,
string $name,
ConstantFloatType $bound,
?Type $default,
)
{
parent::__construct($bound->getValue());
$this->scope = $scope;
$this->strategy = $templateTypeStrategy;
$this->variance = $templateTypeVariance;
$this->name = $name;
$this->bound = $bound;
$this->default = $default;
}

}
49 changes: 49 additions & 0 deletions src/Type/Generic/TemplateFloatRangeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Generic;

use PHPStan\Turbo\ShadowedByTurboExtension;
use PHPStan\Type\FloatRangeType;
use PHPStan\Type\Type;

/** @api */
#[ShadowedByTurboExtension(implementation: __DIR__ . '/../../../turbo-ext/src/TemplateFloatRangeType.cpp')]
final class TemplateFloatRangeType extends FloatRangeType implements TemplateType
{

/** @use TemplateTypeTrait<FloatRangeType> */
use TemplateTypeTrait;

/**
* @param non-empty-string $name
*/
public function __construct(
TemplateTypeScope $scope,
TemplateTypeStrategy $templateTypeStrategy,
TemplateTypeVariance $templateTypeVariance,
string $name,
FloatRangeType $bound,
?Type $default,
)
{
parent::__construct($bound->getMin(), $bound->getMax());
$this->scope = $scope;
$this->strategy = $templateTypeStrategy;
$this->variance = $templateTypeVariance;
$this->name = $name;
$this->bound = $bound;
$this->default = $default;
}

/** A T is an unknown part of its range: merged with another range it would no longer be a T. */
public function tryUnion(Type $otherType): ?Type
{
return null;
}

public function tryIntersect(Type $otherType): ?Type
{
return null;
}

}
49 changes: 49 additions & 0 deletions src/Type/Generic/TemplateIntegerRangeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Generic;

use PHPStan\Turbo\ShadowedByTurboExtension;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\Type;

/** @api */
#[ShadowedByTurboExtension(implementation: __DIR__ . '/../../../turbo-ext/src/TemplateIntegerRangeType.cpp')]
final class TemplateIntegerRangeType extends IntegerRangeType implements TemplateType
{

/** @use TemplateTypeTrait<IntegerRangeType> */
use TemplateTypeTrait;

/**
* @param non-empty-string $name
*/
public function __construct(
TemplateTypeScope $scope,
TemplateTypeStrategy $templateTypeStrategy,
TemplateTypeVariance $templateTypeVariance,
string $name,
IntegerRangeType $bound,
?Type $default,
)
{
parent::__construct($bound->getMin(), $bound->getMax());
$this->scope = $scope;
$this->strategy = $templateTypeStrategy;
$this->variance = $templateTypeVariance;
$this->name = $name;
$this->bound = $bound;
$this->default = $default;
}

/** A T is an unknown part of its range: merged with another range it would no longer be a T. */
public function tryUnion(Type $otherType): ?Type
{
return null;
}

public function tryIntersect(Type $otherType): ?Type
{
return null;
}

}
21 changes: 16 additions & 5 deletions src/Type/Generic/TemplateTypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantFloatType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\FloatRangeType;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\IntegerType;
Expand Down Expand Up @@ -77,18 +79,27 @@ public static function create(TemplateTypeScope $scope, string $name, ?Type $bou
return new TemplateConstantStringType($scope, $strategy, $variance, $name, $bound, $default);
}

if ($bound instanceof IntegerType && ($boundClass === IntegerType::class || $bound instanceof IntegerRangeType || $bound instanceof TemplateType)) {
if ($bound instanceof IntegerRangeType && ($boundClass === IntegerRangeType::class || $bound instanceof TemplateType)) {
return new TemplateIntegerRangeType($scope, $strategy, $variance, $name, $bound, $default);
}

if ($bound instanceof IntegerType && ($boundClass === IntegerType::class || $bound instanceof TemplateType)) {
return new TemplateIntegerType($scope, $strategy, $variance, $name, $bound, $default);
}

if ($bound instanceof ConstantIntegerType && ($boundClass === ConstantIntegerType::class || $bound instanceof TemplateType)) {
return new TemplateConstantIntegerType($scope, $strategy, $variance, $name, $bound, $default);
}

// Catches plain FloatType and the float subtypes that narrowing a bound
// produces, a FloatRangeType or a ConstantFloatType, neither of which has a
// dedicated Template* class to fall into.
if ($bound instanceof FloatType) {
if ($bound instanceof FloatRangeType && ($boundClass === FloatRangeType::class || $bound instanceof TemplateType)) {
return new TemplateFloatRangeType($scope, $strategy, $variance, $name, $bound, $default);
}

if ($bound instanceof ConstantFloatType && ($boundClass === ConstantFloatType::class || $bound instanceof TemplateType)) {
return new TemplateConstantFloatType($scope, $strategy, $variance, $name, $bound, $default);
}

if ($bound instanceof FloatType && ($boundClass === FloatType::class || $bound instanceof TemplateType)) {
return new TemplateFloatType($scope, $strategy, $variance, $name, $bound, $default);
}

Expand Down
16 changes: 15 additions & 1 deletion src/Type/IntegerRangeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantFloatType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Generic\TemplateIntegerRangeType;
use PHPStan\Type\Traits\OperatorTypeTrait;
use function array_filter;
use function array_map;
Expand All @@ -42,7 +43,7 @@ class IntegerRangeType extends IntegerType implements CompoundType

use OperatorTypeTrait;

private function __construct(private ?int $min, private ?int $max)
protected function __construct(private ?int $min, private ?int $max)
{
parent::__construct();
assert($min === null || $max === null || $min <= $max);
Expand Down Expand Up @@ -640,6 +641,11 @@ public function toString(): Type
*/
public function tryUnion(Type $otherType): ?Type
{
// a template stands for an unknown part of its range, not for all of it
if ($otherType instanceof TemplateIntegerRangeType) {
return null;
}

if ($otherType instanceof self || $otherType instanceof ConstantIntegerType) {
if ($otherType instanceof self) {
$otherMin = $otherType->min;
Expand Down Expand Up @@ -673,6 +679,10 @@ public function tryUnion(Type $otherType): ?Type
*/
public function tryIntersect(Type $otherType): ?Type
{
if ($otherType instanceof TemplateIntegerRangeType) {
return null;
}

if ($otherType instanceof self || $otherType instanceof ConstantIntegerType) {
if ($otherType instanceof self) {
$otherMin = $otherType->min;
Expand Down Expand Up @@ -722,6 +732,10 @@ public function tryRemove(Type $typeToRemove): ?Type
return new NeverType();
}

if ($typeToRemove instanceof TemplateIntegerRangeType) {
return null;
}

if ($typeToRemove instanceof self || $typeToRemove instanceof ConstantIntegerType) {
if ($typeToRemove instanceof self) {
$removeMin = $typeToRemove->min;
Expand Down
6 changes: 6 additions & 0 deletions src/Type/IntegerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Generic\TemplateIntegerRangeType;
use PHPStan\Type\Traits\NonArrayTypeTrait;
use PHPStan\Type\Traits\NonCallableTypeTrait;
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
Expand Down Expand Up @@ -188,6 +189,11 @@ public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType

public function tryRemove(Type $typeToRemove): ?Type
{
// a template stands for an unknown part of its range, not for all of it
if ($typeToRemove instanceof TemplateIntegerRangeType) {
return null;
}

if ($typeToRemove instanceof IntegerRangeType || $typeToRemove instanceof ConstantIntegerType) {
if ($typeToRemove instanceof IntegerRangeType) {
$removeValueMin = $typeToRemove->getMin();
Expand Down
Loading
Loading