-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix checks for PHP 8.1 and above #746
Conversation
Make it clear which ruleset being tested, even on a test failure.
PHP 8.1 increased the strictness of types used with native functions, and this meant WPCS 2.3.0 had a bug: WordPress/WordPress-Coding-Standards#2035 For the VIPCS ruleset tests with PHP 8.1 or above it would skip over a bunch of lines, and mark other lines as expecting errors or warnings incorrectly. Running: ``` vendor/squizlabs/php_codesniffer/bin/phpcs --standard=WordPressVIPMinimum --severity=1 WordPressVIPMinimum/ruleset-test.inc -s ``` ...shows all the expected violations, but also this for line 1: > An error occurred during processing; checking has been aborted. The error message was: trim(): Passing null to parameter #1 ($string) of type string is deprecated in .../vipcs/vendor/wp-coding-standards/wpcs/WordPress/Sniff.php on line 1144 (Internal.Exception) Line 1144 relate to the retrieval of the `minimum_supported_wp_version` config value. Since it appeared to be coming through as null, the RulesetTest.php now includes setting this as a command-line argument when PHPCS is called to run the ruleset test. When WPCS 3.0 is the minimum supported, these changes can be reverted.
PHP 8.1 increased the strictness of types used with native functions, and this meant WPCS 2.3.0 had a bug: WordPress/WordPress-Coding-Standards#2035 For the VIPCS coding standards check with PHP 8.1 or above it would report incorrect violations from sniffs that included the retrieval of optional command-line arguments. These violations did not appear when running PHPCS with PHP 8.0 or below. Since the retrieval of the optional command-line arguments appeared to be coming through as null, causing the problems, the command to run PHPCS on the VIPCS codebase now includes setting of several command-line arguments. With the `prefixes` enabled, extra sniff behaviour is enabled, and VIPCS has no need to adhere to consistent prefixes (unlike actual WordPress plugin and theme codebases). As such, the PrefixAllGlobals rule is excluded for VIPCS. When WPCS 3.0 is the minimum supported, these changes can be reverted.
When running the unit tests with PHP 8.1 or above, PHPUnit would give a deprecation notice: > PHP Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in .../vipcs/vendor/phpunit/phpunit/src/Util/Getopt.php on line 159 The tests still ran successfully though. This notice originated from the use of `--filter WordPressVIPMinimum`, and was fixed by adding an equals sign: `--filter=WordPressVIPMinimum`. Under both versions, the same number of tests and test files and unique error codes were created.
"$(pwd)/vendor/bin/phpunit" --filter WordPressVIPMinimum "$(pwd)/vendor/squizlabs/php_codesniffer/tests/AllTests.php" --no-coverage --no-configuration --bootstrap=./tests/bootstrap.php --dont-report-useless-tests $@ | ||
"$(pwd)/vendor/bin/phpunit" --filter=WordPressVIPMinimum "$(pwd)/vendor/squizlabs/php_codesniffer/tests/AllTests.php" --no-coverage --no-configuration --bootstrap=./tests/bootstrap.php --dont-report-useless-tests $@ | ||
else | ||
"$(pwd)/vendor/bin/phpunit" --filter WordPressVIPMinimum "$(pwd)/vendor/squizlabs/php_codesniffer/tests/AllTests.php" --no-coverage $@ | ||
"$(pwd)/vendor/bin/phpunit" --filter=WordPressVIPMinimum "$(pwd)/vendor/squizlabs/php_codesniffer/tests/AllTests.php" --no-coverage $@ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one surprises me as WPCS itself does not appear to be afflicted by the same issue, which makes me think something else is going on....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What version of PHPUnit are you running the tests on ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, IIRC, for PHP 8.1+, this is solved via the --no-configuration
bit, so if that's not the case for you, I suspect there is something else going on with your setup.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What version of PHPUnit are you running the tests on ?
7.5.20.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And the --no-configuration
gets applied correctly ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing the Composer script to @php ./vendor/phpunit/phpunit/phpunit --filter WordPress ./vendor/squizlabs/php_codesniffer/tests/AllTests.php --no-configuration
and then running it produces even more errors.
Details
PHP Warning: Undefined global variable $PHP_CODESNIFFER_PEAR in /Users/gary/code/wpcs/vendor/squizlabs/php_codesniffer/tests/AllTests.php on line 12
Warning: Undefined global variable $PHP_CODESNIFFER_PEAR in /Users/gary/code/wpcs/vendor/squizlabs/php_codesniffer/tests/AllTests.php on line 12
PHP Warning: include_once(CodeSniffer/Core/AllTests.php): Failed to open stream: No such file or directory in /Users/gary/code/wpcs/vendor/squizlabs/php_codesniffer/tests/AllTests.php on line 16
Warning: include_once(CodeSniffer/Core/AllTests.php): Failed to open stream: No such file or directory in /Users/gary/code/wpcs/vendor/squizlabs/php_codesniffer/tests/AllTests.php on line 16
PHP Warning: include_once(): Failed opening 'CodeSniffer/Core/AllTests.php' for inclusion (include_path='.:/usr/local/Cellar/php@8.1/8.1.13/share/php@8.1/pear') in /Users/gary/code/wpcs/vendor/squizlabs/php_codesniffer/tests/AllTests.php on line 16
Warning: include_once(): Failed opening 'CodeSniffer/Core/AllTests.php' for inclusion (include_path='.:/usr/local/Cellar/php@8.1/8.1.13/share/php@8.1/pear') in /Users/gary/code/wpcs/vendor/squizlabs/php_codesniffer/tests/AllTests.php on line 16
PHP Warning: include_once(CodeSniffer/Standards/AllSniffs.php): Failed to open stream: No such file or directory in /Users/gary/code/wpcs/vendor/squizlabs/php_codesniffer/tests/AllTests.php on line 17
Warning: include_once(CodeSniffer/Standards/AllSniffs.php): Failed to open stream: No such file or directory in /Users/gary/code/wpcs/vendor/squizlabs/php_codesniffer/tests/AllTests.php on line 17
PHP Warning: include_once(): Failed opening 'CodeSniffer/Standards/AllSniffs.php' for inclusion (include_path='.:/usr/local/Cellar/php@8.1/8.1.13/share/php@8.1/pear') in /Users/gary/code/wpcs/vendor/squizlabs/php_codesniffer/tests/AllTests.php on line 17
Warning: include_once(): Failed opening 'CodeSniffer/Standards/AllSniffs.php' for inclusion (include_path='.:/usr/local/Cellar/php@8.1/8.1.13/share/php@8.1/pear') in /Users/gary/code/wpcs/vendor/squizlabs/php_codesniffer/tests/AllTests.php on line 17
PHP Fatal error: Uncaught Error: Class "PHP_CodeSniffer\Tests\Core\AllTests" not found in /Users/gary/code/wpcs/vendor/squizlabs/php_codesniffer/tests/AllTests.php:56
Stack trace:
#0 [internal function]: PHP_CodeSniffer\Tests\PHP_CodeSniffer_AllTests::suite('PHP_CodeSniffer...')
#1 /Users/gary/code/wpcs/vendor/phpunit/phpunit/src/Runner/BaseTestRunner.php(125): ReflectionMethod->invoke(NULL, 'PHP_CodeSniffer...')
#2 /Users/gary/code/wpcs/vendor/phpunit/phpunit/src/TextUI/Command.php(183): PHPUnit\Runner\BaseTestRunner->getTest('./vendor/squizl...', '/Users/gary/cod...', Array)
#3 /Users/gary/code/wpcs/vendor/phpunit/phpunit/src/TextUI/Command.php(162): PHPUnit\TextUI\Command->run(Array, true)
#4 /Users/gary/code/wpcs/vendor/phpunit/phpunit/phpunit(61): PHPUnit\TextUI\Command::main()
#5 {main}
thrown in /Users/gary/code/wpcs/vendor/squizlabs/php_codesniffer/tests/AllTests.php on line 56
Fatal error: Uncaught Error: Class "PHP_CodeSniffer\Tests\Core\AllTests" not found in /Users/gary/code/wpcs/vendor/squizlabs/php_codesniffer/tests/AllTests.php:56
Stack trace:
#0 [internal function]: PHP_CodeSniffer\Tests\PHP_CodeSniffer_AllTests::suite('PHP_CodeSniffer...')
#1 /Users/gary/code/wpcs/vendor/phpunit/phpunit/src/Runner/BaseTestRunner.php(125): ReflectionMethod->invoke(NULL, 'PHP_CodeSniffer...')
#2 /Users/gary/code/wpcs/vendor/phpunit/phpunit/src/TextUI/Command.php(183): PHPUnit\Runner\BaseTestRunner->getTest('./vendor/squizl...', '/Users/gary/cod...', Array)
#3 /Users/gary/code/wpcs/vendor/phpunit/phpunit/src/TextUI/Command.php(162): PHPUnit\TextUI\Command->run(Array, true)
#4 /Users/gary/code/wpcs/vendor/phpunit/phpunit/phpunit(61): PHPUnit\TextUI\Command::main()
#5 {main}
thrown in /Users/gary/code/wpcs/vendor/squizlabs/php_codesniffer/tests/AllTests.php on line 56
Script @php ./vendor/phpunit/phpunit/phpunit --filter WordPress ./vendor/squizlabs/php_codesniffer/tests/AllTests.php --no-configuration handling the run-tests event returned with error code 255
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds like this would be better debugged via a videocall with screenshare at some point... ? Feel free to ping me to set it up.
Running
composer test
(orcomposer check
after #741 is merged) would lead to several issues under PHP 8.1 and 8.2. This PR fixes those issues.Ruleset tests: Add label before test runs
Make it clear which ruleset is being tested, even on a test failure. This just makes it easier to debug.
Ruleset Tests: Workaround WPCS trim() bug
PHP 8.1 increased the strictness of types used with native functions, and this meant WPCS 2.3.0 had a bug: WordPress/WordPress-Coding-Standards#2035
For the VIPCS ruleset tests with PHP 8.1 or above, it would skip over a bunch of lines in the test file and report other lines as expecting errors or warnings incorrectly.
Running:
...shows all the expected violations, but also this for line 1:
Line 1144 relates to the retrieval of the
minimum_supported_wp_version
config value. Since it appeared to be coming through asnull
, theRulesetTest.php
now includes setting a trivial value via a command-line argument when PHPCS is called to run the ruleset test.When WPCS 3.0 is the minimum supported, this change can be reverted since the original bug would have been fixed.
CS: Work around WPCS trim() bug
PHP 8.1 increased the strictness of types used with native functions, and this meant WPCS 2.3.0 had a bug: WordPress/WordPress-Coding-Standards#2035
The VIPCS coding standards check with PHP 8.1 or above would report incorrect violations from sniffs that included the retrieval of optional command-line arguments. These violations did not appear when running PHPCS with PHP 8.0 or below.
Since the retrieval of the optional command-line arguments appeared to be coming through as null, causing the problems, the command to run PHPCS on the VIPCS codebase now includes the setting of several command-line arguments.
With the
prefixes
enabled, extra sniff behaviour is enabled, and VIPCS does not need to adhere to consistent prefixes (unlike actual WordPress plugin and theme codebases). As such, the PrefixAllGlobals rule is excluded for VIPCS.When WPCS 3.0 is the minimum supported, these changes can be reverted since the original bug would have been fixed.
Unit tests: Fix PHPUnit deprecation
When running the unit tests with PHP 8.1 or above, PHPUnit would give a deprecation notice:
The tests still ran successfully, though.
This notice originated from the use of
--filter WordPressVIPMinimum
and was fixed by adding an equals sign:--filter=WordPressVIPMinimum
.Under both versions, the same number of tests and test files and unique error codes were created.