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
Add list-sets command #5635
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"type": "object", | ||
"properties": { | ||
"sets": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
"type": "object", | ||
"additionalProperties": { | ||
"type": "object", | ||
"properties": { | ||
"description": { | ||
"type": "string" | ||
}, | ||
"isRisky": { | ||
"type": "boolean" | ||
}, | ||
"name": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"description", | ||
"isRisky", | ||
"name" | ||
] | ||
} | ||
} | ||
}, | ||
"required": [ | ||
"sets" | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
There was a problem hiding this comment.
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