Browse Source

Add MagicConstantCasingFixer

Nat Zimmermann 8 years ago
parent
commit
ec30083c58

+ 4 - 0
README.rst

@@ -508,6 +508,10 @@ Choose from the list of available rules:
 
   PHP keywords MUST be in lower case.
 
+* **magic_constant_casing** [@Symfony]
+
+  Magic constants should be referred to using the correct casing.
+
 * **mb_str_functions**
 
   Replace non multibyte-safe functions with corresponding mb function.

+ 1 - 0
composer.json

@@ -25,6 +25,7 @@
         "symfony/options-resolver": "^2.6 || ^3.0",
         "symfony/polyfill-php54": "^1.0",
         "symfony/polyfill-php55": "^1.3",
+        "symfony/polyfill-php70": "^1.0",
         "symfony/polyfill-xml": "^1.3",
         "symfony/process": "^2.3 || ^3.0",
         "symfony/stopwatch": "^2.5 || ^3.0"

+ 9 - 19
doc/COOKBOOK-FIXERS.md

@@ -58,7 +58,7 @@ Put this content inside:
  * with this source code in the file LICENSE.
  */
 
-namespace PhpCsFixer\Fixer\Contrib;
+namespace PhpCsFixer\Fixer\Comment;
 
 use PhpCsFixer\AbstractFixer;
 use PhpCsFixer\Tokenizer\Tokens;
@@ -71,7 +71,7 @@ final class RemoveCommentsFixer extends AbstractFixer
     /**
      * {@inheritdoc}
      */
-    public function fix(\SplFileInfo $file, $content)
+    protected function applyFix(\SplFileInfo $file, Tokens $tokens)
     {
         // Add the fixing logic of the fixer here.
     }
@@ -107,7 +107,7 @@ content inside:
  * with this source code in the file LICENSE.
  */
 
-namespace PhpCsFixer\Tests\Fixer\Contrib;
+namespace PhpCsFixer\Tests\Fixer\Comment;
 
 use PhpCsFixer\Test\AbstractFixerTestCase;
 
@@ -192,7 +192,7 @@ like:
  * with this source code in the file LICENSE.
  */
 
-namespace PhpCsFixer\Tests\Fixer\Contrib;
+namespace PhpCsFixer\Tests\Fixer\Comment;
 
 use PhpCsFixer\Tests\Fixer\AbstractFixerTestBase;
 
@@ -268,7 +268,7 @@ class RemoveCommentsFixer extends AbstractFixer
     /**
      * {@inheritdoc}
      */
-    public function fix(\SplFileInfo $file, Tokens $tokens)
+    protected function applyFix(\SplFileInfo $file, Tokens $tokens)
     {
         // no action
     }
@@ -318,7 +318,7 @@ final class RemoveCommentsFixer extends AbstractFixer
     /**
      * {@inheritdoc}
      */
-    public function fix(\SplFileInfo $file, Tokens $tokens)
+    protected function applyFix(\SplFileInfo $file, Tokens $tokens)
     {
         foreach($tokens as $index => $token){
             if (!$token->isGivenKind(T_COMMENT)) {
@@ -341,7 +341,7 @@ final class RemoveCommentsFixer extends AbstractFixer
     /**
      * {@inheritdoc}
      */
-    public function fix(\SplFileInfo $file, Tokens $tokens)
+    protected function applyFix(\SplFileInfo $file, Tokens $tokens)
     {
         foreach($tokens as $index => $token){
             if (!$token->isGivenKind(T_COMMENT)) {
@@ -374,7 +374,7 @@ So the fixer in the end looks like this:
  *
  */
 
-namespace PhpCsFixer\Fixer\Contrib;
+namespace PhpCsFixer\Fixer\Comment;
 
 use PhpCsFixer\AbstractFixer;
 use PhpCsFixer\Tokenizer\Tokens;
@@ -386,7 +386,7 @@ final class RemoveCommentsFixer extends AbstractFixer {
     /**
      * {@inheritdoc}
      */
-    public function fix(\SplFileInfo $file, Tokens $tokens) {
+    protected function applyFix(\SplFileInfo $file, Tokens $tokens) {
         foreach($tokens as $index => $token){
             if (!$token->isGivenKind(T_COMMENT)) {
                 continue;
@@ -482,16 +482,6 @@ part of the application as soon as possible.
 No. Short arrays were introduced in PHP 5.4 and PHP CS Fixer still
 supports PHP 5.3.6.
 
-#### Why are you steering me to create my fixer at CONTRIB_LEVEL ?
-
-CONTRIB_LEVEL is the most lax level - and it is far more likely to have
-your fixer accepted at CONTRIB_LEVEL and later changed to SYMFOMY_LEVEL
-or PSR2_LEVEL; than the other way around.
-
-If you make your contribution directly at PSR2_LEVEL, eventually the
-relevance debate will take place and your fixer might be pushed to
-CONTRIB_LEVEL.
-
 #### Why am I asked to use `getPrevMeaningfulToken()` instead of `getPrevNonWhitespace()`?
 
 The main difference is that `getPrevNonWhitespace()` ignores only

+ 9 - 9
src/AbstractDoctrineAnnotationFixer.php

@@ -30,7 +30,15 @@ abstract class AbstractDoctrineAnnotationFixer extends AbstractFixer implements
     /**
      * {@inheritdoc}
      */
-    public function fix(\SplFileInfo $file, PhpTokens $phpTokens)
+    public function isCandidate(PhpTokens $tokens)
+    {
+        return $tokens->isTokenKindFound(T_DOC_COMMENT);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function applyFix(\SplFileInfo $file, PhpTokens $phpTokens)
     {
         /** @var PhpToken $docCommentToken */
         foreach ($phpTokens->findGivenKind(T_DOC_COMMENT) as $index => $docCommentToken) {
@@ -47,14 +55,6 @@ abstract class AbstractDoctrineAnnotationFixer extends AbstractFixer implements
         }
     }
 
-    /**
-     * {@inheritdoc}
-     */
-    public function isCandidate(PhpTokens $tokens)
-    {
-        return $tokens->isTokenKindFound(T_DOC_COMMENT);
-    }
-
     /**
      * Fixes Doctrine annotations from the given PHPDoc style comment.
      *

+ 10 - 0
src/AbstractFixer.php

@@ -20,6 +20,7 @@ use PhpCsFixer\Fixer\DefinedFixerInterface;
 use PhpCsFixer\Fixer\FixerInterface;
 use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
 use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
+use PhpCsFixer\Tokenizer\Tokens;
 use Symfony\Component\OptionsResolver\Exception\ExceptionInterface;
 use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
 
@@ -60,6 +61,13 @@ abstract class AbstractFixer implements FixerInterface, DefinedFixerInterface
         }
     }
 
+    public function fix(\SplFileInfo $file, Tokens $tokens)
+    {
+        if (0 < $tokens->count() && $this->isCandidate($tokens) && $this->supports($file)) {
+            $this->applyFix($file, $tokens);
+        }
+    }
+
     /**
      * {@inheritdoc}
      */
@@ -154,6 +162,8 @@ abstract class AbstractFixer implements FixerInterface, DefinedFixerInterface
         $this->whitespacesConfig = $config;
     }
 
+    abstract protected function applyFix(\SplFileInfo $file, Tokens $tokens);
+
     /**
      * @return FixerConfigurationResolverInterface
      */

+ 1 - 1
src/AbstractPhpdocTypesFixer.php

@@ -53,7 +53,7 @@ abstract class AbstractPhpdocTypesFixer extends AbstractFixer
     /**
      * {@inheritdoc}
      */
-    public function fix(\SplFileInfo $file, Tokens $tokens)
+    protected function applyFix(\SplFileInfo $file, Tokens $tokens)
     {
         foreach ($tokens as $token) {
             if (!$token->isGivenKind(T_DOC_COMMENT)) {

+ 1 - 1
src/AbstractProxyFixer.php

@@ -53,7 +53,7 @@ abstract class AbstractProxyFixer extends AbstractFixer
     /**
      * {@inheritdoc}
      */
-    public function fix(\SplFileInfo $file, Tokens $tokens)
+    protected function applyFix(\SplFileInfo $file, Tokens $tokens)
     {
         $this->proxyFixer->fix($file, $tokens);
     }

+ 21 - 9
src/Console/Command/DescribeCommand.php

@@ -45,11 +45,31 @@ final class DescribeCommand extends Command
      */
     private $setNames;
 
+    /**
+     * @var FixerFactory
+     */
+    private $fixerFactory;
+
     /**
      * @var array<string, FixerInterface>
      */
     private $fixers;
 
+    /**
+     * @param FixerFactory|null $fixerFactory
+     */
+    public function __construct(FixerFactory $fixerFactory = null)
+    {
+        parent::__construct();
+
+        if (null === $fixerFactory) {
+            $fixerFactory = new FixerFactory();
+            $fixerFactory->registerBuiltInFixers();
+        }
+
+        $this->fixerFactory = $fixerFactory;
+    }
+
     /**
      * {@inheritdoc}
      */
@@ -223,12 +243,6 @@ final class DescribeCommand extends Command
                 $output->writeln('');
             }
         }
-
-        if (!($fixer instanceof DefinedFixerInterface)) {
-            $output->writeln(sprintf('<question>This rule is not yet described, do you want to help us and describe it?</question>'));
-            $output->writeln('Contribute at <comment>https://github.com/FriendsOfPHP/PHP-CS-Fixer</comment> !');
-            $output->writeln('');
-        }
     }
 
     /**
@@ -276,10 +290,8 @@ final class DescribeCommand extends Command
             return $this->fixers;
         }
 
-        $fixerFactory = new FixerFactory();
         $fixers = array();
-
-        foreach ($fixerFactory->registerBuiltInFixers()->getFixers() as $fixer) {
+        foreach ($this->fixerFactory->getFixers() as $fixer) {
             $fixers[$fixer->getName()] = $fixer;
         }
 

+ 1 - 1
src/Doctrine/Annotation/Token.php

@@ -19,7 +19,7 @@ use Doctrine\Common\Annotations\DocLexer;
  *
  * @internal
  */
-class Token
+final class Token
 {
     /**
      * @var int

+ 1 - 1
src/Doctrine/Annotation/Tokens.php

@@ -20,7 +20,7 @@ use PhpCsFixer\Tokenizer\Token as PhpToken;
  *
  * @internal
  */
-class Tokens extends \SplFixedArray
+final class Tokens extends \SplFixedArray
 {
     /**
      * @param PhpToken $input

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