Skip to content

Commit

Permalink
Refactor phpstan config files to support multiple php versions
Browse files Browse the repository at this point in the history
  • Loading branch information
tigitz committed May 25, 2022
1 parent 5a355bf commit c359234
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 8 additions & 25 deletions phpstan.neon → phpstan.7.4.neon
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
44 changes: 44 additions & 0 deletions phpstan.8.1.neon
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions phpstan.base.neon
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/Utils/LineAndOffset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit c359234

Please sign in to comment.