Skip to content

Commit

Permalink
Symfony linters (#2)
Browse files Browse the repository at this point in the history
* feat: Add Symfony linters

* fixup! feat: Add Symfony linters

* Add lint YAML

* Add roave infection

* Disable command timeout

* chore: Update PHP ^7.4

* fixup! chore: Update PHP ^7.4
  • Loading branch information
Frank Verhoeven authored Dec 10, 2021
1 parent 72c7dc9 commit e082a03
Show file tree
Hide file tree
Showing 14 changed files with 185 additions and 64 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
},
"require": {
"php": "^7.2 || ^8.0",
"php": "^7.4 || ^8.0",
"symfony/console": "^5.3",
"composer/semver": "^3.2",
"symfony/process": "^5.3"
Expand Down
3 changes: 1 addition & 2 deletions src/Command/AnalyzeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ final class AnalyzeCommand extends Command
/** @var string|null */
protected static $defaultDescription = 'Run all enabled tools.';

/** @var Configuration */
private $configuration;
private Configuration $configuration;

public function __construct(Configuration $configuration)
{
Expand Down
15 changes: 6 additions & 9 deletions src/Command/CodesnifferCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace MyOnlineStore\DevTools\Command;

use MyOnlineStore\DevTools\Configuration;

final class CodesnifferCommand extends DevToolsCommand
{
/** @var string|null */
Expand All @@ -17,18 +19,13 @@ final class CodesnifferCommand extends DevToolsCommand
protected function getCommand(): array
{
return [
$this->withBinPath('phpcs'),
$this->withVendorBinPath('phpcs'),
];
}

/**
* @inheritDoc
*/
public static function getPossibleConfigurationFiles(): array
public static function isAvailable(Configuration $configuration): bool
{
return [
'phpcs.xml.dist',
'phpcs.xml',
];
return \is_file($configuration->getRootDir() . 'phpcs.xml.dist')
|| \is_file($configuration->getRootDir() . 'phpcs.xml');
}
}
19 changes: 12 additions & 7 deletions src/Command/DevToolsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

abstract class DevToolsCommand extends Command
{
/** @var Configuration */
protected $configuration;
protected Configuration $configuration;

public function __construct(Configuration $configuration)
{
Expand All @@ -27,7 +26,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
\array_merge(
$this->getCommand(),
(array) ($input->getArguments()['args'] ?? [])
)
),
null,
null,
null,
null
);
$process->start();

Expand All @@ -39,14 +42,16 @@ static function (string $_type, string $buffer) use ($output): void {
}

protected function withBinPath(string $command): string
{
return $this->configuration->getRootDir() . 'bin/' . $command;
}

protected function withVendorBinPath(string $command): string
{
return $this->configuration->getRootDir() . 'vendor/bin/' . $command;
}

/**
* @return list<string>
*/
abstract public static function getPossibleConfigurationFiles(): array;
abstract public static function isAvailable(Configuration $configuration): bool;

/**
* @return list<string>
Expand Down
39 changes: 39 additions & 0 deletions src/Command/LintSymfonyContainerCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);

namespace MyOnlineStore\DevTools\Command;

use MyOnlineStore\DevTools\Configuration;
use Symfony\Component\Process\Process;

final class LintSymfonyContainerCommand extends DevToolsCommand
{
/** @var string|null */
protected static $defaultName = 'lint-container';

/** @var string|null */
protected static $defaultDescription = 'Lint Symfony container';

/**
* @inheritDoc
*/
protected function getCommand(): array
{
return [
$this->withBinPath('console'),
'lint:container',
];
}

public static function isAvailable(Configuration $configuration): bool
{
if (!\is_file($configuration->getRootDir() . 'bin/console')) {
return false;
}

$process = new Process([$configuration->getRootDir() . 'bin/console', 'list']);
$process->run();

return \str_contains($process->getOutput(), 'lint:container');
}
}
44 changes: 44 additions & 0 deletions src/Command/LintYamlCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);

namespace MyOnlineStore\DevTools\Command;

use MyOnlineStore\DevTools\Configuration;
use Symfony\Component\Process\Process;

final class LintYamlCommand extends DevToolsCommand
{
/** @var string|null */
protected static $defaultName = 'lint-yaml';

/** @var string|null */
protected static $defaultDescription = 'Lint YAML';

/**
* @inheritDoc
*/
protected function getCommand(): array
{
return [
$this->withBinPath('console'),
'lint:yaml',
'config',
'--parse-tags',
];
}

public static function isAvailable(Configuration $configuration): bool
{
if (
!\is_file($configuration->getRootDir() . 'bin/console') ||
!\is_dir($configuration->getRootDir() . 'config')
) {
return false;
}

$process = new Process([$configuration->getRootDir() . 'bin/console', 'list']);
$process->run();

return \str_contains($process->getOutput(), 'lint:yaml');
}
}
5 changes: 2 additions & 3 deletions src/Command/ListPhpVersionsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ final class ListPhpVersionsCommand extends Command
/** @var string|null */
protected static $defaultDescription = 'Lists PHP versions allowed by composer.json (in JSON format).';

/** @var Configuration */
private $configuration;
private Configuration $configuration;

public function __construct(Configuration $configuration)
{
Expand All @@ -28,7 +27,7 @@ public function __construct(Configuration $configuration)

protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->write(\json_encode($this->configuration->getPhpVersions()));
$output->write(\json_encode($this->configuration->getPhpVersions(), \JSON_THROW_ON_ERROR));

return 0;
}
Expand Down
5 changes: 2 additions & 3 deletions src/Command/ListToolsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ final class ListToolsCommand extends Command
/** @var string|null */
protected static $defaultDescription = 'Lists enabled tools (in JSON).';

/** @var Configuration */
private $configuration;
private Configuration $configuration;

public function __construct(Configuration $configuration)
{
Expand All @@ -28,7 +27,7 @@ public function __construct(Configuration $configuration)

protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->write(\json_encode(\array_keys($this->configuration->getEnabledTools())));
$output->write(\json_encode(\array_keys($this->configuration->getEnabledTools()), \JSON_THROW_ON_ERROR));

return 0;
}
Expand Down
15 changes: 6 additions & 9 deletions src/Command/PhpUnitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace MyOnlineStore\DevTools\Command;

use MyOnlineStore\DevTools\Configuration;

final class PhpUnitCommand extends DevToolsCommand
{
/** @var string|null */
Expand All @@ -17,18 +19,13 @@ final class PhpUnitCommand extends DevToolsCommand
protected function getCommand(): array
{
return [
$this->withBinPath('phpunit'),
$this->withVendorBinPath('phpunit'),
];
}

/**
* @inheritDoc
*/
public static function getPossibleConfigurationFiles(): array
public static function isAvailable(Configuration $configuration): bool
{
return [
'phpunit.xml.dist',
'phpunit.xml',
];
return \is_file($configuration->getRootDir() . 'phpunit.xml.dist')
|| \is_file($configuration->getRootDir() . 'phpunit.xml');
}
}
15 changes: 6 additions & 9 deletions src/Command/PsalmCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace MyOnlineStore\DevTools\Command;

use MyOnlineStore\DevTools\Configuration;

final class PsalmCommand extends DevToolsCommand
{
/** @var string|null */
Expand All @@ -17,18 +19,13 @@ final class PsalmCommand extends DevToolsCommand
protected function getCommand(): array
{
return [
$this->withBinPath('psalm'),
$this->withVendorBinPath('psalm'),
];
}

/**
* @inheritDoc
*/
public static function getPossibleConfigurationFiles(): array
public static function isAvailable(Configuration $configuration): bool
{
return [
'psalm.xml.dist',
'psalm.xml',
];
return \is_file($configuration->getRootDir() . 'psalm.xml.dist')
|| \is_file($configuration->getRootDir() . 'psalm.xml');
}
}
36 changes: 36 additions & 0 deletions src/Command/RoaveInfectionCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);

namespace MyOnlineStore\DevTools\Command;

use MyOnlineStore\DevTools\Configuration;

final class RoaveInfectionCommand extends DevToolsCommand
{
/** @var string|null */
protected static $defaultName = 'infection';

/** @var string|null */
protected static $defaultDescription = 'Roave Infection';

/**
* @inheritDoc
*/
protected function getCommand(): array
{
return [
$this->withVendorBinPath('roave-infection-static-analysis-plugin'),
'--only-covered',
'--show-mutations',
];
}

public static function isAvailable(Configuration $configuration): bool
{
return \is_file($configuration->getRootDir() . 'vendor/bin/roave-infection-static-analysis-plugin')
&& (
\is_file($configuration->getRootDir() . 'infection.json.dist') ||
\is_file($configuration->getRootDir() . 'infection.json')
);
}
}
29 changes: 15 additions & 14 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
final class Configuration
{
private const PHP_VERSIONS = [
'7.2',
'7.4',
'8.0',
'8.1',
Expand All @@ -21,8 +20,7 @@ final class Configuration
/** @var list<string>|null */
private $phpVersions;

/** @var string */
private $rootDir;
private string $rootDir;

public function __construct()
{
Expand All @@ -49,7 +47,12 @@ public function __construct()
private function gatherPhpVersions(): array
{
/** @var array<array-key, mixed> $composer */
$composer = \json_decode(\file_get_contents($this->rootDir . 'composer.json'), true);
$composer = \json_decode(
\file_get_contents($this->rootDir . 'composer.json'),
true,
512,
\JSON_THROW_ON_ERROR
);

if (!isset($composer['require']['php'])) {
throw new \RuntimeException('Required PHP version not specified in composer.json');
Expand Down Expand Up @@ -94,19 +97,17 @@ private function gatherEnabledTools(): array
$enabledTools = [];

foreach ($this->gatherAvailableCommands() as $command) {
foreach ($command::getPossibleConfigurationFiles() as $configurationFile) {
if (!\is_file($this->rootDir . $configurationFile)) {
continue;
}

$commandName = $command::getDefaultName();
if (!$command::isAvailable($this)) {
continue;
}

if (!\is_string($commandName)) {
throw new \RuntimeException(\sprintf('Command "%s" has not configured a name', $command));
}
$commandName = $command::getDefaultName();

$enabledTools[$commandName] = $command;
if (!\is_string($commandName)) {
throw new \RuntimeException(\sprintf('Command "%s" has not configured a name', $command));
}

$enabledTools[$commandName] = $command;
}

return $enabledTools;
Expand Down
Loading

0 comments on commit e082a03

Please sign in to comment.