Fixers may be registered (made the factory aware of them) by registering a custom fixer and default, built in fixers. Then, one can attach Config instance to fixer instances. Finally factory creates a ready to use group of fixers.
Author: Dariusz Rumiński (dariusz.ruminski@gmail.com)
 /**
  * @dataProvider provideAllRulesFromSets
  */
 public function testIfAllRulesInSetsExists($rule)
 {
     $factory = new FixerFactory();
     $factory->registerBuiltInFixers();
     $fixers = array();
     foreach ($factory->getFixers() as $fixer) {
         $fixers[$fixer->getName()] = $fixer;
     }
     $this->assertArrayHasKey($rule, $fixers);
 }
示例#2
0
 /**
  * Parses the '--CONFIG--' block of a '.test' file and determines what fixers should be used.
  *
  * @param string $config
  *
  * @return FixerInterface[]
  */
 protected function determineFixers($config)
 {
     $ruleSet = json_decode($config, true);
     if (JSON_ERROR_NONE !== json_last_error()) {
         throw new \InvalidArgumentException('Malformed JSON configuration.');
     }
     return FixerFactory::create()->registerBuiltInFixers()->useRuleSet(new RuleSet($ruleSet))->getFixers();
 }
 /**
  * Resolve fixers to run based on rules.
  */
 private function resolveFixers()
 {
     $this->fixers = $this->fixerFactory->useRuleSet($this->ruleSet)->getFixers();
     if (true === $this->allowRisky) {
         return;
     }
     $riskyFixers = array_map(function (FixerInterface $fixer) {
         return $fixer->getName();
     }, array_filter($this->fixers, function (FixerInterface $fixer) {
         return $fixer->isRisky();
     }));
     if (!empty($riskyFixers)) {
         throw new InvalidConfigurationException(sprintf('The rules contain risky fixers (%s), but they are not allowed to run. Perhaps you forget to use --allow-risky option?', implode(', ', $riskyFixers)));
     }
 }
示例#4
0
 protected function getFixersHelp()
 {
     $help = '';
     $fixerFactory = new FixerFactory();
     $fixers = $fixerFactory->registerBuiltInFixers()->getFixers();
     // sort fixers by name
     usort($fixers, function (FixerInterface $a, FixerInterface $b) {
         return strcmp($a->getName(), $b->getName());
     });
     $ruleSets = array();
     foreach (RuleSet::create()->getSetDefinitionNames() as $setName) {
         $ruleSets[$setName] = new RuleSet(array($setName => true));
     }
     $getSetsWithRule = function ($rule) use($ruleSets) {
         $sets = array();
         foreach ($ruleSets as $setName => $ruleSet) {
             if ($ruleSet->hasRule($rule)) {
                 $sets[] = $setName;
             }
         }
         return $sets;
     };
     $count = count($fixers) - 1;
     foreach ($fixers as $i => $fixer) {
         $sets = $getSetsWithRule($fixer->getName());
         $description = $fixer->getDescription();
         $attributes = array();
         if ($fixer->isRisky()) {
             $attributes[] = 'risky';
         }
         if ($this->isFixerConfigurable($fixer)) {
             $attributes[] = 'configurable';
         }
         $description = wordwrap($description, 72, "\n   | ");
         if (!empty($sets)) {
             $help .= sprintf(" * <comment>%s</comment> [%s]\n   | %s\n", $fixer->getName(), implode(', ', $sets), $description);
         } else {
             $help .= sprintf(" * <comment>%s</comment>\n   | %s\n", $fixer->getName(), $description);
         }
         if (count($attributes)) {
             sort($attributes);
             $help .= sprintf("   | *Rule is: %s.*\n", implode(', ', $attributes));
         }
         if ($count !== $i) {
             $help .= "\n";
         }
     }
     return $help;
 }
 public function testHasRuleWithChangedRuleSet()
 {
     $factory = new FixerFactory();
     $f1 = $this->createFixerMock('f1');
     $f2 = $this->createFixerMock('f2');
     $factory->registerFixer($f1);
     $factory->registerFixer($f2);
     $this->assertTrue($factory->hasRule('f1'), 'Should have f1 fixer');
     $this->assertTrue($factory->hasRule('f2'), 'Should have f2 fixer');
     $factory->useRuleSet(new RuleSet(array('f2' => true)));
     $this->assertFalse($factory->hasRule('f1'), 'Should not have f1 fixer');
     $this->assertTrue($factory->hasRule('f2'), 'Should have f2 fixer');
 }
 /**
  * Create fixer factory with all needed fixers registered.
  *
  * @return FixerFactory
  */
 protected function createFixerFactory()
 {
     return FixerFactory::create()->registerBuiltInFixers();
 }
 /**
  * Returns fixers.
  *
  * @return FixerInterface[] An array of FixerInterface
  */
 public function getFixers()
 {
     if (null === $this->fixers) {
         $fixerFactory = new FixerFactory();
         $fixerFactory->registerBuiltInFixers();
         $fixerFactory->registerCustomFixers($this->getConfig()->getCustomFixers());
         $this->fixers = $fixerFactory->useRuleSet($this->getRuleSet())->setWhitespacesConfig(new WhitespacesFixerConfig($this->config->getIndent(), $this->config->getLineEnding()))->getFixers();
         if (false === $this->getRiskyAllowed()) {
             $riskyFixers = array_map(function (FixerInterface $fixer) {
                 return $fixer->getName();
             }, array_filter($this->fixers, function (FixerInterface $fixer) {
                 return $fixer->isRisky();
             }));
             if (count($riskyFixers)) {
                 throw new InvalidConfigurationException(sprintf('The rules contain risky fixers (%s), but they are not allowed to run. Perhaps you forget to use --allow-risky option?', implode(', ', $riskyFixers)));
             }
         }
     }
     return $this->fixers;
 }
示例#8
0
 protected function getFixersHelp()
 {
     $help = '';
     $maxName = 0;
     $fixerFactory = new FixerFactory();
     $fixers = $fixerFactory->registerBuiltInFixers()->getFixers();
     // sort fixers by name
     usort($fixers, function (FixerInterface $a, FixerInterface $b) {
         return strcmp($a->getName(), $b->getName());
     });
     foreach ($fixers as $fixer) {
         if (strlen($fixer->getName()) > $maxName) {
             $maxName = strlen($fixer->getName());
         }
     }
     $ruleSets = array();
     foreach (RuleSet::create()->getSetDefinitionNames() as $setName) {
         $ruleSets[$setName] = new RuleSet(array($setName => true));
     }
     $getSetsWithRule = function ($rule) use($ruleSets) {
         $sets = array();
         foreach ($ruleSets as $setName => $ruleSet) {
             if ($ruleSet->hasRule($rule)) {
                 $sets[] = $setName;
             }
         }
         return $sets;
     };
     $count = count($fixers) - 1;
     foreach ($fixers as $i => $fixer) {
         $sets = $getSetsWithRule($fixer->getName());
         $description = $fixer->getDescription();
         if ($fixer->isRisky()) {
             $description .= ' (Risky fixer!)';
         }
         if (!empty($sets)) {
             $chunks = explode("\n", wordwrap(sprintf("[%s]\n%s", implode(', ', $sets), $description), 72 - $maxName, "\n"));
             $help .= sprintf(" * <comment>%s</comment>%s %s\n", $fixer->getName(), str_repeat(' ', $maxName - strlen($fixer->getName())), array_shift($chunks));
         } else {
             $chunks = explode("\n", wordwrap(sprintf("\n%s", $description), 72 - $maxName, "\n"));
             $help .= sprintf(" * <comment>%s</comment>%s\n", $fixer->getName(), array_shift($chunks));
         }
         while ($c = array_shift($chunks)) {
             $help .= str_repeat(' ', $maxName + 4) . $c . "\n";
         }
         if ($count !== $i) {
             $help .= "\n";
         }
     }
     return $help;
 }
示例#9
0
 private function getAllFixers()
 {
     $factory = new FixerFactory();
     return $factory->registerBuiltInFixers()->getFixers();
 }
示例#10
0
 /**
  * @dataProvider provideConflictingFixersRules
  * @expectedException \UnexpectedValueException
  * @expectedExceptionMessageRegExp #^Rule contains conflicting fixers:\n#
  */
 public function testConflictingFixers(RuleSet $ruleSet)
 {
     FixerFactory::create()->registerBuiltInFixers()->useRuleSet($ruleSet);
 }
 /**
  * @return array<string, FixerInterface>
  */
 private function getFixers()
 {
     if (null !== $this->fixers) {
         return $this->fixers;
     }
     $fixerFactory = new FixerFactory();
     $fixers = array();
     foreach ($fixerFactory->registerBuiltInFixers()->getFixers() as $fixer) {
         $fixers[$fixer->getName()] = $fixer;
     }
     $this->fixers = $fixers;
     ksort($this->fixers);
     return $this->fixers;
 }
 /**
  * Parses the '--RULESET--' block of a '.test' file and determines what fixers should be used.
  *
  * @param string                 $config
  * @param WhitespacesFixerConfig $sharedFixerConfig
  *
  * @return FixerInterface[]
  */
 protected function determineFixers($config, WhitespacesFixerConfig $sharedFixerConfig)
 {
     return FixerFactory::create()->registerBuiltInFixers()->useRuleSet(new RuleSet($this->parseJson($config)))->setWhitespacesConfig($sharedFixerConfig)->getFixers();
 }
 /**
  * @param IntegrationCase $case
  *
  * @return FixerInterface[]
  */
 private function createFixers(IntegrationCase $case)
 {
     $config = $case->getConfig();
     return FixerFactory::create()->registerBuiltInFixers()->useRuleSet($case->getRuleset())->setWhitespacesConfig(new WhitespacesFixerConfig($config['indent'], $config['lineEnding']))->getFixers();
 }