Browse Source

BinaryOperatorSpacesFixer - add (un)align configuration options

SpacePossum 8 years ago
parent
commit
65aae8cae7

+ 0 - 12
README.rst

@@ -174,12 +174,6 @@ automatically fix anything:
 
 
 Choose from the list of available fixers:
 Choose from the list of available fixers:
 
 
-* **align_double_arrow**
-   Align double arrow symbols in consecutive lines.
-
-* **align_equals**
-   Align equals symbols in consecutive lines.
-
 * **binary_operator_spaces** [@Symfony]
 * **binary_operator_spaces** [@Symfony]
    Binary operators should be surrounded by at least one space.
    Binary operators should be surrounded by at least one space.
 
 
@@ -594,12 +588,6 @@ Choose from the list of available fixers:
    Arrays should be formatted like function/method arguments, without
    Arrays should be formatted like function/method arguments, without
    leading or trailing single line space.
    leading or trailing single line space.
 
 
-* **unalign_double_arrow** [@Symfony]
-   Unalign double arrow symbols.
-
-* **unalign_equals** [@Symfony]
-   Unalign equals symbols.
-
 * **unary_operator_spaces** [@Symfony]
 * **unary_operator_spaces** [@Symfony]
    Unary operators should be placed adjacent to their operands.
    Unary operators should be placed adjacent to their operands.
 
 

+ 6 - 2
UPGRADE.md

@@ -11,7 +11,7 @@ Default ruleset was changed from Symfony standard to more generic PSR2. You can
 
 
 The term of risky fixers was introduced. Risky fixer is a fixer that may change the meaning of code (like `StrictComparisonFixer` fixer, which will change `==` into `===`). No rules that are followed by risky fixers are run by default. You need to explicitly permit risky fixers to run them.
 The term of risky fixers was introduced. Risky fixer is a fixer that may change the meaning of code (like `StrictComparisonFixer` fixer, which will change `==` into `===`). No rules that are followed by risky fixers are run by default. You need to explicitly permit risky fixers to run them.
 
 
-Default configuraton changes
+Default configuration changes
 ----------------------------
 ----------------------------
 By default, PSR2 rules are used instead of Symfony rules.
 By default, PSR2 rules are used instead of Symfony rules.
 Files that will be fixed are php/phpt/twig instead of php/twig/xml/yml.
 Files that will be fixed are php/phpt/twig instead of php/twig/xml/yml.
@@ -71,13 +71,15 @@ Config and Finder classes
 -------------------------
 -------------------------
 All off `Symfony\CS\Config\*` and `Symfony\CS\Finder\*` classes have been removed, instead use `PhpCsFixer\Config` and `PhpCsFixer\Finder`.
 All off `Symfony\CS\Config\*` and `Symfony\CS\Finder\*` classes have been removed, instead use `PhpCsFixer\Config` and `PhpCsFixer\Finder`.
 
 
-For that reason you can not set config class by `--config` CLI argument, from now it is used to set configuration file. Thanks to this the `--config-file` CLI argument is no longer available.
+For that reason you can not set config class by `--config` CLI argument, from now it is used to set configuration file. Therefor the `--config-file` CLI argument is no longer available.
 
 
 Renamed rules
 Renamed rules
 -------------
 -------------
 
 
 Old name | New name | Note
 Old name | New name | Note
 -------- | -------- | ----
 -------- | -------- | ----
+align_double_arrow                             | binary_operator_spaces                            | use configuration option 'align_double_arrow: true'
+align_equals                                   | binary_operator_spaces                            | use configuration option 'align_equals: true'
 array_element_no_space_before_comma            | no_whitespace_before_comma_in_array
 array_element_no_space_before_comma            | no_whitespace_before_comma_in_array
 array_element_white_space_after_comma          | whitespace_after_comma_in_array
 array_element_white_space_after_comma          | whitespace_after_comma_in_array
 blankline_after_open_tag                       | blank_line_after_opening_tag
 blankline_after_open_tag                       | blank_line_after_opening_tag
@@ -123,6 +125,8 @@ standardize_not_equal                          | standardize_not_equals
 strict                                         | strict_comparison
 strict                                         | strict_comparison
 ternary_spaces                                 | ternary_operator_spaces
 ternary_spaces                                 | ternary_operator_spaces
 trailing_spaces                                | no_trailing_whitespace
 trailing_spaces                                | no_trailing_whitespace
+unalign_double_arrow                           | binary_operator_spaces                            | use configuration option 'align_double_arrow: false'
+unalign_equals                                 | binary_operator_spaces                            | use configuration option 'align_equals: false'
 unary_operators_spaces                         | unary_operator_spaces
 unary_operators_spaces                         | unary_operator_spaces
 unneeded_control_parentheses                   | no_unneeded_control_parentheses
 unneeded_control_parentheses                   | no_unneeded_control_parentheses
 unused_use                                     | no_unused_imports
 unused_use                                     | no_unused_imports

+ 30 - 2
src/AbstractAlignFixer.php → src/AbstractAlignFixerHelper.php

@@ -19,7 +19,7 @@ use PhpCsFixer\Tokenizer\Tokens;
  *
  *
  * @internal
  * @internal
  */
  */
-abstract class AbstractAlignFixer extends AbstractFixer
+abstract class AbstractAlignFixerHelper
 {
 {
     /**
     /**
      * @const Placeholder used as anchor for right alignment.
      * @const Placeholder used as anchor for right alignment.
@@ -33,7 +33,35 @@ abstract class AbstractAlignFixer extends AbstractFixer
      *
      *
      * @var int
      * @var int
      */
      */
-    protected $deepestLevel;
+    protected $deepestLevel = 0;
+
+    /**
+     * {@inheritdoc}
+     */
+    public function fix(\SplFileInfo $file, Tokens $tokens)
+    {
+        // This fixer works partially on Tokens and partially on string representation of code.
+        // During the process of fixing internal state of single Token may be affected by injecting ALIGNABLE_PLACEHOLDER to its content.
+        // The placeholder will be resolved by `replacePlaceholder` method by removing placeholder or changing it into spaces.
+        // That way of fixing the code causes disturbances in marking Token as changed - if code is perfectly valid then placeholder
+        // still be injected and removed, which will cause the `changed` flag to be set.
+        // To handle that unwanted behavior we work on clone of Tokens collection and then override original collection with fixed collection.
+        $tokensClone = clone $tokens;
+
+        $this->injectAlignmentPlaceholders($tokensClone, 0, count($tokens));
+        $content = $this->replacePlaceholder($tokensClone);
+
+        $tokens->setCode($content);
+    }
+
+    /**
+     * Inject into the text placeholders of candidates of vertical alignment.
+     *
+     * @param Tokens $tokens
+     * @param int    $startAt
+     * @param int    $endAt
+     */
+    abstract protected function injectAlignmentPlaceholders(Tokens $tokens, $startAt, $endAt);
 
 
     /**
     /**
      * Look for group of placeholders, and provide vertical alignment.
      * Look for group of placeholders, and provide vertical alignment.

+ 1 - 1
src/Fixer/ArrayNotation/NoMultilineWhitespaceAroundDoubleArrowFixer.php

@@ -62,7 +62,7 @@ final class NoMultilineWhitespaceAroundDoubleArrowFixer extends AbstractFixer
      */
      */
     public function getPriority()
     public function getPriority()
     {
     {
-        // should be run before the TrailingCommaInMultilineArrayFixer and AlignDoubleArrowFixer
+        // should be run before the TrailingCommaInMultilineArrayFixer and BinaryOperatorSpacesFixer.
         return 1;
         return 1;
     }
     }
 
 

+ 1 - 1
src/Fixer/ArrayNotation/ShortArraySyntaxFixer.php

@@ -62,7 +62,7 @@ final class ShortArraySyntaxFixer extends AbstractFixer
      */
      */
     public function getPriority()
     public function getPriority()
     {
     {
-        // should be run before the UnalignEqualsFixer and TernarySpacesFixer.
+        // should be run before the BinaryOperatorSpacesFixer and TernarySpacesFixer.
         return 1;
         return 1;
     }
     }
 }
 }

+ 0 - 66
src/Fixer/ArrayNotation/UnalignDoubleArrowFixer.php

@@ -1,66 +0,0 @@
-<?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\Fixer\ArrayNotation;
-
-use PhpCsFixer\AbstractFixer;
-use PhpCsFixer\Tokenizer\Token;
-use PhpCsFixer\Tokenizer\Tokens;
-
-/**
- * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
- */
-final class UnalignDoubleArrowFixer extends AbstractFixer
-{
-    /**
-     * {@inheritdoc}
-     */
-    public function isCandidate(Tokens $tokens)
-    {
-        return $tokens->isTokenKindFound(T_DOUBLE_ARROW);
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function getDescription()
-    {
-        return 'Unalign double arrow symbols.';
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function fix(\SplFileInfo $file, Tokens $tokens)
-    {
-        foreach ($tokens as $index => $token) {
-            if (!$token->isGivenKind(T_DOUBLE_ARROW)) {
-                continue;
-            }
-
-            $this->fixWhitespace($tokens[$index - 1]);
-            $this->fixWhitespace($tokens[$index + 1]);
-        }
-    }
-
-    /**
-     * If given token is a single line whitespace then fix it to be a single space.
-     *
-     * @param Token $token
-     */
-    private function fixWhitespace(Token $token)
-    {
-        if ($token->isWhitespace(" \t")) {
-            $token->setContent(' ');
-        }
-    }
-}

+ 1 - 1
src/Fixer/ClassNotation/SingleClassElementPerStatementFixer.php

@@ -55,7 +55,7 @@ final class SingleClassElementPerStatementFixer extends AbstractFixer
 
 
         foreach ($configuration as $name) {
         foreach ($configuration as $name) {
             if (!in_array($name, self::$defaultConfiguration, true)) {
             if (!in_array($name, self::$defaultConfiguration, true)) {
-                throw new InvalidFixerConfigurationException($this->getName(), sprintf('Unknown configuration option "%s".', $name));
+                throw new InvalidFixerConfigurationException($this->getName(), sprintf('Unknown configuration option "%s". Expected any of "%s".', $name, implode('", "', self::$defaultConfiguration)));
             }
             }
         }
         }
 
 

+ 5 - 58
src/Fixer/ArrayNotation/AlignDoubleArrowFixer.php → src/Fixer/Operator/AlignDoubleArrowFixerHelper.php

@@ -10,9 +10,9 @@
  * with this source code in the file LICENSE.
  * with this source code in the file LICENSE.
  */
  */
 
 
-namespace PhpCsFixer\Fixer\ArrayNotation;
+namespace PhpCsFixer\Fixer\Operator;
 
 
-use PhpCsFixer\AbstractAlignFixer;
+use PhpCsFixer\AbstractAlignFixerHelper;
 use PhpCsFixer\Tokenizer\Tokens;
 use PhpCsFixer\Tokenizer\Tokens;
 
 
 /**
 /**
@@ -20,7 +20,7 @@ use PhpCsFixer\Tokenizer\Tokens;
  * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  * @author Graham Campbell <graham@alt-three.com>
  * @author Graham Campbell <graham@alt-three.com>
  */
  */
-final class AlignDoubleArrowFixer extends AbstractAlignFixer
+final class AlignDoubleArrowFixerHelper extends AbstractAlignFixerHelper
 {
 {
     /**
     /**
      * Level counter of the current nest level.
      * Level counter of the current nest level.
@@ -29,65 +29,12 @@ final class AlignDoubleArrowFixer extends AbstractAlignFixer
      *
      *
      * @var int
      * @var int
      */
      */
-    private $currentLevel;
+    private $currentLevel = 0;
 
 
     /**
     /**
      * {@inheritdoc}
      * {@inheritdoc}
      */
      */
-    public function isCandidate(Tokens $tokens)
-    {
-        return $tokens->isTokenKindFound(T_DOUBLE_ARROW);
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function fix(\SplFileInfo $file, Tokens $tokens)
-    {
-        $this->currentLevel = 0;
-        $this->deepestLevel = 0;
-
-        // This fixer works partially on Tokens and partially on string representation of code.
-        // During the process of fixing internal state of single Token may be affected by injecting ALIGNABLE_PLACEHOLDER to its content.
-        // The placeholder will be resolved by `replacePlaceholder` method by removing placeholder or changing it into spaces.
-        // That way of fixing the code causes disturbances in marking Token as changed - if code is perfectly valid then placeholder
-        // still be injected and removed, which will cause the `changed` flag to be set.
-        // To handle that unwanted behavior we work on clone of Tokens collection and then override original collection with fixed collection.
-        $tokensClone = clone $tokens;
-
-        $this->injectAlignmentPlaceholders($tokensClone, 0, count($tokens));
-        $content = $this->replacePlaceholder($tokensClone);
-
-        $tokens->setCode($content);
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function getDescription()
-    {
-        return 'Align double arrow symbols in consecutive lines.';
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function getPriority()
-    {
-        // should be run after the BinaryOperatorSpacesFixer
-        return -10;
-    }
-
-    /**
-     * Inject into the text placeholders of candidates of vertical alignment.
-     *
-     * @param Tokens $tokens
-     * @param int    $startAt
-     * @param int    $endAt
-     *
-     * @return array($code, $context_counter)
-     */
-    private function injectAlignmentPlaceholders(Tokens $tokens, $startAt, $endAt)
+    protected function injectAlignmentPlaceholders(Tokens $tokens, $startAt, $endAt)
     {
     {
         for ($index = $startAt; $index < $endAt; ++$index) {
         for ($index = $startAt; $index < $endAt; ++$index) {
             $token = $tokens[$index];
             $token = $tokens[$index];

+ 0 - 111
src/Fixer/Operator/AlignEqualsFixer.php

@@ -1,111 +0,0 @@
-<?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\Fixer\Operator;
-
-use PhpCsFixer\AbstractAlignFixer;
-use PhpCsFixer\Tokenizer\Tokens;
-
-/**
- * @author Carlos Cirello <carlos.cirello.nl@gmail.com>
- * @author Graham Campbell <graham@alt-three.com>
- */
-final class AlignEqualsFixer extends AbstractAlignFixer
-{
-    /**
-     * {@inheritdoc}
-     */
-    public function isCandidate(Tokens $tokens)
-    {
-        return $tokens->isTokenKindFound('=');
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function fix(\SplFileInfo $file, Tokens $tokens)
-    {
-        $this->deepestLevel = 0;
-
-        // This fixer works partially on Tokens and partially on string representation of code.
-        // During the process of fixing internal state of single Token may be affected by injecting ALIGNABLE_PLACEHOLDER to its content.
-        // The placeholder will be resolved by `replacePlaceholder` method by removing placeholder or changing it into spaces.
-        // That way of fixing the code causes disturbances in marking Token as changed - if code is perfectly valid then placeholder
-        // still be injected and removed, which will cause the `changed` flag to be set.
-        // To handle that unwanted behavior we work on clone of Tokens collection and then override original collection with fixed collection.
-        $tokensClone = clone $tokens;
-
-        $this->injectAlignmentPlaceholders($tokensClone);
-        $content = $this->replacePlaceholder($tokensClone);
-
-        $tokens->setCode($content);
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function getDescription()
-    {
-        return 'Align equals symbols in consecutive lines.';
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function getPriority()
-    {
-        // should be run after the BinaryOperatorSpacesFixer
-        return -10;
-    }
-
-    /**
-     * Inject into the text placeholders of candidates of vertical alignment.
-     *
-     * @param Tokens $tokens
-     */
-    private function injectAlignmentPlaceholders(Tokens $tokens)
-    {
-        $deepestLevel = 0;
-        $limit = $tokens->count();
-
-        for ($index = 0; $index < $limit; ++$index) {
-            $token = $tokens[$index];
-
-            if ($token->equals('=')) {
-                $token->setContent(sprintf(self::ALIGNABLE_PLACEHOLDER, $deepestLevel).$token->getContent());
-                continue;
-            }
-
-            if ($token->isGivenKind(T_FUNCTION)) {
-                ++$deepestLevel;
-                continue;
-            }
-
-            if ($token->equals('(')) {
-                $index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index);
-                continue;
-            }
-
-            if ($token->equals('[')) {
-                $index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_INDEX_SQUARE_BRACE, $index);
-                continue;
-            }
-
-            if ($token->isGivenKind(CT_ARRAY_SQUARE_BRACE_OPEN)) {
-                $index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $index);
-                continue;
-            }
-        }
-
-        $this->deepestLevel = $deepestLevel;
-    }
-}

+ 58 - 0
src/Fixer/Operator/AlignEqualsFixerHelper.php

@@ -0,0 +1,58 @@
+<?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\Fixer\Operator;
+
+use PhpCsFixer\AbstractAlignFixerHelper;
+use PhpCsFixer\Tokenizer\Tokens;
+
+/**
+ * @author Carlos Cirello <carlos.cirello.nl@gmail.com>
+ * @author Graham Campbell <graham@alt-three.com>
+ */
+final class AlignEqualsFixerHelper extends AbstractAlignFixerHelper
+{
+    /**
+     * {@inheritdoc}
+     */
+    protected function injectAlignmentPlaceholders(Tokens $tokens, $startAt, $endAt)
+    {
+        for ($index = $startAt; $index < $endAt; ++$index) {
+            $token = $tokens[$index];
+
+            if ($token->equals('=')) {
+                $token->setContent(sprintf(self::ALIGNABLE_PLACEHOLDER, $this->deepestLevel).$token->getContent());
+                continue;
+            }
+
+            if ($token->isGivenKind(T_FUNCTION)) {
+                ++$this->deepestLevel;
+                continue;
+            }
+
+            if ($token->equals('(')) {
+                $index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index);
+                continue;
+            }
+
+            if ($token->equals('[')) {
+                $index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_INDEX_SQUARE_BRACE, $index);
+                continue;
+            }
+
+            if ($token->isGivenKind(CT_ARRAY_SQUARE_BRACE_OPEN)) {
+                $index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $index);
+                continue;
+            }
+        }
+    }
+}

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