Skip to content

Commit b73a8aa

Browse files
committed
Introduce Type::getTemplateType() and deprecate GenericTypeVariableResolver
1 parent 1409717 commit b73a8aa

20 files changed

Lines changed: 139 additions & 72 deletions

phpstan-baseline.neon

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@ parameters:
3636
count: 2
3737
path: src/Analyser/MutatingScope.php
3838

39-
-
40-
message: "#^Doing instanceof PHPStan\\\\Type\\\\TypeWithClassName is error\\-prone and deprecated\\. Use Type\\:\\:getObjectClassNames\\(\\) instead\\.$#"
41-
count: 2
42-
path: src/Analyser/MutatingScope.php
43-
4439
-
4540
message: "#^Only numeric types are allowed in pre\\-decrement, bool\\|float\\|int\\|string\\|null given\\.$#"
4641
count: 1
@@ -463,16 +458,6 @@ parameters:
463458
count: 1
464459
path: src/Rules/DirectRegistry.php
465460

466-
-
467-
message: "#^Doing instanceof PHPStan\\\\Type\\\\TypeWithClassName is error\\-prone and deprecated\\. Use Type\\:\\:getObjectClassNames\\(\\) instead\\.$#"
468-
count: 1
469-
path: src/Rules/FunctionReturnTypeCheck.php
470-
471-
-
472-
message: "#^Doing instanceof PHPStan\\\\Type\\\\TypeWithClassName is error\\-prone and deprecated\\. Use Type\\:\\:getObjectClassNames\\(\\) instead\\.$#"
473-
count: 2
474-
path: src/Rules/Generators/YieldFromTypeRule.php
475-
476461
-
477462
message: "#^Function class_implements\\(\\) is a runtime reflection concept that might not work in PHPStan because it uses fully static reflection engine\\. Use objects retrieved from ReflectionProvider instead\\.$#"
478463
count: 1
@@ -508,11 +493,6 @@ parameters:
508493
count: 1
509494
path: src/Rules/Methods/MethodParameterComparisonHelper.php
510495

511-
-
512-
message: "#^Doing instanceof PHPStan\\\\Type\\\\TypeWithClassName is error\\-prone and deprecated\\. Use Type\\:\\:getObjectClassNames\\(\\) instead\\.$#"
513-
count: 1
514-
path: src/Rules/Missing/MissingReturnRule.php
515-
516496
-
517497
message: "#^Doing instanceof PHPStan\\\\Type\\\\NullType is error\\-prone and deprecated\\. Use Type\\:\\:isNull\\(\\) instead\\.$#"
518498
count: 2

src/Analyser/MutatingScope.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@
8787
use PHPStan\Type\Generic\TemplateType;
8888
use PHPStan\Type\Generic\TemplateTypeHelper;
8989
use PHPStan\Type\Generic\TemplateTypeMap;
90-
use PHPStan\Type\GenericTypeVariableResolver;
9190
use PHPStan\Type\IntegerRangeType;
9291
use PHPStan\Type\IntegerType;
9392
use PHPStan\Type\IntersectionType;
@@ -1426,25 +1425,16 @@ private function resolveType(string $exprString, Expr $node): Type
14261425
}
14271426

14281427
$returnType = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
1429-
if (!$returnType instanceof TypeWithClassName) {
1430-
return new MixedType();
1431-
}
1432-
1433-
$generatorSendType = GenericTypeVariableResolver::getType($returnType, Generator::class, 'TSend');
1434-
if ($generatorSendType === null) {
1428+
$generatorSendType = $returnType->getTemplateType(Generator::class, 'TSend');
1429+
if ($generatorSendType instanceof ErrorType) {
14351430
return new MixedType();
14361431
}
14371432

14381433
return $generatorSendType;
14391434
} elseif ($node instanceof Expr\YieldFrom) {
14401435
$yieldFromType = $this->getType($node->expr);
1441-
1442-
if (!$yieldFromType instanceof TypeWithClassName) {
1443-
return new MixedType();
1444-
}
1445-
1446-
$generatorReturnType = GenericTypeVariableResolver::getType($yieldFromType, Generator::class, 'TReturn');
1447-
if ($generatorReturnType === null) {
1436+
$generatorReturnType = $yieldFromType->getTemplateType(Generator::class, 'TReturn');
1437+
if ($generatorReturnType instanceof ErrorType) {
14481438
return new MixedType();
14491439
}
14501440

src/Rules/FunctionReturnTypeCheck.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
use PhpParser\Node;
77
use PhpParser\Node\Expr;
88
use PHPStan\Analyser\Scope;
9-
use PHPStan\Type\GenericTypeVariableResolver;
9+
use PHPStan\Type\ErrorType;
1010
use PHPStan\Type\NeverType;
1111
use PHPStan\Type\Type;
1212
use PHPStan\Type\TypeUtils;
13-
use PHPStan\Type\TypeWithClassName;
1413
use PHPStan\Type\VerbosityLevel;
1514
use PHPStan\Type\VoidType;
1615
use function sprintf;
@@ -48,16 +47,8 @@ public function checkReturnType(
4847
}
4948

5049
if ($isGenerator) {
51-
if (!$returnType instanceof TypeWithClassName) {
52-
return [];
53-
}
54-
55-
$returnType = GenericTypeVariableResolver::getType(
56-
$returnType,
57-
Generator::class,
58-
'TReturn',
59-
);
60-
if ($returnType === null) {
50+
$returnType = $returnType->getTemplateType(Generator::class, 'TReturn');
51+
if ($returnType instanceof ErrorType) {
6152
return [];
6253
}
6354
}

src/Rules/Generators/YieldFromTypeRule.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
use PHPStan\Rules\Rule;
1111
use PHPStan\Rules\RuleErrorBuilder;
1212
use PHPStan\Rules\RuleLevelHelper;
13-
use PHPStan\Type\GenericTypeVariableResolver;
13+
use PHPStan\Type\ErrorType;
1414
use PHPStan\Type\MixedType;
15-
use PHPStan\Type\TypeWithClassName;
1615
use PHPStan\Type\VerbosityLevel;
1716
use function sprintf;
1817

@@ -99,18 +98,10 @@ public function processNode(Node $node, Scope $scope): array
9998
return $messages;
10099
}
101100

102-
if (!$exprType instanceof TypeWithClassName) {
103-
return $messages;
104-
}
105-
106101
$currentReturnType = ParametersAcceptorSelector::selectSingle($scopeFunction->getVariants())->getReturnType();
107-
if (!$currentReturnType instanceof TypeWithClassName) {
108-
return $messages;
109-
}
110-
111-
$exprSendType = GenericTypeVariableResolver::getType($exprType, Generator::class, 'TSend');
112-
$thisSendType = GenericTypeVariableResolver::getType($currentReturnType, Generator::class, 'TSend');
113-
if ($exprSendType === null || $thisSendType === null) {
102+
$exprSendType = $exprType->getTemplateType(Generator::class, 'TSend');
103+
$thisSendType = $currentReturnType->getTemplateType(Generator::class, 'TSend');
104+
if ($exprSendType instanceof ErrorType || $thisSendType instanceof ErrorType) {
114105
return $messages;
115106
}
116107

src/Rules/Missing/MissingReturnRule.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@
1111
use PHPStan\Rules\Rule;
1212
use PHPStan\Rules\RuleErrorBuilder;
1313
use PHPStan\ShouldNotHappenException;
14+
use PHPStan\Type\ErrorType;
1415
use PHPStan\Type\Generic\TemplateMixedType;
15-
use PHPStan\Type\GenericTypeVariableResolver;
1616
use PHPStan\Type\MixedType;
1717
use PHPStan\Type\NeverType;
1818
use PHPStan\Type\TypeCombinator;
1919
use PHPStan\Type\TypeUtils;
20-
use PHPStan\Type\TypeWithClassName;
2120
use PHPStan\Type\VerbosityLevel;
2221
use PHPStan\Type\VoidType;
2322
use function sprintf;
@@ -74,13 +73,9 @@ public function processNode(Node $node, Scope $scope): array
7473
}
7574

7675
if ($statementResult->hasYield()) {
77-
if ($returnType instanceof TypeWithClassName && $this->checkPhpDocMissingReturn) {
78-
$generatorReturnType = GenericTypeVariableResolver::getType(
79-
$returnType,
80-
Generator::class,
81-
'TReturn',
82-
);
83-
if ($generatorReturnType !== null) {
76+
if ($this->checkPhpDocMissingReturn) {
77+
$generatorReturnType = $returnType->getTemplateType(Generator::class, 'TReturn');
78+
if (!$generatorReturnType instanceof ErrorType) {
8479
$returnType = $generatorReturnType;
8580
if ($returnType->isVoid()->yes()) {
8681
return [];

src/Type/ClosureType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ public function isObject(): TrinaryLogic
180180
return $this->objectType->isObject();
181181
}
182182

183+
public function getTemplateType(string $ancestorClassName, string $templateTypeName): Type
184+
{
185+
return $this->objectType->getTemplateType($ancestorClassName, $templateTypeName);
186+
}
187+
183188
public function canAccessProperties(): TrinaryLogic
184189
{
185190
return $this->objectType->canAccessProperties();

src/Type/GenericTypeVariableResolver.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
class GenericTypeVariableResolver
99
{
1010

11+
/**
12+
* @deprecated Use Type::getTemplateType() instead.
13+
*/
1114
public static function getType(
1215
TypeWithClassName $type,
1316
string $genericClassName,

src/Type/IntersectionType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,11 @@ private function describeItself(VerbosityLevel $level, bool $skipAccessoryTypes)
388388
return implode('&', $describedTypes);
389389
}
390390

391+
public function getTemplateType(string $ancestorClassName, string $templateTypeName): Type
392+
{
393+
return $this->intersectTypes(static fn (Type $type): Type => $type->getTemplateType($ancestorClassName, $templateTypeName));
394+
}
395+
391396
public function isObject(): TrinaryLogic
392397
{
393398
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isObject());

src/Type/MixedType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,11 @@ public function isAcceptedWithReasonBy(Type $acceptingType, bool $strictTypes):
315315
return AcceptsResult::createYes();
316316
}
317317

318+
public function getTemplateType(string $ancestorClassName, string $templateTypeName): Type
319+
{
320+
return new self();
321+
}
322+
318323
public function isObject(): TrinaryLogic
319324
{
320325
if ($this->subtractedType !== null) {

src/Type/NeverType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ public function describe(VerbosityLevel $level): string
110110
return '*NEVER*';
111111
}
112112

113+
public function getTemplateType(string $ancestorClassName, string $templateTypeName): Type
114+
{
115+
return new NeverType();
116+
}
117+
113118
public function isObject(): TrinaryLogic
114119
{
115120
return TrinaryLogic::createNo();

0 commit comments

Comments
 (0)