Browse Source

Merge branch '2.2' into 2.11

# Conflicts:
#	README.rst
#	src/Console/Command/DescribeCommand.php
#	src/Console/Command/HelpCommand.php
#	src/Fixer/ClassNotation/ClassDefinitionFixer.php
#	src/Fixer/Comment/HeaderCommentFixer.php
#	src/Fixer/Import/OrderedImportsFixer.php
#	src/RuleSet.php
#	tests/AutoReview/FixerTest.php
#	tests/Console/Command/DescribeCommandTest.php
#	tests/Fixer/ClassNotation/ClassDefinitionFixerTest.php
#	tests/Fixer/Comment/HeaderCommentFixerTest.php
#	tests/Fixer/Import/OrderedImportsFixerTest.php
Dariusz Ruminski 6 years ago
parent
commit
5172d8d197

+ 15 - 13
README.rst

@@ -351,12 +351,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**
 
@@ -666,8 +668,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'``
@@ -1087,11 +1089,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'``, ``'none'``): whether the statements should
-    be sorted alphabetically or by length, or not sorted; defaults to
-    ``'alpha'``
+  - ``imports_order`` (``array``, ``null``): defines the order of import types; defaults
+    to ``null``; DEPRECATED alias: ``importsOrder``
+  - ``sort_algorithm`` (``'alpha'``, ``'length'``, ``'none'``): whether the statements
+    should be sorted alphabetically or by length, or not sorted; defaults
+    to ``'alpha'``; DEPRECATED alias: ``sortAlgorithm``
 
 * **php_unit_construct** [@Symfony:risky]
 

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

@@ -19,6 +19,7 @@ use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
 use PhpCsFixer\Fixer\DefinedFixerInterface;
 use PhpCsFixer\Fixer\DeprecatedFixerInterface;
 use PhpCsFixer\Fixer\FixerInterface;
+use PhpCsFixer\FixerConfiguration\AliasedFixerOption;
 use PhpCsFixer\FixerConfiguration\AllowedValueSubset;
 use PhpCsFixer\FixerConfiguration\DeprecatedFixerOption;
 use PhpCsFixer\FixerDefinition\CodeSampleInterface;
@@ -228,6 +229,9 @@ final class DescribeCommand extends Command
                         OutputFormatter::escape(lcfirst($option->getDeprecationMessage()))
                     );
                 }
+                if ($option instanceof AliasedFixerOption) {
+                    $line .= '; <error>DEPRECATED</error> alias: <comment>'.$option->getAlias().'</comment>';
+                }
 
                 $output->writeln($line);
             }

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

@@ -18,6 +18,7 @@ use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
 use PhpCsFixer\Fixer\DefinedFixerInterface;
 use PhpCsFixer\Fixer\DeprecatedFixerInterface;
 use PhpCsFixer\Fixer\FixerInterface;
+use PhpCsFixer\FixerConfiguration\AliasedFixerOption;
 use PhpCsFixer\FixerConfiguration\AllowedValueSubset;
 use PhpCsFixer\FixerConfiguration\DeprecatedFixerOption;
 use PhpCsFixer\FixerConfiguration\FixerOptionInterface;
@@ -564,6 +565,10 @@ EOF
                             );
                         }
 
+                        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;
@@ -71,7 +72,7 @@ extends Bar
 implements Baz, BarBaz
 {}
 ',
-                    ['singleLine' => true]
+                    ['single_line' => true]
                 ),
                 new CodeSample(
 '<?php
@@ -81,7 +82,7 @@ extends Bar
 implements Baz
 {}
 ',
-                    ['singleItemSingleLine' => true]
+                    ['single_item_single_line' => true]
                 ),
                 new CodeSample(
 '<?php
@@ -90,7 +91,7 @@ interface Bar extends
     Bar, BarBaz, FooBarBaz
 {}
 ',
-                    ['multiLineExtendsEachSingleLine' => true]
+                    ['multi_line_extends_each_single_line' => true]
                 ),
             ]
         );
@@ -123,15 +124,24 @@ interface Bar extends
     protected function createConfigurationDefinition()
     {
         return new FixerConfigurationResolver([
-            (new FixerOptionBuilder('multiLineExtendsEachSingleLine', 'Whether definitions should be multiline.'))
+            (new AliasedFixerOptionBuilder(
+                new FixerOptionBuilder('multi_line_extends_each_single_line', 'Whether definitions should be multiline.'),
+                'multiLineExtendsEachSingleLine'
+            ))
                 ->setAllowedTypes(['bool'])
                 ->setDefault(false)
                 ->getOption(),
-            (new FixerOptionBuilder('singleItemSingleLine', 'Whether definitions should be single line when including a single item.'))
+            (new AliasedFixerOptionBuilder(
+                new FixerOptionBuilder('single_item_single_line', 'Whether definitions should be single line when including a single item.'),
+                'singleItemSingleLine'
+            ))
                 ->setAllowedTypes(['bool'])
                 ->setDefault(false)
                 ->getOption(),
-            (new FixerOptionBuilder('singleLine', 'Whether definitions should be single line.'))
+            (new AliasedFixerOptionBuilder(
+                new FixerOptionBuilder('single_line', 'Whether definitions should be single line.'),
+                'singleLine'
+            ))
                 ->setAllowedTypes(['bool'])
                 ->setDefault(false)
                 ->getOption(),
@@ -196,13 +206,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;
         }
@@ -221,10 +231,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;
 ',
                     [
                         'header' => 'Made with love.',
-                        'commentType' => 'PHPDoc',
+                        'comment_type' => 'PHPDoc',
                         'location' => 'after_open',
                         'separate' => 'bottom',
                     ]
@@ -92,7 +93,7 @@ echo 1;
 ',
                     [
                         'header' => 'Made with love.',
-                        'commentType' => 'comment',
+                        'comment_type' => 'comment',
                         'location' => 'after_declare_strict',
                     ]
                 ),
@@ -155,7 +156,10 @@ echo 1;
                     return $value;
                 })
                 ->getOption(),
-            (new FixerOptionBuilder('commentType', 'Comment syntax type.'))
+            (new AliasedFixerOptionBuilder(
+                new FixerOptionBuilder('comment_type', 'Comment syntax type.'),
+                'commentType'
+            ))
                 ->setAllowedValues([self::HEADER_PHPDOC, self::HEADER_COMMENT])
                 ->setDefault(self::HEADER_COMMENT)
                 ->getOption(),
@@ -179,7 +183,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) {
@@ -327,6 +331,6 @@ echo 1;
      */
     private function insertHeader(Tokens $tokens, $index)
     {
-        $tokens->insertAt($index, new Token([self::HEADER_COMMENT === $this->configuration['commentType'] ? T_COMMENT : T_DOC_COMMENT, $this->getHeaderAsComment()]));
+        $tokens->insertAt($index, new Token([self::HEADER_COMMENT === $this->configuration['comment_type'] ? T_COMMENT : T_DOC_COMMENT, $this->getHeaderAsComment()]));
     }
 }

+ 21 - 14
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;
@@ -79,7 +80,7 @@ use Acme;
 use Barr;
 use Acme\Bar;
 ',
-                    ['sortAlgorithm' => self::SORT_LENGTH]
+                    ['sort_algorithm' => self::SORT_LENGTH]
                 ),
                 new VersionSpecificCodeSample(
                     "<?php\nuse function AAA;\nuse const AAB;\nuse AAC;\n",
@@ -99,8 +100,8 @@ use function DDD;
 ',
                     new VersionSpecification(70000),
                     [
-                        'sortAlgorithm' => self::SORT_LENGTH,
-                        'importsOrder' => [
+                        'sort_algorithm' => self::SORT_LENGTH,
+                        'imports_order' => [
                             self::IMPORT_TYPE_CONST,
                             self::IMPORT_TYPE_CLASS,
                             self::IMPORT_TYPE_FUNCTION,
@@ -121,8 +122,8 @@ use function CCC\AA;
 ',
                     new VersionSpecification(70000),
                     [
-                        'sortAlgorithm' => self::SORT_ALPHA,
-                        'importsOrder' => [
+                        'sort_algorithm' => self::SORT_ALPHA,
+                        'imports_order' => [
                             self::IMPORT_TYPE_CONST,
                             self::IMPORT_TYPE_CLASS,
                             self::IMPORT_TYPE_FUNCTION,
@@ -143,8 +144,8 @@ use Bar;
 ',
                     new VersionSpecification(70000),
                     [
-                        'sortAlgorithm' => self::SORT_NONE,
-                        'importsOrder' => [
+                        'sort_algorithm' => self::SORT_NONE,
+                        'imports_order' => [
                             self::IMPORT_TYPE_CONST,
                             self::IMPORT_TYPE_CLASS,
                             self::IMPORT_TYPE_FUNCTION,
@@ -228,11 +229,17 @@ use Bar;
         $supportedSortTypes = $this->supportedSortTypes;
 
         return new FixerConfigurationResolver([
-            (new FixerOptionBuilder('sortAlgorithm', 'whether the statements should be sorted alphabetically or by length, or not sorted'))
+            (new AliasedFixerOptionBuilder(
+                new FixerOptionBuilder('sort_algorithm', 'whether the statements should be sorted alphabetically or by length, or not sorted'),
+                'sortAlgorithm'
+            ))
                 ->setAllowedValues($this->supportedSortAlgorithms)
                 ->setDefault(self::SORT_ALPHA)
                 ->getOption(),
-            (new FixerOptionBuilder('importsOrder', 'Defines the order of import types.'))
+            (new AliasedFixerOptionBuilder(
+                new FixerOptionBuilder('imports_order', 'Defines the order of import types.'),
+                'importsOrder'
+            ))
                 ->setAllowedTypes(['array', 'null'])
                 ->setAllowedValues([static function ($value) use ($supportedSortTypes) {
                     if (null !== $value) {
@@ -351,7 +358,7 @@ use Bar;
                 $token = $tokens[$index];
 
                 if ($index === $endIndex || (!$group && $token->equals(','))) {
-                    if ($group && self::SORT_NONE !== $this->configuration['sortAlgorithm']) {
+                    if ($group && self::SORT_NONE !== $this->configuration['sort_algorithm']) {
                         // if group import, sort the items within the group definition
 
                         // figure out where the list of namespace parts within the group def. starts
@@ -453,7 +460,7 @@ use Bar;
         }
 
         // 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 = [];
             foreach ($indexes as $startIndex => $item) {
@@ -467,7 +474,7 @@ use Bar;
 
             // Ordering groups
             $sortedGroups = [];
-            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;
@@ -498,9 +505,9 @@ use Bar;
      */
     private function sortByAlgorithm(array $indexes)
     {
-        if (self::SORT_ALPHA === $this->configuration['sortAlgorithm']) {
+        if (self::SORT_ALPHA === $this->configuration['sort_algorithm']) {
             uasort($indexes, [$this, 'sortAlphabetically']);
-        } elseif (self::SORT_LENGTH === $this->configuration['sortAlgorithm']) {
+        } elseif (self::SORT_LENGTH === $this->configuration['sort_algorithm']) {
             uasort($indexes, [$this, 'sortByLength']);
         }
 

+ 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

@@ -67,7 +67,7 @@ final class RuleSet implements RuleSetInterface
             ],
             'cast_spaces' => true,
             'class_attributes_separation' => ['elements' => ['method']],
-            'class_definition' => ['singleLine' => true],
+            'class_definition' => ['single_line' => true],
             'concat_space' => ['spacing' => 'none'],
             'declare_equal_normalize' => true,
             'function_typehint_space' => true,

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