Description
Describe the bug
While testing #1112, I noticed that a fatal error occurs when parallel execution is used and the same file is passed to PHPCS twice.
To reproduce
Steps to reproduce the behavior:
- Run
bin/phpcs test.php test.php --parallel=2
- See error message displayed
PHP Fatal error: Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: trim(): Passing null to parameter #1 ($string) of type string is deprecated in src/Files/LocalFile.php on line 32 in src/Runner.php:624
Stack trace:
#0 [internal function]: PHP_CodeSniffer\Runner->handleErrors()
#1 src/Files/LocalFile.php(32): trim()
#2 src/Files/FileList.php(197): PHP_CodeSniffer\Files\LocalFile->__construct()
#3 src/Runner.php(504): PHP_CodeSniffer\Files\FileList->current()
#4 src/Runner.php(120): PHP_CodeSniffer\Runner->run()
#5 bin/phpcs(14): PHP_CodeSniffer\Runner->runPHPCS()
#6 {main}
thrown in src/Runner.php on line 624
EE 2 / 2 (100%)
PHP Fatal error: Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: One or more child processes failed to run in src/Runner.php:562
Stack trace:
#0 src/Runner.php(120): PHP_CodeSniffer\Runner->run()
#1 bin/phpcs(14): PHP_CodeSniffer\Runner->runPHPCS()
#2 {main}
thrown in src/Runner.php on line 562
Expected behavior
I would expect PHPCS to check the file only once instead of failing with a fatal error.
Versions (please complete the following information)
Operating System | Ubuntu 24.04 |
PHP version | 8.3 |
PHP_CodeSniffer version | master |
Standard | N/A |
Install type | git clone |
Additional context
I believe the problem is in the FileList class. When the same file is passed twice, it adds it only once to the FileList::$files
array, but increments the FileList::$numFiles
twice:
PHP_CodeSniffer/src/Files/FileList.php
Lines 134 to 137 in 8b83793
This causes problems when the Runner class uses the total number of files to calculate the number of files per batch and which files to run in which batch:
PHP_CodeSniffer/src/Runner.php
Lines 463 to 563 in 0632c4c
Please confirm
- I have searched the issue list and am not opening a duplicate issue.
- I have read the Contribution Guidelines and this is not a support question.
- I confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
- I have verified the issue still exists in the
master
branch of PHP_CodeSniffer.