Browse Source

PhpdocAlignFixer - Fix linebreak inconsistency

SpacePossum 7 years ago
parent
commit
10aaafbf93
2 changed files with 28 additions and 12 deletions
  1. 11 8
      src/Fixer/Phpdoc/PhpdocAlignFixer.php
  2. 17 4
      tests/Fixer/Phpdoc/PhpdocAlignFixerTest.php

+ 11 - 8
src/Fixer/Phpdoc/PhpdocAlignFixer.php

@@ -46,7 +46,7 @@ final class PhpdocAlignFixer extends AbstractFixer implements WhitespacesAwareFi
         $desc = '(?:\s+(?P<desc>\V*))';
 
         $this->regex = '/^'.$indent.' \* @(?:'.$paramTag.'|'.$otherTags.')'.$desc.'\s*$/u';
-        $this->regexCommentLine = '/^'.$indent.' \*(?! @)(?:\s+(?P<desc>\V+))(?<!\*\/)$/u';
+        $this->regexCommentLine = '/^'.$indent.' \*(?! @)(?:\s+(?P<desc>\V+))(?<!\*\/)\r?$/u';
     }
 
     /**
@@ -97,8 +97,14 @@ final class PhpdocAlignFixer extends AbstractFixer implements WhitespacesAwareFi
     protected function applyFix(\SplFileInfo $file, Tokens $tokens)
     {
         foreach ($tokens as $index => $token) {
-            if ($token->isGivenKind(T_DOC_COMMENT)) {
-                $tokens[$index] = new Token(array(T_DOC_COMMENT, $this->fixDocBlock($token->getContent())));
+            if (!$token->isGivenKind(T_DOC_COMMENT)) {
+                continue;
+            }
+
+            $content = $token->getContent();
+            $newContent = $this->fixDocBlock($content);
+            if ($newContent !== $content) {
+                $tokens[$index] = new Token(array(T_DOC_COMMENT, $newContent));
             }
         }
     }
@@ -113,9 +119,7 @@ final class PhpdocAlignFixer extends AbstractFixer implements WhitespacesAwareFi
         $lineEnding = $this->whitespacesConfig->getLineEnding();
         $lines = Utils::splitLines($content);
 
-        $l = count($lines);
-
-        for ($i = 0; $i < $l; ++$i) {
+        for ($i = 0, $l = count($lines); $i < $l; ++$i) {
             $items = array();
             $matches = $this->getMatches($lines[$i]);
 
@@ -132,8 +136,7 @@ final class PhpdocAlignFixer extends AbstractFixer implements WhitespacesAwareFi
                 }
 
                 $matches = $this->getMatches($lines[$i], true);
-
-                if (!$matches) {
+                if (null === $matches) {
                     break;
                 }
 

+ 17 - 4
tests/Fixer/Phpdoc/PhpdocAlignFixerTest.php

@@ -446,14 +446,15 @@ EOF;
     }
 
     /**
-     * @param string      $expected
-     * @param null|string $input
+     * @param string                 $expected
+     * @param string                 $input
+     * @param WhitespacesFixerConfig $config
      *
      * @dataProvider provideMessyWhitespacesCases
      */
-    public function testMessyWhitespaces($expected, $input = null)
+    public function testMessyWhitespaces($expected, $input, WhitespacesFixerConfig $config)
     {
-        $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
+        $this->fixer->setWhitespacesConfig($config);
 
         $this->doTest($expected, $input);
     }
@@ -464,10 +465,22 @@ EOF;
             array(
                 "<?php\r\n\t/**\r\n\t * @type Type This is a variable.\r\n\t */",
                 "<?php\r\n\t/**\r\n\t * @type   Type   This is a variable.\r\n\t */",
+                new WhitespacesFixerConfig("\t", "\r\n"),
             ),
             array(
                 "<?php\r\n/**\r\n * @param int    \$limit\r\n * @param string \$more\r\n *\r\n * @return array\r\n */",
                 "<?php\r\n/**\r\n * @param   int       \$limit\r\n * @param   string       \$more\r\n *\r\n * @return array\r\n */",
+                new WhitespacesFixerConfig("\t", "\r\n"),
+            ),
+            array(
+                "<?php\n/**\n * @param int \$a\n * @param int \$b\n *               ABC\n */",
+                "<?php\n/**\n * @param    int \$a\n * @param    int   \$b\n * ABC\n */",
+                new WhitespacesFixerConfig('    ', "\n"),
+            ),
+            array(
+                "<?php\r\n/**\r\n * @param int \$z\r\n * @param int \$b\r\n *               XYZ\r\n */",
+                "<?php\r\n/**\r\n * @param    int \$z\r\n * @param    int   \$b\r\n * XYZ\r\n */",
+                new WhitespacesFixerConfig('    ', "\r\n"),
             ),
         );
     }