Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b798793

Browse files
Implement template default types
1 parent 2727758 commit b798793

33 files changed

+199
-40
lines changed

‎composer.json‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"ondram/ci-detector": "^3.4.0",
2525
"ondrejmirtes/better-reflection": "6.1.0",
2626
"phpstan/php-8-stubs": "0.3.38",
27-
"phpstan/phpdoc-parser": "1.9.0",
27+
"phpstan/phpdoc-parser": "dev-template-default as 1.10.1",
2828
"react/async": "^3",
2929
"react/child-process": "^0.6.4",
3030
"react/event-loop": "^1.2",
@@ -99,5 +99,11 @@
9999
"prefer-stable": true,
100100
"bin": [
101101
"bin/phpstan"
102+
],
103+
"repositories": [
104+
{
105+
"type": "vcs",
106+
"url": "https://github.com/rvanvelzen/phpdoc-parser.git"
107+
}
102108
]
103109
}

‎composer.lock‎

Lines changed: 25 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/Analyser/MutatingScope.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5169,6 +5169,11 @@ private function exactInstantiation(New_ $node, string $className): ?Type
51695169
$list[] = $templateType;
51705170
continue;
51715171
}
5172+
$default = $tag->getDefault();
5173+
if ($default !== null) {
5174+
$list[] = $default;
5175+
continue;
5176+
}
51725177
$bound = $tag->getBound();
51735178
if ($bound instanceof MixedType && $bound->isExplicitMixed()) {
51745179
$bound = new MixedType(false);

‎src/PhpDoc/PhpDocNodeResolver.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,12 @@ public function resolveTemplateTags(PhpDocNode $phpDocNode, NameScope $nameScope
282282
}
283283
}
284284

285+
$nameScopeWithoutCurrent = $nameScope->unsetTemplateType($valueNode->name);
286+
285287
$resolved[$valueNode->name] = new TemplateTag(
286288
$valueNode->name,
287-
$valueNode->bound !== null ? $this->typeNodeResolver->resolve($valueNode->bound, $nameScope->unsetTemplateType($valueNode->name)) : new MixedType(true),
289+
$valueNode->bound !== null ? $this->typeNodeResolver->resolve($valueNode->bound, $nameScopeWithoutCurrent) : new MixedType(true),
290+
$valueNode->default !== null ? $this->typeNodeResolver->resolve($valueNode->default, $nameScopeWithoutCurrent) : null,
288291
$variance,
289292
);
290293
$resolvedPrefix[$valueNode->name] = $prefix;

‎src/PhpDoc/Tag/TemplateTag.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class TemplateTag
1010
{
1111

12-
public function __construct(private string $name, private Type $bound, private TemplateTypeVariance $variance)
12+
public function __construct(private string $name, private Type $bound, private ?Type$default, privateTemplateTypeVariance $variance)
1313
{
1414
}
1515

@@ -23,6 +23,11 @@ public function getBound(): Type
2323
return $this->bound;
2424
}
2525

26+
public function getDefault(): ?Type
27+
{
28+
return $this->default;
29+
}
30+
2631
public function getVariance(): TemplateTypeVariance
2732
{
2833
return $this->variance;

‎src/Reflection/ClassReflection.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,7 @@ public function typeMapFromList(array $types): TemplateTypeMap
12551255
$map = [];
12561256
$i = 0;
12571257
foreach ($resolvedPhpDoc->getTemplateTags() as $tag) {
1258-
$map[$tag->getName()] = $types[$i] ?? new ErrorType();
1258+
$map[$tag->getName()] = $types[$i] ?? $tag->getDefault() ?? new ErrorType();
12591259
$i++;
12601260
}
12611261

@@ -1272,7 +1272,7 @@ public function typeMapToList(TemplateTypeMap $typeMap): array
12721272

12731273
$list = [];
12741274
foreach ($resolvedPhpDoc->getTemplateTags() as $tag) {
1275-
$list[] = $typeMap->getType($tag->getName()) ?? $tag->getBound();
1275+
$list[] = $typeMap->getType($tag->getName()) ?? $tag->getDefault() ?? $tag->getBound();
12761276
}
12771277

12781278
return $list;

‎src/Rules/FunctionCallParametersCheck.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ static function (Type $type, callable $traverse) use (&$returnTemplateTypes): Ty
359359
$type = $type->resolve();
360360
}
361361

362-
if ($type instanceof TemplateType) {
362+
if ($type instanceof TemplateType && $type->getDefault() === null) {
363363
$returnTemplateTypes[$type->getName()] = true;
364364
return $type;
365365
}

‎src/Type/Generic/TemplateArrayType.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHPStan\Type\ArrayType;
66
use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
7+
use PHPStan\Type\Type;
78

89
/** @api */
910
final class TemplateArrayType extends ArrayType implements TemplateType
@@ -19,6 +20,7 @@ public function __construct(
1920
TemplateTypeVariance $templateTypeVariance,
2021
string $name,
2122
ArrayType $bound,
23+
?Type $default,
2224
)
2325
{
2426
parent::__construct($bound->getKeyType(), $bound->getItemType());
@@ -27,6 +29,7 @@ public function __construct(
2729
$this->variance = $templateTypeVariance;
2830
$this->name = $name;
2931
$this->bound = $bound;
32+
$this->default = $default;
3033
}
3134

3235
protected function shouldGeneralizeInferredType(): bool

‎src/Type/Generic/TemplateBenevolentUnionType.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function __construct(
1818
TemplateTypeVariance $templateTypeVariance,
1919
string $name,
2020
BenevolentUnionType $bound,
21+
?Type $default,
2122
)
2223
{
2324
parent::__construct($bound->getTypes());
@@ -27,6 +28,7 @@ public function __construct(
2728
$this->variance = $templateTypeVariance;
2829
$this->name = $name;
2930
$this->bound = $bound;
31+
$this->default = $default;
3032
}
3133

3234
/** @param Type[] $types */
@@ -38,6 +40,7 @@ public function withTypes(array $types): self
3840
$this->variance,
3941
$this->name,
4042
new BenevolentUnionType($types),
43+
$this->default,
4144
);
4245
}
4346

‎src/Type/Generic/TemplateBooleanType.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHPStan\Type\BooleanType;
66
use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
7+
use PHPStan\Type\Type;
78

89
/** @api */
910
final class TemplateBooleanType extends BooleanType implements TemplateType
@@ -19,6 +20,7 @@ public function __construct(
1920
TemplateTypeVariance $templateTypeVariance,
2021
string $name,
2122
BooleanType $bound,
23+
?Type $default,
2224
)
2325
{
2426
parent::__construct();
@@ -27,6 +29,7 @@ public function __construct(
2729
$this->variance = $templateTypeVariance;
2830
$this->name = $name;
2931
$this->bound = $bound;
32+
$this->default = $default;
3033
}
3134

3235
protected function shouldGeneralizeInferredType(): bool

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /