Browse Source

Merge PHPDoc tag replace fixers in to generic one.

SpacePossum 8 years ago
parent
commit
7a3632fae4

+ 4 - 13
README.rst

@@ -255,10 +255,6 @@ Choose from the list of available rules:
    | Configured annotations should be omitted from phpdocs.
    | *Rule is: configurable.*
 
-* **general_phpdoc_annotation_rename**
-   | Configured annotations inside phpdocs should be renamed.
-   | *Rule is: configurable.*
-
 * **hash_to_slash_comment** [@Symfony]
    | Single line comments should use double slashes (//) and not hash (#).
 
@@ -469,6 +465,10 @@ Choose from the list of available rules:
 * **phpdoc_no_access** [@Symfony]
    | @access annotations should be omitted from phpdocs.
 
+* **phpdoc_no_alias_tag** [@Symfony]
+   | No alias PHPDoc tags should be used.
+   | *Rule is: configurable.*
+
 * **phpdoc_no_empty_return** [@Symfony]
    | @return void and @return null annotations should be omitted from
    | phpdocs.
@@ -480,9 +480,6 @@ Choose from the list of available rules:
    | Annotations in phpdocs should be ordered so that param annotations come
    | first, then throws annotations, then return annotations.
 
-* **phpdoc_property**
-   | @property tags should be used rather than other variants.
-
 * **phpdoc_scalar** [@Symfony]
    | Scalar types should always be written in the same form. "int", not
    | "integer"; "bool", not "boolean"; "float", not "real" or "double".
@@ -506,15 +503,9 @@ Choose from the list of available rules:
    | Phpdocs should start and end with content, excluding the very first and
    | last line of the docblocks.
 
-* **phpdoc_type_to_var** [@Symfony]
-   | @type should always be written as @var.
-
 * **phpdoc_types** [@Symfony]
    | The correct case must be used for standard PHP types in phpdoc.
 
-* **phpdoc_var_to_type**
-   | @var should always be written as @type.
-
 * **phpdoc_var_without_name** [@Symfony]
    | @var and @type annotations should not contain the variable name.
 

+ 4 - 0
UPGRADE.md

@@ -89,6 +89,7 @@ empty_return                                   | simplified_null_return
 eof_ending                                     | single_blank_line_at_eof
 extra_empty_lines                              | no_extra_consecutive_blank_lines
 function_call_space                            | no_spaces_after_function_name
+general_phpdoc_annotation_rename               | phpdoc_no_alias_tag                               | use configuration option [property-read' => 'property', 'property-write' => 'property']
 indentation                                    | no_tab_indentation
 join_function                                  | no_alias_functions                                | new one fixes more aliases
 line_after_namespace                           | blank_line_after_namespace
@@ -112,7 +113,10 @@ parenthesis                                    | no_spaces_inside_parenthesis
 php4_constructor                               | no_php4_constructor
 php_closing_tag                                | no_closing_tag
 phpdoc_params                                  | phpdoc_align
+phpdoc_property                                | phpdoc_no_alias_tag                               | use configuration option ['type' => 'var']
 phpdoc_short_description                       | phpdoc_summary
+phpdoc_type_to_var                             | phpdoc_no_alias_tag                               | use configuration option ['type' => 'var']
+phpdoc_var_to_type                             | phpdoc_no_alias_tag                               | use configuration option ['var' => 'type']
 remove_leading_slash_use                       | no_leading_import_slash
 remove_lines_between_uses                      | no_extra_consecutive_blank_lines                  | use configuration option 'use'
 return                                         | blank_line_before_return

+ 0 - 90
src/Fixer/Phpdoc/GeneralPhpdocAnnotationRenameFixer.php

@@ -1,90 +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\Phpdoc;
-
-use PhpCsFixer\AbstractFixer;
-use PhpCsFixer\DocBlock\DocBlock;
-use PhpCsFixer\Tokenizer\Tokens;
-
-/**
- * @author Graham Campbell <graham@alt-three.com>
- * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
- */
-final class GeneralPhpdocAnnotationRenameFixer extends AbstractFixer
-{
-    /**
-     * @var array
-     */
-    protected $configuration;
-
-    /**
-     * {@inheritdoc}
-     */
-    public function configure(array $configuration = null)
-    {
-        if (null === $configuration) {
-            $this->configuration = array();
-
-            return;
-        }
-
-        $this->configuration = $configuration;
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function isCandidate(Tokens $tokens)
-    {
-        return $tokens->isTokenKindFound(T_DOC_COMMENT);
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function fix(\SplFileInfo $file, Tokens $tokens)
-    {
-        if (!count($this->configuration)) {
-            return;
-        }
-
-        $searchFor = array_keys($this->configuration);
-
-        foreach ($tokens as $token) {
-            if (!$token->isGivenKind(T_DOC_COMMENT)) {
-                continue;
-            }
-
-            $doc = new DocBlock($token->getContent());
-            $annotations = $doc->getAnnotationsOfType($searchFor);
-
-            if (empty($annotations)) {
-                continue;
-            }
-
-            foreach ($annotations as $annotation) {
-                $annotation->getTag()->setName($this->configuration[$annotation->getTag()->getName()]);
-            }
-
-            $token->setContent($doc->getContent());
-        }
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function getDescription()
-    {
-        return 'Configured annotations inside phpdocs should be renamed.';
-    }
-}

+ 120 - 0
src/Fixer/Phpdoc/PhpdocNoAliasTagFixer.php

@@ -0,0 +1,120 @@
+<?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\Phpdoc;
+
+use PhpCsFixer\AbstractFixer;
+use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
+use PhpCsFixer\DocBlock\DocBlock;
+use PhpCsFixer\Tokenizer\Tokens;
+
+/**
+ * Case sensitive tag replace fixer (does not process inline tags like {@inheritdoc}).
+ *
+ * @author Graham Campbell <graham@alt-three.com>
+ * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
+ * @author SpacePossum
+ */
+final class PhpdocNoAliasTagFixer extends AbstractFixer
+{
+    /**
+     * @var array<string, string>
+     */
+    private $configuration;
+
+    private static $defaultConfiguration = array(
+        'property-read' => 'property',
+        'property-write' => 'property',
+        'type' => 'var',
+        'link' => 'see',
+    );
+
+    /**
+     * Key value pairs of string, replace from -> to tags (without '@').
+     *
+     * @param string[]|null $configuration
+     */
+    public function configure(array $configuration = null)
+    {
+        if (null === $configuration) {
+            $this->configuration = self::$defaultConfiguration;
+
+            return;
+        }
+
+        $this->configuration = array();
+        foreach ($configuration as $from => $to) {
+            if (!is_string($from)) {
+                throw new InvalidFixerConfigurationException($this->getName(), 'Tag to replace must be a string.');
+            }
+
+            if (!is_string($to)) {
+                throw new InvalidFixerConfigurationException($this->getName(), sprintf('Tag to replace to from "%s" must be a string.', $from));
+            }
+
+            if (1 !== preg_match('#^[^\s]+$#', $to) || false !== strpos($to, '*/')) {
+                throw new InvalidFixerConfigurationException($this->getName(), sprintf('Tag "%s" cannot be replaced by invalid tag "%s".', $from, $to));
+            }
+
+            $this->configuration[trim($from)] = trim($to);
+            if (isset($this->configuration[$to])) {
+                throw new InvalidFixerConfigurationException(
+                    $this->getName(),
+                    sprintf('Cannot change tag "%s" to tag "%s", as the tag is set configured to be replaced to "%s".', $from, $to, $this->configuration[$to])
+                );
+            }
+        }
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function isCandidate(Tokens $tokens)
+    {
+        return $tokens->isTokenKindFound(T_DOC_COMMENT);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function fix(\SplFileInfo $file, Tokens $tokens)
+    {
+        $searchFor = array_keys($this->configuration);
+
+        foreach ($tokens as $token) {
+            if (!$token->isGivenKind(T_DOC_COMMENT)) {
+                continue;
+            }
+
+            $doc = new DocBlock($token->getContent());
+            $annotations = $doc->getAnnotationsOfType($searchFor);
+
+            if (empty($annotations)) {
+                continue;
+            }
+
+            foreach ($annotations as $annotation) {
+                $annotation->getTag()->setName($this->configuration[$annotation->getTag()->getName()]);
+            }
+
+            $token->setContent($doc->getContent());
+        }
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getDescription()
+    {
+        return 'No alias PHPDoc tags should be used.';
+    }
+}

+ 0 - 44
src/Fixer/Phpdoc/PhpdocPropertyFixer.php

@@ -1,44 +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\Phpdoc;
-
-use PhpCsFixer\AbstractProxyFixer;
-
-/**
- * @author Graham Campbell <graham@alt-three.com>
- * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
- */
-final class PhpdocPropertyFixer extends AbstractProxyFixer
-{
-    /**
-     * {@inheritdoc}
-     */
-    public function getDescription()
-    {
-        return '@property tags should be used rather than other variants.';
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    protected function createProxyFixer()
-    {
-        $fixer = new GeneralPhpdocAnnotationRenameFixer();
-        $fixer->configure(array(
-            'property-read' => 'property',
-            'property-write' => 'property',
-        ));
-
-        return $fixer;
-    }
-}

+ 1 - 1
src/Fixer/Phpdoc/PhpdocSingleLineVarSpacingFixer.php

@@ -68,7 +68,7 @@ final class PhpdocSingleLineVarSpacingFixer extends AbstractFixer
      */
     public function getPriority()
     {
-        // should be ran after the PhpdocTypeToVarFixer.
+        // should be ran after PhpdocNoAliasTagFixer.
         return -10;
     }
 

+ 0 - 43
src/Fixer/Phpdoc/PhpdocTypeToVarFixer.php

@@ -1,43 +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\Phpdoc;
-
-use PhpCsFixer\AbstractProxyFixer;
-
-/**
- * @author Graham Campbell <graham@alt-three.com>
- * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
- */
-final class PhpdocTypeToVarFixer extends AbstractProxyFixer
-{
-    /**
-     * {@inheritdoc}
-     */
-    public function getDescription()
-    {
-        return '@type should always be written as @var.';
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    protected function createProxyFixer()
-    {
-        $fixer = new GeneralPhpdocAnnotationRenameFixer();
-        $fixer->configure(array(
-            'type' => 'var',
-        ));
-
-        return $fixer;
-    }
-}

+ 0 - 43
src/Fixer/Phpdoc/PhpdocVarToTypeFixer.php

@@ -1,43 +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\Phpdoc;
-
-use PhpCsFixer\AbstractProxyFixer;
-
-/**
- * @author Graham Campbell <graham@alt-three.com>
- * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
- */
-final class PhpdocVarToTypeFixer extends AbstractProxyFixer
-{
-    /**
-     * {@inheritdoc}
-     */
-    public function getDescription()
-    {
-        return '@var should always be written as @type.';
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    protected function createProxyFixer()
-    {
-        $fixer = new GeneralPhpdocAnnotationRenameFixer();
-        $fixer->configure(array(
-            'var' => 'type',
-        ));
-
-        return $fixer;
-    }
-}

+ 0 - 1
src/FixerFactory.php

@@ -234,7 +234,6 @@ final class FixerFactory
             'concat_with_spaces' => array('concat_without_spaces'),
             'echo_to_print' => array('print_to_echo'),
             'no_blank_lines_before_namespace' => array('single_blank_line_before_namespace'),
-            'phpdoc_type_to_var' => array('phpdoc_var_to_type'),
         );
 
         $fixerName = $fixer->getName();

+ 1 - 1
src/RuleSet.php

@@ -103,7 +103,7 @@ final class RuleSet implements RuleSetInterface
             'phpdoc_summary' => true,
             'phpdoc_to_comment' => true,
             'phpdoc_trim' => true,
-            'phpdoc_type_to_var' => true,
+            'phpdoc_no_alias_tag' => array('type' => 'var'),
             'phpdoc_types' => true,
             'phpdoc_var_without_name' => true,
             'php_unit_fqcn_annotation' => true,

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