From deaf0c649211426756ce4e24654f27d8cdee73a3 Mon Sep 17 00:00:00 2001 From: Jan Nedbal Date: Tue, 12 Dec 2023 14:47:17 +0100 Subject: [PATCH] Enable more PHPStan's extra strictness (#187) --- phpstan.neon.dist | 4 ++++ src/Rule/ForbidCheckedExceptionInCallableRule.php | 8 ++++---- src/Rule/ForbidUnusedMatchResultRule.php | 10 +++++----- src/Visitor/ImmediatelyCalledCallableVisitor.php | 1 + tests/RuleTestCase.php | 1 + 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 1e33671..b90e74a 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -16,6 +16,8 @@ parameters: tmpDir: cache/phpstan/ checkMissingCallableSignature: true checkUninitializedProperties: true + checkBenevolentUnionTypes: true + checkImplicitMixed: true checkTooWideReturnTypesInProtectedAndPublicMethods: true exceptions: check: @@ -34,6 +36,8 @@ parameters: PHPStan\Rules\Rule: Rule PhpParser\NodeVisitor: Visitor ShipMonk\PHPStan\RuleTestCase: RuleTest + forbidAssignmentNotMatchingVarDoc: + enabled: false # native check is better now; this rule will be dropped / reworked in 3.0 ignoreErrors: - diff --git a/src/Rule/ForbidCheckedExceptionInCallableRule.php b/src/Rule/ForbidCheckedExceptionInCallableRule.php index bbd4821..aabfb9f 100644 --- a/src/Rule/ForbidCheckedExceptionInCallableRule.php +++ b/src/Rule/ForbidCheckedExceptionInCallableRule.php @@ -67,14 +67,14 @@ public function __construct( array $allowedCheckedExceptionCallables ) { + /** @var array> $callablesWithAllowedCheckedExceptions */ + $callablesWithAllowedCheckedExceptions = array_merge_recursive($immediatelyCalledCallables, $allowedCheckedExceptionCallables); + $this->callablesAllowingCheckedExceptions = array_map( function ($argumentIndexes): array { return $this->normalizeArgumentIndexes($argumentIndexes); }, - array_merge_recursive( - $immediatelyCalledCallables, - $allowedCheckedExceptionCallables, - ), + $callablesWithAllowedCheckedExceptions, ); $this->exceptionTypeResolver = $exceptionTypeResolver; $this->reflectionProvider = $reflectionProvider; diff --git a/src/Rule/ForbidUnusedMatchResultRule.php b/src/Rule/ForbidUnusedMatchResultRule.php index 366d621..2d59079 100644 --- a/src/Rule/ForbidUnusedMatchResultRule.php +++ b/src/Rule/ForbidUnusedMatchResultRule.php @@ -10,6 +10,7 @@ use PHPStan\Rules\RuleError; use PHPStan\Rules\RuleErrorBuilder; use PHPStan\Type\NeverType; +use PHPStan\Type\Type; use PHPStan\Type\TypeCombinator; use PHPStan\Type\VerbosityLevel; use ShipMonk\PHPStan\Visitor\UnusedMatchVisitor; @@ -35,11 +36,10 @@ public function processNode(Node $node, Scope $scope): array $returnedTypes = []; foreach ($node->arms as $arm) { - if (method_exists($scope, 'getKeepVoidType')) { // Needed since https://github.com/phpstan/phpstan/releases/tag/1.10.49 - $armType = $scope->getKeepVoidType($arm->body); - } else { - $armType = $scope->getType($arm->body); - } + /** @var Type $armType */ + $armType = method_exists($scope, 'getKeepVoidType') // Needed since https://github.com/phpstan/phpstan/releases/tag/1.10.49, can be dropped once we bump PHPStan version gte that + ? $scope->getKeepVoidType($arm->body) + : $scope->getType($arm->body); if (!$armType->isVoid()->yes() && !$armType instanceof NeverType && !$arm->body instanceof Assign) { $returnedTypes[] = $armType; diff --git a/src/Visitor/ImmediatelyCalledCallableVisitor.php b/src/Visitor/ImmediatelyCalledCallableVisitor.php index f81017b..fbd1eb8 100644 --- a/src/Visitor/ImmediatelyCalledCallableVisitor.php +++ b/src/Visitor/ImmediatelyCalledCallableVisitor.php @@ -52,6 +52,7 @@ public function __construct( array $allowedCheckedExceptionCallables = [] ) { + /** @var array> $callablesWithAllowedCheckedExceptions */ $callablesWithAllowedCheckedExceptions = array_merge_recursive($immediatelyCalledCallables, $allowedCheckedExceptionCallables); foreach ($callablesWithAllowedCheckedExceptions as $call => $arguments) { diff --git a/tests/RuleTestCase.php b/tests/RuleTestCase.php index 79c7ba4..8ac7feb 100644 --- a/tests/RuleTestCase.php +++ b/tests/RuleTestCase.php @@ -40,6 +40,7 @@ private function parseExpectedErrors(string $file): array foreach ($fileDataLines as $line => $row) { $matches = []; + /** @var array{0: list, 1: list} $matches */ $matched = preg_match_all('#// error:(.+)#', $row, $matches); if ($matched === false) {