Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
869 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": { | ||
"type": "object", | ||
"additionalProperties": { | ||
"type": "object", | ||
"properties": { | ||
"description": { | ||
"type": "string" | ||
}, | ||
"isRisky": { | ||
"type": "boolean" | ||
}, | ||
"name": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"description", | ||
"isRisky", | ||
"name" | ||
] | ||
} | ||
} | ||
}, | ||
"required": [ | ||
"sets" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.