From fccd0d0c7013257be6c7b4a081807d583be72604 Mon Sep 17 00:00:00 2001 From: Edward Wu Date: Thu, 1 Nov 2018 05:34:50 -0700 Subject: [PATCH] Update to Drupal Coder 8.3.x. (#3132) Fixes #2289 -------- Changes proposed: --------- (What are you proposing we change?) - Update composer contraint on Drupal Coder newest release (8.3.1 as of this date) - Update composer constraint on squizlabs/php_codesniffer to 3.0.1 (matching Coder) - Remove the use of a bootstrap file with PHPCS. File paths that are not allowed by filesets won't be sniffed by default. - Add a tests:phpcs:sniff:modified command to sniff unstaged modified or added files in repo for convenience. --- composer.json | 4 +- src/Robo/Commands/Validate/PhpcsCommand.php | 73 ++++++++++++------- .../phpcs-validate-files-bootstrap.php | 50 ------------- tests/phpunit/Blt/PhpCsTest.php | 2 +- 4 files changed, 48 insertions(+), 81 deletions(-) delete mode 100644 src/Robo/Commands/Validate/phpcs-validate-files-bootstrap.php diff --git a/composer.json b/composer.json index a87d4b3c4..d6ec99bcb 100644 --- a/composer.json +++ b/composer.json @@ -25,14 +25,14 @@ "dflydev/dot-access-data": "^1.1.0", "doctrine/common": "^2.5", "doctrine/inflector": "~1.1.0", - "drupal/coder": "^8.2.11", + "drupal/coder": "^8.3.1", "drush/drush": "^9.4.0", "grasmash/drupal-security-warning": "^1.0.0", "grasmash/yaml-cli": "^1.0.0", "grasmash/yaml-expander": "^1.2.0", "oomphinc/composer-installers-extender": "^1.1", "phpunit/phpunit": "^4.8|^6.5", - "squizlabs/php_codesniffer": "^2.7", + "squizlabs/php_codesniffer": "^3.0.1", "symfony/console": "^3.4.0", "symfony/twig-bridge": "^3.3", "symfony/yaml": "^3.2.8", diff --git a/src/Robo/Commands/Validate/PhpcsCommand.php b/src/Robo/Commands/Validate/PhpcsCommand.php index 28a1c31f9..992c24bf9 100644 --- a/src/Robo/Commands/Validate/PhpcsCommand.php +++ b/src/Robo/Commands/Validate/PhpcsCommand.php @@ -67,46 +67,63 @@ public function sniffFileList($file_list) { $this->say("Sniffing directories containing changed files..."); $files = explode("\n", $file_list); $files = array_filter($files); - $exit_code = $this->doSniffFileList($files); + if ($files) { + $temp_path = $this->getConfigValue('repo.root') . '/tmp/phpcs-fileset'; + $this->taskWriteToFile($temp_path) + ->lines($files) + ->run(); + $arguments = "--file-list='$temp_path' -l"; + $exit_code = $this->doSniff($arguments); + unlink($temp_path); - return $exit_code; + return $exit_code; + } + + return 0; } /** - * Executes PHP Code Sniffer against an array of files. + * Executes PHPCS against (unstaged) modified or untracked files in repo. * - * @param array $files - * A flat array of absolute file paths. + * This command will execute PHP Codesniffer against modified/untracked files + * if those files are a subset of the phpcs.filesets filesets. + * + * @command tests:phpcs:sniff:modified + * @aliases tpsm * * @return int */ - protected function doSniffFileList(array $files) { - if ($files) { - $temp_path = $this->getConfigValue('repo.root') . '/tmp/phpcs-fileset'; - $this->taskWriteToFile($temp_path) - ->lines($files) - ->run(); + public function sniffModified() { + $this->say("Sniffing modified and untracked files in repo..."); + $arguments = "--filter=gitmodified " . $this->getConfigValue('repo.root'); + $exit_code = $this->doSniff($arguments); - $bin = $this->getConfigValue('composer.bin') . '/phpcs'; - $bootstrap = __DIR__ . "/phpcs-validate-files-bootstrap.php"; - $command = "'$bin' --file-list='$temp_path' --bootstrap='$bootstrap' -l"; - if ($this->output()->isVerbose()) { - $command .= ' -v'; - } - elseif ($this->output()->isVeryVerbose()) { - $command .= ' -vv'; - } - $result = $this->taskExecStack() - ->exec($command) - ->printMetadata(FALSE) - ->run(); - - unlink($temp_path); + return $exit_code; + } - return $result->getExitCode(); + /** + * Executes PHP Code Sniffer using specified options/arguments. + * + * @param string $arguments + * The command arguments/options. + * + * @return int + */ + protected function doSniff($arguments) { + $bin = $this->getConfigValue('composer.bin') . '/phpcs'; + $command = "'$bin' $arguments"; + if ($this->output()->isVerbose()) { + $command .= ' -v'; } + elseif ($this->output()->isVeryVerbose()) { + $command .= ' -vv'; + } + $result = $this->taskExecStack() + ->exec($command) + ->printMetadata(FALSE) + ->run(); - return 0; + return $result->getExitCode(); } } diff --git a/src/Robo/Commands/Validate/phpcs-validate-files-bootstrap.php b/src/Robo/Commands/Validate/phpcs-validate-files-bootstrap.php deleted file mode 100644 index eb07a173f..000000000 --- a/src/Robo/Commands/Validate/phpcs-validate-files-bootstrap.php +++ /dev/null @@ -1,50 +0,0 @@ -cli; -$specified_files = $cli->values['files']; -$cli->values['files'] = []; - -// Re-initialize standard to get a list of files and directories that would be -// scanned by default. -$phpcs = new PHP_CodeSniffer($values['verbosity'], NULL, NULL, NULL); -$phpcs->setCli($cli); -$phpcs->initStandard($standard, $values['sniffs'], $values['exclude']); -$values = $this->values; - -// Compute the intersection of the specified files in the default files. -$intersection = []; -foreach ($values['files'] as $file) { - foreach ($specified_files as $specified_file) { - // Scan the specified file if a portion of its path matches one of the - // default files or directories. - if (strpos($specified_file, $file) !== FALSE) { - $intersection[] = $specified_file; - } - } -} - -// Overwrite the files list using the computed intersection. -$values['files'] = $intersection; - -// Return early to prevent hang in stream_get_contents(). -if (empty($values['files'])) { - exit(0); -} diff --git a/tests/phpunit/Blt/PhpCsTest.php b/tests/phpunit/Blt/PhpCsTest.php index 65716edc3..f94bab29d 100644 --- a/tests/phpunit/Blt/PhpCsTest.php +++ b/tests/phpunit/Blt/PhpCsTest.php @@ -18,7 +18,7 @@ class PhpCsTest extends BltTestBase { * @dataProvider testPhpCsFilesBootstrapProvider */ public function testPhpCsFilesBootstrap($filename, $needle, $contains) { - $process = new Process("./vendor/bin/phpcs $filename --bootstrap=src/Robo/Commands/Validate/phpcs-validate-files-bootstrap.php -v"); + $process = new Process("./vendor/bin/phpcs -v $filename"); $process->setWorkingDirectory($this->bltDirectory); $process->run(); $output = $process->getOutput();