Skip to content

Commit e0396cf

Browse files
committed
bring implementation up to date
1 parent cba95b2 commit e0396cf

File tree

9 files changed

+36
-5
lines changed

9 files changed

+36
-5
lines changed

src/Analyser/MutatingScope.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2539,6 +2539,7 @@ private function createFirstClassCallable(
25392539
$templateTags[$templateType->getName()] = new TemplateTag(
25402540
$templateType->getName(),
25412541
$templateType->getBound(),
2542+
$templateType->getDefault(),
25422543
$templateType->getVariance(),
25432544
);
25442545
}

src/Dependency/DependencyResolver.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,17 @@ private function addClassToDependencies(string $className, array &$dependenciesR
531531
}
532532
$dependenciesReflections[] = $this->reflectionProvider->getClass($referencedClass);
533533
}
534+
535+
$default = $templateTag->getDefault();
536+
if ($default === null) {
537+
continue;
538+
}
539+
foreach ($default->getReferencedClasses() as $referencedClass) {
540+
if (!$this->reflectionProvider->hasClass($referencedClass)) {
541+
continue;
542+
}
543+
$dependenciesReflections[] = $this->reflectionProvider->getClass($referencedClass);
544+
}
534545
}
535546

536547
foreach ($classReflection->getPropertyTags() as $propertyTag) {

src/PhpDoc/PhpDocNodeResolver.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ public function resolveMethodTags(PhpDocNode $phpDocNode, NameScope $nameScope):
176176
$templateType->bound !== null
177177
? $this->typeNodeResolver->resolve($templateType->bound, $nameScope)
178178
: new MixedType(),
179+
$templateType->default !== null
180+
? $this->typeNodeResolver->resolve($templateType->default, $nameScope)
181+
: null,
179182
TemplateTypeVariance::createInvariant(),
180183
);
181184
}

src/PhpDoc/TypeNodeResolver.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,9 @@ private function resolveCallableTypeNode(CallableTypeNode $typeNode, NameScope $
920920
$templateType->bound !== null
921921
? $this->resolve($templateType->bound, $nameScope)
922922
: new MixedType(),
923+
$templateType->default !== null
924+
? $this->resolve($templateType->default, $nameScope)
925+
: null,
923926
TemplateTypeVariance::createInvariant(),
924927
);
925928
}

src/Type/Generic/TemplateObjectShapeType.php

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

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

89
/** @api */
910
final class TemplateObjectShapeType extends ObjectShapeType implements TemplateType
@@ -22,6 +23,7 @@ public function __construct(
2223
TemplateTypeVariance $templateTypeVariance,
2324
string $name,
2425
ObjectShapeType $bound,
26+
?Type $default,
2527
)
2628
{
2729
parent::__construct($bound->getProperties(), $bound->getOptionalProperties());
@@ -30,6 +32,7 @@ public function __construct(
3032
$this->variance = $templateTypeVariance;
3133
$this->name = $name;
3234
$this->bound = $bound;
35+
$this->default = $default;
3336
}
3437

3538
protected function shouldGeneralizeInferredType(): bool

src/Type/Generic/TemplateTypeTrait.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,11 @@ public function equals(Type $type): bool
176176
return $type instanceof self
177177
&& $type->scope->equals($this->scope)
178178
&& $type->name === $this->name
179-
&& $this->bound->equals($type->bound);
179+
&& $this->bound->equals($type->bound)
180+
&& (
181+
($this->default === null && $type->default === null)
182+
|| ($this->default !== null && $type->default !== null && $this->default->equals($type->default))
183+
);
180184
}
181185

182186
public function isAcceptedBy(Type $acceptingType, bool $strictTypes): TrinaryLogic
@@ -342,6 +346,7 @@ public function traverse(callable $cb): Type
342346
$bound,
343347
$this->getVariance(),
344348
$this->getStrategy(),
349+
$default,
345350
);
346351
}
347352

@@ -352,7 +357,9 @@ public function traverseSimultaneously(Type $right, callable $cb): Type
352357
}
353358

354359
$bound = $cb($this->getBound(), $right->getBound());
355-
if ($this->getBound() === $bound) {
360+
$default = $this->getDefault() !== null && $right->getDefault() !== null ? $cb($this->getDefault(), $right->getDefault()) : null;
361+
362+
if ($this->getBound() === $bound && $this->getDefault() === $default) {
356363
return $this;
357364
}
358365

@@ -379,6 +386,7 @@ public function tryRemove(Type $typeToRemove): ?Type
379386
$bound,
380387
$this->getVariance(),
381388
$this->getStrategy(),
389+
$this->getDefault(),
382390
);
383391
}
384392

src/Type/TypeCombinator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@ private static function processArrayTypes(array $arrayTypes): array
750750
$templateArray->getVariance(),
751751
$templateArray->getName(),
752752
$arrayType,
753+
$templateArray->getDefault(),
753754
);
754755
}
755756

src/Type/TypeUtils.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ public static function toStrictUnion(Type $type): Type
292292
$type->getVariance(),
293293
$type->getName(),
294294
static::toStrictUnion($type->getBound()),
295+
$type->getDefault(),
295296
);
296297
}
297298

tests/PHPStan/Analyser/data/template-default.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function () {
6060
assertType('TemplateDefault\\Builder<true, false>', $qb);
6161
$qb->two();
6262
assertType('TemplateDefault\\Builder<true, true>', $qb);
63-
assertType('void', $qb->execute());
63+
assertType('null', $qb->execute());
6464
};
6565

6666
function () {
@@ -70,13 +70,13 @@ function () {
7070
assertType('TemplateDefault\\Builder<false, true>', $qb);
7171
$qb->one();
7272
assertType('TemplateDefault\\Builder<true, true>', $qb);
73-
assertType('void', $qb->execute());
73+
assertType('null', $qb->execute());
7474
};
7575

7676
function () {
7777
$qb = new Builder();
7878
assertType('TemplateDefault\\Builder<false, false>', $qb);
7979
$qb->one();
8080
assertType('TemplateDefault\\Builder<true, false>', $qb);
81-
assertType('*NEVER*', $qb->execute());
81+
assertType('never', $qb->execute());
8282
};

0 commit comments

Comments
 (0)