Skip to content

Commit

Permalink
Build/Test Tools: Introduce automated PHP compatibility checking.
Browse files Browse the repository at this point in the history
This change introduces a new Composer script, `compat` that will scan the codebase for (detectable) potential PHP compatibility issues using the `PHP_CodeSniffer` and a custom ruleset based off of the `PHPCompayibilityWP` ruleset (`phpcompat.xml.dist`).

The command will be run as a separate job within each Travis build. While many compatibility issues and false positives have already been corrected in this commit and other Trac tickets, there are still some remaining. For that reason, the job is allowed to fail while the remainder of the potential compatibility issues are investigated and addressed. After those are resolved, the job should be set as required to pass to help prevent new compatibility issues from being introduced.

Props desrosj, jrf, all PHPCompatibilityWP and PHPCompatibility contributors.
Fixes #46152.

git-svn-id: https://develop.svn.wordpress.org/trunk@46290 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
desrosj committed Sep 25, 2019
1 parent bad0295 commit 3369608
Show file tree
Hide file tree
Showing 12 changed files with 251 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ matrix:
name: PHP Linting
- env: WP_TRAVISCI=travis:js WP_INSTALL=false
name: JS Tests
- env: WP_TRAVISCI=test:compat COMPOSER_INSTALL=true NPM_INSTALL=false WP_INSTALL=false
name: "PHP Compatibility Check"
- env: LOCAL_PHP=7.3-fpm WP_TRAVISCI=test:php
name: "PHPUnit Tests: PHP 7.3"
- env: LOCAL_PHP=7.3-fpm LOCAL_PHP_MEMCACHED=true WP_TRAVISCI=test:php
Expand All @@ -46,6 +48,7 @@ matrix:
- env: LOCAL_PHP=8.0-fpm WP_TRAVISCI=test:php
name: "PHPUnit Tests: PHP 8.0"
allow_failures:
- env: WP_TRAVISCI=test:compat COMPOSER_INSTALL=true NPM_INSTALL=false WP_INSTALL=false
- env: LOCAL_PHP=7.4-fpm WP_TRAVISCI=test:php
- env: LOCAL_PHP=8.0-fpm WP_TRAVISCI=test:php
fast_finish: true
Expand Down Expand Up @@ -120,6 +123,8 @@ script:
docker-compose run --rm php composer format &&
docker-compose run --rm php composer lint:errors &&
docker-compose run --rm php composer lint tests
elif [[ "$WP_TRAVISCI" == "test:compat" ]]; then
docker-compose run --rm php composer compat
else
npm run grunt $WP_TRAVISCI
fi
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "~0.5.0",
"wp-coding-standards/wpcs": "~2.1.0"
"wp-coding-standards/wpcs": "~2.1.0",
"phpcompatibility/phpcompatibility-wp": "^2.1.0"
},
"scripts": {
"compat": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcs --standard=phpcompat.xml.dist --report=summary,source",
"format": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcbf --report=summary,source --cache -d memory_limit=256M",
"lint": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcs --report=summary,source --cache -d memory_limit=256M",
"lint:errors": "@lint -n"
Expand Down
164 changes: 162 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions phpcompat.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0"?>
<ruleset name="WordPress PHP Compatibility">
<description>Apply PHP compatibility checks to all WordPress Core files</description>

<rule ref="PHPCompatibilityWP"/>

<!-- WordPress Core currently supports PHP 5.6+ -->
<config name="testVersion" value="5.6-"/>

<!-- Only scan PHP files. -->
<arg name="extensions" value="php"/>

<!-- Whenever possible, cache the scan results and re-use those for unchanged files on the next scan. -->
<arg name="cache"/>

<!-- Set the memory limit to 256M.
For most standard PHP configurations, this means the memory limit will temporarily be raised.
Ref: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#specifying-phpini-settings
-->
<ini name="memory_limit" value="256M"/>

<!-- Strip the filepaths down to the relevant bit. -->
<arg name="basepath" value="./"/>

<!-- Check up to 20 files simultaneously. -->
<arg name="parallel" value="20"/>

<!-- Show sniff codes in all reports -->
<arg value="ps"/>

<!-- For now, only the files in src are scanned. -->
<file>./src/</file>

<!-- Code which doesn't go into production may have different requirements. -->
<exclude-pattern>/node_modules/*</exclude-pattern>

<!--
Currently, there are no dependencies managed by Composer.
This will need to be modified when that changes to ensure external packages meet compatibility requirements.
-->
<exclude-pattern>/vendor/*</exclude-pattern>

<!--
PHPCompatibilityParagonieRandomCompat prevents false positives in `random_compat`.
However, because these files are included in a non-standard path, false positives are triggered in WordPress Core.
-->
<rule ref="PHPCompatibility.IniDirectives.RemovedIniDirectives.mbstring_func_overloadDeprecated">
<exclude-pattern>/random_compat/byte_safe_strings\.php$</exclude-pattern>
</rule>
<rule ref="PHPCompatibility.Constants.RemovedConstants.mcrypt_dev_urandomDeprecatedRemoved">
<exclude-pattern>/random_compat/random_bytes_mcrypt\.php$</exclude-pattern>
</rule>
<rule ref="PHPCompatibility.Extensions.RemovedExtensions.mcryptDeprecatedRemoved">
<exclude-pattern>/random_compat/random_bytes_mcrypt\.php$</exclude-pattern>
</rule>
<rule ref="PHPCompatibility.FunctionUse.RemovedFunctions.mcrypt_create_ivDeprecatedRemoved">
<exclude-pattern>/random_compat/random_bytes_mcrypt\.php$</exclude-pattern>
</rule>

<!-- Whitelist the WP DB Class for use of `mysql_` extension in PHP < 7.0. -->
<rule ref="PHPCompatibility.Extensions.RemovedExtensions">
<exclude-pattern>/src/wp-includes/wp-db\.php</exclude-pattern>
</rule>
</ruleset>
2 changes: 1 addition & 1 deletion src/wp-admin/includes/class-wp-debug-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ static function debug_data() {
if ( isset( $wpdb->use_mysqli ) && $wpdb->use_mysqli ) {
$client_version = $wpdb->dbh->client_info;
} else {
// phpcs:ignore WordPress.DB.RestrictedFunctions.mysql_mysql_get_client_info
// phpcs:ignore WordPress.DB.RestrictedFunctions.mysql_mysql_get_client_info,PHPCompatibility.Extensions.RemovedExtensions.mysql_DeprecatedRemoved
if ( preg_match( '|[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}|', mysql_get_client_info(), $matches ) ) {
$client_version = $matches[0];
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/wp-admin/includes/class-wp-site-health.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private function prepare_sql_data() {
// phpcs:ignore WordPress.DB.RestrictedFunctions.mysql_mysqli_get_server_info
$mysql_server_type = mysqli_get_server_info( $wpdb->dbh );
} else {
// phpcs:ignore WordPress.DB.RestrictedFunctions.mysql_mysql_get_server_info
// phpcs:ignore WordPress.DB.RestrictedFunctions.mysql_mysql_get_server_info,PHPCompatibility.Extensions.RemovedExtensions.mysql_DeprecatedRemoved
$mysql_server_type = mysql_get_server_info( $wpdb->dbh );
}

Expand Down Expand Up @@ -1171,7 +1171,7 @@ public function get_test_utf8mb4_support() {
// phpcs:ignore WordPress.DB.RestrictedFunctions.mysql_mysqli_get_client_info
$mysql_client_version = mysqli_get_client_info();
} else {
// phpcs:ignore WordPress.DB.RestrictedFunctions.mysql_mysql_get_client_info
// phpcs:ignore WordPress.DB.RestrictedFunctions.mysql_mysql_get_client_info,PHPCompatibility.Extensions.RemovedExtensions.mysql_DeprecatedRemoved
$mysql_client_version = mysql_get_client_info();
}

Expand Down
3 changes: 3 additions & 0 deletions src/wp-admin/includes/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,7 @@ function verify_file_signature( $filename, $signatures, $filename_for_errors = f
),
array(
'php' => phpversion(),
// phpcs:ignore PHPCompatibility.Constants.NewConstants.sodium_library_versionFound
'sodium' => defined( 'SODIUM_LIBRARY_VERSION' ) ? SODIUM_LIBRARY_VERSION : ( defined( 'ParagonIE_Sodium_Compat::VERSION_STRING' ) ? ParagonIE_Sodium_Compat::VERSION_STRING : false ),
)
);
Expand Down Expand Up @@ -1313,6 +1314,7 @@ function verify_file_signature( $filename, $signatures, $filename_for_errors = f
),
array(
'php' => phpversion(),
// phpcs:ignore PHPCompatibility.Constants.NewConstants.sodium_library_versionFound
'sodium' => defined( 'SODIUM_LIBRARY_VERSION' ) ? SODIUM_LIBRARY_VERSION : ( defined( 'ParagonIE_Sodium_Compat::VERSION_STRING' ) ? ParagonIE_Sodium_Compat::VERSION_STRING : false ),
'polyfill_is_fast' => false,
'max_execution_time' => ini_get( 'max_execution_time' ),
Expand Down Expand Up @@ -1386,6 +1388,7 @@ function verify_file_signature( $filename, $signatures, $filename_for_errors = f
'skipped_key' => $skipped_key,
'skipped_sig' => $skipped_signature,
'php' => phpversion(),
// phpcs:ignore PHPCompatibility.Constants.NewConstants.sodium_library_versionFound
'sodium' => defined( 'SODIUM_LIBRARY_VERSION' ) ? SODIUM_LIBRARY_VERSION : ( defined( 'ParagonIE_Sodium_Compat::VERSION_STRING' ) ? ParagonIE_Sodium_Compat::VERSION_STRING : false ),
)
);
Expand Down
2 changes: 1 addition & 1 deletion src/wp-admin/includes/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2475,7 +2475,7 @@ function get_alloptions_110() {
* @param string $setting Option name.
* @return mixed
*/
function __get_option( $setting ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionDoubleUnderscore
function __get_option( $setting ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionDoubleUnderscore,PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.FunctionDoubleUnderscore
global $wpdb;

if ( $setting == 'home' && defined( 'WP_HOME' ) ) {
Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/author-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,6 @@ function is_multi_author() {
* @since 3.2.0
* @access private
*/
function __clear_multi_author_cache() { //phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionDoubleUnderscore
function __clear_multi_author_cache() { //phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionDoubleUnderscore,PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.FunctionDoubleUnderscore
delete_transient( 'is_multi_author' );
}
4 changes: 2 additions & 2 deletions src/wp-includes/deprecated.php
Original file line number Diff line number Diff line change
Expand Up @@ -1798,7 +1798,7 @@ function _nc( $single, $plural, $number, $domain = 'default' ) {
* @deprecated 2.8.0 Use _n()
* @see _n()
*/
function __ngettext( ...$args ) {
function __ngettext( ...$args ) { // phpcs:ignore PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.FunctionDoubleUnderscore
_deprecated_function( __FUNCTION__, '2.8.0', '_n()' );
return _n( ...$args );
}
Expand All @@ -1810,7 +1810,7 @@ function __ngettext( ...$args ) {
* @deprecated 2.8.0 Use _n_noop()
* @see _n_noop()
*/
function __ngettext_noop( ...$args ) {
function __ngettext_noop( ...$args ) { // phpcs:ignore PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.FunctionDoubleUnderscore
_deprecated_function( __FUNCTION__, '2.8.0', '_n_noop()' );
return _n_noop( ...$args );

Expand Down
Loading

0 comments on commit 3369608

Please sign in to comment.