Browse Source

Fixer base

Dariusz Rumiński 10 years ago
parent
commit
7f0e2fd473

+ 1 - 1
README.rst

@@ -219,7 +219,7 @@ Choose from the list of available fixers:
 * **concat_without_spaces** [all] Concatenation should be used without
             spaces.
 
-* **controls_spaces** [all] A single space should be between:
+* **control_spaces** [all] A single space should be between:
             the closing brace and the control, the
             control and the opening parentheses, the
             closing parentheses and the opening brace.

+ 78 - 0
Symfony/CS/AbstractFixer.php

@@ -0,0 +1,78 @@
+<?php
+
+/*
+ * This file is part of the PHP CS utility.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+namespace Symfony\CS;
+
+/**
+ * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
+ */
+abstract class AbstractFixer implements FixerInterface
+{
+    protected static function camelCaseToUnderscore($string)
+    {
+        return preg_replace_callback(
+            '/(^|[a-z])([A-Z])/',
+            function (array $matches) {
+                return strtolower(strlen($matches[1]) ? $matches[1].'_'.$matches[2] : $matches[2]);
+            },
+            $string
+        );
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getLevel()
+    {
+        static $map = array(
+            'PSR0' => FixerInterface::PSR0_LEVEL,
+            'PSR1' => FixerInterface::PSR1_LEVEL,
+            'PSR2' => FixerInterface::PSR2_LEVEL,
+            'All' => FixerInterface::ALL_LEVEL,
+            'Contrib' => FixerInterface::CONTRIB_LEVEL,
+        );
+
+        $level = current(explode('\\', substr(get_called_class(), strlen(__NAMESPACE__.'\\Fixer\\'))));
+
+        if (!isset($map[$level])) {
+            throw new \LogicException("Can not determine Fixer level");
+        }
+
+        return $map[$level];
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getName()
+    {
+        $nameParts = explode('\\', get_called_class());
+        $name = substr(end($nameParts), 0, -strlen('Fixer'));
+
+        return self::camelCaseToUnderscore($name);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getPriority()
+    {
+        return 0;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function supports(\SplFileInfo $file)
+    {
+        return true;
+    }
+}

+ 2 - 34
Symfony/CS/Fixer/All/ConcatWithoutSpacesFixer.php

@@ -11,13 +11,13 @@
 
 namespace Symfony\CS\Fixer\All;
 
-use Symfony\CS\FixerInterface;
+use Symfony\CS\AbstractFixer;
 use Symfony\CS\Tokens;
 
 /**
  * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  */
-class ConcatWithoutSpacesFixer implements FixerInterface
+class ConcatWithoutSpacesFixer extends AbstractFixer
 {
     /**
      * {@inheritdoc}
@@ -37,38 +37,6 @@ class ConcatWithoutSpacesFixer implements FixerInterface
         return $tokens->generateCode();
     }
 
-    /**
-     * {@inheritdoc}
-     */
-    public function getLevel()
-    {
-        return FixerInterface::ALL_LEVEL;
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function getPriority()
-    {
-        return 0;
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function supports(\SplFileInfo $file)
-    {
-        return true;
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function getName()
-    {
-        return 'concat_without_spaces';
-    }
-
     /**
      * {@inheritdoc}
      */

+ 2 - 18
Symfony/CS/Fixer/All/ControlSpacesFixer.php

@@ -11,14 +11,14 @@
 
 namespace Symfony\CS\Fixer\All;
 
-use Symfony\CS\FixerInterface;
+use Symfony\CS\AbstractFixer;
 
 /**
  * @author Adrien Brault <adrien.brault@gmail.com>
  * @author Саша Стаменковић <umpirsky@gmail.com>
  * @author Niko Kivelä <niko@tovrleaf.com>
  */
-class ControlSpacesFixer implements FixerInterface
+class ControlSpacesFixer extends AbstractFixer
 {
     /**
      * {@inheritdoc}
@@ -35,14 +35,6 @@ class ControlSpacesFixer implements FixerInterface
         return $content;
     }
 
-    /**
-     * {@inheritdoc}
-     */
-    public function getLevel()
-    {
-        return FixerInterface::ALL_LEVEL;
-    }
-
     /**
      * {@inheritdoc}
      */
@@ -60,14 +52,6 @@ class ControlSpacesFixer implements FixerInterface
         return 'php' === pathinfo($file->getFilename(), PATHINFO_EXTENSION);
     }
 
-    /**
-     * {@inheritdoc}
-     */
-    public function getName()
-    {
-        return 'controls_spaces';
-    }
-
     /**
      * {@inheritdoc}
      */

+ 2 - 34
Symfony/CS/Fixer/All/ExtraEmptyLinesFixer.php

@@ -11,13 +11,13 @@
 
 namespace Symfony\CS\Fixer\All;
 
-use Symfony\CS\FixerInterface;
+use Symfony\CS\AbstractFixer;
 use Symfony\CS\Tokens;
 
 /**
  * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  */
-class ExtraEmptyLinesFixer implements FixerInterface
+class ExtraEmptyLinesFixer extends AbstractFixer
 {
     /**
      * {@inheritdoc}
@@ -55,38 +55,6 @@ class ExtraEmptyLinesFixer implements FixerInterface
         return $tokens->generateCode();
     }
 
-    /**
-     * {@inheritdoc}
-     */
-    public function getLevel()
-    {
-        return FixerInterface::ALL_LEVEL;
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function getPriority()
-    {
-        return 0;
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function supports(\SplFileInfo $file)
-    {
-        return true;
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function getName()
-    {
-        return 'extra_empty_lines';
-    }
-
     /**
      * {@inheritdoc}
      */

+ 2 - 34
Symfony/CS/Fixer/All/IncludeFixer.php

@@ -11,7 +11,7 @@
 
 namespace Symfony\CS\Fixer\All;
 
-use Symfony\CS\FixerInterface;
+use Symfony\CS\AbstractFixer;
 use Symfony\CS\Token;
 use Symfony\CS\Tokens;
 
@@ -19,7 +19,7 @@ use Symfony\CS\Tokens;
  * @author Sebastiaan Stok <s.stok@rollerscapes.net>
  * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  */
-class IncludeFixer implements FixerInterface
+class IncludeFixer extends AbstractFixer
 {
     /**
      * {@inheritdoc}
@@ -160,38 +160,6 @@ class IncludeFixer implements FixerInterface
         return $includies;
     }
 
-    /**
-     * {@inheritdoc}
-     */
-    public function getLevel()
-    {
-        return FixerInterface::ALL_LEVEL;
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function getPriority()
-    {
-        return 0;
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function supports(\SplFileInfo $file)
-    {
-        return true;
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function getName()
-    {
-        return 'include';
-    }
-
     /**
      * {@inheritdoc}
      */

+ 2 - 34
Symfony/CS/Fixer/All/MultilineArrayTrailingCommaFixer.php

@@ -11,14 +11,14 @@
 
 namespace Symfony\CS\Fixer\All;
 
-use Symfony\CS\FixerInterface;
+use Symfony\CS\AbstractFixer;
 use Symfony\CS\Token;
 use Symfony\CS\Tokens;
 
 /**
  * @author Sebastiaan Stok <s.stok@rollerscapes.net>
  */
-class MultilineArrayTrailingCommaFixer implements FixerInterface
+class MultilineArrayTrailingCommaFixer extends AbstractFixer
 {
     /**
      * {@inheritdoc}
@@ -36,38 +36,6 @@ class MultilineArrayTrailingCommaFixer implements FixerInterface
         return $tokens->generateCode();
     }
 
-    /**
-     * {@inheritdoc}
-     */
-    public function getLevel()
-    {
-        return FixerInterface::ALL_LEVEL;
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function getPriority()
-    {
-        return 0;
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function supports(\SplFileInfo $file)
-    {
-        return true;
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function getName()
-    {
-        return 'multiline_array_trailing_comma';
-    }
-
     /**
      * {@inheritdoc}
      */

+ 2 - 34
Symfony/CS/Fixer/All/NewWithBracesFixer.php

@@ -11,14 +11,14 @@
 
 namespace Symfony\CS\Fixer\All;
 
-use Symfony\CS\FixerInterface;
+use Symfony\CS\AbstractFixer;
 use Symfony\CS\Token;
 use Symfony\CS\Tokens;
 
 /**
  * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  */
-class NewWithBracesFixer implements FixerInterface
+class NewWithBracesFixer extends AbstractFixer
 {
     /**
      * {@inheritdoc}
@@ -68,38 +68,6 @@ class NewWithBracesFixer implements FixerInterface
         return $tokens->generateCode();
     }
 
-    /**
-     * {@inheritdoc}
-     */
-    public function getLevel()
-    {
-        return FixerInterface::ALL_LEVEL;
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function getPriority()
-    {
-        return 0;
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function supports(\SplFileInfo $file)
-    {
-        return true;
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function getName()
-    {
-        return 'new_with_braces';
-    }
-
     /**
      * {@inheritdoc}
      */

+ 2 - 34
Symfony/CS/Fixer/All/ObjectOperatorFixer.php

@@ -11,14 +11,14 @@
 
 namespace Symfony\CS\Fixer\All;
 
-use Symfony\CS\FixerInterface;
+use Symfony\CS\AbstractFixer;
 use Symfony\CS\Tokens;
 
 /**
  * @author Fabien Potencier <fabien@symfony.com>
  * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  */
-class ObjectOperatorFixer implements FixerInterface
+class ObjectOperatorFixer extends AbstractFixer
 {
     /**
      * {@inheritdoc}
@@ -48,38 +48,6 @@ class ObjectOperatorFixer implements FixerInterface
         return $tokens->generateCode();
     }
 
-    /**
-     * {@inheritdoc}
-     */
-    public function getLevel()
-    {
-        return FixerInterface::ALL_LEVEL;
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function getPriority()
-    {
-        return 0;
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function supports(\SplFileInfo $file)
-    {
-        return true;
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function getName()
-    {
-        return 'object_operator';
-    }
-
     /**
      * {@inheritdoc}
      */

+ 2 - 34
Symfony/CS/Fixer/All/OperatorsSpacesFixer.php

@@ -11,14 +11,14 @@
 
 namespace Symfony\CS\Fixer\All;
 
-use Symfony\CS\FixerInterface;
+use Symfony\CS\AbstractFixer;
 use Symfony\CS\Token;
 use Symfony\CS\Tokens;
 
 /**
  * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  */
-class OperatorsSpacesFixer implements FixerInterface
+class OperatorsSpacesFixer extends AbstractFixer
 {
     /**
      * {@inheritdoc}
@@ -83,38 +83,6 @@ class OperatorsSpacesFixer implements FixerInterface
         return $token->isArray() ? isset($arrayOperators[$token->id]) : isset($nonArrayOperators[$token->content]);
     }
 
-    /**
-     * {@inheritdoc}
-     */
-    public function getLevel()
-    {
-        return FixerInterface::ALL_LEVEL;
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function getPriority()
-    {
-        return 0;
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function supports(\SplFileInfo $file)
-    {
-        return true;
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function getName()
-    {
-        return 'operators_spaces';
-    }
-
     /**
      * {@inheritdoc}
      */

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