Skip to content

Commit

Permalink
Revert "Update to Drupal Coder 8.3.x. (#3132)"
Browse files Browse the repository at this point in the history
  • Loading branch information
lcatlett committed Nov 2, 2018
1 parent 8269d5f commit c4a0cb7
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 47 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"grasmash/yaml-expander": "^1.2.0",
"oomphinc/composer-installers-extender": "^1.1",
"phpunit/phpunit": "^4.8|^6.5",
"squizlabs/php_codesniffer": "^3.0.1",
"squizlabs/php_codesniffer": "^2.7",
"symfony/console": "^3.4.0",
"symfony/twig-bridge": "^3.3",
"symfony/yaml": "^3.2.8",
Expand Down
73 changes: 28 additions & 45 deletions src/Robo/Commands/Validate/PhpcsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,63 +67,46 @@ public function sniffFileList($file_list) {
$this->say("Sniffing directories containing changed files...");
$files = explode("\n", $file_list);
$files = array_filter($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;
}
$exit_code = $this->doSniffFileList($files);

return 0;
return $exit_code;
}

/**
* Executes PHPCS against (unstaged) modified or untracked files in repo.
* Executes PHP Code Sniffer against an array of files.
*
* 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
* @param array $files
* A flat array of absolute file paths.
*
* @return int
*/
public function sniffModified() {
$this->say("Sniffing modified and untracked files in repo...");
$arguments = "--filter=gitmodified " . $this->getConfigValue('repo.root');
$exit_code = $this->doSniff($arguments);
protected function doSniffFileList(array $files) {
if ($files) {
$temp_path = $this->getConfigValue('repo.root') . '/tmp/phpcs-fileset';
$this->taskWriteToFile($temp_path)
->lines($files)
->run();

return $exit_code;
}
$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();

/**
* 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';
unlink($temp_path);

return $result->getExitCode();
}
$result = $this->taskExecStack()
->exec($command)
->printMetadata(FALSE)
->run();

return $result->getExitCode();
return 0;
}

}
50 changes: 50 additions & 0 deletions src/Robo/Commands/Validate/phpcs-validate-files-bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/**
* @file
* This bootstrap file should be used only with tests:phpcs:sniff:files.
*
* Its purpose is to take a list of files specified for sniffing, and filter it
* according to the file patterns established in phpcs.xml. By default, phpcs
* will ignore any file patterns defined in phpcs.xml when a list of files is
* specified.
*
* "If you have asked PHP_CodeSniffer to check a specific file rather than an
* entire directory, the extension of the specified file will be ignored."
*
* @see https://github.com/acquia/blt/pull/2126
* @see https://github.com/acquia/blt/issues/2129
* @see https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#specifying-valid-file-extensions
*/

// Unset the list of specified files.
$cli = $phpcs->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);
}
2 changes: 1 addition & 1 deletion tests/phpunit/Blt/PhpCsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class PhpCsTest extends BltTestBase {
* @dataProvider testPhpCsFilesBootstrapProvider
*/
public function testPhpCsFilesBootstrap($filename, $needle, $contains) {
$process = new Process("./vendor/bin/phpcs -v $filename");
$process = new Process("./vendor/bin/phpcs $filename --bootstrap=src/Robo/Commands/Validate/phpcs-validate-files-bootstrap.php -v");
$process->setWorkingDirectory($this->bltDirectory);
$process->run();
$output = $process->getOutput();
Expand Down

0 comments on commit c4a0cb7

Please sign in to comment.