Просмотр исходного кода

feature: general_phpdoc_annotation_remove - allow add case_sensitive option (#6660)

Dariusz Rumiński 2 лет назад
Родитель
Сommit
2ef2025800

+ 4 - 0
doc/list.rst

@@ -848,6 +848,10 @@ List of Available Rules
      | List of annotations to remove, e.g. `["author"]`.
      | Allowed types: ``array``
      | Default value: ``[]``
+   - | ``case_sensitive``
+     | Should annotations be case sensitive.
+     | Allowed types: ``bool``
+     | Default value: ``true``
 
 
    `Source PhpCsFixer\\Fixer\\Phpdoc\\GeneralPhpdocAnnotationRemoveFixer <./../src/Fixer/Phpdoc/GeneralPhpdocAnnotationRemoveFixer.php>`_

+ 27 - 0
doc/rules/phpdoc/general_phpdoc_annotation_remove.rst

@@ -16,6 +16,15 @@ Allowed types: ``array``
 
 Default value: ``[]``
 
+``case_sensitive``
+~~~~~~~~~~~~~~~~~~
+
+Should annotations be case sensitive.
+
+Allowed types: ``bool``
+
+Default value: ``true``
+
 Examples
 --------
 
@@ -32,12 +41,30 @@ With configuration: ``['annotations' => ['author']]``.
     /**
      * @internal
    - * @author John Doe
+     * @AuThOr Jane Doe
      */
     function foo() {}
 
 Example #2
 ~~~~~~~~~~
 
+With configuration: ``['annotations' => ['author'], 'case_sensitive' => false]``.
+
+.. code-block:: diff
+
+   --- Original
+   +++ New
+    <?php
+    /**
+     * @internal
+   - * @author John Doe
+   - * @AuThOr Jane Doe
+     */
+    function foo() {}
+
+Example #3
+~~~~~~~~~~
+
 With configuration: ``['annotations' => ['package', 'subpackage']]``.
 
 .. code-block:: diff

+ 5 - 6
src/DocBlock/DocBlock.php

@@ -185,15 +185,14 @@ final class DocBlock
      */
     public function getAnnotationsOfType($types): array
     {
+        $typesToSearchFor = (array) $types;
+
         $annotations = [];
-        $types = (array) $types;
 
         foreach ($this->getAnnotations() as $annotation) {
-            $tag = $annotation->getTag()->getName();
-            foreach ($types as $type) {
-                if ($type === $tag) {
-                    $annotations[] = $annotation;
-                }
+            $tagName = $annotation->getTag()->getName();
+            if (\in_array($tagName, $typesToSearchFor, true)) {
+                $annotations[] = $annotation;
             }
         }
 

+ 43 - 1
src/Fixer/Phpdoc/GeneralPhpdocAnnotationRemoveFixer.php

@@ -15,6 +15,7 @@ declare(strict_types=1);
 namespace PhpCsFixer\Fixer\Phpdoc;
 
 use PhpCsFixer\AbstractFixer;
+use PhpCsFixer\DocBlock\Annotation;
 use PhpCsFixer\DocBlock\DocBlock;
 use PhpCsFixer\Fixer\ConfigurableFixerInterface;
 use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
@@ -45,6 +46,7 @@ final class GeneralPhpdocAnnotationRemoveFixer extends AbstractFixer implements
 /**
  * @internal
  * @author John Doe
+ * @AuThOr Jane Doe
  */
 function foo() {}
 ',
@@ -52,6 +54,17 @@ function foo() {}
                 ),
                 new CodeSample(
                     '<?php
+/**
+ * @internal
+ * @author John Doe
+ * @AuThOr Jane Doe
+ */
+function foo() {}
+',
+                    ['annotations' => ['author'], 'case_sensitive' => false]
+                ),
+                new CodeSample(
+                    '<?php
 /**
  * @author John Doe
  * @package ACME API
@@ -100,7 +113,7 @@ function foo() {}
             }
 
             $doc = new DocBlock($token->getContent());
-            $annotations = $doc->getAnnotationsOfType($this->configuration['annotations']);
+            $annotations = $this->getAnnotationsToRemove($doc);
 
             // nothing to do if there are no annotations
             if (0 === \count($annotations)) {
@@ -129,6 +142,35 @@ function foo() {}
                 ->setAllowedTypes(['array'])
                 ->setDefault([])
                 ->getOption(),
+            (new FixerOptionBuilder('case_sensitive', 'Should annotations be case sensitive.'))
+                ->setAllowedTypes(['bool'])
+                ->setDefault(true)
+                ->getOption(),
         ]);
     }
+
+    /**
+     * @return list<Annotation>
+     */
+    private function getAnnotationsToRemove(DocBlock $doc): array
+    {
+        if (true === $this->configuration['case_sensitive']) {
+            return $doc->getAnnotationsOfType($this->configuration['annotations']);
+        }
+
+        $typesToSearchFor = array_map(function (string $type): string {
+            return strtolower($type);
+        }, $this->configuration['annotations']);
+
+        $annotations = [];
+
+        foreach ($doc->getAnnotations() as $annotation) {
+            $tagName = strtolower($annotation->getTag()->getName());
+            if (\in_array($tagName, $typesToSearchFor, true)) {
+                $annotations[] = $annotation;
+            }
+        }
+
+        return $annotations;
+    }
 }

+ 32 - 0
tests/Fixer/Phpdoc/GeneralPhpdocAnnotationRemoveFixerTest.php

@@ -181,6 +181,38 @@ while ($something = myFunction($foo)) {}
 ',
                 ['annotations' => ['noinspection']],
             ],
+
+            [
+                '<?php
+/**
+* @internal
+* @AuThOr Jane Doe
+*/
+function foo() {}',
+                '<?php
+/**
+* @internal
+* @author John Doe
+* @AuThOr Jane Doe
+*/
+function foo() {}',
+                ['annotations' => ['author'], 'case_sensitive' => true],
+            ],
+            [
+                '<?php
+/**
+* @internal
+*/
+function foo() {}',
+                '<?php
+/**
+* @internal
+* @author John Doe
+* @AuThOr Jane Doe
+*/
+function foo() {}',
+                ['annotations' => ['author'], 'case_sensitive' => false],
+            ],
         ];
     }
 }