Skip to content

Commit

Permalink
Add list-sets command
Browse files Browse the repository at this point in the history
  • Loading branch information
keradus committed Apr 24, 2021
1 parent 5eaa06b commit ddf9c0e
Show file tree
Hide file tree
Showing 17 changed files with 869 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitattributes
@@ -1,6 +1,7 @@
# @TODO 3.0 replace following `tests/... export-ignore` with single `tests/ export-ignore`
/tests/**/*Test.php export-ignore
/tests/AbstractDoctrineAnnotationFixerTestCase.php export-ignore
/tests/Console/Report/ListSetsReport/AbstractReporterTestCase.php export-ignore
/tests/Console/TestToolInfo.php export-ignore
/tests/Differ/AbstractDifferTestCase.php export-ignore
/tests/Fixtures/ export-ignore
Expand Down
31 changes: 31 additions & 0 deletions doc/schemas/list-sets/schema.json
@@ -0,0 +1,31 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"sets": {
"type": "object",
"additionalProperties": {
"type": "object",
"properties": {
"description": {
"type": "string"
},
"isRisky": {
"type": "boolean"
},
"name": {
"type": "string"
}
},
"required": [
"description",
"isRisky",
"name"
]
}
}
},
"required": [
"sets"
]
}
2 changes: 2 additions & 0 deletions src/Console/Application.php
Expand Up @@ -16,6 +16,7 @@
use PhpCsFixer\Console\Command\FixCommand;
use PhpCsFixer\Console\Command\HelpCommand;
use PhpCsFixer\Console\Command\ListFilesCommand;
use PhpCsFixer\Console\Command\ListSetsCommand;
use PhpCsFixer\Console\Command\SelfUpdateCommand;
use PhpCsFixer\Console\SelfUpdate\GithubClient;
use PhpCsFixer\Console\SelfUpdate\NewVersionChecker;
Expand Down Expand Up @@ -57,6 +58,7 @@ public function __construct()
$this->add(new DescribeCommand());
$this->add(new FixCommand($this->toolInfo));
$this->add(new ListFilesCommand($this->toolInfo));
$this->add(new ListSetsCommand());
$this->add(new SelfUpdateCommand(
new NewVersionChecker(new GithubClient()),
$this->toolInfo,
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Command/ListFilesCommand.php
Expand Up @@ -74,7 +74,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
[
'config' => $passedConfig,
],
getcwd(),
$cwd,
$this->toolInfo
);

Expand Down
86 changes: 86 additions & 0 deletions src/Console/Command/ListSetsCommand.php
@@ -0,0 +1,86 @@
<?php

/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace PhpCsFixer\Console\Command;

use PhpCsFixer\ConfigurationException\InvalidConfigurationException;
use PhpCsFixer\Console\Report\ListSetsReport\ReporterFactory;
use PhpCsFixer\Console\Report\ListSetsReport\ReportSummary;
use PhpCsFixer\Console\Report\ListSetsReport\TextReporter;
use PhpCsFixer\RuleSet\RuleSets;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @internal
*/
final class ListSetsCommand extends Command
{
protected static $defaultName = 'list-sets';

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setDefinition(
[
new InputOption('format', '', InputOption::VALUE_REQUIRED, 'To output results in other formats.', (new TextReporter())->getFormat()),
]
)
->setDescription('List all available RuleSets.')
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$reporter = $this->resolveReporterWithFactory(
$input->getOption('format'),
new ReporterFactory()
);
$reportSummary = new ReportSummary(
array_values(RuleSets::getSetDefinitions())
);
$report = $reporter->generate($reportSummary);

$output->isDecorated()
? $output->write(OutputFormatter::escape($report))
: $output->write($report, false, OutputInterface::OUTPUT_RAW)
;

return 0;
}

/**
* @param string $format
*/
private function resolveReporterWithFactory($format, ReporterFactory $factory)
{
try {
$factory->registerBuiltInReporters();
$reporter = $factory->getReporter($format);
} catch (\UnexpectedValueException $e) {
$formats = $factory->getFormats();
sort($formats);

throw new InvalidConfigurationException(sprintf('The format "%s" is not defined, supported are "%s".', $format, implode('", "', $formats)));
}

return $reporter;
}
}
54 changes: 54 additions & 0 deletions src/Console/Report/ListSetsReport/JsonReporter.php
@@ -0,0 +1,54 @@
<?php

/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace PhpCsFixer\Console\Report\ListSetsReport;

use PhpCsFixer\RuleSet\RuleSetDescriptionInterface;

/**
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @internal
*/
final class JsonReporter implements ReporterInterface
{
/**
* {@inheritdoc}
*/
public function getFormat()
{
return 'json';
}

/**
* {@inheritdoc}
*/
public function generate(ReportSummary $reportSummary)
{
$json = ['sets' => []];

$sets = $reportSummary->getSets();
usort($sets, function (RuleSetDescriptionInterface $a, RuleSetDescriptionInterface $b) {
return $a->getName() > $b->getName() ? 1 : -1;
});

foreach ($sets as $set) {
$json['sets'][$set->getName()] = [
'description' => $set->getDescription(),
'isRisky' => $set->isRisky(),
'name' => $set->getName(),
];
}

return json_encode($json, JSON_PRETTY_PRINT);
}
}
45 changes: 45 additions & 0 deletions src/Console/Report/ListSetsReport/ReportSummary.php
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace PhpCsFixer\Console\Report\ListSetsReport;

use PhpCsFixer\RuleSet\RuleSetDescriptionInterface;

/**
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @internal
*/
final class ReportSummary
{
/**
* @var RuleSetDescriptionInterface[]
*/
private $sets;

/**
* @param RuleSetDescriptionInterface[] $sets
*/
public function __construct(
array $sets
) {
$this->sets = $sets;
}

/**
* @return RuleSetDescriptionInterface[]
*/
public function getSets()
{
return $this->sets;
}
}
95 changes: 95 additions & 0 deletions src/Console/Report/ListSetsReport/ReporterFactory.php
@@ -0,0 +1,95 @@
<?php

/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace PhpCsFixer\Console\Report\ListSetsReport;

use Symfony\Component\Finder\Finder as SymfonyFinder;
use Symfony\Component\Finder\SplFileInfo;

/**
* @author Boris Gorbylev <ekho@ekho.name>
*
* @internal
*/
final class ReporterFactory
{
/** @var ReporterInterface[] */
private $reporters = [];

public function registerBuiltInReporters()
{
/** @var null|string[] $builtInReporters */
static $builtInReporters;

if (null === $builtInReporters) {
$builtInReporters = [];

/** @var SplFileInfo $file */
foreach (SymfonyFinder::create()->files()->name('*Reporter.php')->in(__DIR__) as $file) {
$relativeNamespace = $file->getRelativePath();
$builtInReporters[] = sprintf(
'%s\\%s%s',
__NAMESPACE__,
$relativeNamespace ? $relativeNamespace.'\\' : '',
$file->getBasename('.php')
);
}
}

foreach ($builtInReporters as $reporterClass) {
$this->registerReporter(new $reporterClass());
}

return $this;
}

/**
* @return $this
*/
public function registerReporter(ReporterInterface $reporter)
{
$format = $reporter->getFormat();

if (isset($this->reporters[$format])) {
throw new \UnexpectedValueException(sprintf('Reporter for format "%s" is already registered.', $format));
}

$this->reporters[$format] = $reporter;

return $this;
}

/**
* @return string[]
*/
public function getFormats()
{
$formats = array_keys($this->reporters);
sort($formats);

return $formats;
}

/**
* @param string $format
*
* @return ReporterInterface
*/
public function getReporter($format)
{
if (!isset($this->reporters[$format])) {
throw new \UnexpectedValueException(sprintf('Reporter for format "%s" is not registered.', $format));
}

return $this->reporters[$format];
}
}
33 changes: 33 additions & 0 deletions src/Console/Report/ListSetsReport/ReporterInterface.php
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace PhpCsFixer\Console\Report\ListSetsReport;

/**
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @internal
*/
interface ReporterInterface
{
/**
* @return string
*/
public function getFormat();

/**
* Process changed files array. Returns generated report.
*
* @return string
*/
public function generate(ReportSummary $reportSummary);
}

0 comments on commit ddf9c0e

Please sign in to comment.