Apply RuleSet on fixers to filter out all unwanted fixers.
public useRuleSet ( phpcsfixer\RuleSetInterface $ruleSet ) | ||
$ruleSet | phpcsfixer\RuleSetInterface |
/** * 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))); } }
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'); }
/** * 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; }