Skip to content

Commit cac733d

Browse files
committedFeb 2, 2022
Allow ignoring PHPStan errors by PHP version
This is the approach PHPStan uses to ignore errors conditionally based on the current PHP version We will need this shortly to allow us to use code that uses enums, while still performing analysis on PHP 7.1
1 parent a7ad894 commit cac733d

4 files changed

+32
-0
lines changed
 

‎.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@
2020
/packager.php export-ignore
2121
/phpunit.xml.dist export-ignore
2222
/phpstan.neon.dist export-ignore
23+
/phpstan export-ignore

‎phpstan.neon.dist

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
includes:
2+
- phpstan/ignore-by-php-version.neon.php
3+
14
parameters:
25
level: 6
36

‎phpstan/baseline-less-than-8.1.neon

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
parameters:
2+
ignoreErrors: []
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Ignore errors based on the current PHP version.
7+
*
8+
* This is useful in cases where errors are caused by the use of features that
9+
* do not exist in the current version of PHP, e.g. enums
10+
*
11+
* This is the approach used by PHPStan itself:
12+
* https://github.com/phpstan/phpstan-src/blob/master/build/ignore-by-php-version.neon.php
13+
*/
14+
15+
use PHPStan\DependencyInjection\NeonAdapter;
16+
17+
$config = [];
18+
$adapter = new NeonAdapter();
19+
20+
if (version_compare(PHP_VERSION, '8.1.0', '<')) {
21+
$config = array_merge_recursive($config, $adapter->load(__DIR__.'/baseline-less-than-8.1.neon'));
22+
}
23+
24+
$config['parameters']['phpVersion'] = PHP_VERSION_ID;
25+
26+
return $config;

0 commit comments

Comments
 (0)
Please sign in to comment.