/**
  * Extract the var comment docblock
  *
  * @param array $tokens
  * @param string $commentToken
  * @param int $stackPtr  The position of the current token in the stack passed in $tokens.
  * @return int|false
  */
 protected function extractVarDocBlock($tokens, $commentToken, $stackPtr)
 {
     $commentEnd = $this->helper->getCurrentFile()->findPrevious($commentToken, $stackPtr - 3);
     $break = false;
     if ($commentEnd !== false && $tokens[$commentEnd]['code'] === T_COMMENT) {
         $this->helper->addMessage($stackPtr, Helper::WRONG_STYLE, ['variable']);
         $break = true;
     } elseif ($commentEnd === false || $tokens[$commentEnd]['code'] !== T_DOC_COMMENT) {
         $this->helper->addMessage($stackPtr, Helper::MISSING, ['variable']);
         $break = true;
     } else {
         // Make sure the comment we have found belongs to us.
         $commentFor = $this->helper->getCurrentFile()->findNext([T_VARIABLE, T_CLASS, T_INTERFACE], $commentEnd + 1);
         if ($commentFor !== $stackPtr) {
             $this->helper->addMessage($stackPtr, Helper::MISSING, ['variable']);
             $break = true;
         }
     }
     return $break ? false : $commentEnd;
 }
Beispiel #2
0
 /**
  * Process the function parameter comments.
  *
  * @param int $commentStart The position in the stack where
  *                          the comment started.
  * @param int $commentEnd   The position in the stack where
  *                          the comment ended.
  *
  * @return void
  */
 protected function processParams($commentStart, $commentEnd)
 {
     $realParams = $this->helper->getCurrentFile()->getMethodParameters($this->_functionToken);
     $params = $this->commentParser->getParams();
     $foundParams = array();
     if (empty($params) === false) {
         $subStrCount = substr_count($params[count($params) - 1]->getWhitespaceAfter(), $this->helper->getCurrentFile()->eolChar);
         if ($subStrCount !== 2) {
             $errorPos = $params[count($params) - 1]->getLine() + $commentStart;
             $this->helper->addMessage($errorPos, Helper::SPACING_AFTER_PARAMS);
         }
         // Parameters must appear immediately after the comment.
         if ($params[0]->getOrder() !== 2) {
             $errorPos = $params[0]->getLine() + $commentStart;
             $this->helper->addMessage($errorPos, Helper::SPACING_BEFORE_PARAMS);
         }
         $previousParam = null;
         $spaceBeforeVar = 10000;
         $spaceBeforeComment = 10000;
         $longestType = 0;
         $longestVar = 0;
         foreach ($params as $param) {
             $paramComment = trim($param->getComment());
             $errorPos = $param->getLine() + $commentStart;
             // Make sure that there is only one space before the var type.
             if ($param->getWhitespaceBeforeType() !== ' ') {
                 $this->helper->addMessage($errorPos, Helper::SPACING_BEFORE_PARAM_TYPE);
             }
             $spaceCount = substr_count($param->getWhitespaceBeforeVarName(), ' ');
             if ($spaceCount < $spaceBeforeVar) {
                 $spaceBeforeVar = $spaceCount;
                 $longestType = $errorPos;
             }
             $spaceCount = substr_count($param->getWhitespaceBeforeComment(), ' ');
             if ($spaceCount < $spaceBeforeComment && $paramComment !== '') {
                 $spaceBeforeComment = $spaceCount;
                 $longestVar = $errorPos;
             }
             // Make sure they are in the correct order, and have the correct name.
             $pos = $param->getPosition();
             $paramName = $param->getVarName() !== '' ? $param->getVarName() : '[ UNKNOWN ]';
             if ($previousParam !== null) {
                 $previousName = $previousParam->getVarName() !== '' ? $previousParam->getVarName() : 'UNKNOWN';
             }
             // Variable must be one of the supported standard type.
             $typeNames = explode('|', $param->getType());
             foreach ($typeNames as $typeName) {
                 $suggestedName = $this->helper->suggestType($typeName);
                 if ($typeName !== $suggestedName) {
                     $data = array($suggestedName, $typeName, $paramName, $pos);
                     $this->helper->addMessage($errorPos, Helper::INCORRECT_PARAM_VAR_NAME, $data);
                 } elseif ($this->helper->isAmbiguous($typeName, $matches)) {
                     // Warn about ambiguous types ie array or mixed
                     $data = array($matches[1], $paramName, ' at position ' . $pos . ' is NOT recommended');
                     $this->helper->addMessage($commentEnd + 2, Helper::AMBIGUOUS_TYPE, $data);
                 } elseif (count($typeNames) === 1) {
                     // Check type hint for array and custom type.
                     $suggestedTypeHint = '';
                     if (strpos($suggestedName, 'array') !== false) {
                         $suggestedTypeHint = 'array';
                     } elseif (strpos($suggestedName, 'callable') !== false) {
                         $suggestedTypeHint = 'callable';
                     } elseif (in_array($typeName, $this->helper->getAllowedTypes()) === false) {
                         $suggestedTypeHint = $suggestedName;
                     } else {
                         $suggestedTypeHint = $this->helper->suggestType($typeName);
                     }
                     if ($suggestedTypeHint !== '' && isset($realParams[$pos - 1]) === true) {
                         $typeHint = $realParams[$pos - 1]['type_hint'];
                         if ($typeHint === '') {
                             $data = array($suggestedTypeHint, $paramName, $pos);
                             $this->helper->addMessage($commentEnd + 2, Helper::TYPE_HINT_MISSING, $data);
                         } elseif ($typeHint !== $suggestedTypeHint) {
                             $data = array($suggestedTypeHint, $typeHint, $paramName, $pos);
                             $this->helper->addMessage($commentEnd + 2, Helper::INCORRECT_TYPE_HINT, $data);
                         }
                     } elseif ($suggestedTypeHint === '' && isset($realParams[$pos - 1]) === true) {
                         $typeHint = $realParams[$pos - 1]['type_hint'];
                         if ($typeHint !== '') {
                             $data = array($typeHint, $paramName, $pos);
                             $this->helper->addMessage($commentEnd + 2, Helper::INVALID_TYPE_HINT, $data);
                         }
                     }
                 }
             }
             // Make sure the names of the parameter comment matches the
             // actual parameter.
             if (isset($realParams[$pos - 1]) === true) {
                 $realName = $realParams[$pos - 1]['name'];
                 $foundParams[] = $realName;
                 // Append ampersand to name if passing by reference.
                 if ($realParams[$pos - 1]['pass_by_reference'] === true) {
                     $realName = '&' . $realName;
                 }
                 if ($realName !== $paramName) {
                     $code = Helper::PARAM_NAME_NO_MATCH;
                     $data = array($paramName, $realName, $pos);
                     if (strtolower($paramName) === strtolower($realName)) {
                         $code = Helper::PARAM_NAME_NO_CASE_MATCH;
                     }
                     $this->helper->addMessage($errorPos, $code, $data);
                 }
             } elseif (substr($paramName, -4) !== ',...') {
                 // We must have an extra parameter comment.
                 $this->helper->addMessage($errorPos, Helper::EXTRA_PARAM_COMMENT, array($pos));
             }
             if ($param->getVarName() === '') {
                 $this->helper->addMessage($errorPos, Helper::MISSING_PARAM_NAME, array($pos));
             }
             if ($param->getType() === '') {
                 $this->helper->addMessage($errorPos, Helper::MISSING_PARAM_TYPE, array($pos));
             }
             if ($paramComment === '') {
                 $data = array($paramName, $pos);
                 $this->helper->addMessage($errorPos, Helper::MISSING_PARAM_COMMENT, $data);
             } else {
                 // Param comments must start with a capital letter and
                 // end with the full stop.
                 $firstChar = $paramComment[0];
                 if (preg_match('|\\p{Lu}|u', $firstChar) === 0) {
                     $this->helper->addMessage($errorPos, Helper::PARAM_COMMENT_NOT_CAPITAL);
                 }
                 $lastChar = $paramComment[strlen($paramComment) - 1];
                 if ($lastChar !== '.') {
                     $this->helper->addMessage($errorPos, Helper::PARAM_COMMENT_FULL_STOP);
                 }
             }
             $previousParam = $param;
         }
         if ($spaceBeforeVar !== 1 && $spaceBeforeVar !== 10000 && $spaceBeforeComment !== 10000) {
             $this->helper->addMessage($longestType, Helper::SPACING_AFTER_LONG_TYPE);
         }
         if ($spaceBeforeComment !== 1 && $spaceBeforeComment !== 10000) {
             $this->helper->addMessage($longestVar, Helper::SPACING_AFTER_LONG_NAME);
         }
     }
     $realNames = array();
     foreach ($realParams as $realParam) {
         $realNames[] = $realParam['name'];
     }
     // Report missing comments.
     $diff = array_diff($realNames, $foundParams);
     foreach ($diff as $neededParam) {
         if (count($params) !== 0) {
             $errorPos = $params[count($params) - 1]->getLine() + $commentStart;
         } else {
             $errorPos = $commentStart;
         }
         $data = array($neededParam);
         $this->helper->addMessage($errorPos, Helper::MISSING_PARAM_TAG, $data);
     }
 }