From 40d93a147556aa696d1aa6c0e1dad9c7530820e3 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Wed, 5 May 2021 15:03:23 +0200 Subject: [PATCH] missingCheckedExceptionInThrows: true --- build/phpstan.neon | 22 ++++++++++++++++++- src/Command/FixerApplication.php | 3 +++ src/Parser/Parser.php | 2 ++ src/PhpDoc/PhpDocStringResolver.php | 2 +- src/PhpDoc/TypeStringResolver.php | 2 +- .../FileReadTrapStreamWrapper.php | 2 +- src/Testing/ErrorFormatterTestCase.php | 2 +- tests/PHPStan/Command/CommandHelperTest.php | 1 + tests/PHPStan/Tests/AssertionClass.php | 9 +++++--- tests/PHPStan/Tests/AssertionException.php | 8 +++++++ 10 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 tests/PHPStan/Tests/AssertionException.php diff --git a/build/phpstan.neon b/build/phpstan.neon index 59bef3d31a..ec9baf5078 100644 --- a/build/phpstan.neon +++ b/build/phpstan.neon @@ -34,6 +34,8 @@ parameters: - ../tests/PHPStan/Command/IgnoredRegexValidatorTest.php - ../src/Command/IgnoredRegexValidator.php exceptions: + uncheckedExceptionRegexes: + - '~^Exception$~' uncheckedExceptionClasses: - 'PHPStan\ShouldNotHappenException' - 'Symfony\Component\Console\Exception\InvalidArgumentException' @@ -44,8 +46,26 @@ parameters: - 'PHPStan\DependencyInjection\ParameterNotFoundException' - 'PHPStan\Analyser\UndefinedVariableException' - 'RuntimeException' + - 'Nette\Neon\Exception' + - 'Nette\Utils\JsonException' + - 'PHPStan\File\CouldNotReadFileException' + - 'PHPStan\File\CouldNotWriteFileException' + - 'PHPStan\Parser\ParserErrorsException' + - 'ReflectionException' + - 'Nette\Utils\AssertionException' + - 'PHPStan\File\PathNotFoundException' + - 'PHPStan\Broker\ClassNotFoundException' + - 'PHPStan\Broker\FunctionNotFoundException' + - 'PHPStan\Broker\ConstantNotFoundException' + - 'PHPStan\Reflection\MissingMethodFromReflectionException' + - 'PHPStan\Reflection\MissingPropertyFromReflectionException' + - 'PHPStan\Reflection\MissingConstantFromReflectionException' + - 'PHPStan\Type\CircularTypeAliasDefinitionException' + - 'PHPStan\Broker\ClassAutoloadingException' + - 'LogicException' + - 'TypeError' check: - missingCheckedExceptionInThrows: false + missingCheckedExceptionInThrows: true tooWideThrowType: true featureToggles: readComposerPhpVersion: false diff --git a/src/Command/FixerApplication.php b/src/Command/FixerApplication.php index 0f402f1c3f..e231f0d7f5 100644 --- a/src/Command/FixerApplication.php +++ b/src/Command/FixerApplication.php @@ -268,6 +268,9 @@ public function log(string $message): void return 0; } + /** + * @throws FixerProcessException + */ private function getFixerProcess(OutputInterface $output, int $serverPort): Process { if (!@mkdir($this->fixerTmpDir, 0777) && !is_dir($this->fixerTmpDir)) { diff --git a/src/Parser/Parser.php b/src/Parser/Parser.php index 4cea58f895..4ed0100778 100644 --- a/src/Parser/Parser.php +++ b/src/Parser/Parser.php @@ -8,12 +8,14 @@ interface Parser /** * @param string $file path to a file to parse * @return \PhpParser\Node\Stmt[] + * @throws \PHPStan\Parser\ParserErrorsException */ public function parseFile(string $file): array; /** * @param string $sourceCode * @return \PhpParser\Node\Stmt[] + * @throws \PHPStan\Parser\ParserErrorsException */ public function parseString(string $sourceCode): array; diff --git a/src/PhpDoc/PhpDocStringResolver.php b/src/PhpDoc/PhpDocStringResolver.php index 736210037f..b503e5cd13 100644 --- a/src/PhpDoc/PhpDocStringResolver.php +++ b/src/PhpDoc/PhpDocStringResolver.php @@ -24,7 +24,7 @@ public function resolve(string $phpDocString): PhpDocNode { $tokens = new TokenIterator($this->phpDocLexer->tokenize($phpDocString)); $phpDocNode = $this->phpDocParser->parse($tokens); - $tokens->consumeTokenType(Lexer::TOKEN_END); + $tokens->consumeTokenType(Lexer::TOKEN_END); // @phpstan-ignore-line return $phpDocNode; } diff --git a/src/PhpDoc/TypeStringResolver.php b/src/PhpDoc/TypeStringResolver.php index 1f868b9cba..91e8ec4df0 100644 --- a/src/PhpDoc/TypeStringResolver.php +++ b/src/PhpDoc/TypeStringResolver.php @@ -28,7 +28,7 @@ public function resolve(string $typeString, ?NameScope $nameScope = null): Type { $tokens = new TokenIterator($this->typeLexer->tokenize($typeString)); $typeNode = $this->typeParser->parse($tokens); - $tokens->consumeTokenType(Lexer::TOKEN_END); + $tokens->consumeTokenType(Lexer::TOKEN_END); // @phpstan-ignore-line return $this->typeNodeResolver->resolve($typeNode, $nameScope ?? new NameScope(null, [])); } diff --git a/src/Reflection/BetterReflection/SourceLocator/FileReadTrapStreamWrapper.php b/src/Reflection/BetterReflection/SourceLocator/FileReadTrapStreamWrapper.php index 2db6d376a6..7efba32de1 100644 --- a/src/Reflection/BetterReflection/SourceLocator/FileReadTrapStreamWrapper.php +++ b/src/Reflection/BetterReflection/SourceLocator/FileReadTrapStreamWrapper.php @@ -160,7 +160,7 @@ public function stream_stat() public function url_stat($path, $flags) { if (self::$registeredStreamWrapperProtocols === null) { - throw new \LogicException(self::class . ' not registered: cannot operate. Do not call this method directly.'); + throw new \PHPStan\ShouldNotHappenException(self::class . ' not registered: cannot operate. Do not call this method directly.'); } foreach (self::$registeredStreamWrapperProtocols as $protocol) { diff --git a/src/Testing/ErrorFormatterTestCase.php b/src/Testing/ErrorFormatterTestCase.php index 01c48c9c4d..24f38c5848 100644 --- a/src/Testing/ErrorFormatterTestCase.php +++ b/src/Testing/ErrorFormatterTestCase.php @@ -61,7 +61,7 @@ protected function getOutputContent(): string protected function getAnalysisResult(int $numFileErrors, int $numGenericErrors): AnalysisResult { if ($numFileErrors > 4 || $numFileErrors < 0 || $numGenericErrors > 2 || $numGenericErrors < 0) { - throw new \Exception(); + throw new \PHPStan\ShouldNotHappenException(); } $fileErrors = array_slice([ diff --git a/tests/PHPStan/Command/CommandHelperTest.php b/tests/PHPStan/Command/CommandHelperTest.php index 28b8772e46..028f8f2edf 100644 --- a/tests/PHPStan/Command/CommandHelperTest.php +++ b/tests/PHPStan/Command/CommandHelperTest.php @@ -233,6 +233,7 @@ public function dataResolveRelativePaths(): array * @dataProvider dataResolveRelativePaths * @param string $configFile * @param array $expectedParameters + * @throws \PHPStan\Command\InceptionNotSuccessfulException */ public function testResolveRelativePaths( string $configFile, diff --git a/tests/PHPStan/Tests/AssertionClass.php b/tests/PHPStan/Tests/AssertionClass.php index 589993afc2..c529cc62da 100644 --- a/tests/PHPStan/Tests/AssertionClass.php +++ b/tests/PHPStan/Tests/AssertionClass.php @@ -5,18 +5,20 @@ class AssertionClass { + /** @throws \PHPStan\Tests\AssertionException */ public function assertString(?string $arg): bool { if ($arg === null) { - throw new \Exception(); + throw new \PHPStan\Tests\AssertionException(); } return true; } + /** @throws AssertionException */ public static function assertInt(?int $arg): bool { if ($arg === null) { - throw new \Exception(); + throw new \PHPStan\Tests\AssertionException(); } return true; } @@ -24,11 +26,12 @@ public static function assertInt(?int $arg): bool /** * @param mixed $arg * @return bool + * @throws AssertionException */ public function assertNotInt($arg): bool { if (is_int($arg)) { - throw new \Exception(); + throw new \PHPStan\Tests\AssertionException(); } return true; diff --git a/tests/PHPStan/Tests/AssertionException.php b/tests/PHPStan/Tests/AssertionException.php new file mode 100644 index 0000000000..af86b5a256 --- /dev/null +++ b/tests/PHPStan/Tests/AssertionException.php @@ -0,0 +1,8 @@ +