From 3f886f2f24a610a69eb76b31340895be81847e74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Sun, 1 Dec 2024 17:41:41 +0100 Subject: [PATCH] Fix: Remove conditions that should never evaluate to true --- composer-require-checker.json | 1 - src/Classes/FinalRule.php | 9 --------- src/Classes/NoExtendsRule.php | 9 --------- src/Classes/PHPUnit/Framework/TestCaseWithSuffixRule.php | 9 --------- src/Closures/NoNullableReturnTypeDeclarationRule.php | 9 --------- src/Closures/NoParameterWithNullDefaultValueRule.php | 9 --------- .../NoParameterWithNullableTypeDeclarationRule.php | 9 --------- src/Expressions/NoCompactRule.php | 9 --------- src/Files/DeclareStrictTypesRule.php | 9 --------- src/Functions/NoNullableReturnTypeDeclarationRule.php | 9 --------- src/Functions/NoParameterWithNullDefaultValueRule.php | 9 --------- .../NoParameterWithNullableTypeDeclarationRule.php | 9 --------- src/Methods/FinalInAbstractClassRule.php | 9 --------- .../NoConstructorParameterWithDefaultValueRule.php | 9 --------- src/Methods/NoNullableReturnTypeDeclarationRule.php | 9 --------- .../NoParameterWithContainerTypeDeclarationRule.php | 9 --------- src/Methods/NoParameterWithNullDefaultValueRule.php | 9 --------- .../NoParameterWithNullableTypeDeclarationRule.php | 9 --------- src/Methods/PrivateInFinalClassRule.php | 9 --------- 19 files changed, 163 deletions(-) diff --git a/composer-require-checker.json b/composer-require-checker.json index c8a7dc42..80e14648 100644 --- a/composer-require-checker.json +++ b/composer-require-checker.json @@ -31,7 +31,6 @@ "PHPStan\\Rules\\Rule", "PHPStan\\Rules\\RuleError", "PHPStan\\Rules\\RuleErrorBuilder", - "PHPStan\\ShouldNotHappenException", "self", "string", "true" diff --git a/src/Classes/FinalRule.php b/src/Classes/FinalRule.php index 1121f47d..0e1af1dd 100644 --- a/src/Classes/FinalRule.php +++ b/src/Classes/FinalRule.php @@ -18,7 +18,6 @@ use PhpParser\Node; use PHPStan\Analyser; use PHPStan\Rules; -use PHPStan\ShouldNotHappenException; /** * @implements Rules\Rule @@ -74,14 +73,6 @@ public function processNode( Node $node, Analyser\Scope $scope ): array { - if (!$node instanceof Node\Stmt\Class_) { - throw new ShouldNotHappenException(\sprintf( - 'Expected node to be instance of "%s", but got instance of "%s" instead.', - Node\Stmt\Class_::class, - \get_class($node), - )); - } - if (!isset($node->namespacedName)) { return []; } diff --git a/src/Classes/NoExtendsRule.php b/src/Classes/NoExtendsRule.php index bbc3acb7..40e4d70f 100644 --- a/src/Classes/NoExtendsRule.php +++ b/src/Classes/NoExtendsRule.php @@ -17,7 +17,6 @@ use PhpParser\Node; use PHPStan\Analyser; use PHPStan\Rules; -use PHPStan\ShouldNotHappenException; /** * @implements Rules\Rule @@ -59,14 +58,6 @@ public function processNode( Node $node, Analyser\Scope $scope ): array { - if (!$node instanceof Node\Stmt\Class_) { - throw new ShouldNotHappenException(\sprintf( - 'Expected node to be instance of "%s", but got instance of "%s" instead.', - Node\Stmt\Class_::class, - \get_class($node), - )); - } - if (!$node->extends instanceof Node\Name) { return []; } diff --git a/src/Classes/PHPUnit/Framework/TestCaseWithSuffixRule.php b/src/Classes/PHPUnit/Framework/TestCaseWithSuffixRule.php index e0b7b4ba..7f4589a6 100644 --- a/src/Classes/PHPUnit/Framework/TestCaseWithSuffixRule.php +++ b/src/Classes/PHPUnit/Framework/TestCaseWithSuffixRule.php @@ -18,7 +18,6 @@ use PHPStan\Analyser; use PHPStan\Reflection; use PHPStan\Rules; -use PHPStan\ShouldNotHappenException; /** * @implements Rules\Rule @@ -47,14 +46,6 @@ public function processNode( Node $node, Analyser\Scope $scope ): array { - if (!$node instanceof Node\Stmt\Class_) { - throw new ShouldNotHappenException(\sprintf( - 'Expected node to be instance of "%s", but got instance of "%s" instead.', - Node\Stmt\Class_::class, - \get_class($node), - )); - } - if ($node->isAbstract()) { return []; } diff --git a/src/Closures/NoNullableReturnTypeDeclarationRule.php b/src/Closures/NoNullableReturnTypeDeclarationRule.php index c07d646c..12ab6710 100644 --- a/src/Closures/NoNullableReturnTypeDeclarationRule.php +++ b/src/Closures/NoNullableReturnTypeDeclarationRule.php @@ -17,7 +17,6 @@ use PhpParser\Node; use PHPStan\Analyser; use PHPStan\Rules; -use PHPStan\ShouldNotHappenException; /** * @implements Rules\Rule @@ -33,14 +32,6 @@ public function processNode( Node $node, Analyser\Scope $scope ): array { - if (!$node instanceof Node\Expr\Closure) { - throw new ShouldNotHappenException(\sprintf( - 'Expected node to be instance of "%s", but got instance of "%s" instead.', - Node\Expr\Closure::class, - \get_class($node), - )); - } - if (!self::hasNullableReturnType($node)) { return []; } diff --git a/src/Closures/NoParameterWithNullDefaultValueRule.php b/src/Closures/NoParameterWithNullDefaultValueRule.php index 31a72ec9..83579c52 100644 --- a/src/Closures/NoParameterWithNullDefaultValueRule.php +++ b/src/Closures/NoParameterWithNullDefaultValueRule.php @@ -17,7 +17,6 @@ use PhpParser\Node; use PHPStan\Analyser; use PHPStan\Rules; -use PHPStan\ShouldNotHappenException; /** * @implements Rules\Rule @@ -33,14 +32,6 @@ public function processNode( Node $node, Analyser\Scope $scope ): array { - if (!$node instanceof Node\Expr\Closure) { - throw new ShouldNotHappenException(\sprintf( - 'Expected node to be instance of "%s", but got instance of "%s" instead.', - Node\Expr\Closure::class, - \get_class($node), - )); - } - if (0 === \count($node->params)) { return []; } diff --git a/src/Closures/NoParameterWithNullableTypeDeclarationRule.php b/src/Closures/NoParameterWithNullableTypeDeclarationRule.php index 3250d189..ef75bc3c 100644 --- a/src/Closures/NoParameterWithNullableTypeDeclarationRule.php +++ b/src/Closures/NoParameterWithNullableTypeDeclarationRule.php @@ -17,7 +17,6 @@ use PhpParser\Node; use PHPStan\Analyser; use PHPStan\Rules; -use PHPStan\ShouldNotHappenException; /** * @implements Rules\Rule @@ -33,14 +32,6 @@ public function processNode( Node $node, Analyser\Scope $scope ): array { - if (!$node instanceof Node\Expr\Closure) { - throw new ShouldNotHappenException(\sprintf( - 'Expected node to be instance of "%s", but got instance of "%s" instead.', - Node\Expr\Closure::class, - \get_class($node), - )); - } - if (0 === \count($node->params)) { return []; } diff --git a/src/Expressions/NoCompactRule.php b/src/Expressions/NoCompactRule.php index f1fe4b7a..58da4044 100644 --- a/src/Expressions/NoCompactRule.php +++ b/src/Expressions/NoCompactRule.php @@ -17,7 +17,6 @@ use PhpParser\Node; use PHPStan\Analyser; use PHPStan\Rules; -use PHPStan\ShouldNotHappenException; /** * @implements Rules\Rule @@ -33,14 +32,6 @@ public function processNode( Node $node, Analyser\Scope $scope ): array { - if (!$node instanceof Node\Expr\FuncCall) { - throw new ShouldNotHappenException(\sprintf( - 'Expected node to be instance of "%s", but got instance of "%s" instead.', - Node\Expr\FuncCall::class, - \get_class($node), - )); - } - if (!$node->name instanceof Node\Name) { return []; } diff --git a/src/Files/DeclareStrictTypesRule.php b/src/Files/DeclareStrictTypesRule.php index 04a283bc..11c615b1 100644 --- a/src/Files/DeclareStrictTypesRule.php +++ b/src/Files/DeclareStrictTypesRule.php @@ -18,7 +18,6 @@ use PHPStan\Analyser; use PHPStan\Node\FileNode; use PHPStan\Rules; -use PHPStan\ShouldNotHappenException; /** * @implements Rules\Rule @@ -34,14 +33,6 @@ public function processNode( Node $node, Analyser\Scope $scope ): array { - if (!$node instanceof FileNode) { - throw new ShouldNotHappenException(\sprintf( - 'Expected node to be instance of "%s", but got instance of "%s" instead.', - FileNode::class, - \get_class($node), - )); - } - $nodes = $node->getNodes(); if (0 === \count($nodes)) { diff --git a/src/Functions/NoNullableReturnTypeDeclarationRule.php b/src/Functions/NoNullableReturnTypeDeclarationRule.php index 683bd9d9..0759fdc5 100644 --- a/src/Functions/NoNullableReturnTypeDeclarationRule.php +++ b/src/Functions/NoNullableReturnTypeDeclarationRule.php @@ -17,7 +17,6 @@ use PhpParser\Node; use PHPStan\Analyser; use PHPStan\Rules; -use PHPStan\ShouldNotHappenException; /** * @implements Rules\Rule @@ -33,14 +32,6 @@ public function processNode( Node $node, Analyser\Scope $scope ): array { - if (!$node instanceof Node\Stmt\Function_) { - throw new ShouldNotHappenException(\sprintf( - 'Expected node to be instance of "%s", but got instance of "%s" instead.', - Node\Stmt\Function_::class, - \get_class($node), - )); - } - if (!isset($node->namespacedName)) { return []; } diff --git a/src/Functions/NoParameterWithNullDefaultValueRule.php b/src/Functions/NoParameterWithNullDefaultValueRule.php index 76b6ffd4..40067482 100644 --- a/src/Functions/NoParameterWithNullDefaultValueRule.php +++ b/src/Functions/NoParameterWithNullDefaultValueRule.php @@ -17,7 +17,6 @@ use PhpParser\Node; use PHPStan\Analyser; use PHPStan\Rules; -use PHPStan\ShouldNotHappenException; /** * @implements Rules\Rule @@ -33,14 +32,6 @@ public function processNode( Node $node, Analyser\Scope $scope ): array { - if (!$node instanceof Node\Stmt\Function_) { - throw new ShouldNotHappenException(\sprintf( - 'Expected node to be instance of "%s", but got instance of "%s" instead.', - Node\Stmt\Function_::class, - \get_class($node), - )); - } - if (0 === \count($node->params)) { return []; } diff --git a/src/Functions/NoParameterWithNullableTypeDeclarationRule.php b/src/Functions/NoParameterWithNullableTypeDeclarationRule.php index b70c7adb..47c7fe23 100644 --- a/src/Functions/NoParameterWithNullableTypeDeclarationRule.php +++ b/src/Functions/NoParameterWithNullableTypeDeclarationRule.php @@ -17,7 +17,6 @@ use PhpParser\Node; use PHPStan\Analyser; use PHPStan\Rules; -use PHPStan\ShouldNotHappenException; /** * @implements Rules\Rule @@ -33,14 +32,6 @@ public function processNode( Node $node, Analyser\Scope $scope ): array { - if (!$node instanceof Node\Stmt\Function_) { - throw new ShouldNotHappenException(\sprintf( - 'Expected node to be instance of "%s", but got instance of "%s" instead.', - Node\Stmt\Function_::class, - \get_class($node), - )); - } - if (0 === \count($node->params)) { return []; } diff --git a/src/Methods/FinalInAbstractClassRule.php b/src/Methods/FinalInAbstractClassRule.php index 7e191c73..88c8fdfe 100644 --- a/src/Methods/FinalInAbstractClassRule.php +++ b/src/Methods/FinalInAbstractClassRule.php @@ -18,7 +18,6 @@ use PHPStan\Analyser; use PHPStan\Reflection; use PHPStan\Rules; -use PHPStan\ShouldNotHappenException; /** * @implements Rules\Rule @@ -34,14 +33,6 @@ public function processNode( Node $node, Analyser\Scope $scope ): array { - if (!$node instanceof Node\Stmt\ClassMethod) { - throw new ShouldNotHappenException(\sprintf( - 'Expected node to be instance of "%s", but got instance of "%s" instead.', - Node\Stmt\ClassMethod::class, - \get_class($node), - )); - } - /** @var Reflection\ClassReflection $containingClass */ $containingClass = $scope->getClassReflection(); diff --git a/src/Methods/NoConstructorParameterWithDefaultValueRule.php b/src/Methods/NoConstructorParameterWithDefaultValueRule.php index 01df671e..9e43803d 100644 --- a/src/Methods/NoConstructorParameterWithDefaultValueRule.php +++ b/src/Methods/NoConstructorParameterWithDefaultValueRule.php @@ -18,7 +18,6 @@ use PHPStan\Analyser; use PHPStan\Reflection; use PHPStan\Rules; -use PHPStan\ShouldNotHappenException; /** * @implements Rules\Rule @@ -34,14 +33,6 @@ public function processNode( Node $node, Analyser\Scope $scope ): array { - if (!$node instanceof Node\Stmt\ClassMethod) { - throw new ShouldNotHappenException(\sprintf( - 'Expected node to be instance of "%s", but got instance of "%s" instead.', - Node\Stmt\ClassMethod::class, - \get_class($node), - )); - } - if ('__construct' !== $node->name->toLowerString()) { return []; } diff --git a/src/Methods/NoNullableReturnTypeDeclarationRule.php b/src/Methods/NoNullableReturnTypeDeclarationRule.php index d6ef9a82..cb59229b 100644 --- a/src/Methods/NoNullableReturnTypeDeclarationRule.php +++ b/src/Methods/NoNullableReturnTypeDeclarationRule.php @@ -18,7 +18,6 @@ use PHPStan\Analyser; use PHPStan\Reflection; use PHPStan\Rules; -use PHPStan\ShouldNotHappenException; /** * @implements Rules\Rule @@ -34,14 +33,6 @@ public function processNode( Node $node, Analyser\Scope $scope ): array { - if (!$node instanceof Node\Stmt\ClassMethod) { - throw new ShouldNotHappenException(\sprintf( - 'Expected node to be instance of "%s", but got instance of "%s" instead.', - Node\Stmt\ClassMethod::class, - \get_class($node), - )); - } - if (!self::hasNullableReturnType($node)) { return []; } diff --git a/src/Methods/NoParameterWithContainerTypeDeclarationRule.php b/src/Methods/NoParameterWithContainerTypeDeclarationRule.php index 3d2a3be1..0e282050 100644 --- a/src/Methods/NoParameterWithContainerTypeDeclarationRule.php +++ b/src/Methods/NoParameterWithContainerTypeDeclarationRule.php @@ -18,7 +18,6 @@ use PHPStan\Analyser; use PHPStan\Reflection; use PHPStan\Rules; -use PHPStan\ShouldNotHappenException; /** * @implements Rules\Rule @@ -67,14 +66,6 @@ public function processNode( Node $node, Analyser\Scope $scope ): array { - if (!$node instanceof Node\Stmt\ClassMethod) { - throw new ShouldNotHappenException(\sprintf( - 'Expected node to be instance of "%s", but got instance of "%s" instead.', - Node\Stmt\ClassMethod::class, - \get_class($node), - )); - } - if (0 === \count($this->interfacesImplementedByContainers)) { return []; } diff --git a/src/Methods/NoParameterWithNullDefaultValueRule.php b/src/Methods/NoParameterWithNullDefaultValueRule.php index dd4c0305..d741b5cf 100644 --- a/src/Methods/NoParameterWithNullDefaultValueRule.php +++ b/src/Methods/NoParameterWithNullDefaultValueRule.php @@ -18,7 +18,6 @@ use PHPStan\Analyser; use PHPStan\Reflection; use PHPStan\Rules; -use PHPStan\ShouldNotHappenException; /** * @implements Rules\Rule @@ -34,14 +33,6 @@ public function processNode( Node $node, Analyser\Scope $scope ): array { - if (!$node instanceof Node\Stmt\ClassMethod) { - throw new ShouldNotHappenException(\sprintf( - 'Expected node to be instance of "%s", but got instance of "%s" instead.', - Node\Stmt\ClassMethod::class, - \get_class($node), - )); - } - if (0 === \count($node->params)) { return []; } diff --git a/src/Methods/NoParameterWithNullableTypeDeclarationRule.php b/src/Methods/NoParameterWithNullableTypeDeclarationRule.php index c31ceed2..44e40450 100644 --- a/src/Methods/NoParameterWithNullableTypeDeclarationRule.php +++ b/src/Methods/NoParameterWithNullableTypeDeclarationRule.php @@ -18,7 +18,6 @@ use PHPStan\Analyser; use PHPStan\Reflection; use PHPStan\Rules; -use PHPStan\ShouldNotHappenException; /** * @implements Rules\Rule @@ -34,14 +33,6 @@ public function processNode( Node $node, Analyser\Scope $scope ): array { - if (!$node instanceof Node\Stmt\ClassMethod) { - throw new ShouldNotHappenException(\sprintf( - 'Expected node to be instance of "%s", but got instance of "%s" instead.', - Node\Stmt\ClassMethod::class, - \get_class($node), - )); - } - if (0 === \count($node->params)) { return []; } diff --git a/src/Methods/PrivateInFinalClassRule.php b/src/Methods/PrivateInFinalClassRule.php index 1a478131..cd546874 100644 --- a/src/Methods/PrivateInFinalClassRule.php +++ b/src/Methods/PrivateInFinalClassRule.php @@ -18,7 +18,6 @@ use PHPStan\Analyser; use PHPStan\Reflection; use PHPStan\Rules; -use PHPStan\ShouldNotHappenException; /** * @implements Rules\Rule @@ -34,14 +33,6 @@ public function processNode( Node $node, Analyser\Scope $scope ): array { - if (!$node instanceof Node\Stmt\ClassMethod) { - throw new ShouldNotHappenException(\sprintf( - 'Expected node to be instance of "%s", but got instance of "%s" instead.', - Node\Stmt\ClassMethod::class, - \get_class($node), - )); - } - /** @var Reflection\ClassReflection $containingClass */ $containingClass = $scope->getClassReflection();