Skip to content

Commit

Permalink
feat: Add a new phpcs command to run phpcs analysis (#28)
Browse files Browse the repository at this point in the history
* Create PhpCsCommand.php

* Update install.yaml

* Update drupal

* Update PhpCsCommand.php

* Add more linting commands

* Fixes to lint commands and add documentation

---------

Co-authored-by: Sally Young <[email protected]>
  • Loading branch information
plopesc and justafish authored Oct 7, 2024
1 parent cd06f6c commit 5fff83a
Show file tree
Hide file tree
Showing 11 changed files with 301 additions and 5 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,20 @@ a11y test for a custom admin theme
```
ddev nightwatch --tag a11y:admin --adminTheme seven
```

## Core Linting

This will run static tests against core standards.

```
ddev drupal lint:phpstan
ddev drupal lint:phpcs
ddev drupal lint:js
ddev drupal lint:css
ddev drupal lint:cspell
# CSpell against only modified files
ddev drupal lint:cspell --modified-only
```

You can run all linting with `ddev drupal lint`, or with fail-fast turned on:
`ddev drupal lint --stop-on-failure`
18 changes: 15 additions & 3 deletions commands/web/drupal
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@

use Drupal\Core\Command\GenerateTheme;
use Drupal\Core\Command\InstallCommand;
use Symfony\Component\Console\Application;
use DrupalCoreDev\Command\CacheCommand;
use DrupalCoreDev\Command\AdminLoginCommand;
use DrupalCoreDev\Command\TestCommand;
use DrupalCoreDev\Command\CacheCommand;
use DrupalCoreDev\Command\LintCommand;
use DrupalCoreDev\Command\LintCspellCommand;
use DrupalCoreDev\Command\LintCssCommand;
use DrupalCoreDev\Command\LintJsCommand;
use DrupalCoreDev\Command\LintPhpCsCommand;
use DrupalCoreDev\Command\LintPhpStanCommand;
use DrupalCoreDev\Command\TestBrowserCommand;
use DrupalCoreDev\Command\TestCommand;
use DrupalCoreDev\Command\UninstallCommand;
use Symfony\Component\Console\Application;

if (PHP_SAPI !== 'cli') {
return;
Expand All @@ -39,5 +45,11 @@ $application->add(new AdminLoginCommand($loader));
$application->add(new TestCommand());
$application->add(new TestBrowserCommand());
$application->add(new UninstallCommand());
$application->add(new LintPhpCsCommand());
$application->add(new LintPhpStanCommand());
$application->add(new LintCssCommand());
$application->add(new LintJsCommand());
$application->add(new LintCspellCommand());
$application->add(new LintCommand());

$application->run();
3 changes: 2 additions & 1 deletion core-dev/phpunit-chrome.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
# cspell:ignore ddev
#ddev-generated
-->
<!-- For how to customize PHPUnit configuration, see core/tests/README.md. -->
Expand Down Expand Up @@ -36,7 +37,7 @@

<!-- Deprecation testing is managed through Symfony's PHPUnit Bridge.
The environment variable SYMFONY_DEPRECATIONS_HELPER is used to configure
the behaviour of the deprecation tests.
the behavior of the deprecation tests.
See https://symfony.com/doc/current/components/phpunit_bridge.html#configuration
Drupal core's testing framework is setting this variable to its defaults.
Projects with their own requirements need to manage this variable
Expand Down
3 changes: 2 additions & 1 deletion core-dev/phpunit-firefox.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
# cspell:ignore ddev
#ddev-generated
-->
<!-- For how to customize PHPUnit configuration, see core/tests/README.md. -->
Expand Down Expand Up @@ -36,7 +37,7 @@

<!-- Deprecation testing is managed through Symfony's PHPUnit Bridge.
The environment variable SYMFONY_DEPRECATIONS_HELPER is used to configure
the behaviour of the deprecation tests.
the behavior of the deprecation tests.
See https://symfony.com/doc/current/components/phpunit_bridge.html#configuration
Drupal core's testing framework is setting this variable to its defaults.
Projects with their own requirements need to manage this variable
Expand Down
51 changes: 51 additions & 0 deletions core-dev/src/Command/LintCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
#ddev-generated

namespace DrupalCoreDev\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;

class LintCommand extends Command {
/**
* {@inheritdoc}
*/
protected function configure(): void {
$this->setName('lint')
->setDescription('Run lint tests.')
->addOption('stop-on-failure', null, InputOption::VALUE_NONE, 'Stop all test execution once a failure is found.');
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$return = 0;
$stop_on_failure = $input->getOption('stop-on-failure');

$commands = [
new LintPhpCsCommand(),
new LintPhpStanCommand(),
new LintCssCommand(),
new LintJsCommand(),
new LintCspellCommand(),
];

foreach ($commands as $command) {
$return_command = $command->execute($input, $output);
if (!$return_command) {
continue;
}
$return = $return_command;

if ($stop_on_failure) {
return $return;
}
}

return $return;
}
}
42 changes: 42 additions & 0 deletions core-dev/src/Command/LintCspellCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
#ddev-generated

namespace DrupalCoreDev\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;

class LintCspellCommand extends Command {
/**
* {@inheritdoc}
*/
protected function configure(): void {
$this->setName('lint:cspell')
->setDescription('Run CSpell analysis.')
->addOption('modified-only', null, InputOption::VALUE_NONE, 'Only run cspell on modified files.');
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$modified_only = $input->getOption('modified-only');
$command = "cd core && yarn run spellcheck:core --no-must-find-files";
if ($modified_only) {
$command = "cd core && git diff --name-only | sed \"s_^_../_\" | yarn run spellcheck:core --no-must-find-files --file-list stdin";
}
$phpcs = Process::fromShellCommandline($command);
$output->writeln($command);
$phpcs->setTimeout(0);
$phpcs->run(function ($type, $data) use ($output) {
$output->write($data);
});
if ($phpcs->getExitCode()) {
return 1;
}
return 0;
}
}
47 changes: 47 additions & 0 deletions core-dev/src/Command/LintCssCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
#ddev-generated

namespace DrupalCoreDev\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Process\Process;

class LintCssCommand extends Command {
/**
* {@inheritdoc}
*/
protected function configure(): void {
$this->setName('lint:css')
->setDescription('Run CSS coding standard analysis against core.');
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$io = new SymfonyStyle($input, $output);
$yarn = getcwd() . '/core/.yarn';
$node_modules = getcwd() . '/core/node_modules';

// Check if dependencies folder exists before to start.
if(!file_exists($yarn) || !file_exists($node_modules)) {
$io->error('Missing Yarn dependencies. Ensure that you run yarn install before executing this command.');
return 1;
}

$command = "cd core && yarn run lint:css --color --custom-formatter=node_modules/stylelint-formatter-gitlab";
$phpcs = Process::fromShellCommandline($command);
$output->writeln($command);
$phpcs->setTimeout(0);
$phpcs->run(function ($type, $data) use ($output) {
$output->write($data);
});
if ($phpcs->getExitCode()) {
return 1;
}
return 0;
}
}
47 changes: 47 additions & 0 deletions core-dev/src/Command/LintJsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
#ddev-generated

namespace DrupalCoreDev\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Process\Process;

class LintJsCommand extends Command {
/**
* {@inheritdoc}
*/
protected function configure(): void {
$this->setName('lint:js')
->setDescription('Run JS coding standard analysis against core.');
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$io = new SymfonyStyle($input, $output);
$yarn = getcwd() . '/core/.yarn';
$node_modules = getcwd() . '/core/node_modules';

// Check if dependencies folder exists before to start.
if(!file_exists($yarn) || !file_exists($node_modules)) {
$io->error('Missing Yarn dependencies. Ensure that you run yarn install before executing this command.');
return 1;
}

$command = "cd core && yarn run lint:core-js-passing";
$phpcs = Process::fromShellCommandline($command);
$output->writeln($command);
$phpcs->setTimeout(0);
$phpcs->run(function ($type, $data) use ($output) {
$output->write($data);
});
if ($phpcs->getExitCode()) {
return 1;
}
return 0;
}
}
36 changes: 36 additions & 0 deletions core-dev/src/Command/LintPhpCsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
#ddev-generated

namespace DrupalCoreDev\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;

class LintPhpCsCommand extends Command {
/**
* {@inheritdoc}
*/
protected function configure(): void {
$this->setName('lint:phpcs')
->setDescription('Run PHPCS coding standard analysis against core.');
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$command = "composer phpcs -- --report-full --report-summary";
$phpcs = Process::fromShellCommandline($command);
$output->writeln($command);
$phpcs->setTimeout(0);
$phpcs->run(function ($type, $data) use ($output) {
$output->write($data);
});
if ($phpcs->getExitCode()) {
return 1;
}
return 0;
}
}
36 changes: 36 additions & 0 deletions core-dev/src/Command/LintPhpStanCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
#ddev-generated

namespace DrupalCoreDev\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;

class LintPhpStanCommand extends Command {
/**
* {@inheritdoc}
*/
protected function configure(): void {
$this->setName('lint:phpstan')
->setDescription('Run PHPStan code quality analysis against core.');
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$command = "php vendor/bin/phpstan analyze --configuration=./core/phpstan.neon.dist --error-format=table";
$phpcs = Process::fromShellCommandline($command);
$output->writeln($command);
$phpcs->setTimeout(0);
$phpcs->run(function ($type, $data) use ($output) {
$output->write($data);
});
if ($phpcs->getExitCode()) {
return 1;
}
return 0;
}
}
6 changes: 6 additions & 0 deletions install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ project_files:
- core-dev/src/Command/TestCommand.php
- core-dev/src/Command/TestBrowserCommand.php
- core-dev/src/Command/UninstallCommand.php
- core-dev/src/Command/LintPhpCsCommand.php
- core-dev/src/Command/LintPhpStanCommand.php
- core-dev/src/Command/LintCssCommand.php
- core-dev/src/Command/LintJsCommand.php
- core-dev/src/Command/LintCspellCommand.php
- core-dev/src/Command/LintCommand.php

post_install_actions:
- cp core-dev/phpunit-chrome.xml ../core/phpunit.xml
Expand Down

0 comments on commit 5fff83a

Please sign in to comment.