Skip to content

Commit ebbfb85

Browse files
committed
Revert "Adjust rules for GeneratorScope"
This reverts commit 745d02b.
1 parent 482c092 commit ebbfb85

File tree

9 files changed

+22
-112
lines changed

9 files changed

+22
-112
lines changed

src/Analyser/MutatingScope.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
use PHPStan\Node\Expr\GetIterableKeyTypeExpr;
3434
use PHPStan\Node\Expr\GetIterableValueTypeExpr;
3535
use PHPStan\Node\Expr\GetOffsetValueTypeExpr;
36-
use PHPStan\Node\Expr\IdentifiedTypeExpr;
3736
use PHPStan\Node\Expr\IntertwinedVariableByReferenceWithExpr;
3837
use PHPStan\Node\Expr\NativeTypeExpr;
3938
use PHPStan\Node\Expr\OriginalForeachKeyExpr;
@@ -895,10 +894,6 @@ private function resolveType(string $exprString, Expr $node): Type
895894
return $this->expressionTypes[$exprString]->getType();
896895
}
897896

898-
if ($node instanceof IdentifiedTypeExpr) {
899-
return $node->getExprType();
900-
}
901-
902897
if ($node instanceof AlwaysRememberedExpr) {
903898
return $this->nativeTypesPromoted ? $node->getNativeExprType() : $node->getExprType();
904899
}

src/Node/Expr/IdentifiedTypeExpr.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/Node/Printer/Printer.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use PHPStan\Node\Expr\GetIterableKeyTypeExpr;
1111
use PHPStan\Node\Expr\GetIterableValueTypeExpr;
1212
use PHPStan\Node\Expr\GetOffsetValueTypeExpr;
13-
use PHPStan\Node\Expr\IdentifiedTypeExpr;
1413
use PHPStan\Node\Expr\IntertwinedVariableByReferenceWithExpr;
1514
use PHPStan\Node\Expr\NativeTypeExpr;
1615
use PHPStan\Node\Expr\OriginalForeachKeyExpr;
@@ -42,11 +41,6 @@ protected function pPHPStan_Node_NativeTypeExpr(NativeTypeExpr $expr): string //
4241
return sprintf('__phpstanNativeType(%s, %s)', $expr->getPhpDocType()->describe(VerbosityLevel::precise()), $expr->getNativeType()->describe(VerbosityLevel::precise()));
4342
}
4443

45-
protected function pPHPStan_Node_IdentifiedTypeExpr(IdentifiedTypeExpr $expr): string // phpcs:ignore
46-
{
47-
return sprintf('__phpstanIdentifiedType(%s, %s)', $this->p($expr->expr), $expr->getExprType()->describe(VerbosityLevel::precise()));
48-
}
49-
5044
protected function pPHPStan_Node_GetOffsetValueTypeExpr(GetOffsetValueTypeExpr $expr): string // phpcs:ignore
5145
{
5246
return sprintf('__phpstanGetOffsetValueType(%s, %s)', $this->p($expr->getVar()), $this->p($expr->getDim()));

src/Reflection/ParametersAcceptorSelector.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Closure;
66
use PhpParser\Node;
77
use PHPStan\Analyser\ArgumentsNormalizer;
8-
use PHPStan\Analyser\Generator\GeneratorScope;
98
use PHPStan\Analyser\MutatingScope;
109
use PHPStan\Analyser\Scope;
1110
use PHPStan\Node\Expr\ParameterVariableOriginalValueExpr;
@@ -498,13 +497,13 @@ public static function selectFromArgs(
498497
}
499498
}
500499

501-
if ($parameter !== null && ($scope instanceof MutatingScope || $scope instanceof GeneratorScope)) {
500+
if ($parameter !== null && $scope instanceof MutatingScope) {
502501
$scope = $scope->pushInFunctionCall(null, $parameter);
503502
}
504503

505504
$type = $scope->getType($originalArg->value);
506505

507-
if ($parameter !== null && ($scope instanceof MutatingScope || $scope instanceof GeneratorScope)) {
506+
if ($parameter !== null && $scope instanceof MutatingScope) {
508507
$scope = $scope->popInFunctionCall();
509508
}
510509

src/Rules/Comparison/ImpossibleCheckTypeHelper.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use PhpParser\Node\Expr\FuncCall;
99
use PhpParser\Node\Expr\MethodCall;
1010
use PhpParser\Node\Expr\StaticCall;
11-
use PHPStan\Analyser\Generator\GeneratorScope;
1211
use PHPStan\Analyser\MutatingScope;
1312
use PHPStan\Analyser\Scope;
1413
use PHPStan\Analyser\TypeSpecifier;
@@ -249,7 +248,7 @@ public function findSpecifiedType(
249248
}
250249
}
251250

252-
if (!$scope instanceof MutatingScope && !$scope instanceof GeneratorScope) {
251+
if (!$scope instanceof MutatingScope) {
253252
throw new ShouldNotHappenException();
254253
}
255254

src/Rules/Comparison/StrictComparisonOfDifferentTypesRule.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace PHPStan\Rules\Comparison;
44

55
use PhpParser\Node;
6-
use PHPStan\Analyser\Generator\GeneratorScope;
76
use PHPStan\Analyser\MutatingScope;
87
use PHPStan\Analyser\RicherScopeGetTypeHelper;
98
use PHPStan\Analyser\Scope;
@@ -45,7 +44,7 @@ public function getNodeType(): string
4544

4645
public function processNode(Node $node, Scope $scope): array
4746
{
48-
if (!$scope instanceof MutatingScope && !$scope instanceof GeneratorScope) {
47+
if (!$scope instanceof MutatingScope) {
4948
throw new ShouldNotHappenException();
5049
}
5150

src/Rules/Debug/DebugScopeRule.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace PHPStan\Rules\Debug;
44

55
use PhpParser\Node;
6-
use PHPStan\Analyser\Generator\GeneratorScope;
76
use PHPStan\Analyser\MutatingScope;
87
use PHPStan\Analyser\Scope;
98
use PHPStan\DependencyInjection\AutowiredService;
@@ -46,7 +45,7 @@ public function processNode(Node $node, Scope $scope): array
4645
return [];
4746
}
4847

49-
if (!$scope instanceof MutatingScope && !$scope instanceof GeneratorScope) {
48+
if (!$scope instanceof MutatingScope) {
5049
return [];
5150
}
5251

src/Rules/FunctionCallParametersCheck.php

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

55
use PhpParser\Node;
66
use PhpParser\Node\Expr;
7-
use PHPStan\Analyser\Generator\GeneratorScope;
87
use PHPStan\Analyser\MutatingScope;
98
use PHPStan\Analyser\Scope;
109
use PHPStan\DependencyInjection\AutowiredParameter;
@@ -319,12 +318,12 @@ public function check(
319318
}
320319

321320
if ($argumentValueType === null) {
322-
if ($scope instanceof MutatingScope || $scope instanceof GeneratorScope) {
321+
if ($scope instanceof MutatingScope) {
323322
$scope = $scope->pushInFunctionCall(null, $parameter);
324323
}
325324
$argumentValueType = $scope->getType($argumentValue);
326325

327-
if ($scope instanceof MutatingScope || $scope instanceof GeneratorScope) {
326+
if ($scope instanceof MutatingScope) {
328327
$scope = $scope->popInFunctionCall();
329328
}
330329
}

src/Type/Php/ArrayFilterFunctionReturnTypeHelper.php

Lines changed: 15 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace PHPStan\Type\Php;
44

5-
use Override;
6-
use PhpParser\Node;
75
use PhpParser\Node\Arg;
86
use PhpParser\Node\Expr;
97
use PhpParser\Node\Expr\ArrowFunction;
@@ -15,15 +13,13 @@
1513
use PhpParser\Node\Expr\Variable;
1614
use PhpParser\Node\Name;
1715
use PhpParser\Node\Stmt\Return_;
18-
use PhpParser\NodeTraverser;
19-
use PhpParser\NodeVisitor\CloningVisitor;
20-
use PhpParser\NodeVisitorAbstract;
16+
use PHPStan\Analyser\MutatingScope;
2117
use PHPStan\Analyser\Scope;
2218
use PHPStan\DependencyInjection\AutowiredService;
23-
use PHPStan\Node\Expr\IdentifiedTypeExpr;
2419
use PHPStan\Php\PhpVersion;
2520
use PHPStan\Reflection\ReflectionProvider;
2621
use PHPStan\ShouldNotHappenException;
22+
use PHPStan\TrinaryLogic;
2723
use PHPStan\Type\ArrayType;
2824
use PHPStan\Type\BenevolentUnionType;
2925
use PHPStan\Type\Constant\ConstantArrayType;
@@ -196,6 +192,10 @@ private function removeFalsey(Type $type): Type
196192

197193
private function filterByTruthyValue(Scope $scope, Error|Variable|null $itemVar, Type $arrayType, Error|Variable|null $keyVar, Expr $expr): Type
198194
{
195+
if (!$scope instanceof MutatingScope) {
196+
throw new ShouldNotHappenException();
197+
}
198+
199199
$constantArrays = $arrayType->getConstantArrays();
200200
if (count($constantArrays) > 0) {
201201
$results = [];
@@ -235,56 +235,24 @@ private function filterByTruthyValue(Scope $scope, Error|Variable|null $itemVar,
235235
/**
236236
* @return array{Type, Type, bool}
237237
*/
238-
private function processKeyAndItemType(Scope $scope, Type $keyType, Type $itemType, Error|Variable|null $itemVar, Error|Variable|null $keyVar, Expr $expr): array
238+
private function processKeyAndItemType(MutatingScope $scope, Type $keyType, Type $itemType, Error|Variable|null $itemVar, Error|Variable|null $keyVar, Expr $expr): array
239239
{
240-
$itemTypeExpr = null;
241-
$cloneTraverser = new NodeTraverser(new CloningVisitor());
242-
/** @var Expr $expr */
243-
[$expr] = $cloneTraverser->traverse([$expr]);
244-
245-
$changeTraverserFactory = static fn (string $varName, IdentifiedTypeExpr $typeExpr) => new NodeTraverser(new class ($varName, $typeExpr) extends NodeVisitorAbstract {
246-
247-
public function __construct(private string $varName, private IdentifiedTypeExpr $typeExpr)
248-
{
249-
}
250-
251-
#[Override]
252-
public function leaveNode(Node $node): ?IdentifiedTypeExpr
253-
{
254-
$node->setAttribute('phpstan_cache_printer', null);
255-
if (!$node instanceof Variable) {
256-
return null;
257-
}
258-
if ($node->name !== $this->varName) {
259-
return null;
260-
}
261-
262-
return $this->typeExpr;
263-
}
264-
265-
});
266-
240+
$itemVarName = null;
267241
if ($itemVar !== null) {
268242
if (!$itemVar instanceof Variable || !is_string($itemVar->name)) {
269243
throw new ShouldNotHappenException();
270244
}
271-
$itemTypeExpr = new IdentifiedTypeExpr($itemType, $itemVar);
272-
$changeTraverser = $changeTraverserFactory($itemVar->name, $itemTypeExpr);
273-
274-
/** @var Expr $expr */
275-
[$expr] = $changeTraverser->traverse([$expr]);
245+
$itemVarName = $itemVar->name;
246+
$scope = $scope->assignVariable($itemVarName, $itemType, new MixedType(), TrinaryLogic::createYes());
276247
}
277248

278-
$keyTypeExpr = null;
249+
$keyVarName = null;
279250
if ($keyVar !== null) {
280251
if (!$keyVar instanceof Variable || !is_string($keyVar->name)) {
281252
throw new ShouldNotHappenException();
282253
}
283-
$keyTypeExpr = new IdentifiedTypeExpr($keyType, $keyVar);
284-
$changeTraverser = $changeTraverserFactory($keyVar->name, $keyTypeExpr);
285-
286-
/** @var Expr $expr */
287-
[$expr] = $changeTraverser->traverse([$expr]);
254+
$keyVarName = $keyVar->name;
255+
$scope = $scope->assignVariable($keyVarName, $keyType, new MixedType(), TrinaryLogic::createYes());
288256
}
289257

290258
$booleanResult = $scope->getType($expr)->toBoolean();
@@ -295,8 +263,8 @@ public function leaveNode(Node $node): ?IdentifiedTypeExpr
295263
$scope = $scope->filterByTruthyValue($expr);
296264

297265
return [
298-
$keyTypeExpr !== null ? $scope->getType($keyTypeExpr) : $keyType,
299-
$itemTypeExpr !== null ? $scope->getType($itemTypeExpr) : $itemType,
266+
$keyVarName !== null ? $scope->getVariableType($keyVarName) : $keyType,
267+
$itemVarName !== null ? $scope->getVariableType($itemVarName) : $itemType,
300268
!$booleanResult->isTrue()->yes(),
301269
];
302270
}

0 commit comments

Comments
 (0)