Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add list-sets command #5635

Merged
merged 1 commit into from Apr 24, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@
{
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MaXal , would that fit your needs?
// part one: schema

"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"sets": {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering about this special nesting level (let me call it "envelope") -> for now, we don't really need it.
Yet, we had cases in past, when Rule was expecting configuration like [foo, bar] and we couldn't extend it, causing a BC break to introduce {items => [foo, bar], otherOption: true}. let's be future ready

"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);
}