Browse Source

Allow phpdoc for language constructs, fixes #2634

Marc Ypes 8 years ago
parent
commit
a9d8198255

+ 12 - 7
src/Fixer/Phpdoc/PhpdocToCommentFixer.php

@@ -78,6 +78,11 @@ foreach($connections as $key => $sqlite) {
             T_WHILE,
             T_FOR,
         );
+        static $languageStructures = array(
+            T_LIST,
+            T_PRINT,
+            T_ECHO,
+        );
 
         foreach ($tokens as $index => $token) {
             if (!$token->isGivenKind(T_DOC_COMMENT)) {
@@ -104,7 +109,7 @@ foreach($connections as $key => $sqlite) {
                 continue;
             }
 
-            if ($nextToken->isGivenKind(T_LIST) && $this->isValidList($tokens, $token, $nextIndex)) {
+            if ($nextToken->isGivenKind($languageStructures) && $this->isValidLanguageConstruct($tokens, $token, $nextIndex)) {
                 continue;
             }
 
@@ -179,20 +184,20 @@ foreach($connections as $key => $sqlite) {
     }
 
     /**
-     * Checks variable assignments through `list()` calls for correct docblock usage.
+     * Checks variable assignments through `list()`, `print()` etc. calls for correct docblock usage.
      *
      * @param Tokens $tokens
-     * @param Token  $docsToken docs Token
-     * @param int    $listIndex index of variable Token
+     * @param Token  $docsToken              docs Token
+     * @param int    $languageConstructIndex index of variable Token
      *
      * @return bool
      */
-    private function isValidList(Tokens $tokens, Token $docsToken, $listIndex)
+    private function isValidLanguageConstruct(Tokens $tokens, Token $docsToken, $languageConstructIndex)
     {
-        $endIndex = $tokens->getNextTokenOfKind($listIndex, array(')'));
+        $endIndex = $tokens->getNextTokenOfKind($languageConstructIndex, array(')'));
         $docsContent = $docsToken->getContent();
 
-        for ($index = $listIndex + 1; $index < $endIndex; ++$index) {
+        for ($index = $languageConstructIndex + 1; $index < $endIndex; ++$index) {
             $token = $tokens[$index];
 
             if (

+ 14 - 0
tests/Fixer/Phpdoc/PhpdocToCommentFixerTest.php

@@ -518,6 +518,20 @@ function getNumberFormatter()
 ',
         );
 
+        $cases[] = array(
+            '<?php
+
+class A
+{
+    public function b()
+    {
+        /** @var int $c */
+        print($c = 0);
+    }
+}
+',
+        );
+
         return $cases;
     }