From 6598d09d5800bb13b955d6ad52bf9b25bfd30219 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20Dobe=C5=A1?= Date: Tue, 29 Apr 2025 17:11:57 +0200 Subject: [PATCH 1/3] Check exceptions with PHPStan --- phpstan.neon | 8 ++++++++ src/GrammarProcessing/Ebnf/Parser.php | 4 ++++ src/GrammarProcessing/Grammar.php | 4 ++++ src/GrammarProcessing/TokenStream.php | 3 +++ src/GrammarProcessing/Vocabulary/Symbol.php | 1 + 5 files changed, 20 insertions(+) diff --git a/phpstan.neon b/phpstan.neon index 8d70467..656f76e 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -9,6 +9,14 @@ parameters: - 'var_dump()' message: 'avoid committing debug calls' + exceptions: + check: + missingCheckedExceptionInThrows: true + tooWideThrowType: true + + uncheckedExceptionClasses: + - LogicException + level: 8 paths: - src diff --git a/src/GrammarProcessing/Ebnf/Parser.php b/src/GrammarProcessing/Ebnf/Parser.php index 634880c..e1859b0 100644 --- a/src/GrammarProcessing/Ebnf/Parser.php +++ b/src/GrammarProcessing/Ebnf/Parser.php @@ -37,6 +37,10 @@ public function __construct( + /** + * @throws GrammarProcessing\CannotConsumeTokenException + * @throws GrammarProcessing\UnexpectedTokenException + */ public function parseGrammarFromSource(string $source): GrammarProcessing\Grammar { return $this->parseGrammarFromAbstractSyntaxTree( diff --git a/src/GrammarProcessing/Grammar.php b/src/GrammarProcessing/Grammar.php index 78a504a..c3a5e5d 100644 --- a/src/GrammarProcessing/Grammar.php +++ b/src/GrammarProcessing/Grammar.php @@ -43,6 +43,9 @@ public function __construct( + /** + * @throws UnexpectedTokenException + */ public function tokenizeSource(string $source): TokenStream { return $this->lexicalGrammar->parseSource($source); @@ -55,6 +58,7 @@ public function tokenizeSource(string $source): TokenStream * @param TRootSymbol $rootSymbol * @return AbstractSyntaxTree * @throws CannotConsumeTokenException + * @throws UnexpectedTokenException */ public function parseSource(string $source, string $rootSymbol): AbstractSyntaxTree { diff --git a/src/GrammarProcessing/TokenStream.php b/src/GrammarProcessing/TokenStream.php index 76f34b0..2203bc9 100644 --- a/src/GrammarProcessing/TokenStream.php +++ b/src/GrammarProcessing/TokenStream.php @@ -102,6 +102,9 @@ public function consumeTokenWithType(string $type): Token + /** + * @throws CannotConsumeTokenException + */ public function consumeTokenWithValue(string $value): Token { if (isset($this->tokens[$this->currentToken]) === FALSE) { diff --git a/src/GrammarProcessing/Vocabulary/Symbol.php b/src/GrammarProcessing/Vocabulary/Symbol.php index 7696e0a..d0ced16 100644 --- a/src/GrammarProcessing/Vocabulary/Symbol.php +++ b/src/GrammarProcessing/Vocabulary/Symbol.php @@ -17,6 +17,7 @@ function getPattern(array $nonterminals): string; /** * @param array $nonterminals + * @throws GrammarProcessing\CannotConsumeTokenException */ function acceptNode( GrammarProcessing\Error $error, From aafd34f8d496da35b1ee5a7e73953bbb32f24641 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20Dobe=C5=A1?= Date: Tue, 29 Apr 2025 17:13:52 +0200 Subject: [PATCH 2/3] Enable checkMissingCallableSignature: true in PHPStan --- phpstan.neon | 1 + 1 file changed, 1 insertion(+) diff --git a/phpstan.neon b/phpstan.neon index 656f76e..f09d260 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -3,6 +3,7 @@ includes: - vendor/spaze/phpstan-disallowed-calls/extension.neon parameters: + checkMissingCallableSignature: true disallowedFunctionCalls: - function: - 'dump()' From 4f4660164dbbae2c1873d76d510dc66256531f1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20Dobe=C5=A1?= Date: Tue, 29 Apr 2025 17:15:50 +0200 Subject: [PATCH 3/3] Enable phpstan-strict-rules --- composer.json | 1 + composer.lock | 50 ++++++++++++++++++++++++++++++++++++++++++- phpstan-baseline.neon | 6 ++++++ phpstan.neon | 1 + 4 files changed, 57 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index ebc2bb4..3d7f02a 100644 --- a/composer.json +++ b/composer.json @@ -35,6 +35,7 @@ "require-dev": { "nette/tester": "^2.5.4", "phpstan/phpstan": "^2.1.12", + "phpstan/phpstan-strict-rules": "^2.0.4", "spaze/phpstan-disallowed-calls": "^4.5.0", "tracy/tracy": "^2.10.9" }, diff --git a/composer.lock b/composer.lock index 379cc00..587c714 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "aea19a6b932b3ab92ec26d21f5e10b70", + "content-hash": "e5b5acae37b59710218e5b85a6e9bd7b", "packages": [], "packages-dev": [ { @@ -140,6 +140,54 @@ ], "time": "2025-04-16T13:19:18+00:00" }, + { + "name": "phpstan/phpstan-strict-rules", + "version": "2.0.4", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan-strict-rules.git", + "reference": "3e139cbe67fafa3588e1dbe27ca50f31fdb6236a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan-strict-rules/zipball/3e139cbe67fafa3588e1dbe27ca50f31fdb6236a", + "reference": "3e139cbe67fafa3588e1dbe27ca50f31fdb6236a", + "shasum": "" + }, + "require": { + "php": "^7.4 || ^8.0", + "phpstan/phpstan": "^2.0.4" + }, + "require-dev": { + "php-parallel-lint/php-parallel-lint": "^1.2", + "phpstan/phpstan-deprecation-rules": "^2.0", + "phpstan/phpstan-phpunit": "^2.0", + "phpunit/phpunit": "^9.6" + }, + "type": "phpstan-extension", + "extra": { + "phpstan": { + "includes": [ + "rules.neon" + ] + } + }, + "autoload": { + "psr-4": { + "PHPStan\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Extra strict and opinionated rules for PHPStan", + "support": { + "issues": "https://github.com/phpstan/phpstan-strict-rules/issues", + "source": "https://github.com/phpstan/phpstan-strict-rules/tree/2.0.4" + }, + "time": "2025-03-18T11:42:40+00:00" + }, { "name": "spaze/phpstan-disallowed-calls", "version": "v4.5.0", diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index b1a5ba7..01487cf 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -24,6 +24,12 @@ parameters: count: 1 path: src/GrammarProcessing/LexicalGrammar.php + - + message: '#^Only numeric types are allowed in \-, int\|false given on the right side\.$#' + identifier: minus.rightNonNumeric + count: 1 + path: src/GrammarProcessing/LocationGetter.php + - message: '#^Parameter \$column of class Vojtechdobes\\GrammarProcessing\\Location constructor expects int\<0, max\>, int given\.$#' identifier: argument.type diff --git a/phpstan.neon b/phpstan.neon index f09d260..f63e137 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,6 @@ includes: - phpstan-baseline.neon + - vendor/phpstan/phpstan-strict-rules/rules.neon - vendor/spaze/phpstan-disallowed-calls/extension.neon parameters: