From c3592343073dc979373ecbed1982c5ef76ff5a42 Mon Sep 17 00:00:00 2001 From: tigitz Date: Wed, 25 May 2022 09:29:11 +0200 Subject: [PATCH] Refactor phpstan config files to support multiple php versions --- .github/workflows/phpstan.yml | 2 +- Makefile | 9 ++++--- phpstan.neon => phpstan.7.4.neon | 33 ++++++------------------ phpstan.8.1.neon | 44 ++++++++++++++++++++++++++++++++ phpstan.base.neon | 19 ++++++++++++++ src/Utils/LineAndOffset.php | 2 +- 6 files changed, 78 insertions(+), 31 deletions(-) rename phpstan.neon => phpstan.7.4.neon (70%) create mode 100644 phpstan.8.1.neon create mode 100644 phpstan.base.neon diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index b411eca..14b72ed 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -33,4 +33,4 @@ jobs: run: composer install -n --prefer-dist - name: Run PHPStan - run: ./vendor/bin/phpstan analyse src + run: ./vendor/bin/phpstan analyse src -c phpstan.${{ matrix.php }}.neon diff --git a/Makefile b/Makefile index 7946277..9a3baf7 100644 --- a/Makefile +++ b/Makefile @@ -66,13 +66,14 @@ phpcbf: PHP_VERSION=7.4 $(PHP_CS_FIXER) phpstan: vendor - $(EXEC_PHP) vendor/bin/phpstan analyse src -c phpstan.neon -a vendor/autoload.php + $(EXEC_PHP) vendor/bin/phpstan analyse src -c phpstan.$(PHP_VERSION).neon -a vendor/autoload.php -phpstan: vendor - $(EXEC_PHP) vendor/bin/phpstan analyse src -c phpstan.neon -a vendor/autoload.php +phpstan-all-php-versions: + PHP_VERSION=7.4 make phpstan + PHP_VERSION=8.1 make phpstan phpstan-baseline: vendor - $(EXEC_PHP) vendor/bin/phpstan analyse src -c phpstan.neon -a vendor/autoload.php --generate-baseline + $(EXEC_PHP) vendor/bin/phpstan analyse src -c phpstan.$(PHP_VERSION).neon -a vendor/autoload.php --generate-baseline infection: vendor $(EXEC_PHP) vendor/bin/phpunit --coverage-xml=build/coverage/coverage-xml --log-junit=build/coverage/phpunit.junit.xml diff --git a/phpstan.neon b/phpstan.7.4.neon similarity index 70% rename from phpstan.neon rename to phpstan.7.4.neon index 96eb35f..b00843d 100644 --- a/phpstan.neon +++ b/phpstan.7.4.neon @@ -1,22 +1,9 @@ includes: - - vendor/phpstan/phpstan-webmozart-assert/extension.neon - - vendor/phpstan/phpstan-strict-rules/rules.neon - - vendor/thecodingmachine/phpstan-safe-rule/phpstan-safe-rule.neon - - vendor/phpstan/phpstan-phpunit/extension.neon + - phpstan.base.neon parameters: - level: max - - # Some errors ignored are specific to a php version so they're reported as unmatched when phpstan is ran with a - # different php version - reportUnmatchedIgnoredErrors: false - - treatPhpDocTypesAsCertain: false ignoreErrors: - # Missing strict comparison - - '#^Construct empty\(\) is not allowed. Use more strict comparison.$#' - - # thecodingmachine/safe and thecodingmachine/phpstan-safe-rule don't support 8.1 Pspell + # php 8.1 code - message: "#^Function pspell_config_create is unsafe to use\\. It can return FALSE instead of throwing an exception\\. Please add 'use function Safe\\\\pspell_config_create;' at the beginning of the file to use the variant provided by the 'thecodingmachine/safe' library\\.$#" count: 1 @@ -37,16 +24,6 @@ parameters: count: 1 path: src/Spellchecker/PHPPspell.php - - - message: "#^Parameter \\#1 \\$dictionary of function pspell_check expects int, int\\|false given\\.$#" - count: 1 - path: src/Spellchecker/PHPPspell.php - - - - message: "#^Parameter \\#1 \\$dictionary of function pspell_suggest expects int, int\\|false given\\.$#" - count: 1 - path: src/Spellchecker/PHPPspell.php - - message: "#^Parameter \\#1 \\$conf of function pspell_config_ignore expects int, int\\|false given\\.$#" count: 1 @@ -71,3 +48,9 @@ parameters: message: "#^Parameter \\#1 \\$pspell of function pspell_suggest expects int, int\\|false given\\.$#" count: 1 path: src/Spellchecker/PHPPspell.php + + # preg_match_all can be null in php >8 + - + message: "#^Casting to int something that's already int\\.$#" + count: 1 + path: src/Utils/LineAndOffset.php diff --git a/phpstan.8.1.neon b/phpstan.8.1.neon new file mode 100644 index 0000000..aaf1961 --- /dev/null +++ b/phpstan.8.1.neon @@ -0,0 +1,44 @@ +includes: + - phpstan.base.neon + +parameters: + ignoreErrors: + - + message: "#^Function pspell_config_create is unsafe to use\\. It can return FALSE instead of throwing an exception\\. Please add 'use function Safe\\\\pspell_config_create;' at the beginning of the file to use the variant provided by the 'thecodingmachine/safe' library\\.$#" + count: 1 + path: src/Spellchecker/PHPPspell.php + + - + message: "#^Function pspell_config_ignore is unsafe to use\\. It can return FALSE instead of throwing an exception\\. Please add 'use function Safe\\\\pspell_config_ignore;' at the beginning of the file to use the variant provided by the 'thecodingmachine/safe' library\\.$#" + count: 1 + path: src/Spellchecker/PHPPspell.php + + - + message: "#^Function pspell_config_mode is unsafe to use\\. It can return FALSE instead of throwing an exception\\. Please add 'use function Safe\\\\pspell_config_mode;' at the beginning of the file to use the variant provided by the 'thecodingmachine/safe' library\\.$#" + count: 1 + path: src/Spellchecker/PHPPspell.php + + - + message: "#^Function pspell_new_config is unsafe to use\\. It can return FALSE instead of throwing an exception\\. Please add 'use function Safe\\\\pspell_new_config;' at the beginning of the file to use the variant provided by the 'thecodingmachine/safe' library\\.$#" + count: 1 + path: src/Spellchecker/PHPPspell.php + + - + message: "#^Parameter \\#1 \\$dictionary of function pspell_check expects PSpell\\\\Dictionary, PSpell\\\\Dictionary\\|false given\\.$#" + count: 1 + path: src/Spellchecker/PHPPspell.php + + - + message: "#^Parameter \\#1 \\$dictionary of function pspell_check expects PSpell\\\\Dictionary, int given\\.$#" + count: 1 + path: src/Spellchecker/PHPPspell.php + + - + message: "#^Parameter \\#1 \\$dictionary of function pspell_suggest expects PSpell\\\\Dictionary, PSpell\\\\Dictionary\\|false given\\.$#" + count: 1 + path: src/Spellchecker/PHPPspell.php + + - + message: "#^Parameter \\#1 \\$dictionary of function pspell_suggest expects PSpell\\\\Dictionary, int given\\.$#" + count: 1 + path: src/Spellchecker/PHPPspell.php diff --git a/phpstan.base.neon b/phpstan.base.neon new file mode 100644 index 0000000..47a5b80 --- /dev/null +++ b/phpstan.base.neon @@ -0,0 +1,19 @@ +includes: + - vendor/phpstan/phpstan-webmozart-assert/extension.neon + - vendor/phpstan/phpstan-strict-rules/rules.neon + - vendor/thecodingmachine/phpstan-safe-rule/phpstan-safe-rule.neon + - vendor/phpstan/phpstan-phpunit/extension.neon + +parameters: + level: max + + treatPhpDocTypesAsCertain: false + ignoreErrors: + # Missing strict comparison + - '#^Construct empty\(\) is not allowed. Use more strict comparison.$#' + + # function_exists call + - + message: "#^Class PhpSpellcheck\\\\t not found\\.$#" + count: 1 + path: src/Text/functions.php diff --git a/src/Utils/LineAndOffset.php b/src/Utils/LineAndOffset.php index 872d404..ea1371d 100644 --- a/src/Utils/LineAndOffset.php +++ b/src/Utils/LineAndOffset.php @@ -31,7 +31,7 @@ public static function findFromFirstCharacterOffset(string $text, int $offsetFro } $textBeforeOffset = mb_substr($text, 0, $offsetFromFirstCharacter); - $line = \Safe\preg_match_all('/\R/u', $textBeforeOffset, $matches) + 1; + $line = ((int) \Safe\preg_match_all('/\R/u', $textBeforeOffset, $matches)) + 1; $offsetOfPreviousLinebreak = mb_strrpos($textBeforeOffset, PHP_EOL, 0); $offset = $offsetFromFirstCharacter - ($offsetOfPreviousLinebreak !== false ? $offsetOfPreviousLinebreak + 1 : 0);