Ejemplo n.º 1
0
 /**
  * @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);
 }
 /**
  * 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 \UnexpectedValueException(sprintf('The rules contain risky fixers (%s), but they are not allowed to run. Perhaps you forget to use --allow-risky option?', implode(', ', $riskyFixers)));
     }
 }
Ejemplo n.º 4
0
 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();
 }
Ejemplo n.º 6
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;
 }
Ejemplo n.º 7
0
 private function getAllFixers()
 {
     $factory = new FixerFactory();
     return $factory->registerBuiltInFixers()->getFixers();
 }
Ejemplo n.º 8
0
 public function testHasRuleWithChangedRuleSet()
 {
     $factory = new FixerFactory();
     $factory->registerBuiltInFixers();
     $f1 = $this->getMock('Symfony\\CS\\FixerInterface');
     $f1->expects($this->any())->method('getName')->willReturn('f1');
     $f2 = $this->getMock('Symfony\\CS\\FixerInterface');
     $f2->expects($this->any())->method('getName')->willReturn('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');
 }