Skip to content
Open
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
60 changes: 54 additions & 6 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -624,12 +624,6 @@ parameters:
count: 1
path: src/Rules/Generics/GenericAncestorsCheck.php

-
rawMessage: Doing instanceof PHPStan\Type\IntersectionType is error-prone and deprecated.
identifier: phpstanApi.instanceofType
count: 1
path: src/Rules/Generics/TemplateTypeCheck.php

-
rawMessage: 'Method PHPStan\Rules\LazyRegistry::getRulesByNodeType() return type with generic interface PHPStan\Rules\Rule does not specify its types: TNodeType'
identifier: missingType.generics
Expand Down Expand Up @@ -1146,12 +1140,42 @@ parameters:
count: 3
path: src/Type/Generic/TemplateBooleanType.php

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

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

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

-
rawMessage: Doing instanceof PHPStan\Type\IntersectionType is error-prone and deprecated.
identifier: phpstanApi.instanceofType
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 @@ -1176,6 +1200,12 @@ parameters:
count: 3
path: src/Type/Generic/TemplateFloatType.php

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

-
rawMessage: Doing instanceof PHPStan\Type\IntersectionType is error-prone and deprecated.
identifier: phpstanApi.instanceofType
Expand Down Expand Up @@ -1260,6 +1290,18 @@ parameters:
count: 1
path: src/Type/Generic/TemplateTypeFactory.php

-
rawMessage: 'Doing instanceof PHPStan\Type\CallableType is error-prone and deprecated. Use Type::isCallable() and Type::getCallableParametersAcceptors() instead.'
identifier: phpstanApi.instanceofType
count: 1
path: src/Type/Generic/TemplateTypeFactory.php

-
rawMessage: 'Doing instanceof PHPStan\Type\ClassStringType is error-prone and deprecated. Use Type::isClassStringType() instead.'
identifier: phpstanApi.instanceofType
count: 1
path: src/Type/Generic/TemplateTypeFactory.php

-
rawMessage: 'Doing instanceof PHPStan\Type\Constant\ConstantArrayType is error-prone and deprecated. Use Type::getConstantArrays() instead.'
identifier: phpstanApi.instanceofType
Expand All @@ -1278,6 +1320,12 @@ parameters:
count: 1
path: src/Type/Generic/TemplateTypeFactory.php

-
rawMessage: 'Doing instanceof PHPStan\Type\Generic\GenericClassStringType is error-prone and deprecated. Use Type::isClassStringType() and Type::getClassStringObjectType() instead.'
identifier: phpstanApi.instanceofType
count: 1
path: src/Type/Generic/TemplateTypeFactory.php

-
rawMessage: Doing instanceof PHPStan\Type\Generic\GenericObjectType is error-prone and deprecated.
identifier: phpstanApi.instanceofType
Expand Down
47 changes: 5 additions & 42 deletions src/Rules/Generics/TemplateTypeCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,12 @@
use PHPStan\Rules\ClassNameUsageLocation;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\FloatType;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\Generic\TemplateTypeFactory;
use PHPStan\Type\Generic\TemplateTypeScope;
use PHPStan\Type\IntegerType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\IterableType;
use PHPStan\Type\KeyOfType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectShapeType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\StringType;
use PHPStan\Type\TypeAliasResolver;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
use function array_map;
use function array_merge;
use function get_class;
use function sprintf;

#[AutowiredService]
Expand Down Expand Up @@ -116,28 +97,10 @@ public function check(
'templateTagName' => $templateTagName,
]), $this->checkClassCaseSensitivity));

$boundTypeClass = get_class($boundType);
if (
$boundTypeClass !== MixedType::class
&& $boundTypeClass !== ConstantArrayType::class
&& $boundTypeClass !== ArrayType::class
&& $boundTypeClass !== ConstantStringType::class
&& $boundTypeClass !== StringType::class
&& $boundTypeClass !== ConstantIntegerType::class
&& $boundTypeClass !== IntegerType::class
&& $boundTypeClass !== FloatType::class
&& $boundTypeClass !== BooleanType::class
&& $boundTypeClass !== ObjectWithoutClassType::class
&& $boundTypeClass !== ObjectType::class
&& $boundTypeClass !== ObjectShapeType::class
&& $boundTypeClass !== GenericObjectType::class
&& $boundTypeClass !== KeyOfType::class
&& $boundTypeClass !== IterableType::class
&& $boundTypeClass !== NullType::class
&& !$boundType instanceof UnionType
&& !$boundType instanceof IntersectionType
&& !$boundType instanceof TemplateType
) {
// TemplateTypeFactory silently widens bounds it has no Template* class for
// to `mixed`. Ask it instead of maintaining a second list of supported
// bound types that can drift out of sync with it.
if (TemplateTypeFactory::fromTemplateTag($templateTypeScope, $templateTag)->getBound() !== $boundType) {
$messages[] = RuleErrorBuilder::message(sprintf($notSupportedBoundMessage, $templateTagName, $boundType->describe(VerbosityLevel::typeOnly())))
->identifier('generics.notSupportedBound')
->build();
Expand Down
47 changes: 47 additions & 0 deletions src/Type/Generic/TemplateCallableType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Generic;

use PHPStan\Type\CallableType;
use PHPStan\Type\Type;

/** @api */
final class TemplateCallableType extends CallableType implements TemplateType
{

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

/**
* @param non-empty-string $name
*/
public function __construct(
TemplateTypeScope $scope,
TemplateTypeStrategy $templateTypeStrategy,
TemplateTypeVariance $templateTypeVariance,
string $name,
CallableType $bound,
?Type $default,
)
{
$isCommonCallable = $bound->isCommonCallable();
parent::__construct(
$isCommonCallable ? null : $bound->getParameters(),
$isCommonCallable ? null : $bound->getReturnType(),
$bound->isVariadic(),
$bound->getTemplateTypeMap(),
$bound->getResolvedTemplateTypeMap(),
$bound->getTemplateTags(),
$bound->isPure(),
$bound->getAsserts(),
);

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

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

namespace PHPStan\Type\Generic;

use PHPStan\Type\ClassStringType;
use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
use PHPStan\Type\Type;

/** @api */
final class TemplateClassStringType extends ClassStringType implements TemplateType
{

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

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

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

namespace PHPStan\Type\Generic;

use PHPStan\Type\ClosureType;
use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
use PHPStan\Type\Type;

/** @api */
final class TemplateClosureType extends ClosureType implements TemplateType
{

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

/**
* @param non-empty-string $name
*/
public function __construct(
TemplateTypeScope $scope,
TemplateTypeStrategy $templateTypeStrategy,
TemplateTypeVariance $templateTypeVariance,
string $name,
ClosureType $bound,
?Type $default,
)
{
$isCommonCallable = $bound->isCommonCallable();
parent::__construct(
$isCommonCallable ? null : $bound->getParameters(),
$isCommonCallable ? null : $bound->getReturnType(),
$bound->isVariadic(),
$bound->getTemplateTypeMap(),
$bound->getResolvedTemplateTypeMap(),
$bound->getCallSiteVarianceMap(),
$bound->getTemplateTags(),
$bound->getThrowPoints(),
$bound->getImpurePoints(),
$bound->getInvalidateExpressions(),
$bound->getUsedVariables(),
$bound->acceptsNamedArguments(),
$bound->mustUseReturnValue(),
$bound->getAsserts(),
$bound->isStaticClosure(),
);

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

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

namespace PHPStan\Type\Generic;

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

/** @api */
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;
}

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

namespace PHPStan\Type\Generic;

use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
use PHPStan\Type\Type;

/** @api */
final class TemplateGenericClassStringType extends GenericClassStringType implements TemplateType
{

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

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

}
Loading
Loading