Browse Source

FixerConfiguration - strictify and simplify

Dariusz Ruminski 8 years ago
parent
commit
54cb58ba4b

+ 16 - 16
README.rst

@@ -212,10 +212,10 @@ Choose from the list of available rules:
 
   Configuration options:
 
-  - ``align_equals`` (``true``, ``false``, ``NULL``): whether to apply, remove or ignore
-    equals alignment; defaults to ``false``
-  - ``align_double_arrow`` (``true``, ``false``, ``NULL``): whether to apply, remove or
+  - ``align_double_arrow`` (``NULL``, ``false``, ``true``): whether to apply, remove or
     ignore double arrows alignment; defaults to ``false``
+  - ``align_equals`` (``NULL``, ``false``, ``true``): whether to apply, remove or ignore
+    equals alignment; defaults to ``false``
 
 * **blank_line_after_namespace** [@PSR2, @Symfony]
 
@@ -251,12 +251,12 @@ Choose from the list of available rules:
 
   Configuration options:
 
-  - ``singleLine`` (``bool``): whether definitions should be single line; defaults
-    to ``false``
-  - ``singleItemSingleLine`` (``bool``): whether definitions should be single line
-    when including a single item; defaults to ``false``
   - ``multiLineExtendsEachSingleLine`` (``bool``): whether definitions should be
     multiline; defaults to ``false``
+  - ``singleItemSingleLine`` (``bool``): whether definitions should be single line
+    when including a single item; defaults to ``false``
+  - ``singleLine`` (``bool``): whether definitions should be single line; defaults
+    to ``false``
 
 * **class_keyword_remove**
 
@@ -272,7 +272,7 @@ Choose from the list of available rules:
 
   Configuration options:
 
-  - ``spacing`` (``'one'``, ``'none'``): spacing to apply around concatenation operator;
+  - ``spacing`` (``'none'``, ``'one'``): spacing to apply around concatenation operator;
     defaults to ``'none'``
 
 * **declare_equal_normalize** [@Symfony]
@@ -282,7 +282,7 @@ Choose from the list of available rules:
 
   Configuration options:
 
-  - ``space`` (``'single'``, ``'none'``): spacing to apply around the equal sign;
+  - ``space`` (``'none'``, ``'single'``): spacing to apply around the equal sign;
     defaults to ``'none'``
 
 * **declare_strict_types**
@@ -346,12 +346,12 @@ Choose from the list of available rules:
 
   Configuration options:
 
-  - ``header`` (``string``): proper header content; required
   - ``commentType`` (``'PHPDoc'``, ``'comment'``): comment syntax type; defaults to
     ``'comment'``
-  - ``location`` (``'after_open'``, ``'after_declare_strict'``): the location of the
+  - ``header`` (``string``): proper header content; required
+  - ``location`` (``'after_declare_strict'``, ``'after_open'``): the location of the
     inserted header; defaults to ``'after_declare_strict'``
-  - ``separate`` (``'both'``, ``'top'``, ``'bottom'``, ``'none'``): whether the header should be
+  - ``separate`` (``'both'``, ``'bottom'``, ``'none'``, ``'top'``): whether the header should be
     separated from the file content with a new line; defaults to ``'both'``
 
 * **heredoc_to_nowdoc**
@@ -496,7 +496,7 @@ Choose from the list of available rules:
 
   Configuration options:
 
-  - ``use`` (``'print'``, ``'echo'``): the desired language construct; defaults to
+  - ``use`` (``'echo'``, ``'print'``): the desired language construct; defaults to
     ``'echo'``
 
 * **no_multiline_whitespace_around_double_arrow** [@Symfony]
@@ -633,10 +633,10 @@ Choose from the list of available rules:
 
   Configuration options:
 
-  - ``sortAlgorithm`` (``'alpha'``, ``'length'``): whether the statements should be
-    sorted alphabetically or by length; defaults to ``'alpha'``
   - ``importsOrder`` (``array``, ``null``): defines the order of import types; defaults
     to ``NULL``
+  - ``sortAlgorithm`` (``'alpha'``, ``'length'``): whether the statements should be
+    sorted alphabetically or by length; defaults to ``'alpha'``
 
 * **php_unit_construct** [@Symfony:risky]
 
@@ -841,7 +841,7 @@ Choose from the list of available rules:
 
   Configuration options:
 
-  - ``space_before`` (``'one'``, ``'none'``): spacing to apply before colon; defaults to
+  - ``space_before`` (``'none'``, ``'one'``): spacing to apply before colon; defaults to
     ``'none'``
 
 * **self_accessor** [@Symfony]

+ 15 - 5
src/Console/Command/CommandHelp.php

@@ -16,7 +16,7 @@ use PhpCsFixer\Fixer\ConfigurableFixerInterface;
 use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
 use PhpCsFixer\Fixer\DefinedFixerInterface;
 use PhpCsFixer\Fixer\FixerInterface;
-use PhpCsFixer\FixerConfiguration\FixerOption;
+use PhpCsFixer\FixerConfiguration\FixerOptionInterface;
 use PhpCsFixer\FixerFactory;
 use PhpCsFixer\RuleSet;
 
@@ -273,11 +273,11 @@ EOF
     /**
      * Returns the allowed values of the given option that can be converted to a string.
      *
-     * @param FixerOption $option
+     * @param FixerOptionInterface $option
      *
      * @return array|null
      */
-    public static function getDisplayableAllowedValues(FixerOption $option)
+    public static function getDisplayableAllowedValues(FixerOptionInterface $option)
     {
         $allowed = $option->getAllowedValues();
 
@@ -286,6 +286,8 @@ EOF
                 return !is_callable($value);
             });
 
+            sort($allowed);
+
             if (0 === count($allowed)) {
                 $allowed = null;
             }
@@ -350,10 +352,18 @@ EOF
 
             if ($fixer instanceof ConfigurationDefinitionFixerInterface) {
                 $configurationDefinition = $fixer->getConfigurationDefinition();
-                if (count($configurationDefinition->getOptions())) {
+                $configurationDefinitionOptions = $configurationDefinition->getOptions();
+                if (count($configurationDefinitionOptions)) {
                     $help .= "   |\n   | Configuration options:\n";
 
-                    foreach ($configurationDefinition->getOptions() as $option) {
+                    usort(
+                        $configurationDefinitionOptions,
+                        function (FixerOptionInterface $optionA, FixerOptionInterface $optionB) {
+                            return strcmp($optionA->getName(), $optionB->getName());
+                        }
+                    );
+
+                    foreach ($configurationDefinitionOptions as $option) {
                         $line = '<info>'.$option->getName().'</info>';
 
                         $allowed = self::getDisplayableAllowedValues($option);

+ 3 - 5
src/Fixer/Alias/NoMixedEchoPrintFixer.php

@@ -63,17 +63,15 @@ final class NoMixedEchoPrintFixer extends AbstractFixer implements Configuration
      */
     public function getConfigurationDefinition()
     {
-        $configurationDefinition = new FixerConfigurationResolver();
-
         $use = new FixerOption('use', 'The desired language construct.');
         $use
             ->setAllowedValues(array('print', 'echo'))
             ->setDefault('echo')
         ;
 
-        return $configurationDefinition
-            ->addOption($use)
-        ;
+        return new FixerConfigurationResolver(array(
+            $use,
+        ));
     }
 
     /**

+ 8 - 11
src/Fixer/Alias/RandomApiMigrationFixer.php

@@ -14,13 +14,12 @@ namespace PhpCsFixer\Fixer\Alias;
 
 use PhpCsFixer\AbstractFunctionReferenceFixer;
 use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
-use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
+use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverRootless;
 use PhpCsFixer\FixerConfiguration\FixerOption;
 use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
 use PhpCsFixer\Tokenizer\Tokens;
 use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
-use Symfony\Component\OptionsResolver\Options;
 
 /**
  * @author Vladimir Reznichenko <kalessil@gmail.com>
@@ -58,12 +57,11 @@ final class RandomApiMigrationFixer extends AbstractFunctionReferenceFixer imple
     public function getConfigurationDefinition()
     {
         $argumentCounts = self::$argumentCounts;
-        $configurationDefinition = new FixerConfigurationResolver();
 
         $replacements = new FixerOption('replacements', 'Mapping between replaced functions with the new ones.');
         $replacements
-            ->setAllowedTypes('array')
-            ->setNormalizer(function (Options $options, $value) use ($argumentCounts) {
+            ->setAllowedTypes(array('array'))
+            ->setAllowedValues(array(function ($value) use ($argumentCounts) {
                 foreach ($value as $functionName => $replacement) {
                     if (!array_key_exists($functionName, $argumentCounts)) {
                         throw new InvalidOptionsException(sprintf(
@@ -81,8 +79,8 @@ final class RandomApiMigrationFixer extends AbstractFunctionReferenceFixer imple
                     }
                 }
 
-                return $value;
-            })
+                return true;
+            }))
             ->setDefault(array(
                 'getrandmax' => 'mt_getrandmax',
                 'mt_rand' => 'mt_rand',
@@ -91,10 +89,9 @@ final class RandomApiMigrationFixer extends AbstractFunctionReferenceFixer imple
             ))
         ;
 
-        return $configurationDefinition
-            ->addOption($replacements)
-            ->mapRootConfigurationTo('replacements')
-        ;
+        return new FixerConfigurationResolverRootless('replacements', array(
+            $replacements,
+        ));
     }
 
     /**

+ 3 - 5
src/Fixer/ArrayNotation/ArraySyntaxFixer.php

@@ -53,8 +53,6 @@ final class ArraySyntaxFixer extends AbstractFixer implements ConfigurationDefin
      */
     public function getConfigurationDefinition()
     {
-        $configurationDefinition = new FixerConfigurationResolver();
-
         $syntax = new FixerOption('syntax', 'Whether to use the `long` or `short` array syntax.');
         $syntax
             ->setAllowedValues(array('long', 'short'))
@@ -71,9 +69,9 @@ final class ArraySyntaxFixer extends AbstractFixer implements ConfigurationDefin
             ->setDefault('long')
         ;
 
-        return $configurationDefinition
-            ->addOption($syntax)
-        ;
+        return new FixerConfigurationResolver(array(
+            $syntax,
+        ));
     }
 
     /**

+ 4 - 6
src/Fixer/Basic/BracesFixer.php

@@ -36,17 +36,15 @@ final class BracesFixer extends AbstractFixer implements ConfigurationDefinition
      */
     public function getConfigurationDefinition()
     {
-        $configurationDefinition = new FixerConfigurationResolver();
-
         $allowSingleLineClosure = new FixerOption('allow_single_line_closure', 'Whether single line lambda notation should be allowed.');
         $allowSingleLineClosure
-            ->setAllowedTypes('bool')
+            ->setAllowedTypes(array('bool'))
             ->setDefault(false)
         ;
 
-        return $configurationDefinition
-            ->addOption($allowSingleLineClosure)
-        ;
+        return new FixerConfigurationResolver(array(
+            $allowSingleLineClosure,
+        ));
     }
 
     /**

+ 4 - 6
src/Fixer/Basic/Psr0Fixer.php

@@ -33,14 +33,12 @@ final class Psr0Fixer extends AbstractPsrAutoloadingFixer implements Configurati
      */
     public function getConfigurationDefinition()
     {
-        $configurationDefinition = new FixerConfigurationResolver();
-
         $dir = new FixerOption('dir', 'The directory where the project code is placed.');
-        $dir->setAllowedTypes('string');
+        $dir->setAllowedTypes(array('string'));
 
-        return $configurationDefinition
-            ->addOption($dir)
-        ;
+        return new FixerConfigurationResolver(array(
+            $dir,
+        ));
     }
 
     /**

+ 12 - 14
src/Fixer/ClassNotation/ClassDefinitionFixer.php

@@ -37,31 +37,29 @@ final class ClassDefinitionFixer extends AbstractFixer implements ConfigurationD
      */
     public function getConfigurationDefinition()
     {
-        $configurationDefinition = new FixerConfigurationResolver();
-
-        $singleLine = new FixerOption('singleLine', 'Whether definitions should be single line.');
-        $singleLine
-            ->setAllowedTypes('bool')
+        $multiLineExtendsEachSingleLine = new FixerOption('multiLineExtendsEachSingleLine', 'Whether definitions should be multiline.');
+        $multiLineExtendsEachSingleLine
+            ->setAllowedTypes(array('bool'))
             ->setDefault(false)
         ;
 
         $singleItemSingleLine = new FixerOption('singleItemSingleLine', 'Whether definitions should be single line when including a single item.');
         $singleItemSingleLine
-            ->setAllowedTypes('bool')
+            ->setAllowedTypes(array('bool'))
             ->setDefault(false)
         ;
 
-        $multiLineExtendsEachSingleLine = new FixerOption('multiLineExtendsEachSingleLine', 'Whether definitions should be multiline.');
-        $multiLineExtendsEachSingleLine
-            ->setAllowedTypes('bool')
+        $singleLine = new FixerOption('singleLine', 'Whether definitions should be single line.');
+        $singleLine
+            ->setAllowedTypes(array('bool'))
             ->setDefault(false)
         ;
 
-        return $configurationDefinition
-            ->addOption($singleLine)
-            ->addOption($singleItemSingleLine)
-            ->addOption($multiLineExtendsEachSingleLine)
-        ;
+        return new FixerConfigurationResolver(array(
+            $multiLineExtendsEachSingleLine,
+            $singleItemSingleLine,
+            $singleLine,
+        ));
     }
 
     /**

+ 10 - 7
src/Fixer/ClassNotation/OrderedClassElementsFixer.php

@@ -14,8 +14,9 @@ namespace PhpCsFixer\Fixer\ClassNotation;
 
 use PhpCsFixer\AbstractFixer;
 use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
-use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
+use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverRootless;
 use PhpCsFixer\FixerConfiguration\FixerOption;
+use PhpCsFixer\FixerConfiguration\FixerOptionValidatorGenerator;
 use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
 use PhpCsFixer\Tokenizer\CT;
@@ -120,11 +121,14 @@ final class OrderedClassElementsFixer extends AbstractFixer implements Configura
      */
     public function getConfigurationDefinition()
     {
-        $configurationDefinition = new FixerConfigurationResolver();
+        $generator = new FixerOptionValidatorGenerator();
 
         $order = new FixerOption('order', 'List of strings defining order of elements.');
         $order
-            ->setAllowedValueIsSubsetOf(array_keys(array_merge(self::$typeHierarchy, self::$specialTypes)))
+            ->setAllowedTypes(array('array'))
+            ->setAllowedValues(array(
+                $generator->allowedValueIsSubsetOf(array_keys(array_merge(self::$typeHierarchy, self::$specialTypes))),
+            ))
             ->setDefault(array(
                 'use_trait',
                 'constant_public',
@@ -143,10 +147,9 @@ final class OrderedClassElementsFixer extends AbstractFixer implements Configura
             ))
         ;
 
-        return $configurationDefinition
-            ->addOption($order)
-            ->mapRootConfigurationTo('order')
-        ;
+        return new FixerConfigurationResolverRootless('order', array(
+            $order,
+        ));
     }
 
     /**

+ 10 - 7
src/Fixer/ClassNotation/SingleClassElementPerStatementFixer.php

@@ -15,8 +15,9 @@ namespace PhpCsFixer\Fixer\ClassNotation;
 use PhpCsFixer\AbstractFixer;
 use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
 use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
-use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
+use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverRootless;
 use PhpCsFixer\FixerConfiguration\FixerOption;
+use PhpCsFixer\FixerConfiguration\FixerOptionValidatorGenerator;
 use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
 use PhpCsFixer\Tokenizer\CT;
@@ -38,20 +39,22 @@ final class SingleClassElementPerStatementFixer extends AbstractFixer implements
      */
     public function getConfigurationDefinition()
     {
-        $configurationDefinition = new FixerConfigurationResolver();
+        $generator = new FixerOptionValidatorGenerator();
 
         $values = array('const', 'property');
 
         $elements = new FixerOption('elements', 'List of strings which element should be modified.');
         $elements
             ->setDefault($values)
-            ->setAllowedValueIsSubsetOf($values)
+            ->setAllowedTypes(array('array'))
+            ->setAllowedValues(array(
+                $generator->allowedValueIsSubsetOf($values),
+            ))
         ;
 
-        return $configurationDefinition
-            ->addOption($elements)
-            ->mapRootConfigurationTo('elements')
-        ;
+        return new FixerConfigurationResolverRootless('elements', array(
+            $elements,
+        ));
     }
 
     /**

Some files were not shown because too many files changed in this diff