Browse Source

Convert PhpdocTrimAfterDescriptionFixer into PhpdocTrimConsecutiveBlankLineSeparationFixer

Dariusz Ruminski 6 years ago
parent
commit
8a4de863ee

+ 1 - 1
.php_cs.dist

@@ -66,7 +66,7 @@ $config = PhpCsFixer\Config::create()
         'php_unit_test_class_requires_covers' => true,
         'phpdoc_add_missing_param_annotation' => true,
         'phpdoc_order' => true,
-        'phpdoc_trim_after_description' => true,
+        'phpdoc_trim_consecutive_blank_line_separation' => true,
         'phpdoc_types_order' => true,
         'return_assignment' => true,
         'semicolon_after_instruction' => true,

+ 1 - 1
README.rst

@@ -1407,7 +1407,7 @@ Choose from the list of available rules:
   PHPDoc should start and end with content, excluding the very first and
   last line of the docblocks.
 
-* **phpdoc_trim_after_description**
+* **phpdoc_trim_consecutive_blank_line_separation**
 
   Removes extra blank lines after summary and after description in PHPDoc.
 

+ 1 - 1
src/DocBlock/DocBlock.php

@@ -173,7 +173,7 @@ class DocBlock
             }
 
             if (!$line->containsUsefulContent()) {
-                // if we next line is also non-useful, or contains a tag, then we're done here
+                // if next line is also non-useful, or contains a tag, then we're done here
                 $next = $this->getLine($index + 1);
                 if (null === $next || !$next->containsUsefulContent() || $next->containsATag()) {
                     break;

+ 31 - 9
src/Fixer/Phpdoc/PhpdocTrimAfterDescriptionFixer.php → src/Fixer/Phpdoc/PhpdocTrimConsecutiveBlankLineSeparationFixer.php

@@ -23,8 +23,9 @@ use PhpCsFixer\Tokenizer\Tokens;
 
 /**
  * @author Nobu Funaki <nobu.funaki@gmail.com>
+ * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  */
-final class PhpdocTrimAfterDescriptionFixer extends AbstractFixer
+final class PhpdocTrimConsecutiveBlankLineSeparationFixer extends AbstractFixer
 {
     /**
      * {@inheritdoc}
@@ -40,10 +41,16 @@ final class PhpdocTrimAfterDescriptionFixer extends AbstractFixer
  * Summary.
  *
  *
- * Description.
+ * Description that contain 4 lines,
+ *
+ *
+ * while 2 of them are blank!
  *
  *
  * @param string $foo
+ *
+ *
+ * @dataProvider provideFixCases
  */
 function fnc($foo) {}
 '
@@ -73,12 +80,12 @@ function fnc($foo) {}
             $doc = new DocBlock($token->getContent());
             $summaryEnd = (new ShortDescription($doc))->getEnd();
 
-            if (null === $summaryEnd) {
-                continue;
+            if (null !== $summaryEnd) {
+                $this->fixSummary($doc, $summaryEnd);
+                $this->fixDescription($doc, $summaryEnd);
             }
 
-            $this->fixSummary($doc, $summaryEnd);
-            $this->fixDescription($doc, $summaryEnd);
+            $this->fixAllTheRest($doc);
 
             $tokens[$index] = new Token([T_DOC_COMMENT, $doc->getContent()]);
         }
@@ -101,7 +108,8 @@ function fnc($foo) {}
      */
     private function fixDescription(DocBlock $doc, $summaryEnd)
     {
-        $annotationStart = $this->findFirstAnnotation($doc);
+        $annotationStart = $this->findFirstAnnotationOrEnd($doc);
+
         // assuming the end of the Description appears before the first Annotation
         $descriptionEnd = $this->reverseFindLastUsefulContent($doc, $annotationStart);
 
@@ -109,9 +117,23 @@ function fnc($foo) {}
             return; // no Description
         }
 
+        if ($annotationStart === count($doc->getLines()) - 1) {
+            return; // no content after Description
+        }
+
         $this->removeExtraBlankLinesBetween($doc, $descriptionEnd, $annotationStart);
     }
 
+    private function fixAllTheRest(DocBlock $doc)
+    {
+        $annotationStart = $this->findFirstAnnotationOrEnd($doc);
+        $lastLine = $this->reverseFindLastUsefulContent($doc, count($doc->getLines()) - 1);
+
+        if (null !== $lastLine && $annotationStart !== $lastLine) {
+            $this->removeExtraBlankLinesBetween($doc, $annotationStart, $lastLine);
+        }
+    }
+
     /**
      * @param DocBlock $doc
      * @param int      $from
@@ -160,9 +182,9 @@ function fnc($foo) {}
     /**
      * @param DocBlock $doc
      *
-     * @return null|int
+     * @return int
      */
-    private function findFirstAnnotation(DocBlock $doc)
+    private function findFirstAnnotationOrEnd(DocBlock $doc)
     {
         $index = null;
         foreach ($doc->getLines() as $index => $line) {

+ 2 - 0
tests/DocBlock/ShortDescriptionTest.php

@@ -65,6 +65,8 @@ final class ShortDescriptionTest extends TestCase
                   *
                   * There might be extra blank lines.
                   *
+                  *
+                  * And here is description...
                   */'],
         ];
     }

+ 0 - 1
tests/Fixer/ClassNotation/ClassAttributesSeparationFixerTest.php

@@ -684,7 +684,6 @@ public function B(); // allowed comment
      * @param string      $expected
      * @param null|string $input
      *
-     *
      * @dataProvider provideFixTraitsCases
      */
     public function testFixTraits($expected, $input = null)

+ 0 - 1
tests/Fixer/ClassNotation/MethodSeparationFixerTest.php

@@ -573,7 +573,6 @@ function test2() {
      * @param string      $expected
      * @param null|string $input
      *
-     *
      * @dataProvider provideFixTraitsCases
      */
     public function testFixTraits($expected, $input = null)

+ 0 - 1
tests/Fixer/ClassNotation/NoBlankLinesAfterClassOpeningFixerTest.php

@@ -39,7 +39,6 @@ final class NoBlankLinesAfterClassOpeningFixerTest extends AbstractFixerTestCase
      * @param string      $expected
      * @param null|string $input
      *
-     *
      * @dataProvider provideTraitsCases
      */
     public function testFixTraits($expected, $input = null)

+ 0 - 1
tests/Fixer/FunctionNotation/MethodArgumentSpaceFixerTest.php

@@ -700,7 +700,6 @@ INPUT
      * @param string $expected
      * @param string $input
      *
-     *
      * @dataProvider provideFix56Cases
      */
     public function testFix56($expected, $input)

+ 0 - 1
tests/Fixer/Phpdoc/PhpdocAlignFixerTest.php

@@ -1130,7 +1130,6 @@ EOF;
      * @param string $expected
      * @param string $input
      *
-     *
      * @dataProvider provideVariadicCases
      */
     public function testVariadicParams(array $config, $expected, $input)

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