[Bug] Incorrect directories provided for PHPStan analyse
StudioMaX opened this issue · comments
According to the documentation of PHPStan we can provide directories with two ways:
- via command line arguments like
vendor/bin/phpstan analyse src tests
(Link) - inside
phpstan.neon
atparameters.paths
(Link)
Also you may notice a big warning here:
You should only analyse files with the code you’ve written yourself. There’s no need to analyse the vendor directory with 3rd party dependencies because it’s not in your power to fix all the mistakes made by the developers you don’t work with directly.
Yes, PHPStan needs to know about all the classes, interfaces, traits, and functions your code uses, but that’s achieved through discovering symbols, not by including the files in the analysis.
So an issue is that we should provide just a single/multiple directory name without an absolute path, or don't provide a directory path at all so that PHPStan will use the directories form phpstan.neon
.
Looking at the history of commits I found that
- At first these arguments were implemented correctly here - #279 . In this case
<BUILD_PATH>
wasn't provided at all to theanalyze
command, and PHPStan picked up these directories from thephpstan.neon
- Then there were an issue with this behavior here - #311 and according to the output log someone specified multiple categories separated with space:
Plugin options: {"allow_failures":true,"binary_path":"vendor/bin","directory":"app tests"}
And with this case PHPStan should be executed like vendor/bin/phpstan analyse app tests
(without absolute paths).
There is also another issue for ignoring unnecessary directories - in fact, these paths do not need to be ignored, since PHPStan only needs to pass the names of the subdirectories that we want to analyze (instead of passing one path to the build root directory).
Expected behavior
PHPStan should analyze only provided subdirectories instead of the main <BUILD_PATH>
.
The "directory" property must be an array to specify multiple directories, or a space-separated string. We do not need to specify the absolute path to the subdirectories, but only the names of these directories. If the "directory" property is not set, then there is no need to pass the path to the build directory to phpstan analyze
so that PHPStan uses the paths from the config or determines the paths to be checked itself.
...
test:
php_stan:
directory: src tests
...
or
...
test:
php_stan:
directory/directories:
- src
- tests
...
With that config this command must be something like:
Shell command: cd "<BUILD_PATH>/" && <BUILD_PATH>/vendor/bin/phpstan analyze --error-format=json src tests
Actual behavior
When test.php_stan.directory
is not specified:
Shell command: cd "<BUILD_PATH>/" && <BUILD_PATH>/vendor/bin/phpstan analyze --error-format=json "<BUILD_PATH>/"
When test.php_stan.directory
is set to src tests
:
Shell command: cd "<BUILD_PATH>/" && <BUILD_PATH>/vendor/bin/phpstan analyze --error-format=json "<BUILD_PATH>/src tests/"
Steps to reproduce
...
test:
php_stan:
directory: src tests
...
and
...
test:
php_stan:
directory: ~
...
Environment
- PHP Censor version: 2.0.3
- Operating System: CentOS 7
- PHP version: 7.4.18
- MySQL/PostgreSQL version: MariaDB 10.1.48
@StudioMaX Thank you for the detailed report!