squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.66k stars 1.48k forks source link

Parallel running with multiple file directives leads to "passing null to non-nullable" notice on PHP 8.1 #3677

Open Kleinast opened 2 years ago

Kleinast commented 2 years ago

Describe the bug I get an error when run phpcs or phpcs-fix when I upgrade my project on php 8.1. vendor/squizlabs/php_codesniffer/src/Files/LocalFile.php:32 it seem some path files are "null" in LocalFile->files and php 8.1

PHPCS output here

PHP Fatal error: Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /srv/vendor/squizlabs/php_codesniffer/src/Files/LocalFile.php on line 32 in /srv/vendor/squizlabs/php_codesniffer/src/Runner.php:604 Stack trace:

0 [internal function]: PHP_CodeSniffer\Runner->handleErrors()

1 /srv/vendor/squizlabs/php_codesniffer/src/Files/LocalFile.php(32): trim()

2 /srv/vendor/squizlabs/php_codesniffer/src/Files/FileList.php(192): PHP_CodeSniffer\Files\LocalFile->__construct()

3 /srv/vendor/squizlabs/php_codesniffer/src/Runner.php(484): PHP_CodeSniffer\Files\FileList->current()

4 /srv/vendor/squizlabs/php_codesniffer/src/Runner.php(114): PHP_CodeSniffer\Runner->run()

5 /srv/vendor/squizlabs/php_codesniffer/bin/phpcs(18): PHP_CodeSniffer\Runner->runPHPCS()

6 /srv/vendor/bin/phpcs(120): include('...')

7 {main}

thrown in /srv/vendor/squizlabs/php_codesniffer/src/Runner.php on line 604

Fatal error: Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /srv/vendor/squizlabs/php_codesniffer/src/Files/LocalFile.php on line 32 in /srv/vendor/squizlabs/php_codesniffer/src/Runner.php on line 604

Versions (please complete the following information):

This small PR seem fix the problem https://github.com/squizlabs/PHP_CodeSniffer/pull/3676 (but as said in PR, not sure it is a total fix)

jrfnl commented 2 years ago

@Kleinast Could you please elaborate how you got this deprecation notice and how it can be reproduced ?

Kleinast commented 2 years ago
* What type of setup are you using ? (Composer, PHAR, etc)

We use composer

  • How are you running PHPCS ? (CLI, via IDE, etc) running from cli
  • What command are you using ? php vendor/bin/phpcs php vendor/bin/php-cs-fixer fix --dry-run --diff
  • Are you using a custom ruleset or one of the build-in rulesets ? And if so, which ? we are using this package for ruleset https://github.com/Geolid/phpcs
  • Are you using any external standards ? Not sure what you mean by external standards
jrfnl commented 2 years ago

I'll have a look see, but may still need more information.

php vendor/bin/phpcs
php vendor/bin/php-cs-fixer fix --dry-run --diff

Just FYI: php-cs-fixer is a completely different tool. The fixer which is included with PHPCS can be called using phpcbf.

jrfnl commented 2 years ago

Tried to reproduce and failed.

  1. I created a new project.
  2. Ran composer require --dev geolid/phpcs
  3. Created a simple dummy file to run tests on.
  4. Copied the phpcs.xml.dist file to the project root and adjusted the file directive.
  5. Ran vendor/bin/phpcs -ps . and saw the expected CS errors and no errors related to the LocalFile class.

Ran with PHPCS develop and PHP 8.1.10.

In other words, please post an exact reproduction scenario as, as things are, the issue is not reproducible.

Not sure what you mean by external standards

The geolid/phpcs package is an external standard which provides a ruleset.

jrfnl commented 2 years ago

Note: I do see some weird stuff going on in that repo, but not sure if it is related and you should probably report all that to the maintainers of that package:

Based on this, I have a feeling this is an issue with the external standard you are using doing things wrong and not something which needs to be solved in PHPCS.

Kleinast commented 2 years ago

thx a lot for your tests, i'll dig into the geolid/phpcs package or try to reproduce the error on tiny project

Kleinast commented 2 years ago

I tryed to dig, and it seem it is linked to the parallel feature, I get no error on whe it is set to 1 and I get some trim() error when more than 1. I think the batches in parrallel may be create some $path = $todo->key(); = null but I dont understand very much what exactly is the cause of that.

Kleinast commented 2 years ago

I try different configs on my project and found what in my conf create the issue, geolid/phpcs is not the cause I tryed without it. In my config:

<?xml version="1.0"?>
<ruleset name="PHP CS">
    <arg name="basepath" value="."/>
    <arg name="colors"/>
    <arg name="parallel" value="75"/>
    <arg name="extensions" value="php"/>
    <arg value="p"/>

    <file>src</file>
    <file>databases</file>
    <file>tests</file>
    <file>public/index.php</file>
    <file>databases/DoctrineMigrations</file>

    <rule ref="./vendor/geolid/phpcs/src/Geolid/ruleset.xml"/>
</ruleset>

The error trigger when parallel is more than 1 and I reference this files to check:

    <file>databases</file>
    <file>databases/DoctrineMigrations</file>

If I keep only one of this two file to check, I don't get the error anymore So it seem that parrallel testing with config referencing common file créate some null path in FileList->current()

saas786 commented 1 year ago

Same issue here... Apologies, my issue was related to https://github.com/WordPress/WordPress-Coding-Standards

Using

ConEmu64_bn2LNjJKi2

jimmy-sandoval commented 1 year ago

I have the same error in multiple php files:

Using

Screenshot 📷 image

jrfnl commented 1 year ago

@jimmy-sandoval Not the same issue. Different problem source.

jrfnl commented 1 year ago

what can you recommend?

@jimmy-sandoval

  1. Wrong repo - PHPCompatibility is maintained elsewhere.
  2. Don't pollute an unrelated issue with your own issue.
  3. In your case - run PHPCS like so: phpcs -d error_reporting="E_ALL&~E_DEPRECATED" (or adjust in your php.ini) and you should be fine.
jimmy-sandoval commented 1 year ago

@jrfnl thanks a lot, i will remove my comments