Browse Source

New Autoreview: Correct option casing

Nat Zimmermann 7 years ago
parent
commit
e9234d3f89

+ 15 - 12
README.rst

@@ -272,12 +272,14 @@ Choose from the list of available rules:
 
   Configuration options:
 
-  - ``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``
+  - ``multi_line_extends_each_single_line`` (``bool``): whether definitions should
+    be multiline; defaults to ``false``; DEPRECATED alias:
+    ``multiLineExtendsEachSingleLine``
+  - ``single_item_single_line`` (``bool``): whether definitions should be single
+    line when including a single item; defaults to ``false``; DEPRECATED alias:
+    ``singleItemSingleLine``
+  - ``single_line`` (``bool``): whether definitions should be single line; defaults
+    to ``false``; DEPRECATED alias: ``singleLine``
 
 * **class_keyword_remove**
 
@@ -474,8 +476,8 @@ Choose from the list of available rules:
 
   Configuration options:
 
-  - ``commentType`` (``'comment'``, ``'PHPDoc'``): comment syntax type; defaults to
-    ``'comment'``
+  - ``comment_type`` (``'comment'``, ``'PHPDoc'``): comment syntax type; defaults to
+    ``'comment'``; DEPRECATED alias: ``commentType``
   - ``header`` (``string``): proper header content; required
   - ``location`` (``'after_declare_strict'``, ``'after_open'``): the location of the
     inserted header; defaults to ``'after_declare_strict'``
@@ -790,10 +792,11 @@ Choose from the list of available rules:
 
   Configuration options:
 
-  - ``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'``
+  - ``imports_order`` (``array``, ``null``): defines the order of import types; defaults
+    to ``null``; DEPRECATED alias: ``importsOrder``
+  - ``sort_algorithm`` (``'alpha'``, ``'length'``): whether the statements should be
+    sorted alphabetically or by length; defaults to ``'alpha'``; DEPRECATED
+    alias: ``sortAlgorithm``
 
 * **php_unit_construct** [@Symfony:risky]
 

+ 5 - 0
src/Console/Command/DescribeCommand.php

@@ -18,6 +18,7 @@ use PhpCsFixer\Fixer\ConfigurableFixerInterface;
 use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
 use PhpCsFixer\Fixer\DefinedFixerInterface;
 use PhpCsFixer\Fixer\FixerInterface;
+use PhpCsFixer\FixerConfiguration\AliasedFixerOption;
 use PhpCsFixer\FixerConfiguration\AllowedValueSubset;
 use PhpCsFixer\FixerDefinition\CodeSampleInterface;
 use PhpCsFixer\FixerDefinition\FileSpecificCodeSampleInterface;
@@ -203,6 +204,10 @@ final class DescribeCommand extends Command
                     $line .= '<comment>required</comment>';
                 }
 
+                if ($option instanceof AliasedFixerOption) {
+                    $line .= '; DEPRECATED alias: <comment>'.$option->getAlias().'</comment>';
+                }
+
                 $output->writeln($line);
             }
 

+ 5 - 0
src/Console/Command/HelpCommand.php

@@ -17,6 +17,7 @@ use PhpCsFixer\Fixer\ConfigurableFixerInterface;
 use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
 use PhpCsFixer\Fixer\DefinedFixerInterface;
 use PhpCsFixer\Fixer\FixerInterface;
+use PhpCsFixer\FixerConfiguration\AliasedFixerOption;
 use PhpCsFixer\FixerConfiguration\AllowedValueSubset;
 use PhpCsFixer\FixerConfiguration\FixerOptionInterface;
 use PhpCsFixer\FixerFactory;
@@ -527,6 +528,10 @@ EOF
                             $line .= 'required';
                         }
 
+                        if ($option instanceof AliasedFixerOption) {
+                            $line .= '; DEPRECATED alias: <comment>'.$option->getAlias().'</comment>';
+                        }
+
                         foreach (self::wordwrap($line, 72) as $index => $line) {
                             $help .= (0 === $index ? '   | - ' : '   |   ').$line."\n";
                         }

+ 21 - 11
src/Fixer/ClassNotation/ClassDefinitionFixer.php

@@ -15,6 +15,7 @@ namespace PhpCsFixer\Fixer\ClassNotation;
 use PhpCsFixer\AbstractFixer;
 use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
 use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
+use PhpCsFixer\FixerConfiguration\AliasedFixerOptionBuilder;
 use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
 use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
 use PhpCsFixer\FixerDefinition\CodeSample;
@@ -78,7 +79,7 @@ extends Bar
 implements Baz, BarBaz
 {}
 ',
-                    array('singleLine' => true)
+                    array('single_line' => true)
                 ),
                 new CodeSample(
 '<?php
@@ -88,7 +89,7 @@ extends Bar
 implements Baz
 {}
 ',
-                    array('singleItemSingleLine' => true)
+                    array('single_item_single_line' => true)
                 ),
                 new CodeSample(
 '<?php
@@ -97,7 +98,7 @@ interface Bar extends
     Bar, BarBaz, FooBarBaz
 {}
 ',
-                    array('multiLineExtendsEachSingleLine' => true)
+                    array('multi_line_extends_each_single_line' => true)
                 ),
             )
         );
@@ -129,19 +130,28 @@ interface Bar extends
      */
     protected function createConfigurationDefinition()
     {
-        $multiLineExtendsEachSingleLine = new FixerOptionBuilder('multiLineExtendsEachSingleLine', 'Whether definitions should be multiline.');
+        $multiLineExtendsEachSingleLine = new AliasedFixerOptionBuilder(
+            new FixerOptionBuilder('multi_line_extends_each_single_line', 'Whether definitions should be multiline.'),
+            'multiLineExtendsEachSingleLine'
+        );
         $multiLineExtendsEachSingleLine
             ->setAllowedTypes(array('bool'))
             ->setDefault(false)
         ;
 
-        $singleItemSingleLine = new FixerOptionBuilder('singleItemSingleLine', 'Whether definitions should be single line when including a single item.');
+        $singleItemSingleLine = new AliasedFixerOptionBuilder(
+            new FixerOptionBuilder('single_item_single_line', 'Whether definitions should be single line when including a single item.'),
+            'singleItemSingleLine'
+        );
         $singleItemSingleLine
             ->setAllowedTypes(array('bool'))
             ->setDefault(false)
         ;
 
-        $singleLine = new FixerOptionBuilder('singleLine', 'Whether definitions should be single line.');
+        $singleLine = new AliasedFixerOptionBuilder(
+            new FixerOptionBuilder('single_line', 'Whether definitions should be single line.'),
+            'singleLine'
+        );
         $singleLine
             ->setAllowedTypes(array('bool'))
             ->setDefault(false)
@@ -212,13 +222,13 @@ interface Bar extends
     {
         $endIndex = $tokens->getPrevNonWhitespace($classOpenIndex);
 
-        if ($this->configuration['singleLine'] || false === $classExtendsInfo['multiLine']) {
+        if ($this->configuration['single_line'] || false === $classExtendsInfo['multiLine']) {
             $this->makeClassyDefinitionSingleLine($tokens, $classExtendsInfo['start'], $endIndex);
             $classExtendsInfo['multiLine'] = false;
-        } elseif ($this->configuration['singleItemSingleLine'] && 1 === $classExtendsInfo['numberOfExtends']) {
+        } elseif ($this->configuration['single_item_single_line'] && 1 === $classExtendsInfo['numberOfExtends']) {
             $this->makeClassyDefinitionSingleLine($tokens, $classExtendsInfo['start'], $endIndex);
             $classExtendsInfo['multiLine'] = false;
-        } elseif ($this->configuration['multiLineExtendsEachSingleLine'] && $classExtendsInfo['multiLine']) {
+        } elseif ($this->configuration['multi_line_extends_each_single_line'] && $classExtendsInfo['multiLine']) {
             $this->makeClassyInheritancePartMultiLine($tokens, $classExtendsInfo['start'], $endIndex);
             $classExtendsInfo['multiLine'] = true;
         }
@@ -237,10 +247,10 @@ interface Bar extends
     {
         $endIndex = $tokens->getPrevNonWhitespace($classOpenIndex);
 
-        if ($this->configuration['singleLine'] || false === $classImplementsInfo['multiLine']) {
+        if ($this->configuration['single_line'] || false === $classImplementsInfo['multiLine']) {
             $this->makeClassyDefinitionSingleLine($tokens, $classImplementsInfo['start'], $endIndex);
             $classImplementsInfo['multiLine'] = false;
-        } elseif ($this->configuration['singleItemSingleLine'] && 1 === $classImplementsInfo['numberOfImplements']) {
+        } elseif ($this->configuration['single_item_single_line'] && 1 === $classImplementsInfo['numberOfImplements']) {
             $this->makeClassyDefinitionSingleLine($tokens, $classImplementsInfo['start'], $endIndex);
             $classImplementsInfo['multiLine'] = false;
         } else {

+ 9 - 5
src/Fixer/Comment/HeaderCommentFixer.php

@@ -15,6 +15,7 @@ namespace PhpCsFixer\Fixer\Comment;
 use PhpCsFixer\AbstractFixer;
 use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
 use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
+use PhpCsFixer\FixerConfiguration\AliasedFixerOptionBuilder;
 use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
 use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
 use PhpCsFixer\FixerDefinition\CodeSample;
@@ -77,7 +78,7 @@ echo 1;
 ',
                     array(
                         'header' => 'Made with love.',
-                        'commentType' => 'PHPDoc',
+                        'comment_type' => 'PHPDoc',
                         'location' => 'after_open',
                         'separate' => 'bottom',
                     )
@@ -92,7 +93,7 @@ echo 1;
 ',
                     array(
                         'header' => 'Made with love.',
-                        'commentType' => 'comment',
+                        'comment_type' => 'comment',
                         'location' => 'after_declare_strict',
                     )
                 ),
@@ -156,7 +157,10 @@ echo 1;
             })
         ;
 
-        $commentType = new FixerOptionBuilder('commentType', 'Comment syntax type.');
+        $commentType = new AliasedFixerOptionBuilder(
+            new FixerOptionBuilder('comment_type', 'Comment syntax type.'),
+            'commentType'
+        );
         $commentType
             ->setAllowedValues(array(self::HEADER_PHPDOC, self::HEADER_COMMENT))
             ->setDefault(self::HEADER_COMMENT)
@@ -191,7 +195,7 @@ echo 1;
     {
         $lineEnding = $this->whitespacesConfig->getLineEnding();
 
-        $comment = (self::HEADER_COMMENT === $this->configuration['commentType'] ? '/*' : '/**').$lineEnding;
+        $comment = (self::HEADER_COMMENT === $this->configuration['comment_type'] ? '/*' : '/**').$lineEnding;
         $lines = explode("\n", str_replace("\r", '', $this->configuration['header']));
 
         foreach ($lines as $line) {
@@ -339,6 +343,6 @@ echo 1;
      */
     private function insertHeader(Tokens $tokens, $index)
     {
-        $tokens->insertAt($index, new Token(array(self::HEADER_COMMENT === $this->configuration['commentType'] ? T_COMMENT : T_DOC_COMMENT, $this->getHeaderAsComment())));
+        $tokens->insertAt($index, new Token(array(self::HEADER_COMMENT === $this->configuration['comment_type'] ? T_COMMENT : T_DOC_COMMENT, $this->getHeaderAsComment())));
     }
 }

+ 19 - 12
src/Fixer/Import/OrderedImportsFixer.php

@@ -15,6 +15,7 @@ namespace PhpCsFixer\Fixer\Import;
 use PhpCsFixer\AbstractFixer;
 use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
 use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
+use PhpCsFixer\FixerConfiguration\AliasedFixerOptionBuilder;
 use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
 use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
 use PhpCsFixer\FixerDefinition\CodeSample;
@@ -77,7 +78,7 @@ use Acme;
 use Barr;
 use Acme\Bar;
 ',
-                    array('sortAlgorithm' => self::SORT_LENGTH)
+                    array('sort_algorithm' => self::SORT_LENGTH)
                 ),
                 new VersionSpecificCodeSample(
                     "<?php\nuse function AAA;\nuse const AAB;\nuse AAC;",
@@ -97,8 +98,8 @@ use function DDD;
 ',
                     new VersionSpecification(70000),
                     array(
-                        'sortAlgorithm' => self::SORT_LENGTH,
-                        'importsOrder' => array(
+                        'sort_algorithm' => self::SORT_LENGTH,
+                        'imports_order' => array(
                             self::IMPORT_TYPE_CONST,
                             self::IMPORT_TYPE_CLASS,
                             self::IMPORT_TYPE_FUNCTION,
@@ -119,8 +120,8 @@ use function CCC\AA;
 ',
                     new VersionSpecification(70000),
                     array(
-                        'sortAlgorithm' => self::SORT_ALPHA,
-                        'importsOrder' => array(
+                        'sort_algorithm' => self::SORT_ALPHA,
+                        'imports_order' => array(
                             self::IMPORT_TYPE_CONST,
                             self::IMPORT_TYPE_CLASS,
                             self::IMPORT_TYPE_FUNCTION,
@@ -202,13 +203,19 @@ use function CCC\AA;
     {
         $supportedSortTypes = $this->supportedSortTypes;
 
-        $sortAlgorithm = new FixerOptionBuilder('sortAlgorithm', 'whether the statements should be sorted alphabetically or by length');
+        $sortAlgorithm = new AliasedFixerOptionBuilder(
+            new FixerOptionBuilder('sort_algorithm', 'whether the statements should be sorted alphabetically or by length'),
+            'sortAlgorithm'
+        );
         $sortAlgorithm
             ->setAllowedValues($this->supportedSortAlgorithms)
             ->setDefault(self::SORT_ALPHA)
         ;
 
-        $importsOrder = new FixerOptionBuilder('importsOrder', 'Defines the order of import types.');
+        $importsOrder = new AliasedFixerOptionBuilder(
+            new FixerOptionBuilder('imports_order', 'Defines the order of import types.'),
+            'importsOrder'
+        );
         $importsOrder
             ->setAllowedTypes(array('array', 'null'))
             ->setAllowedValues(array(function ($value) use ($supportedSortTypes) {
@@ -434,7 +441,7 @@ use function CCC\AA;
         }
 
         // Is sort types provided, sorting by groups and each group by algorithm
-        if ($this->configuration['importsOrder']) {
+        if ($this->configuration['imports_order']) {
             // Grouping indexes by import type.
             $groupedByTypes = array();
             foreach ($indexes as $startIndex => $item) {
@@ -448,7 +455,7 @@ use function CCC\AA;
 
             // Ordering groups
             $sortedGroups = array();
-            foreach ($this->configuration['importsOrder'] as $type) {
+            foreach ($this->configuration['imports_order'] as $type) {
                 if (isset($groupedByTypes[$type]) && !empty($groupedByTypes[$type])) {
                     foreach ($groupedByTypes[$type] as $startIndex => $item) {
                         $sortedGroups[$startIndex] = $item;
@@ -479,12 +486,12 @@ use function CCC\AA;
      */
     private function sortByAlgorithm(array $indexes)
     {
-        if (self::SORT_ALPHA === $this->configuration['sortAlgorithm']) {
+        if (self::SORT_ALPHA === $this->configuration['sort_algorithm']) {
             uasort($indexes, array($this, 'sortAlphabetically'));
-        } elseif (self::SORT_LENGTH === $this->configuration['sortAlgorithm']) {
+        } elseif (self::SORT_LENGTH === $this->configuration['sort_algorithm']) {
             uasort($indexes, array($this, 'sortByLength'));
         } else {
-            throw new \LogicException(sprintf('Sort algorithm "%s" is not supported.', $this->configuration['sortAlgorithm']));
+            throw new \LogicException(sprintf('Sort algorithm "%s" is not supported.', $this->configuration['sort_algorithm']));
         }
 
         return $indexes;

+ 103 - 0
src/FixerConfiguration/AliasedFixerOption.php

@@ -0,0 +1,103 @@
+<?php
+
+/*
+ * This file is part of PHP CS Fixer.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *     Dariusz Rumiński <dariusz.ruminski@gmail.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+namespace PhpCsFixer\FixerConfiguration;
+
+/**
+ * @author ntzm
+ *
+ * @internal
+ *
+ * @todo 3.0 Drop this class
+ */
+final class AliasedFixerOption implements FixerOptionInterface
+{
+    /**
+     * @var FixerOptionInterface
+     */
+    private $fixerOption;
+
+    /**
+     * @var string
+     */
+    private $alias;
+
+    public function __construct(FixerOptionInterface $fixerOption, $alias)
+    {
+        $this->fixerOption = $fixerOption;
+        $this->alias = $alias;
+    }
+
+    /**
+     * @return string
+     */
+    public function getAlias()
+    {
+        return $this->alias;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getName()
+    {
+        return $this->fixerOption->getName();
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getDescription()
+    {
+        return $this->fixerOption->getDescription();
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function hasDefault()
+    {
+        return $this->fixerOption->hasDefault();
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getDefault()
+    {
+        return $this->fixerOption->getDefault();
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getAllowedTypes()
+    {
+        return $this->fixerOption->getAllowedTypes();
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getAllowedValues()
+    {
+        return $this->fixerOption->getAllowedValues();
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getNormalizer()
+    {
+        return $this->fixerOption->getNormalizer();
+    }
+}

+ 98 - 0
src/FixerConfiguration/AliasedFixerOptionBuilder.php

@@ -0,0 +1,98 @@
+<?php
+
+/*
+ * This file is part of PHP CS Fixer.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *     Dariusz Rumiński <dariusz.ruminski@gmail.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+namespace PhpCsFixer\FixerConfiguration;
+
+/**
+ * @author ntzm
+ *
+ * @internal
+ *
+ * @todo 3.0 Drop this class
+ */
+final class AliasedFixerOptionBuilder
+{
+    /**
+     * @var FixerOptionBuilder
+     */
+    private $optionBuilder;
+
+    /**
+     * @var string
+     */
+    private $alias;
+
+    public function __construct(FixerOptionBuilder $optionBuilder, $alias)
+    {
+        $this->optionBuilder = $optionBuilder;
+        $this->alias = $alias;
+    }
+
+    /**
+     * @param mixed $default
+     *
+     * @return $this
+     */
+    public function setDefault($default)
+    {
+        $this->optionBuilder->setDefault($default);
+
+        return $this;
+    }
+
+    /**
+     * @param string[] $allowedTypes
+     *
+     * @return $this
+     */
+    public function setAllowedTypes(array $allowedTypes)
+    {
+        $this->optionBuilder->setAllowedTypes($allowedTypes);
+
+        return $this;
+    }
+
+    /**
+     * @param array $allowedValues
+     *
+     * @return $this
+     */
+    public function setAllowedValues(array $allowedValues)
+    {
+        $this->optionBuilder->setAllowedValues($allowedValues);
+
+        return $this;
+    }
+
+    /**
+     * @param \Closure $normalizer
+     *
+     * @return $this
+     */
+    public function setNormalizer(\Closure $normalizer)
+    {
+        $this->optionBuilder->setNormalizer($normalizer);
+
+        return $this;
+    }
+
+    /**
+     * @return AliasedFixerOption
+     */
+    public function getOption()
+    {
+        return new AliasedFixerOption(
+            $this->optionBuilder->getOption(),
+            $this->alias
+        );
+    }
+}

+ 15 - 0
src/FixerConfiguration/FixerConfigurationResolver.php

@@ -12,6 +12,7 @@
 
 namespace PhpCsFixer\FixerConfiguration;
 
+use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
 use Symfony\Component\OptionsResolver\OptionsResolver;
 
 final class FixerConfigurationResolver implements FixerConfigurationResolverInterface
@@ -58,6 +59,20 @@ final class FixerConfigurationResolver implements FixerConfigurationResolverInte
         foreach ($this->options as $option) {
             $name = $option->getName();
 
+            if ($option instanceof AliasedFixerOption) {
+                $alias = $option->getAlias();
+
+                if (array_key_exists($alias, $options)) {
+                    // @TODO 2.12 Trigger a deprecation notice and add a test for it
+                    if (array_key_exists($name, $options)) {
+                        throw new InvalidOptionsException(sprintf('Aliased option %s/%s is passed multiple times.', $name, $alias));
+                    }
+
+                    $options[$name] = $options[$alias];
+                    unset($options[$alias]);
+                }
+            }
+
             if ($option->hasDefault()) {
                 $resolver->setDefault($name, $option->getDefault());
             } else {

+ 1 - 1
src/RuleSet.php

@@ -64,7 +64,7 @@ final class RuleSet implements RuleSetInterface
                 'allow_single_line_closure' => true,
             ),
             'cast_spaces' => true,
-            'class_definition' => array('singleLine' => true),
+            'class_definition' => array('single_line' => true),
             'concat_space' => array('spacing' => 'none'),
             'declare_equal_normalize' => true,
             'function_typehint_space' => true,

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