Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .ddev/web-build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ RUN set -eux; \
--no-install-recommends \
--no-install-suggests \
optipng \
parallel \
jpegoptim; \
apt-get clean; \
rm -rf /var/lib/apt/lists/*
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ jobs:
- PHP_MEMORY_LIMIT=2G ./vendor/bin/phpstan --no-progress analyse -c phpstan.neon

- stage: Lint
addons:
apt:
packages:
- parallel
name: Drupal coding standard
script:
- composer install || travis_terminate 1;
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

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

62 changes: 40 additions & 22 deletions robo-components/PhpcsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,7 @@ trait PhpcsTrait {
* successful.
*/
public function phpcs(): ?ResultData {
$standards = [
'Drupal',
'DrupalPractice',
];

$commands = [
'phpcbf',
'phpcs',
];
$standards = 'Drupal,DrupalPractice';

$directories = [
'modules/custom',
Expand All @@ -37,26 +29,52 @@ public function phpcs(): ?ResultData {
'sites/bot_trap_protection.php',
'../phpstan-rules',
'../.bootstrap-fast.php',
'../scripts',
];

$error_code = NULL;
$arguments = "--standard=$standards -p --ignore=" . self::$themeName . "/dist,node_modules,.parcel-cache --colors --extensions=php,module,inc,install,test,profile,theme,css,yaml,txt,md";

foreach ($directories as $directory) {
foreach ($standards as $standard) {
$arguments = "--parallel=8 --standard=$standard -p --ignore=" . self::$themeName . "/dist,node_modules,server_default_content/content --colors --extensions=php,module,inc,install,test,profile,theme,css,yaml,yml,txt,md";

foreach ($commands as $command) {
$result = $this->_exec("cd web && ../vendor/bin/$command $directory $arguments");
if (empty($error_code) && !$result->wasSuccessful()) {
$error_code = $result->getExitCode();
}
}
// Step 1: Auto-fix what can be fixed (only if not in CI).
// In CI, phpcbf can't commit changes, so we skip it for better performance.
$is_ci = !empty(getenv('CI'));
if (!$is_ci) {
$this->say('Running phpcbf to auto-fix coding standard violations...');
$command_list = [];
foreach ($directories as $directory) {
// Phpcbf exits with non-zero even on success when it fixes files.
// We don't fail on phpcbf errors since phpcs will catch real issues.
$command_list[] = "cd web && ../vendor/bin/phpcbf $directory $arguments || true";
}

$commands_file = tempnam(sys_get_temp_dir(), 'phpcbf_commands');
file_put_contents($commands_file, implode("\n", $command_list));

$this->_exec("parallel -j+0 < $commands_file");
unlink($commands_file);
}

if (!empty($error_code)) {
return new ResultData($error_code, 'PHPCS found some issues');
// Step 2: Check for remaining violations.
$this->say('Running phpcs to check for coding standard violations...');
$command_list = [];
foreach ($directories as $directory) {
$command_list[] = "cd web && ../vendor/bin/phpcs $directory $arguments";
}

$commands_file = tempnam(sys_get_temp_dir(), 'phpcs_commands');
file_put_contents($commands_file, implode("\n", $command_list));

$result = $this->_exec("parallel -j+0 --halt now,fail=1 < $commands_file");
unlink($commands_file);

if (!$result->wasSuccessful()) {
$this->say('');
$this->yell('PHPCS found coding standard violations!', 40, 'red');
$this->say('Please review the errors above and fix them.');
return new ResultData($result->getExitCode(), 'PHPCS found coding standard violations');
}

$this->say('');
$this->say('✓ No coding standard violations found!');
return NULL;
}

Expand Down