Skip to content

Commit

Permalink
Template tag name cannot be empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 31, 2024
1 parent 745e99e commit 08dbe82
Show file tree
Hide file tree
Showing 31 changed files with 160 additions and 65 deletions.
6 changes: 6 additions & 0 deletions src/PhpDoc/Tag/TemplateTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@
class TemplateTag
{

/**
* @param non-empty-string $name
*/
public function __construct(private string $name, private Type $bound, private TemplateTypeVariance $variance)
{
}

/**
* @return non-empty-string
*/
public function getName(): string
{
return $this->name;
Expand Down
4 changes: 2 additions & 2 deletions src/Type/CallableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class CallableType implements CompoundType, CallableParametersAcceptor
/**
* @api
* @param array<int, ParameterReflection>|null $parameters
* @param array<string, TemplateTag> $templateTags
* @param array<non-empty-string, TemplateTag> $templateTags
*/
public function __construct(
?array $parameters = null,
Expand All @@ -89,7 +89,7 @@ public function __construct(
}

/**
* @return array<string, TemplateTag>
* @return array<non-empty-string, TemplateTag>
*/
public function getTemplateTags(): array
{
Expand Down
4 changes: 2 additions & 2 deletions src/Type/ClosureType.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class ClosureType implements TypeWithClassName, CallableParametersAcceptor
/**
* @api
* @param array<int, ParameterReflection>|null $parameters
* @param array<string, TemplateTag> $templateTags
* @param array<non-empty-string, TemplateTag> $templateTags
* @param SimpleThrowPoint[] $throwPoints
* @param SimpleImpurePoint[] $impurePoints
* @param InvalidateExprNode[] $invalidateExpressions
Expand Down Expand Up @@ -108,7 +108,7 @@ public function __construct(
}

/**
* @return array<string, TemplateTag>
* @return array<non-empty-string, TemplateTag>
*/
public function getTemplateTags(): array
{
Expand Down
3 changes: 3 additions & 0 deletions src/Type/Generic/TemplateArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ final class TemplateArrayType extends ArrayType implements TemplateType
use TemplateTypeTrait;
use UndecidedComparisonCompoundTypeTrait;

/**
* @param non-empty-string $name
*/
public function __construct(
TemplateTypeScope $scope,
TemplateTypeStrategy $templateTypeStrategy,
Expand Down
3 changes: 3 additions & 0 deletions src/Type/Generic/TemplateBenevolentUnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ final class TemplateBenevolentUnionType extends BenevolentUnionType implements T
/** @use TemplateTypeTrait<BenevolentUnionType> */
use TemplateTypeTrait;

/**
* @param non-empty-string $name
*/
public function __construct(
TemplateTypeScope $scope,
TemplateTypeStrategy $templateTypeStrategy,
Expand Down
3 changes: 3 additions & 0 deletions src/Type/Generic/TemplateBooleanType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ final class TemplateBooleanType extends BooleanType implements TemplateType
use TemplateTypeTrait;
use UndecidedComparisonCompoundTypeTrait;

/**
* @param non-empty-string $name
*/
public function __construct(
TemplateTypeScope $scope,
TemplateTypeStrategy $templateTypeStrategy,
Expand Down
3 changes: 3 additions & 0 deletions src/Type/Generic/TemplateConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ final class TemplateConstantArrayType extends ConstantArrayType implements Templ
use TemplateTypeTrait;
use UndecidedComparisonCompoundTypeTrait;

/**
* @param non-empty-string $name
*/
public function __construct(
TemplateTypeScope $scope,
TemplateTypeStrategy $templateTypeStrategy,
Expand Down
3 changes: 3 additions & 0 deletions src/Type/Generic/TemplateConstantIntegerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ final class TemplateConstantIntegerType extends ConstantIntegerType implements T
use TemplateTypeTrait;
use UndecidedComparisonCompoundTypeTrait;

/**
* @param non-empty-string $name
*/
public function __construct(
TemplateTypeScope $scope,
TemplateTypeStrategy $templateTypeStrategy,
Expand Down
3 changes: 3 additions & 0 deletions src/Type/Generic/TemplateConstantStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ final class TemplateConstantStringType extends ConstantStringType implements Tem
use TemplateTypeTrait;
use UndecidedComparisonCompoundTypeTrait;

/**
* @param non-empty-string $name
*/
public function __construct(
TemplateTypeScope $scope,
TemplateTypeStrategy $templateTypeStrategy,
Expand Down
3 changes: 3 additions & 0 deletions src/Type/Generic/TemplateFloatType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ final class TemplateFloatType extends FloatType implements TemplateType
use TemplateTypeTrait;
use UndecidedComparisonCompoundTypeTrait;

/**
* @param non-empty-string $name
*/
public function __construct(
TemplateTypeScope $scope,
TemplateTypeStrategy $templateTypeStrategy,
Expand Down
3 changes: 3 additions & 0 deletions src/Type/Generic/TemplateGenericObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ final class TemplateGenericObjectType extends GenericObjectType implements Templ
/** @use TemplateTypeTrait<GenericObjectType> */
use TemplateTypeTrait;

/**
* @param non-empty-string $name
*/
public function __construct(
TemplateTypeScope $scope,
TemplateTypeStrategy $templateTypeStrategy,
Expand Down
3 changes: 3 additions & 0 deletions src/Type/Generic/TemplateIntegerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ final class TemplateIntegerType extends IntegerType implements TemplateType
use TemplateTypeTrait;
use UndecidedComparisonCompoundTypeTrait;

/**
* @param non-empty-string $name
*/
public function __construct(
TemplateTypeScope $scope,
TemplateTypeStrategy $templateTypeStrategy,
Expand Down
3 changes: 3 additions & 0 deletions src/Type/Generic/TemplateIntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ final class TemplateIntersectionType extends IntersectionType implements Templat
/** @use TemplateTypeTrait<IntersectionType> */
use TemplateTypeTrait;

/**
* @param non-empty-string $name
*/
public function __construct(
TemplateTypeScope $scope,
TemplateTypeStrategy $templateTypeStrategy,
Expand Down
3 changes: 3 additions & 0 deletions src/Type/Generic/TemplateKeyOfType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ final class TemplateKeyOfType extends KeyOfType implements TemplateType
use TemplateTypeTrait;
use UndecidedComparisonCompoundTypeTrait;

/**
* @param non-empty-string $name
*/
public function __construct(
TemplateTypeScope $scope,
TemplateTypeStrategy $templateTypeStrategy,
Expand Down
3 changes: 3 additions & 0 deletions src/Type/Generic/TemplateMixedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ final class TemplateMixedType extends MixedType implements TemplateType
/** @use TemplateTypeTrait<MixedType> */
use TemplateTypeTrait;

/**
* @param non-empty-string $name
*/
public function __construct(
TemplateTypeScope $scope,
TemplateTypeStrategy $templateTypeStrategy,
Expand Down
3 changes: 3 additions & 0 deletions src/Type/Generic/TemplateObjectShapeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ final class TemplateObjectShapeType extends ObjectShapeType implements TemplateT
use TemplateTypeTrait;
use UndecidedComparisonCompoundTypeTrait;

/**
* @param non-empty-string $name
*/
public function __construct(
TemplateTypeScope $scope,
TemplateTypeStrategy $templateTypeStrategy,
Expand Down
3 changes: 3 additions & 0 deletions src/Type/Generic/TemplateObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ final class TemplateObjectType extends ObjectType implements TemplateType
/** @use TemplateTypeTrait<ObjectType> */
use TemplateTypeTrait;

/**
* @param non-empty-string $name
*/
public function __construct(
TemplateTypeScope $scope,
TemplateTypeStrategy $templateTypeStrategy,
Expand Down
3 changes: 3 additions & 0 deletions src/Type/Generic/TemplateObjectWithoutClassType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class TemplateObjectWithoutClassType extends ObjectWithoutClassType implements T
/** @use TemplateTypeTrait<ObjectWithoutClassType> */
use TemplateTypeTrait;

/**
* @param non-empty-string $name
*/
public function __construct(
TemplateTypeScope $scope,
TemplateTypeStrategy $templateTypeStrategy,
Expand Down
3 changes: 3 additions & 0 deletions src/Type/Generic/TemplateStrictMixedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ final class TemplateStrictMixedType extends StrictMixedType implements TemplateT
/** @use TemplateTypeTrait<StrictMixedType> */
use TemplateTypeTrait;

/**
* @param non-empty-string $name
*/
public function __construct(
TemplateTypeScope $scope,
TemplateTypeStrategy $templateTypeStrategy,
Expand Down
3 changes: 3 additions & 0 deletions src/Type/Generic/TemplateStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ final class TemplateStringType extends StringType implements TemplateType
use TemplateTypeTrait;
use UndecidedComparisonCompoundTypeTrait;

/**
* @param non-empty-string $name
*/
public function __construct(
TemplateTypeScope $scope,
TemplateTypeStrategy $templateTypeStrategy,
Expand Down
1 change: 1 addition & 0 deletions src/Type/Generic/TemplateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
interface TemplateType extends CompoundType
{

/** @return non-empty-string */
public function getName(): string;

public function getScope(): TemplateTypeScope;
Expand Down
3 changes: 3 additions & 0 deletions src/Type/Generic/TemplateTypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
final class TemplateTypeFactory
{

/**
* @param non-empty-string $name
*/
public static function create(TemplateTypeScope $scope, string $name, ?Type $bound, TemplateTypeVariance $variance, ?TemplateTypeStrategy $strategy = null): TemplateType
{
$strategy ??= new TemplateTypeParameterStrategy();
Expand Down
2 changes: 2 additions & 0 deletions src/Type/Generic/TemplateTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
trait TemplateTypeTrait
{

/** @var non-empty-string */
private string $name;

private TemplateTypeScope $scope;
Expand All @@ -36,6 +37,7 @@ trait TemplateTypeTrait
/** @var TBound */
private Type $bound;

/** @return non-empty-string */
public function getName(): string
{
return $this->name;
Expand Down
3 changes: 3 additions & 0 deletions src/Type/Generic/TemplateUnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ final class TemplateUnionType extends UnionType implements TemplateType
/** @use TemplateTypeTrait<UnionType> */
use TemplateTypeTrait;

/**
* @param non-empty-string $name
*/
public function __construct(
TemplateTypeScope $scope,
TemplateTypeStrategy $templateTypeStrategy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ class GenericParametersAcceptorResolverTest extends PHPStanTestCase
*/
public function dataResolve(): array
{
$templateType = static fn (string $name, ?Type $type = null): Type => TemplateTypeFactory::create(
TemplateTypeScope::createWithFunction('a'),
$name,
$type,
TemplateTypeVariance::createInvariant(),
);
$templateType = static function (string $name, ?Type $type = null): Type {
/** @var non-empty-string $name */
return TemplateTypeFactory::create(
TemplateTypeScope::createWithFunction('a'),
$name,
$type,
TemplateTypeVariance::createInvariant(),
);
};

return [
'one param, one arg' => [
Expand Down
15 changes: 9 additions & 6 deletions tests/PHPStan/Type/ArrayTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,15 @@ public function testDescribe(

public function dataInferTemplateTypes(): array
{
$templateType = static fn (string $name): Type => TemplateTypeFactory::create(
TemplateTypeScope::createWithFunction('a'),
$name,
new MixedType(),
TemplateTypeVariance::createInvariant(),
);
$templateType = static function (string $name): Type {
/** @var non-empty-string $name */
return TemplateTypeFactory::create(
TemplateTypeScope::createWithFunction('a'),
$name,
new MixedType(),
TemplateTypeVariance::createInvariant(),
);
};

return [
'valid templated item' => [
Expand Down
15 changes: 9 additions & 6 deletions tests/PHPStan/Type/CallableTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,15 @@ public function dataInferTemplateTypes(): array
null,
);

$templateType = static fn (string $name): Type => TemplateTypeFactory::create(
TemplateTypeScope::createWithFunction('a'),
$name,
new MixedType(),
TemplateTypeVariance::createInvariant(),
);
$templateType = static function (string $name): Type {
/** @var non-empty-string $name */
return TemplateTypeFactory::create(
TemplateTypeScope::createWithFunction('a'),
$name,
new MixedType(),
TemplateTypeVariance::createInvariant(),
);
};

return [
'template param' => [
Expand Down
15 changes: 9 additions & 6 deletions tests/PHPStan/Type/Constant/ConstantArrayTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -610,12 +610,15 @@ public function testIsSuperTypeOf(ConstantArrayType $type, Type $otherType, Trin

public function dataInferTemplateTypes(): array
{
$templateType = static fn (string $name): Type => TemplateTypeFactory::create(
TemplateTypeScope::createWithFunction('a'),
$name,
new MixedType(),
TemplateTypeVariance::createInvariant(),
);
$templateType = static function (string $name): Type {
/** @var non-empty-string $name */
return TemplateTypeFactory::create(
TemplateTypeScope::createWithFunction('a'),
$name,
new MixedType(),
TemplateTypeVariance::createInvariant(),
);
};

return [
'receive constant array' => [
Expand Down
30 changes: 18 additions & 12 deletions tests/PHPStan/Type/Generic/GenericObjectTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,12 +351,15 @@ public function testAccepts(
/** @return array<string,array{Type,Type,array<string,string>}> */
public function dataInferTemplateTypes(): array
{
$templateType = static fn (string $name, ?Type $bound = null): Type => TemplateTypeFactory::create(
TemplateTypeScope::createWithFunction('a'),
$name,
$bound ?? new MixedType(),
TemplateTypeVariance::createInvariant(),
);
$templateType = static function (string $name, ?Type $bound = null): Type {
/** @var non-empty-string $name */
return TemplateTypeFactory::create(
TemplateTypeScope::createWithFunction('a'),
$name,
$bound ?? new MixedType(),
TemplateTypeVariance::createInvariant(),
);
};

return [
'simple' => [
Expand Down Expand Up @@ -464,12 +467,15 @@ public function testResolveTemplateTypes(Type $received, Type $template, array $
/** @return array<array{TemplateTypeVariance,Type,bool,array<TemplateTypeReference>}> */
public function dataGetReferencedTypeArguments(): array
{
$templateType = static fn (string $name, ?Type $bound = null): TemplateType => TemplateTypeFactory::create(
TemplateTypeScope::createWithFunction('a'),
$name,
$bound ?? new MixedType(),
TemplateTypeVariance::createInvariant(),
);
$templateType = static function (string $name, ?Type $bound = null): Type {
/** @var non-empty-string $name */
return TemplateTypeFactory::create(
TemplateTypeScope::createWithFunction('a'),
$name,
$bound ?? new MixedType(),
TemplateTypeVariance::createInvariant(),
);
};

return [
'param: Invariant<T>' => [
Expand Down
Loading

0 comments on commit 08dbe82

Please sign in to comment.