PhpCsFixer\FixerFactory::useRuleSet PHP Method

useRuleSet() public method

Apply RuleSet on fixers to filter out all unwanted fixers.
public useRuleSet ( phpcsfixer\RuleSetInterface $ruleSet )
$ruleSet phpcsfixer\RuleSetInterface
    public function useRuleSet(RuleSetInterface $ruleSet)
    {
        $fixers = array();
        $fixersByName = array();
        $fixerConflicts = array();
        $fixerNames = array_keys($ruleSet->getRules());
        foreach ($fixerNames as $name) {
            if (!array_key_exists($name, $this->fixersByName)) {
                throw new \UnexpectedValueException(sprintf('Rule "%s" does not exist.', $name));
            }
            $fixer = $this->fixersByName[$name];
            $config = $ruleSet->getRuleConfiguration($name);
            if (null !== $config) {
                if ($fixer instanceof ConfigurableFixerInterface) {
                    if (!is_array($config) || !count($config)) {
                        throw new InvalidFixerConfigurationException($fixer->getName(), 'Configuration must be an array and may not be empty.');
                    }
                    $fixer->configure($config);
                } else {
                    throw new InvalidFixerConfigurationException($fixer->getName(), 'Is not configurable.');
                }
            }
            $fixers[] = $fixer;
            $fixersByName[$name] = $fixer;
            $conflicts = array_intersect($this->getFixersConflicts($fixer), $fixerNames);
            if (count($conflicts) > 0) {
                $fixerConflicts[$name] = $conflicts;
            }
        }
        if (count($fixerConflicts) > 0) {
            throw new \UnexpectedValueException($this->generateConflictMessage($fixerConflicts));
        }
        $this->fixers = $fixers;
        $this->fixersByName = $fixersByName;
        return $this;
    }

Usage Example

 /**
  * 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)));
     }
 }
All Usage Examples Of PhpCsFixer\FixerFactory::useRuleSet