diff --git a/conf/config.neon b/conf/config.neon index 3e8af44e65..2dc97fbd5c 100644 --- a/conf/config.neon +++ b/conf/config.neon @@ -99,7 +99,6 @@ parameters: phpVersion: null polluteScopeWithLoopInitialAssignments: true polluteScopeWithAlwaysIterableForeach: true - polluteCatchScopeWithTryAssignments: false propertyAlwaysWrittenTags: [] propertyAlwaysReadTags: [] additionalConstructors: [] @@ -285,7 +284,6 @@ parametersSchema: phpVersion: schema(anyOf(schema(int(), min(70100), max(80099))), nullable()) polluteScopeWithLoopInitialAssignments: bool() polluteScopeWithAlwaysIterableForeach: bool() - polluteCatchScopeWithTryAssignments: bool() propertyAlwaysWrittenTags: listOf(string()) propertyAlwaysReadTags: listOf(string()) additionalConstructors: listOf(string()) @@ -477,7 +475,6 @@ services: arguments: classReflector: @nodeScopeResolverClassReflector polluteScopeWithLoopInitialAssignments: %polluteScopeWithLoopInitialAssignments% - polluteCatchScopeWithTryAssignments: %polluteCatchScopeWithTryAssignments% polluteScopeWithAlwaysIterableForeach: %polluteScopeWithAlwaysIterableForeach% earlyTerminatingMethodCalls: %earlyTerminatingMethodCalls% earlyTerminatingFunctionCalls: %earlyTerminatingFunctionCalls% diff --git a/src/Analyser/NodeScopeResolver.php b/src/Analyser/NodeScopeResolver.php index 06ee0c3069..7186cdb1d4 100644 --- a/src/Analyser/NodeScopeResolver.php +++ b/src/Analyser/NodeScopeResolver.php @@ -150,8 +150,6 @@ class NodeScopeResolver private bool $polluteScopeWithLoopInitialAssignments; - private bool $polluteCatchScopeWithTryAssignments; - private bool $polluteScopeWithAlwaysIterableForeach; /** @var string[][] className(string) => methods(string[]) */ @@ -176,7 +174,6 @@ class NodeScopeResolver * @param FileHelper $fileHelper * @param TypeSpecifier $typeSpecifier * @param bool $polluteScopeWithLoopInitialAssignments - * @param bool $polluteCatchScopeWithTryAssignments * @param bool $polluteScopeWithAlwaysIterableForeach * @param string[][] $earlyTerminatingMethodCalls className(string) => methods(string[]) * @param array $earlyTerminatingFunctionCalls @@ -196,7 +193,6 @@ public function __construct( TypeSpecifier $typeSpecifier, DynamicThrowTypeExtensionProvider $dynamicThrowTypeExtensionProvider, bool $polluteScopeWithLoopInitialAssignments, - bool $polluteCatchScopeWithTryAssignments, bool $polluteScopeWithAlwaysIterableForeach, array $earlyTerminatingMethodCalls, array $earlyTerminatingFunctionCalls, @@ -216,7 +212,6 @@ public function __construct( $this->typeSpecifier = $typeSpecifier; $this->dynamicThrowTypeExtensionProvider = $dynamicThrowTypeExtensionProvider; $this->polluteScopeWithLoopInitialAssignments = $polluteScopeWithLoopInitialAssignments; - $this->polluteCatchScopeWithTryAssignments = $polluteCatchScopeWithTryAssignments; $this->polluteScopeWithAlwaysIterableForeach = $polluteScopeWithAlwaysIterableForeach; $this->earlyTerminatingMethodCalls = $earlyTerminatingMethodCalls; $this->earlyTerminatingFunctionCalls = $earlyTerminatingFunctionCalls; @@ -1185,7 +1180,7 @@ private function processStmtNode( foreach ($stmt->catches as $catchNode) { $nodeCallback($catchNode, $scope); - if ($this->preciseExceptionTracking || !$this->polluteCatchScopeWithTryAssignments) { + if ($this->preciseExceptionTracking) { $catchType = TypeCombinator::union(...array_map(static function (Name $name): Type { return new ObjectType($name->toString()); }, $catchNode->types)); diff --git a/src/Testing/RuleTestCase.php b/src/Testing/RuleTestCase.php index 8d6d7f6611..c2bac456d6 100644 --- a/src/Testing/RuleTestCase.php +++ b/src/Testing/RuleTestCase.php @@ -85,7 +85,6 @@ private function getAnalyser(): Analyser $typeSpecifier, self::getContainer()->getByType(DynamicThrowTypeExtensionProvider::class), $this->shouldPolluteScopeWithLoopInitialAssignments(), - $this->shouldPolluteCatchScopeWithTryAssignments(), $this->shouldPolluteScopeWithAlwaysIterableForeach(), [], [], @@ -185,11 +184,6 @@ protected function shouldPolluteScopeWithLoopInitialAssignments(): bool return false; } - protected function shouldPolluteCatchScopeWithTryAssignments(): bool - { - return false; - } - protected function shouldPolluteScopeWithAlwaysIterableForeach(): bool { return true; diff --git a/src/Testing/TypeInferenceTestCase.php b/src/Testing/TypeInferenceTestCase.php index 06774f7fb8..8630cbd3f9 100644 --- a/src/Testing/TypeInferenceTestCase.php +++ b/src/Testing/TypeInferenceTestCase.php @@ -31,9 +31,6 @@ abstract class TypeInferenceTestCase extends \PHPStan\Testing\TestCase { - /** @var bool */ - protected $polluteCatchScopeWithTryAssignments = true; - /** * @param string $file * @param callable(\PhpParser\Node, \PHPStan\Analyser\Scope): void $callback @@ -77,7 +74,6 @@ public function processFile( $typeSpecifier, self::getContainer()->getByType(DynamicThrowTypeExtensionProvider::class), true, - $this->polluteCatchScopeWithTryAssignments, true, $this->getEarlyTerminatingMethodCalls(), $this->getEarlyTerminatingFunctionCalls(), diff --git a/tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php b/tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php index 9f37b047cd..6b12af562f 100644 --- a/tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php @@ -10360,33 +10360,18 @@ public function testTryCatchScope( string $evaluatedPointExpression ): void { - foreach ([true, false] as $polluteCatchScopeWithTryAssignments) { - $this->polluteCatchScopeWithTryAssignments = $polluteCatchScopeWithTryAssignments; - - try { - $this->assertTypes( - __DIR__ . '/data/try-catch-scope.php', - $description, - $expression, - [], - [], - [], - [], - $evaluatedPointExpression, - [], - false - ); - } catch (\PHPUnit\Framework\ExpectationFailedException $e) { - throw new \PHPUnit\Framework\ExpectationFailedException( - sprintf( - '%s (polluteCatchScopeWithTryAssignments: %s)', - $e->getMessage(), - $polluteCatchScopeWithTryAssignments ? 'true' : 'false' - ), - $e->getComparisonFailure() - ); - } - } + $this->assertTypes( + __DIR__ . '/data/try-catch-scope.php', + $description, + $expression, + [], + [], + [], + [], + $evaluatedPointExpression, + [], + false + ); } /** diff --git a/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php b/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php index 3beb38ad2b..7e82582d7d 100644 --- a/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php +++ b/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php @@ -17,9 +17,6 @@ class DefinedVariableRuleTest extends \PHPStan\Testing\RuleTestCase /** @var bool */ private $polluteScopeWithLoopInitialAssignments; - /** @var bool */ - private $polluteCatchScopeWithTryAssignments; - /** @var bool */ private $polluteScopeWithAlwaysIterableForeach; @@ -36,11 +33,6 @@ protected function shouldPolluteScopeWithLoopInitialAssignments(): bool return $this->polluteScopeWithLoopInitialAssignments; } - protected function shouldPolluteCatchScopeWithTryAssignments(): bool - { - return $this->polluteCatchScopeWithTryAssignments; - } - protected function shouldPolluteScopeWithAlwaysIterableForeach(): bool { return $this->polluteScopeWithAlwaysIterableForeach; @@ -51,7 +43,6 @@ public function testDefinedVariables(): void require_once __DIR__ . '/data/defined-variables-definition.php'; $this->cliArgumentsVariablesRegistered = true; $this->polluteScopeWithLoopInitialAssignments = false; - $this->polluteCatchScopeWithTryAssignments = false; $this->checkMaybeUndefinedVariables = true; $this->polluteScopeWithAlwaysIterableForeach = true; $this->analyse([__DIR__ . '/data/defined-variables.php'], [ @@ -254,7 +245,6 @@ public function testDefinedVariablesInClosures(): void { $this->cliArgumentsVariablesRegistered = true; $this->polluteScopeWithLoopInitialAssignments = false; - $this->polluteCatchScopeWithTryAssignments = false; $this->checkMaybeUndefinedVariables = true; $this->polluteScopeWithAlwaysIterableForeach = true; $this->analyse([__DIR__ . '/data/defined-variables-closures.php'], [ @@ -269,7 +259,6 @@ public function testDefinedVariablesInShortArrayDestructuringSyntax(): void { $this->cliArgumentsVariablesRegistered = true; $this->polluteScopeWithLoopInitialAssignments = false; - $this->polluteCatchScopeWithTryAssignments = false; $this->checkMaybeUndefinedVariables = true; $this->polluteScopeWithAlwaysIterableForeach = true; $this->analyse([__DIR__ . '/data/defined-variables-array-destructuring-short-syntax.php'], [ @@ -292,7 +281,6 @@ public function testCliArgumentsVariablesNotRegistered(): void { $this->cliArgumentsVariablesRegistered = false; $this->polluteScopeWithLoopInitialAssignments = false; - $this->polluteCatchScopeWithTryAssignments = false; $this->checkMaybeUndefinedVariables = true; $this->polluteScopeWithAlwaysIterableForeach = true; $this->analyse([__DIR__ . '/data/cli-arguments-variables.php'], [ @@ -311,7 +299,6 @@ public function testCliArgumentsVariablesRegistered(): void { $this->cliArgumentsVariablesRegistered = true; $this->polluteScopeWithLoopInitialAssignments = false; - $this->polluteCatchScopeWithTryAssignments = false; $this->checkMaybeUndefinedVariables = true; $this->polluteScopeWithAlwaysIterableForeach = true; $this->analyse([__DIR__ . '/data/cli-arguments-variables.php'], [ @@ -370,7 +357,6 @@ public function testLoopInitialAssignments( ): void { $this->cliArgumentsVariablesRegistered = false; - $this->polluteCatchScopeWithTryAssignments = false; $this->polluteScopeWithLoopInitialAssignments = $polluteScopeWithLoopInitialAssignments; $this->checkMaybeUndefinedVariables = $checkMaybeUndefinedVariables; $this->polluteScopeWithAlwaysIterableForeach = true; @@ -381,7 +367,6 @@ public function testDefineVariablesInClass(): void { $this->cliArgumentsVariablesRegistered = true; $this->polluteScopeWithLoopInitialAssignments = false; - $this->polluteCatchScopeWithTryAssignments = false; $this->checkMaybeUndefinedVariables = true; $this->polluteScopeWithAlwaysIterableForeach = true; $this->analyse([__DIR__ . '/data/define-variables-class.php'], []); @@ -391,7 +376,6 @@ public function testDeadBranches(): void { $this->cliArgumentsVariablesRegistered = true; $this->polluteScopeWithLoopInitialAssignments = false; - $this->polluteCatchScopeWithTryAssignments = false; $this->checkMaybeUndefinedVariables = true; $this->polluteScopeWithAlwaysIterableForeach = true; $this->analyse([__DIR__ . '/data/dead-branches.php'], [ @@ -422,7 +406,6 @@ public function testForeach(): void { $this->cliArgumentsVariablesRegistered = true; $this->polluteScopeWithLoopInitialAssignments = false; - $this->polluteCatchScopeWithTryAssignments = false; $this->checkMaybeUndefinedVariables = true; $this->polluteScopeWithAlwaysIterableForeach = true; $this->analyse([__DIR__ . '/data/foreach.php'], [ @@ -595,7 +578,6 @@ public function testForeachPolluteScopeWithAlwaysIterableForeach(bool $polluteSc { $this->cliArgumentsVariablesRegistered = true; $this->polluteScopeWithLoopInitialAssignments = false; - $this->polluteCatchScopeWithTryAssignments = false; $this->checkMaybeUndefinedVariables = true; $this->polluteScopeWithAlwaysIterableForeach = $polluteScopeWithAlwaysIterableForeach; $this->analyse([__DIR__ . '/data/foreach-always-iterable.php'], $errors); @@ -605,7 +587,6 @@ public function testBooleanOperatorsTruthyFalsey(): void { $this->cliArgumentsVariablesRegistered = true; $this->polluteScopeWithLoopInitialAssignments = false; - $this->polluteCatchScopeWithTryAssignments = false; $this->checkMaybeUndefinedVariables = true; $this->polluteScopeWithAlwaysIterableForeach = true; $this->analyse([__DIR__ . '/data/boolean-op-truthy-falsey.php'], [ @@ -628,7 +609,6 @@ public function testArrowFunctions(): void $this->cliArgumentsVariablesRegistered = true; $this->polluteScopeWithLoopInitialAssignments = false; - $this->polluteCatchScopeWithTryAssignments = false; $this->checkMaybeUndefinedVariables = true; $this->polluteScopeWithAlwaysIterableForeach = true; $this->analyse([__DIR__ . '/data/defined-variables-arrow-functions.php'], [ @@ -651,7 +631,6 @@ public function testCoalesceAssign(): void $this->cliArgumentsVariablesRegistered = true; $this->polluteScopeWithLoopInitialAssignments = false; - $this->polluteCatchScopeWithTryAssignments = false; $this->checkMaybeUndefinedVariables = true; $this->polluteScopeWithAlwaysIterableForeach = true; $this->analyse([__DIR__ . '/data/defined-variables-coalesce-assign.php'], [ @@ -666,7 +645,6 @@ public function testBug2748(): void { $this->cliArgumentsVariablesRegistered = true; $this->polluteScopeWithLoopInitialAssignments = false; - $this->polluteCatchScopeWithTryAssignments = false; $this->checkMaybeUndefinedVariables = true; $this->polluteScopeWithAlwaysIterableForeach = true; $this->analyse([__DIR__ . '/data/bug-2748.php'], [ @@ -685,7 +663,6 @@ public function testGlobalVariables(): void { $this->cliArgumentsVariablesRegistered = true; $this->polluteScopeWithLoopInitialAssignments = false; - $this->polluteCatchScopeWithTryAssignments = false; $this->checkMaybeUndefinedVariables = true; $this->polluteScopeWithAlwaysIterableForeach = true; $this->analyse([__DIR__ . '/data/global-variables.php'], []); @@ -695,7 +672,6 @@ public function testRootScopeMaybeDefined(): void { $this->cliArgumentsVariablesRegistered = true; $this->polluteScopeWithLoopInitialAssignments = false; - $this->polluteCatchScopeWithTryAssignments = false; $this->checkMaybeUndefinedVariables = false; $this->polluteScopeWithAlwaysIterableForeach = true; $this->analyse([__DIR__ . '/data/root-scope-maybe.php'], []); @@ -705,7 +681,6 @@ public function testRootScopeMaybeDefinedCheck(): void { $this->cliArgumentsVariablesRegistered = true; $this->polluteScopeWithLoopInitialAssignments = false; - $this->polluteCatchScopeWithTryAssignments = false; $this->checkMaybeUndefinedVariables = true; $this->polluteScopeWithAlwaysIterableForeach = true; $this->analyse([__DIR__ . '/data/root-scope-maybe.php'], [ @@ -724,7 +699,6 @@ public function testFormerThisVariableRule(): void { $this->cliArgumentsVariablesRegistered = true; $this->polluteScopeWithLoopInitialAssignments = false; - $this->polluteCatchScopeWithTryAssignments = false; $this->checkMaybeUndefinedVariables = true; $this->polluteScopeWithAlwaysIterableForeach = true; $this->analyse([__DIR__ . '/data/this.php'], [ @@ -751,7 +725,6 @@ public function testClosureUse(): void { $this->cliArgumentsVariablesRegistered = true; $this->polluteScopeWithLoopInitialAssignments = false; - $this->polluteCatchScopeWithTryAssignments = false; $this->checkMaybeUndefinedVariables = true; $this->polluteScopeWithAlwaysIterableForeach = true; $this->analyse([__DIR__ . '/data/defined-variables-anonymous-function-use.php'], [ @@ -790,7 +763,6 @@ public function testNullsafeIsset(): void $this->cliArgumentsVariablesRegistered = true; $this->polluteScopeWithLoopInitialAssignments = false; - $this->polluteCatchScopeWithTryAssignments = false; $this->checkMaybeUndefinedVariables = true; $this->polluteScopeWithAlwaysIterableForeach = true; $this->analyse([__DIR__ . '/data/variable-nullsafe-isset.php'], []); @@ -800,7 +772,6 @@ public function testBug1306(): void { $this->cliArgumentsVariablesRegistered = true; $this->polluteScopeWithLoopInitialAssignments = false; - $this->polluteCatchScopeWithTryAssignments = false; $this->checkMaybeUndefinedVariables = true; $this->polluteScopeWithAlwaysIterableForeach = true; $this->analyse([__DIR__ . '/data/bug-1306.php'], []); @@ -810,7 +781,6 @@ public function testBug3515(): void { $this->cliArgumentsVariablesRegistered = true; $this->polluteScopeWithLoopInitialAssignments = false; - $this->polluteCatchScopeWithTryAssignments = false; $this->checkMaybeUndefinedVariables = true; $this->polluteScopeWithAlwaysIterableForeach = true; $this->analyse([__DIR__ . '/data/bug-3515.php'], [ @@ -829,7 +799,6 @@ public function testBug4412(): void { $this->cliArgumentsVariablesRegistered = true; $this->polluteScopeWithLoopInitialAssignments = false; - $this->polluteCatchScopeWithTryAssignments = false; $this->checkMaybeUndefinedVariables = true; $this->polluteScopeWithAlwaysIterableForeach = true; $this->analyse([__DIR__ . '/data/bug-4412.php'], [ @@ -844,7 +813,6 @@ public function testBug3283(): void { $this->cliArgumentsVariablesRegistered = true; $this->polluteScopeWithLoopInitialAssignments = false; - $this->polluteCatchScopeWithTryAssignments = false; $this->checkMaybeUndefinedVariables = true; $this->polluteScopeWithAlwaysIterableForeach = true; $this->analyse([__DIR__ . '/data/bug-3283.php'], []);