diff --git a/Gruntfile.js b/Gruntfile.js index f96eda8ce5f61..d460e7c64e44b 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -1840,6 +1840,7 @@ module.exports = function(grunt) { grunt.registerTask( 'precommit:php', 'Runs PHPStan and PHPUnit tests in preparation for a commit.', [ 'phpstan', + 'compat:php', 'phpunit' ] ); @@ -1959,11 +1960,11 @@ module.exports = function(grunt) { // Find .php files that have been modified or added. PHPfiles = PHPfiles.filter( function( file ) { - return /^\s*[MA]\s*.*\.php$/.test( file ); + return /^\s*[MA]{1,2}\s+.*\.php$/.test( file ); } ); PHPfiles = PHPfiles.map( function( file ) { - return file.replace( /^\s*[MA]\s*/, '' ); + return file.replace( /^\s*[MA]{1,2}\s+/, '' ); } ); changedFiles = { @@ -2432,9 +2433,7 @@ module.exports = function(grunt) { grunt.registerTask( 'format:php', 'Runs the code formatter on changed files.', function() { var done = this.async(); var flags = this.flags; - var args = changedFiles.php; - - args.unshift( 'format' ); + var args = [ 'format' ].concat( changedFiles.php ); grunt.util.spawn( { cmd: 'composer', @@ -2452,9 +2451,7 @@ module.exports = function(grunt) { grunt.registerTask( 'lint:php', 'Runs the code linter on changed files.', function() { var done = this.async(); var flags = this.flags; - var args = changedFiles.php; - - args.unshift( 'lint' ); + var args = [ 'lint' ].concat( changedFiles.php ); grunt.util.spawn( { cmd: 'composer', @@ -2469,6 +2466,31 @@ module.exports = function(grunt) { } ); } ); + grunt.registerTask( 'compat:php', 'Runs the PHPCompatibility ruleset on changed files.', function() { + var done = this.async(); + + // phpcompat.xml.dist only scans src/; paths passed on the command line bypass its selection. + var files = changedFiles.php.filter( function( file ) { + return 0 === file.indexOf( 'src/' ); + } ); + + if ( changedFiles.php.length && ! files.length ) { + grunt.log.writeln( 'No changed PHP files in src/. Skipping PHP compatibility checks.' ); + done(); + return; + } + + var args = [ 'compat' ].concat( files ); + + grunt.util.spawn( { + cmd: 'composer', + args: args, + opts: { stdio: 'inherit' } + }, function( error ) { + done( ! error ); + } ); + } ); + grunt.registerTask( 'wp-packages:update', 'Update WordPress packages', function() { const distTag = grunt.option('dist-tag') || 'latest'; grunt.log.writeln( `Updating WordPress packages (--dist-tag=${distTag})` );