Skip to content

Commit

Permalink
Skip non-existent fixers
Browse files Browse the repository at this point in the history
  • Loading branch information
soullivaneuh committed Sep 28, 2015
1 parent 613b2b5 commit b911525
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -13,10 +13,10 @@
"require": {
"php": ">=5.3",
"symfony/yaml": "~2.3",
"symfony/dependency-injection": "~2.3"
"symfony/dependency-injection": "~2.3",
"symfony/console": "~2.3"
},
"require-dev": {
"symfony/console": "~2.7",
"twig/twig": "~1.22",
"fabpot/php-cs-fixer": "~1.9",
"satooshi/php-coveralls": "~0.6",
Expand Down
67 changes: 63 additions & 4 deletions src/ConfigBridge.php
Expand Up @@ -6,19 +6,33 @@
use SLLH\StyleCIBridge\Exception\LevelConfigException;
use SLLH\StyleCIBridge\Exception\PresetConfigException;
use SLLH\StyleCIBridge\StyleCI\Fixers;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Yaml\Yaml;
use Symfony\CS\Config\Config;
use Symfony\CS\Finder\DefaultFinder;
use Symfony\CS\Fixer\Contrib\HeaderCommentFixer;
use Symfony\CS\FixerFactory;
use Symfony\CS\FixerInterface;

/**
* @author Sullivan Senechal <soullivaneuh@gmail.com>
*/
final class ConfigBridge
{
/**
* @var OutputInterface
*/
private $output;

/**
* @var FixerInterface[]|null
*/
private $availableFixers = null;

/**
* @var string
*/
Expand All @@ -42,7 +56,10 @@ public function __construct($styleCIConfigDir = null, $finderDirs = null)
{
$this->styleCIConfigDir = null !== $styleCIConfigDir ? $styleCIConfigDir : getcwd();
$this->finderDirs = null !== $finderDirs ? $finderDirs : getcwd();
$this->output = new ConsoleOutput();
$this->output->getFormatter()->setStyle('warning', new OutputFormatterStyle('black', 'yellow'));

$this->loadAvailableFixers();
$this->parseStyleCIConfig();
}

Expand Down Expand Up @@ -166,9 +183,17 @@ public function getRules()
$rules = array();
foreach ($fixers as $fixer) {
if ('-' === $fixer[0]) {
$rules[substr($fixer, 1)] = false;
$name = substr($fixer, 1);
$enabled = false;
} else {
$rules[$fixer] = true;
$name = $fixer;
$enabled = true;
}

if ($this->isFixerAvailable($name)) {
$rules[$name] = $enabled;
} else {
$this->output->writeln(sprintf('<warning>Fixer "%s" does not exist, skipping.</warning>', $name));
}
}

Expand Down Expand Up @@ -210,17 +235,51 @@ private function getPresetFixers()
private function resolveAliases(array $fixers)
{
foreach (Fixers::$aliases as $alias => $name) {
if (in_array($alias, $fixers, true) && !in_array($name, $fixers, true)) {
if (in_array($alias, $fixers, true) && !in_array($name, $fixers, true) && $this->isFixerAvailable($name)) {
array_push($fixers, $name);
}
if (in_array($name, $fixers, true) && !in_array($alias, $fixers, true)) {
if (in_array($name, $fixers, true) && !in_array($alias, $fixers, true) && $this->isFixerAvailable($alias)) {
array_push($fixers, $alias);
}
}

return $fixers;
}

/**
* @param string $name
*
* @return bool
*/
private function isFixerAvailable($name)
{
// PHP-CS-Fixer 1.x BC
if (null === $this->availableFixers) {
return true;
}

return isset($this->availableFixers[$name]);
}

/**
* Can be replaced by Config::getFixersByName if following PR is accepted.
*
* @link https://github.com/FriendsOfPHP/PHP-CS-Fixer/pull/1429
*/
private function loadAvailableFixers()
{
// Remove rules that not exists
if (class_exists('Symfony\CS\FixerFactory')) { // PHP-CS-Fixer 1.x BC
$fixerFactory = FixerFactory::create();
$fixerFactory->registerBuiltInFixers();

$this->availableFixers = array();
foreach ($fixerFactory->getFixers() as $fixer) {
$this->availableFixers[$fixer->getName()] = $fixer;
}
}
}

private function parseStyleCIConfig()
{
if (null === $this->styleCIConfig) {
Expand Down

0 comments on commit b911525

Please sign in to comment.