Browse Source

Merge branch '2.2' into 2.11

# Conflicts:
#	src/Fixer/Phpdoc/PhpdocAlignFixer.php
#	tests/Fixer/Phpdoc/PhpdocAlignFixerTest.php
Dariusz Ruminski 6 years ago
parent
commit
089d4afd57
2 changed files with 37 additions and 13 deletions
  1. 11 8
      src/Fixer/Phpdoc/PhpdocAlignFixer.php
  2. 26 5
      tests/Fixer/Phpdoc/PhpdocAlignFixerTest.php

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

@@ -85,7 +85,7 @@ final class PhpdocAlignFixer extends AbstractFixer implements ConfigurationDefin
         $desc = '(?:\s+(?P<desc>\V*))';
 
         $this->regex = '/^'.$indent.' \* @(?:'.implode('|', $types).')'.$desc.'\s*$/u';
-        $this->regexCommentLine = '/^'.$indent.' \*(?! @)(?:\s+(?P<desc>\V+))(?<!\*\/)$/u';
+        $this->regexCommentLine = '/^'.$indent.' \*(?! @)(?:\s+(?P<desc>\V+))(?<!\*\/)\r?$/u';
     }
 
     /**
@@ -136,8 +136,14 @@ final class PhpdocAlignFixer extends AbstractFixer implements ConfigurationDefin
     protected function applyFix(\SplFileInfo $file, Tokens $tokens)
     {
         foreach ($tokens as $index => $token) {
-            if ($token->isGivenKind(T_DOC_COMMENT)) {
-                $tokens[$index] = new Token([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([T_DOC_COMMENT, $newContent]);
             }
         }
     }
@@ -181,9 +187,7 @@ final class PhpdocAlignFixer extends AbstractFixer implements ConfigurationDefin
         $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 = [];
             $matches = $this->getMatches($lines[$i]);
 
@@ -200,8 +204,7 @@ final class PhpdocAlignFixer extends AbstractFixer implements ConfigurationDefin
                 }
 
                 $matches = $this->getMatches($lines[$i], true);
-
-                if (!$matches) {
+                if (null === $matches) {
                     break;
                 }
 

+ 26 - 5
tests/Fixer/Phpdoc/PhpdocAlignFixerTest.php

@@ -488,16 +488,17 @@ EOF;
     }
 
     /**
-     * @param array       $config
-     * @param string      $expected
-     * @param null|string $input
+     * @param array                  $config
+     * @param string                 $expected
+     * @param string                 $input
+     * @param WhitespacesFixerConfig $whitespacesFixerConfig
      *
      * @dataProvider provideMessyWhitespacesCases
      */
-    public function testMessyWhitespaces(array $config, $expected, $input = null)
+    public function testMessyWhitespaces(array $config, $expected, $input, WhitespacesFixerConfig $whitespacesFixerConfig)
     {
         $this->fixer->configure($config);
-        $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
+        $this->fixer->setWhitespacesConfig($whitespacesFixerConfig);
 
         $this->doTest($expected, $input);
     }
@@ -509,11 +510,31 @@ EOF;
                 ['tags' => ['type']],
                 "<?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"),
             ],
             [
                 ['tags' => ['param', 'return']],
                 "<?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"),
+            ],
+            [
+                [],
+                "<?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"),
+            ],
+            [
+                [],
+                "<?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"),
+            ],
+            [
+                [],
+                "<?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"),
             ],
         ];
     }