suggestType() public static method

If type is not one of the standard type, it must be a custom type. Returns the correct type name suggestion if type name is invalid.
public static suggestType ( string $varType ) : string
$varType string The variable type to process.
return string
 /**
  * Process the var tag.
  *
  * @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 processVar($commentStart, $commentEnd)
 {
     $var = $this->commentParser->getVar();
     if ($var !== null) {
         $errorPos = $commentStart + $var->getLine();
         $index = array_keys($this->commentParser->getTagOrders(), 'var');
         if (count($index) > 1) {
             $error = 'Only 1 @var tag is allowed in variable comment';
             $this->currentFile->addError($error, $errorPos, 'DuplicateVar');
             return;
         }
         if ($index[0] !== 1) {
             $error = 'The @var tag must be the first tag in a variable comment';
             $this->currentFile->addError($error, $errorPos, 'VarOrder');
         }
         $content = $var->getContent();
         if (empty($content) === true) {
             $error = 'Var type missing for @var tag in variable comment';
             $this->currentFile->addError($error, $errorPos, 'MissingVarType');
             return;
         } else {
             $suggestedType = PHP_CodeSniffer::suggestType($content);
             if ($suggestedType !== $content) {
                 // Hotfix - somehow they do not like "int" and "bool".
                 switch ($content) {
                     case 'int':
                         $suggestedType = 'int';
                         break;
                     case 'bool':
                         $suggestedType = 'bool';
                         break;
                     default:
                 }
             }
             if ($content !== $suggestedType) {
                 $error = 'Expected "%s"; found "%s" for @var tag in variable comment';
                 $data = array($suggestedType, $content);
                 $this->currentFile->addError($error, $errorPos, 'IncorrectVarType', $data);
             }
         }
         $spacing = substr_count($var->getWhitespaceBeforeContent(), ' ');
         if ($spacing !== 1) {
             $error = '@var tag indented incorrectly; expected 1 space but found %s';
             $data = array($spacing);
             $this->currentFile->addError($error, $errorPos, 'VarIndent', $data);
         }
     } else {
         $error = 'Missing @var tag in variable comment';
         $this->currentFile->addError($error, $commentEnd, 'MissingVar');
     }
     //end if
 }
 /**
  * Process the function parameter comments.
  *
  * @param PHP_CodeSniffer_File $phpcsFile    The file being scanned.
  * @param int                  $stackPtr     The position of the current token
  *                                           in the stack passed in $tokens.
  * @param int                  $commentStart The position in the stack where the comment started.
  *
  * @return void
  */
 protected function processParams(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart)
 {
     $tokens = $phpcsFile->getTokens();
     $params = array();
     $maxType = 0;
     $maxVar = 0;
     foreach ($tokens[$commentStart]['comment_tags'] as $pos => $tag) {
         if ($tokens[$tag]['content'] !== '@param') {
             continue;
         }
         $type = '';
         $typeSpace = 0;
         $var = '';
         $varSpace = 0;
         $comment = '';
         $commentLines = array();
         if ($tokens[$tag + 2]['code'] === T_DOC_COMMENT_STRING) {
             $matches = array();
             preg_match('/([^$&]+)(?:((?:\\$|&)[^\\s]+)(?:(\\s+)(.*))?)?/', $tokens[$tag + 2]['content'], $matches);
             $typeLen = strlen($matches[1]);
             $type = trim($matches[1]);
             $typeSpace = $typeLen - strlen($type);
             $typeLen = strlen($type);
             if ($typeLen > $maxType) {
                 $maxType = $typeLen;
             }
             if (isset($matches[2]) === true) {
                 $var = $matches[2];
                 $varLen = strlen($var);
                 if ($varLen > $maxVar) {
                     $maxVar = $varLen;
                 }
                 if (isset($matches[4]) === true) {
                     $varSpace = strlen($matches[3]);
                     $comment = $matches[4];
                     $commentLines[] = array('comment' => $comment, 'token' => $tag + 2, 'indent' => $varSpace);
                     // Any strings until the next tag belong to this comment.
                     if (isset($tokens[$commentStart]['comment_tags'][$pos + 1]) === true) {
                         $end = $tokens[$commentStart]['comment_tags'][$pos + 1];
                     } else {
                         $end = $tokens[$commentStart]['comment_closer'];
                     }
                     for ($i = $tag + 3; $i < $end; $i++) {
                         if ($tokens[$i]['code'] === T_DOC_COMMENT_STRING) {
                             $indent = 0;
                             if ($tokens[$i - 1]['code'] === T_DOC_COMMENT_WHITESPACE) {
                                 $indent = strlen($tokens[$i - 1]['content']);
                             }
                             $comment .= ' ' . $tokens[$i]['content'];
                             $commentLines[] = array('comment' => $tokens[$i]['content'], 'token' => $i, 'indent' => $indent);
                         }
                     }
                 } else {
                     $error = 'Missing parameter comment';
                     $phpcsFile->addError($error, $tag, 'MissingParamComment');
                     $commentLines[] = array('comment' => '');
                 }
                 //end if
             } else {
                 $error = 'Missing parameter name';
                 $phpcsFile->addError($error, $tag, 'MissingParamName');
             }
             //end if
         } else {
             $error = 'Missing parameter type';
             $phpcsFile->addError($error, $tag, 'MissingParamType');
         }
         //end if
         $params[] = array('tag' => $tag, 'type' => $type, 'var' => $var, 'comment' => $comment, 'commentLines' => $commentLines, 'type_space' => $typeSpace, 'var_space' => $varSpace);
     }
     //end foreach
     $realParams = $phpcsFile->getMethodParameters($stackPtr);
     $foundParams = array();
     foreach ($params as $pos => $param) {
         // If the type is empty, the whole line is empty.
         if ($param['type'] === '') {
             continue;
         }
         // Check the param type value.
         $typeNames = explode('|', $param['type']);
         foreach ($typeNames as $typeName) {
             $suggestedName = PHP_CodeSniffer::suggestType($typeName);
             if ($typeName !== $suggestedName) {
                 $error = 'Expected "%s" but found "%s" for parameter type';
                 $data = array($suggestedName, $typeName);
                 $fix = $phpcsFile->addFixableError($error, $param['tag'], 'IncorrectParamVarName', $data);
                 if ($fix === true) {
                     $content = $suggestedName;
                     $content .= str_repeat(' ', $param['type_space']);
                     $content .= $param['var'];
                     $content .= str_repeat(' ', $param['var_space']);
                     if (isset($param['commentLines'][0]) === true) {
                         $content .= $param['commentLines'][0]['comment'];
                     }
                     $phpcsFile->fixer->replaceToken($param['tag'] + 2, $content);
                 }
             } else {
                 if (count($typeNames) === 1) {
                     // Check type hint for array and custom type.
                     $suggestedTypeHint = '';
                     if (strpos($suggestedName, 'array') !== false) {
                         $suggestedTypeHint = 'array';
                     } else {
                         if (strpos($suggestedName, 'callable') !== false) {
                             $suggestedTypeHint = 'callable';
                         } else {
                             if (in_array($typeName, PHP_CodeSniffer::$allowedTypes) === false) {
                                 $suggestedTypeHint = $suggestedName;
                             }
                         }
                     }
                     if ($suggestedTypeHint !== '' && isset($realParams[$pos]) === true) {
                         $getType = substr($tokens[$tag + 2]['content'], 0, strlen($suggestedTypeHint));
                         $typeHint = $realParams[$pos]['type_hint'];
                         if ($getType !== $suggestedTypeHint) {
                             if ($typeHint === '') {
                                 $error = 'Type hint "%s" missing for %s';
                                 $data = array($suggestedTypeHint, $param['var']);
                                 $phpcsFile->addError($error, $stackPtr, 'TypeHintMissing', $data);
                             } else {
                                 if ($typeHint !== substr($suggestedTypeHint, strlen($typeHint) * -1)) {
                                     $error = 'Expected type hint "%s"; found "%s" for %s';
                                     $data = array($suggestedTypeHint, $typeHint, $param['var']);
                                     $phpcsFile->addError($error, $stackPtr, 'IncorrectTypeHint', $data);
                                 }
                             }
                         }
                     } else {
                         if ($suggestedTypeHint === '' && isset($realParams[$pos]) === true) {
                             $typeHint = $realParams[$pos]['type_hint'];
                             if ($typeHint !== '') {
                                 $error = 'Unknown type hint "%s" found for %s';
                                 $data = array($typeHint, $param['var']);
                                 $phpcsFile->addError($error, $stackPtr, 'InvalidTypeHint', $data);
                             }
                         }
                     }
                     //end if
                 }
             }
             //end if
         }
         //end foreach
         if ($param['var'] === '') {
             continue;
         }
         $foundParams[] = $param['var'];
         // Check number of spaces after the type.
         $spaces = $maxType - strlen($param['type']) + 1;
         if ($param['type_space'] !== $spaces) {
             $error = 'Expected %s spaces after parameter type; %s found';
             $data = array($spaces, $param['type_space']);
             $fix = $phpcsFile->addFixableError($error, $param['tag'], 'SpacingAfterParamType', $data);
             if ($fix === true) {
                 $phpcsFile->fixer->beginChangeset();
                 $content = $param['type'];
                 $content .= str_repeat(' ', $spaces);
                 $content .= $param['var'];
                 $content .= str_repeat(' ', $param['var_space']);
                 $content .= $param['commentLines'][0]['comment'];
                 $phpcsFile->fixer->replaceToken($param['tag'] + 2, $content);
                 // Fix up the indent of additional comment lines.
                 foreach ($param['commentLines'] as $lineNum => $line) {
                     if ($lineNum === 0 || $param['commentLines'][$lineNum]['indent'] === 0) {
                         continue;
                     }
                     $newIndent = $param['commentLines'][$lineNum]['indent'] + $spaces - $param['type_space'];
                     $phpcsFile->fixer->replaceToken($param['commentLines'][$lineNum]['token'] - 1, str_repeat(' ', $newIndent));
                 }
                 $phpcsFile->fixer->endChangeset();
             }
             //end if
         }
         //end if
         // Make sure the param name is correct.
         if (isset($realParams[$pos]) === true) {
             $realName = $realParams[$pos]['name'];
             if ($realName !== $param['var']) {
                 $code = 'ParamNameNoMatch';
                 $data = array($param['var'], $realName);
                 $error = 'Doc comment for parameter %s does not match ';
                 if (strtolower($param['var']) === strtolower($realName)) {
                     $error .= 'case of ';
                     $code = 'ParamNameNoCaseMatch';
                 }
                 $error .= 'actual variable name %s';
                 $phpcsFile->addError($error, $param['tag'], $code, $data);
             }
         } else {
             if (substr($param['var'], -4) !== ',...') {
                 // We must have an extra parameter comment.
                 $error = 'Superfluous parameter comment';
                 $phpcsFile->addError($error, $param['tag'], 'ExtraParamComment');
             }
         }
         //end if
         if ($param['comment'] === '') {
             continue;
         }
         // Check number of spaces after the var name.
         $spaces = $maxVar - strlen($param['var']) + 1;
         if ($param['var_space'] !== $spaces) {
             $error = 'Expected %s spaces after parameter name; %s found';
             $data = array($spaces, $param['var_space']);
             $fix = $phpcsFile->addFixableError($error, $param['tag'], 'SpacingAfterParamName', $data);
             if ($fix === true) {
                 $phpcsFile->fixer->beginChangeset();
                 $content = $param['type'];
                 $content .= str_repeat(' ', $param['type_space']);
                 $content .= $param['var'];
                 $content .= str_repeat(' ', $spaces);
                 $content .= $param['commentLines'][0]['comment'];
                 $phpcsFile->fixer->replaceToken($param['tag'] + 2, $content);
                 // Fix up the indent of additional comment lines.
                 foreach ($param['commentLines'] as $lineNum => $line) {
                     if ($lineNum === 0 || $param['commentLines'][$lineNum]['indent'] === 0) {
                         continue;
                     }
                     $newIndent = $param['commentLines'][$lineNum]['indent'] + $spaces - $param['var_space'];
                     $phpcsFile->fixer->replaceToken($param['commentLines'][$lineNum]['token'] - 1, str_repeat(' ', $newIndent));
                 }
                 $phpcsFile->fixer->endChangeset();
             }
             //end if
         }
         //end if
         // Param comments must start with a capital letter and end with the full stop.
         $firstChar = $param['comment'][0];
         if (preg_match('|\\p{Lu}|u', $firstChar) === 0) {
             $error = 'Parameter comment must start with a capital letter';
             $phpcsFile->addError($error, $param['tag'], 'ParamCommentNotCapital');
         }
         $lastChar = substr($param['comment'], -1);
         if ($lastChar !== '.') {
             $error = 'Parameter comment must end with a full stop';
             $phpcsFile->addError($error, $param['tag'], 'ParamCommentFullStop');
         }
     }
     //end foreach
     $realNames = array();
     foreach ($realParams as $realParam) {
         $realNames[] = $realParam['name'];
     }
     // Report missing comments.
     $diff = array_diff($realNames, $foundParams);
     foreach ($diff as $neededParam) {
         $error = 'Doc comment for parameter "%s" missing';
         $data = array($neededParam);
         $phpcsFile->addError($error, $commentStart, 'MissingParamTag', $data);
     }
 }
 /**
  * Process the var tag
  *
  * @param  integer $commentStart The position in the stack where the comment started
  * @param  integer $commentEnd   The position in the stack where the comment ended
  * @return void
  */
 public function processVar($commentStart, $commentEnd)
 {
     $var = $this->commentParser->getVar();
     if ($var !== null) {
         $errorPos = $commentStart + $var->getLine();
         $index = array_keys($this->commentParser->getTagOrders(), 'var');
         if (count($index) > 1) {
             $this->currentFile->addError("Le tag @version doit être présent une seule fois dans le commentaire de variable", $errorPos, 'OneVersionTagVariableComment');
             return;
         }
         if ($index[0] !== 1) {
             $this->currentFile->addError("Le tag @version doit être présent une seule fois dans le commentaire de variable", $errorPos, 'OneVersionTagVariableComment');
         }
         $content = $var->getContent();
         if (empty($content) === true) {
             $this->currentFile->addError("Le tag @type doit être suivi du type de variable", $errorPos, 'TypeMissingVarTagVariableComment');
             return;
         } else {
             $suggestedType = PHP_CodeSniffer::suggestType($content);
             if ($content !== $suggestedType) {
                 $this->currentFile->addError('Le tag @type devrait être suivi de "' . $suggestedType . '", "' . $content . '" trouvé', $errorPos, 'ExpectedFoundVarTagVariableComment');
             }
         }
         $spacing = substr_count($var->getWhitespaceBeforeContent(), ' ');
         if ($spacing !== $this->space) {
             $this->currentFile->addError($this->space . ' espace(s) attendu(s), ' . $spacing . ' trouvé(s)', $errorPos, 'ExpectedSpacesFoundVarTagVariableComment');
         }
     } else {
         $this->currentFile->addError('Le tag @var est manquant dans le commentaire de variable', $commentEnd, 'MissingVarTagVariableComment');
     }
 }
Ejemplo n.º 4
0
 /**
  * Process the var tag.
  *
  * @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 processVar($commentStart, $commentEnd)
 {
     $var = $this->commentParser->getVar();
     if ($var !== null) {
         $errorPos = $commentStart + $var->getLine();
         $index = array_keys($this->commentParser->getTagOrders(), 'var');
         if (count($index) > 1) {
             $error = 'Only 1 @var tag is allowed in variable comment';
             $this->currentFile->addError($error, $errorPos);
             return;
         }
         if ($index[0] !== 1) {
             $error = 'The @var tag must be the first tag in a variable comment';
             $this->currentFile->addError($error, $errorPos);
         }
         $content = $var->getContent();
         if (empty($content) === true) {
             $error = 'Var type missing for @var tag in variable comment';
             $this->currentFile->addError($error, $errorPos);
             return;
         } else {
             $suggestedType = PHP_CodeSniffer::suggestType($content);
             if ($content !== $suggestedType) {
                 $error = "Expected \"{$suggestedType}\"; found \"{$content}\" for @var tag in variable comment";
                 $this->currentFile->addError($error, $errorPos);
             }
         }
         $spacing = substr_count($var->getWhitespaceBeforeContent(), ' ');
         if ($spacing !== 3) {
             $error = '@var tag indented incorrectly. ';
             $error .= "Expected 3 spaces but found {$spacing}.";
             $this->currentFile->addError($error, $errorPos);
         }
     } else {
         $error = 'Missing @var tag in variable comment';
         $this->currentFile->addError($error, $commentEnd);
     }
     //end if
 }
 /**
  * Process the function parameter comments.
  *
  * @param  integer $commentStart The position in the stack where the comment started
  * @param  integer $commentEnd   The position in the stack where the comment ended
  * @return void
  */
 protected function processParams($commentStart, $commentEnd)
 {
     $realParams = $this->currentFile->getMethodParameters($this->_functionToken);
     $params = $this->commentParser->getParams();
     $foundParams = array();
     if (empty($params) === false) {
         if (substr_count($params[count($params) - 1]->getWhitespaceAfter(), $this->currentFile->eolChar) !== 1) {
             $error = 'No empty line after last parameter comment allowed';
             $errorPos = $params[count($params) - 1]->getLine() + $commentStart;
             $this->currentFile->addError($error, $errorPos + 1);
         }
         // Parameters must appear immediately after the comment.
         if ($params[0]->getOrder() !== 2) {
             $error = 'Parameters must appear immediately after the comment';
             $errorPos = $params[0]->getLine() + $commentStart;
             $this->currentFile->addError($error, $errorPos);
         }
         $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() !== '  ') {
                 $error = 'Expected 2 space before variable type';
                 $this->currentFile->addError($error, $errorPos);
             }
             $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';
                 // Check to see if the parameters align properly.
                 if ($param->alignsVariableWith($previousParam) === false) {
                     $error = 'The variable names for parameters ' . $previousName . ' (' . ($pos - 1) . ') and ' . $paramName . ' (' . $pos . ') do not align';
                     $this->currentFile->addError($error, $errorPos);
                 }
                 if ($param->alignsCommentWith($previousParam) === false) {
                     $error = 'The comments for parameters ' . $previousName . ' (' . ($pos - 1) . ') and ' . $paramName . ' (' . $pos . ') do not align';
                     $this->currentFile->addError($error, $errorPos);
                 }
             }
             // Variable must be one of the supported standard type.
             $typeNames = explode('|', $param->getType());
             foreach ($typeNames as $typeName) {
                 $suggestedName = PHP_CodeSniffer::suggestType($typeName);
                 if ($typeName !== $suggestedName) {
                     $error = "Expected \"{$suggestedName}\"; found \"{$typeName}\" for {$paramName} at position {$pos}";
                     $this->currentFile->addError($error, $errorPos);
                 } else {
                     if (count($typeNames) === 1) {
                         // Check type hint for array and custom type.
                         $suggestedTypeHint = '';
                         if (strpos($suggestedName, 'array') !== false) {
                             $suggestedTypeHint = 'array';
                         } else {
                             if (in_array($typeName, PHP_CodeSniffer::$allowedTypes) === false) {
                                 $suggestedTypeHint = $suggestedName;
                             }
                         }
                         if ($suggestedTypeHint !== '' && isset($realParams[$pos - 1]) === true) {
                             $typeHint = $realParams[$pos - 1]['type_hint'];
                             if ($typeHint === '') {
                                 $error = "Type hint \"{$suggestedTypeHint}\" missing for {$paramName} at position {$pos}";
                                 $this->currentFile->addError($error, $commentEnd + 2);
                             } else {
                                 if ($typeHint !== $suggestedTypeHint) {
                                     $error = "Expected type hint \"{$suggestedTypeHint}\"; found \"{$typeHint}\" for {$paramName} at position {$pos}";
                                     $this->currentFile->addError($error, $commentEnd + 2);
                                 }
                             }
                         } else {
                             if ($suggestedTypeHint === '' && isset($realParams[$pos - 1]) === true) {
                                 $typeHint = $realParams[$pos - 1]['type_hint'];
                                 if ($typeHint !== '') {
                                     $error = "Unknown type hint \"{$typeHint}\" found for {$paramName} at position {$pos}";
                                     $this->currentFile->addError($error, $commentEnd + 2);
                                 }
                             }
                         }
                     }
                 }
             }
             // 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 !== $param->getVarName()) {
                     $error = 'Doc comment var "' . $paramName;
                     $error .= '" does not match actual variable name "' . $realName;
                     $error .= '" at position ' . $pos;
                     $this->currentFile->addError($error, $errorPos);
                 }
             } else {
                 // We must have an extra parameter comment.
                 $error = 'Superfluous doc comment at position ' . $pos;
                 $this->currentFile->addError($error, $errorPos);
             }
             if ($param->getVarName() === '') {
                 $error = 'Missing parameter name at position ' . $pos;
                 $this->currentFile->addError($error, $errorPos);
             }
             if ($param->getType() === '') {
                 $error = 'Missing type at position ' . $pos;
                 $this->currentFile->addError($error, $errorPos);
             }
             if ($paramComment === '') {
                 $error = 'Missing comment for param "' . $paramName . '" at position ' . $pos;
                 $this->currentFile->addError($error, $errorPos);
             } else {
                 // Param comments must start with a capital letter and
                 // end with the full stop.
                 $firstChar = $paramComment[0];
                 if (preg_match('|[A-Z]|', $firstChar) === 0) {
                     $error = 'Param comment must start with a capital letter';
                     $this->currentFile->addError($error, $errorPos);
                 }
             }
             $previousParam = $param;
         }
         if ($spaceBeforeVar !== 1 && $spaceBeforeVar !== 10000 && $spaceBeforeComment !== 10000) {
             $error = 'Expected 1 space after the longest type';
             $this->currentFile->addError($error, $longestType);
         }
         if ($spaceBeforeComment !== 1 && $spaceBeforeComment !== 10000) {
             $error = 'Expected 1 space after the longest variable name';
             $this->currentFile->addError($error, $longestVar);
         }
     }
     $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;
         }
         $error = 'Doc comment for "' . $neededParam . '" missing';
         $this->currentFile->addError($error, $errorPos);
     }
 }
 /**
  * Called to process class member vars.
  *
  * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  * @param int                  $stackPtr  The position of the current token
  *                                        in the stack passed in $tokens.
  *
  * @return void
  */
 public function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     $commentToken = array(T_COMMENT, T_DOC_COMMENT_CLOSE_TAG);
     $commentEnd = $phpcsFile->findPrevious($commentToken, $stackPtr);
     if ($commentEnd === false) {
         $phpcsFile->addError('Missing member variable doc comment', $stackPtr, 'Missing');
         return;
     }
     if ($tokens[$commentEnd]['code'] === T_COMMENT) {
         $phpcsFile->addError('You must use "/**" style comments for a member variable comment', $stackPtr, 'WrongStyle');
         return;
     } else {
         if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG) {
             $phpcsFile->addError('Missing member variable doc comment', $stackPtr, 'Missing');
             return;
         } else {
             // Make sure the comment we have found belongs to us.
             $commentFor = $phpcsFile->findNext(array(T_VARIABLE, T_CLASS, T_INTERFACE), $commentEnd + 1);
             if ($commentFor !== $stackPtr) {
                 $phpcsFile->addError('Missing member variable doc comment', $stackPtr, 'Missing');
                 return;
             }
         }
     }
     $commentStart = $tokens[$commentEnd]['comment_opener'];
     $foundVar = null;
     foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
         if ($tokens[$tag]['content'] === '@var') {
             if ($foundVar !== null) {
                 $error = 'Only one @var tag is allowed in a member variable comment';
                 $phpcsFile->addError($error, $tag, 'DuplicateVar');
             } else {
                 $foundVar = $tag;
             }
         } else {
             if ($tokens[$tag]['content'] === '@see') {
                 // Make sure the tag isn't empty.
                 $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $tag, $commentEnd);
                 if ($string === false || $tokens[$string]['line'] !== $tokens[$tag]['line']) {
                     $error = 'Content missing for @see tag in member variable comment';
                     $phpcsFile->addError($error, $tag, 'EmptySees');
                 }
             } elseif ($tokens[$tag]['content'] !== '@since') {
                 $error = '%s tag is not allowed in member variable comment';
                 $data = array($tokens[$tag]['content']);
                 $phpcsFile->addWarning($error, $tag, 'TagNotAllowed', $data);
             }
         }
         //end if
     }
     //end foreach
     // The @var tag is the only one we require.
     if ($foundVar === null) {
         $error = 'Missing @var tag in member variable comment';
         $phpcsFile->addError($error, $commentEnd, 'MissingVar');
         return;
     }
     $firstTag = $tokens[$commentStart]['comment_tags'][0];
     if ($foundVar !== null && $tokens[$firstTag]['content'] !== '@var') {
         $error = 'The @var tag must be the first tag in a member variable comment';
         $phpcsFile->addError($error, $foundVar, 'VarOrder');
     }
     // Make sure the tag isn't empty and has the correct padding.
     $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $foundVar, $commentEnd);
     if ($string === false || $tokens[$string]['line'] !== $tokens[$foundVar]['line']) {
         $error = 'Content missing for @var tag in member variable comment';
         $phpcsFile->addError($error, $foundVar, 'EmptyVar');
         return;
     }
     $varType = $tokens[$foundVar + 2]['content'];
     $suggestedType = PHP_CodeSniffer::suggestType($varType);
     if ($varType !== $suggestedType) {
         $error = 'Expected "%s" but found "%s" for @var tag in member variable comment';
         $data = array($suggestedType, $varType);
         $phpcsFile->addError($error, $foundVar + 2, 'IncorrectVarType', $data);
     }
 }
Ejemplo n.º 7
0
 /**
  * Take the type and suggest the correct one.
  *
  * @param string $type
  * @return string
  */
 public function suggestType($type)
 {
     $suggestedName = null;
     // First check to see if this type is a list of types. If so we break it up and check each
     if (preg_match('/^.*?(?:\\|.*)+$/', $type)) {
         // Return list of all types in this string.
         $types = explode('|', $type);
         if (is_array($types)) {
             // Loop over all types and call this method on each.
             $suggestions = [];
             foreach ($types as $t) {
                 $suggestions[] = $this->suggestType($t);
             }
             // Now that we have suggestions put them back together.
             $suggestedName = implode('|', $suggestions);
         } else {
             $suggestedName = 'Unknown';
         }
     } elseif ($this->isClassName($type)) {
         // If this looks like a class name.
         $suggestedName = $type;
     } else {
         // Only one type First check if that type is a base one.
         $lowerVarType = strtolower($type);
         if (in_array($lowerVarType, self::$allowedTypes)) {
             $suggestedName = $lowerVarType;
         }
         // If no name suggested yet then call the phpcs version of this method.
         if (empty($suggestedName)) {
             $suggestedName = PHP_CodeSniffer::suggestType($type);
         }
     }
     return $suggestedName;
 }
 /**
  * Process the function parameter comments
  *
  * @param  integer $commentStart The position in the stack where the comment started
  * @param  integer $commentEnd   The position in the stack where the comment ended
  * @return void
  */
 protected function _processParams($commentStart, $commentEnd)
 {
     $realParams = $this->_currentFile->getMethodParameters($this->_functionToken);
     $params = $this->_commentParser->getParams();
     $foundParams = array();
     if (empty($params) === false) {
         $isSpecialMethod = ($this->_methodName === '__construct' or $this->_methodName === '__destruct');
         if (substr_count($params[count($params) - 1]->getWhitespaceAfter(), $this->_currentFile->eolChar) !== 1 and $isSpecialMethod === false) {
             $errorPos = $params[count($params) - 1]->getLine() + $commentStart;
             $this->_currentFile->addEvent('EMPTY_LINE_LAST_PARAMETER_FUNCTION_COMMENT', array(), $errorPos + 1);
         }
         // Parameters must appear immediately after the comment
         if ($params[0]->getOrder() !== 2) {
             $errorPos = $params[0]->getLine() + $commentStart;
             $this->_currentFile->addEvent('PARAMETER_AFTER_COMMENT_FUNCTION_COMMENT', array(), $errorPos);
         }
         $previousParam = null;
         $spaceBeforeVar = 10000;
         $spaceBeforeComment = 10000;
         $longestType = 0;
         $longestVar = 0;
         if (count($this->_commentParser->getThrows()) !== 0) {
             $isSpecialMethod = false;
         }
         foreach ($params as $param) {
             $paramComment = trim($param->getComment());
             $errorPos = $param->getLine() + $commentStart;
             if ($isSpecialMethod === true and $param->getWhitespaceBeforeType() !== ' ') {
                 $this->_currentFile->addEvent('ONE_SPACE_VARIABLE_FUNCTION_COMMENT', array(), $errorPos);
             }
             $spaceCount = substr_count($param->getWhitespaceBeforeVarName(), ' ');
             if ($spaceCount < $spaceBeforeVar) {
                 $spaceBeforeVar = $spaceCount;
                 $longestType = $errorPos;
             }
             $spaceCount = substr_count($param->getWhitespaceBeforeComment(), ' ');
             if ($spaceCount < $spaceBeforeComment and $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';
                 // Check to see if the parameters align properly
                 if ($param->alignsVariableWith($previousParam) === false) {
                     $this->_currentFile->addEvent('VARIABLES_NAMES_NOT_ALIGN_FUNCTION_COMMENT', array('previousname' => $previousName, 'previousnamepos' => $pos - 1, 'paramname' => $paramName, 'paramnamepos' => $pos), $errorPos);
                 }
                 if ($param->alignsCommentWith($previousParam) === false) {
                     $this->_currentFile->addEvent('COMMENTS_NOT_ALIGN_FUNCTION_COMMENT', array('previousname' => $previousName, 'previousnamepos' => $pos - 1, 'paramname' => $paramName, 'paramnamepos' => $pos), $errorPos);
                 }
             }
             // Variable must be one of the supported standard type
             $typeNames = explode('|', $param->getType());
             foreach ($typeNames as $typeName) {
                 $suggestedName = PHP_CodeSniffer::suggestType($typeName);
                 if ($typeName !== $suggestedName) {
                     $this->_currentFile->addEvent('EXPECTED_FOUND_FUNCTION_COMMENT', array('suggestedname' => $suggestedName, 'paramname' => $paramName, 'typename' => $paramName, 'paramnamepos' => $pos), $errorPos);
                     continue;
                 }
                 if (count($typeNames) !== 1) {
                     continue;
                 }
                 // Check type hint for array and custom type
                 $suggestedTypeHint = '';
                 if (strpos($suggestedName, 'array') !== false) {
                     $suggestedTypeHint = 'array';
                 } else {
                     if (in_array($typeName, PHP_CodeSniffer::$allowedTypes) === false) {
                         $suggestedTypeHint = $suggestedName;
                     }
                 }
                 if ($suggestedTypeHint !== '' and isset($realParams[$pos - 1]) === true) {
                     $typeHint = $realParams[$pos - 1]['type_hint'];
                     if ($typeHint === '') {
                         $this->_currentFile->addEvent('TYPEHINT_MISSING_FUNCTION_COMMENT', array('suggestedtypehint' => $suggestedTypeHint, 'paramname' => $paramName, 'paramnamepos' => $pos), $commentEnd + 2);
                     } else {
                         if ($typeHint !== $suggestedTypeHint) {
                             $this->_currentFile->addEvent('EXPECTED_TYPEHINT_FOUND_FUNCTION_COMMENT', array('suggestedtypehint' => $suggestedTypeHint, 'typehint' => $typeHint, 'paramname' => $paramName, 'paramnamepos' => $pos), $commentEnd + 2);
                         }
                     }
                 } else {
                     if ($suggestedTypeHint === '' and isset($realParams[$pos - 1]) === true) {
                         $typeHint = $realParams[$pos - 1]['type_hint'];
                         if ($typeHint !== '') {
                             $this->_currentFile->addEvent('UNKNOW_TYPEHINT_FOUND_FUNCTION_COMMENT', array('typehint' => $typeHint, 'paramname' => $paramName, 'paramnamepos' => $pos), $commentEnd + 2);
                         }
                     }
                 }
             }
             // 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 !== $param->getVarName()) {
                     $this->_currentFile->addEvent('DOCCOMMENT_NOT_MATCH_FUNCTION_COMMENT', array('paramname' => $paramName, 'realname' => $realName, 'paramnamepos' => $pos), $errorPos);
                 }
             } else {
                 // We must have an extra parameter comment
                 $this->_currentFile->addEvent('SUPERFLUOUS_DOCCOMMENT_FUNCTION_COMMENT', array('doccommentpos' => $pos), $errorPos);
             }
             if ($param->getVarName() === '') {
                 $this->_currentFile->addEvent('MISSING_PARAMETER_FUNCTION_COMMENT', array('parameterpos' => $pos), $errorPos);
             }
             if ($param->getType() === '') {
                 $this->_currentFile->addEvent('MISSING_TYPE_FUNCTION_COMMENT', array('typepos' => $pos), $errorPos);
             }
             if ($paramComment === '') {
                 $this->_currentFile->addEvent('MISSING_COMMENT_PARAM_FUNCTION_COMMENT', array('paramname' => $paramName, 'paramnamepos' => $pos), $errorPos);
             } else {
                 // Check if optional params include (Optional) within their description
                 $functionBegin = $this->_currentFile->findNext(array(T_FUNCTION), $commentStart);
                 $functionName = $this->_currentFile->findNext(array(T_STRING), $functionBegin);
                 $openBracket = $this->_tokens[$functionBegin]['parenthesis_opener'];
                 $closeBracket = $this->_tokens[$functionBegin]['parenthesis_closer'];
                 $nextParam = $this->_currentFile->findNext(T_VARIABLE, $openBracket + 1, $closeBracket);
                 while ($nextParam !== false) {
                     $nextToken = $this->_currentFile->findNext(T_WHITESPACE, $nextParam + 1, $closeBracket + 1, true);
                     if ($nextToken === false and $this->_tokens[$nextParam + 1]['code'] === T_CLOSE_PARENTHESIS) {
                         break;
                     }
                     $nextCode = $this->_tokens[$nextToken]['code'];
                     $arg = $this->_tokens[$nextParam]['content'];
                     if ($nextCode === T_EQUAL and $paramName === $arg) {
                         if (substr($paramComment, 0, 11) !== '(Optional) ') {
                             $this->_currentFile->addEvent('OPTIONAL_PARAM_START_FUNCTION_COMMENT', array('paramname' => $paramName), $errorPos);
                         }
                     }
                     $nextParam = $this->_currentFile->findNext(T_VARIABLE, $nextParam + 1, $closeBracket);
                 }
             }
             $previousParam = $param;
         }
         if ($spaceBeforeVar !== 1 and $spaceBeforeVar !== 10000 and $spaceBeforeComment !== 10000) {
             $this->_currentFile->addEvent('ONE_SPACE_LONGEST_TYPE_FUNCTION_COMMENT', array(), $longestType);
         }
         if ($spaceBeforeComment !== 1 and $spaceBeforeComment !== 10000) {
             $this->_currentFile->addEvent('ONE_SPACE_LONGEST_VARIABLE_FUNCTION_COMMENT', array(), $longestVar);
         }
     }
     $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;
         }
         $this->_currentFile->addEvent('DOCCOMMENT_MISSING_FUNCTION_COMMENT', array('param' => $neededParam), $errorPos);
     }
 }
 /**
  * Process the var tag
  *
  * @param  integer $commentStart The position in the stack where the comment started
  * @param  integer $commentEnd   The position in the stack where the comment ended
  * @return void
  */
 public function processVar($commentStart, $commentEnd)
 {
     $var = $this->commentParser->getVar();
     if ($var !== null) {
         $errorPos = $commentStart + $var->getLine();
         $index = array_keys($this->commentParser->getTagOrders(), 'var');
         if (count($index) > 1) {
             $this->currentFile->addEvent('ONE_VERSION_TAG_VARIABLE_COMMENT', array(), $errorPos);
             return;
         }
         if ($index[0] !== 1) {
             $this->currentFile->addEvent('ONE_VERSION_TAG_VARIABLE_COMMENT', array(), $errorPos);
         }
         $content = $var->getContent();
         if (empty($content) === true) {
             $this->currentFile->addEvent('TYPE_MISSING_VAR_TAG_VARIABLE_COMMENT', array(), $errorPos);
             return;
         } else {
             $suggestedType = PHP_CodeSniffer::suggestType($content);
             if ($content !== $suggestedType) {
                 $this->currentFile->addEvent('EXPECTED_FOUND_VAR_TAG_VARIABLE_COMMENT', array('suggestedyype' => $suggestedType, 'content' => $content), $errorPos);
             }
         }
         $spacing = substr_count($var->getWhitespaceBeforeContent(), ' ');
         if ($spacing !== $this->space) {
             $this->currentFile->addEvent('EXPECTED_SPACES_FOUND_VAR_TAG_VARIABLE_COMMENT', array('space' => $this->space, 'spacing' => $spacing), $errorPos);
         }
     } else {
         $this->currentFile->addEvent('MISSING_VAR_TAG_VARIABLE_COMMENT', array(), $commentEnd);
     }
 }
 /**
  * Process the function parameter comments.
  *
  * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  * @param int $stackPtr The position of the current token in the stack passed in $tokens.
  * @param int $commentStart The position in the stack where the comment started.
  * @return void
  */
 protected function processParams(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart)
 {
     if ($this->isInheritDoc($phpcsFile, $stackPtr)) {
         return;
     }
     $tokens = $phpcsFile->getTokens();
     $params = array();
     $maxType = $maxVar = 0;
     foreach ($tokens[$commentStart]['comment_tags'] as $pos => $tag) {
         if ($tokens[$tag]['content'] !== '@param') {
             continue;
         }
         $type = $var = $comment = '';
         $typeSpace = $varSpace = 0;
         $commentLines = array();
         if ($tokens[$tag + 2]['code'] === T_DOC_COMMENT_STRING) {
             $matches = array();
             preg_match('/([^$&]+)(?:((?:\\$|&)[^\\s]+)(?:(\\s+)(.*))?)?/', $tokens[$tag + 2]['content'], $matches);
             $typeLen = strlen($matches[1]);
             $type = trim($matches[1]);
             $typeSpace = $typeLen - strlen($type);
             $typeLen = strlen($type);
             if ($typeLen > $maxType) {
                 $maxType = $typeLen;
             }
             if (isset($matches[2]) === true) {
                 $var = $matches[2];
                 $varLen = strlen($var);
                 if ($varLen > $maxVar) {
                     $maxVar = $varLen;
                 }
                 if (isset($matches[4]) === true) {
                     $varSpace = strlen($matches[3]);
                     $comment = $matches[4];
                     $commentLines[] = array('comment' => $comment, 'token' => $tag + 2, 'indent' => $varSpace);
                     // Any strings until the next tag belong to this comment.
                     if (isset($tokens[$commentStart]['comment_tags'][$pos + 1]) === true) {
                         $end = $tokens[$commentStart]['comment_tags'][$pos + 1];
                     } else {
                         $end = $tokens[$commentStart]['comment_closer'];
                     }
                     for ($i = $tag + 3; $i < $end; $i++) {
                         if ($tokens[$i]['code'] === T_DOC_COMMENT_STRING) {
                             $indent = 0;
                             if ($tokens[$i - 1]['code'] === T_DOC_COMMENT_WHITESPACE) {
                                 $indent = strlen($tokens[$i - 1]['content']);
                             }
                             $comment .= ' ' . $tokens[$i]['content'];
                             $commentLines[] = array('comment' => $tokens[$i]['content'], 'token' => $i, 'indent' => $indent);
                         }
                     }
                 } else {
                     $error = 'Missing parameter comment';
                     $phpcsFile->addError($error, $tag, 'MissingParamComment');
                     $commentLines[] = array('comment' => '');
                 }
                 //end if
             } else {
                 $error = 'Missing parameter name';
                 $phpcsFile->addError($error, $tag, 'MissingParamName');
             }
             //end if
         } else {
             $error = 'Missing parameter type';
             $phpcsFile->addError($error, $tag, 'MissingParamType');
         }
         //end if
         $params[] = compact('tag', 'type', 'var', 'comment', 'commentLines', 'type_space', 'var_space');
     }
     //end foreach
     $realParams = $phpcsFile->getMethodParameters($stackPtr);
     $foundParams = array();
     foreach ($params as $pos => $param) {
         // If the type is empty, the whole line is empty.
         if ($param['type'] === '') {
             continue;
         }
         // Check the param type value.
         $typeNames = explode('|', $param['type']);
         foreach ($typeNames as $typeName) {
             if ($typeName === 'integer') {
                 $suggestedName = 'int';
             } elseif ($typeName === 'boolean') {
                 $suggestedName = 'bool';
             } elseif (in_array($typeName, array('int', 'bool'))) {
                 $suggestedName = $typeName;
             } else {
                 $suggestedName = PHP_CodeSniffer::suggestType($typeName);
             }
             if ($typeName !== $suggestedName) {
                 $error = 'Expected "%s" but found "%s" for parameter type';
                 $data = array($suggestedName, $typeName);
                 $fix = $phpcsFile->addFixableError($error, $param['tag'], 'IncorrectParamVarName', $data);
                 if ($fix === true) {
                     $content = $suggestedName;
                     $content .= str_repeat(' ', $param['type_space']);
                     $content .= $param['var'];
                     $content .= str_repeat(' ', $param['var_space']);
                     $content .= $param['commentLines'][0]['comment'];
                     $phpcsFile->fixer->replaceToken($param['tag'] + 2, $content);
                 }
             }
         }
         //end foreach
         if ($param['var'] === '') {
             continue;
         }
         $foundParams[] = $param['var'];
         // Make sure the param name is correct.
         if (isset($realParams[$pos]) === true) {
             $realName = $realParams[$pos]['name'];
             if ($realName !== $param['var']) {
                 $code = 'ParamNameNoMatch';
                 $data = array($param['var'], $realName);
                 $error = 'Doc comment for parameter %s does not match ';
                 if (strtolower($param['var']) === strtolower($realName)) {
                     $error .= 'case of ';
                     $code = 'ParamNameNoCaseMatch';
                 }
                 $error .= 'actual variable name %s';
                 $phpcsFile->addWarning($error, $param['tag'], $code, $data);
             }
         } elseif (substr($param['var'], -4) !== ',...') {
             // We must have an extra parameter comment.
             $error = 'Superfluous parameter comment';
             $phpcsFile->addError($error, $param['tag'], 'ExtraParamComment');
         }
         //end if
         if ($param['comment'] === '') {
             continue;
         }
         // Param comments must start with a capital letter and end with the full stop.
         $firstChar = $param['comment'][0];
         if (preg_match('|\\p{Lu}|u', $firstChar) === 0) {
             $error = 'Parameter comment must start with a capital letter';
             $phpcsFile->addWarning($error, $param['tag'], 'ParamCommentNotCapital');
         }
         $lastChar = substr($param['comment'], -1);
         if ($lastChar !== '.') {
             $error = 'Parameter comment must end with a full stop';
             $phpcsFile->addWarning($error, $param['tag'], 'ParamCommentFullStop');
         }
     }
     //end foreach
     $realNames = array();
     foreach ($realParams as $realParam) {
         $realNames[] = $realParam['name'];
     }
     // Report missing comments.
     $diff = array_diff($realNames, $foundParams);
     foreach ($diff as $neededParam) {
         $error = 'Doc comment for parameter "%s" missing';
         $data = array($neededParam);
         $phpcsFile->addWarning($error, $commentStart, 'MissingParamTag', $data);
     }
 }
 /**
  * Called to process class member vars.
  *
  * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  * @param int                  $stackPtr  The position of the current token
  *                                        in the stack passed in $tokens.
  *
  * @return void
  */
 public function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     $commentToken = array(T_COMMENT, T_DOC_COMMENT_CLOSE_TAG);
     $commentEnd = $phpcsFile->findPrevious($commentToken, $stackPtr);
     if ($commentEnd === false) {
         $phpcsFile->addError('Missing member variable doc comment', $stackPtr, 'Missing');
         return;
     }
     if ($tokens[$commentEnd]['code'] === T_COMMENT) {
         $phpcsFile->addError('You must use "/**" style comments for a member variable comment', $stackPtr, 'WrongStyle');
         return;
     } else {
         if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG) {
             $phpcsFile->addError('Missing member variable doc comment', $stackPtr, 'Missing');
             return;
         } else {
             // Make sure the comment we have found belongs to us.
             $commentFor = $phpcsFile->findNext(array(T_VARIABLE, T_CLASS, T_INTERFACE), $commentEnd + 1);
             if ($commentFor !== $stackPtr) {
                 $phpcsFile->addError('Missing member variable doc comment', $stackPtr, 'Missing');
                 return;
             }
         }
     }
     $commentStart = $tokens[$commentEnd]['comment_opener'];
     $foundVar = null;
     foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
         if ($tokens[$tag]['content'] === '@var') {
             if ($foundVar !== null) {
                 $error = 'Only one @var tag is allowed in a member variable comment';
                 $phpcsFile->addError($error, $tag, 'DuplicateVar');
             } else {
                 $foundVar = $tag;
             }
         } else {
             if ($tokens[$tag]['content'] === '@see') {
                 // Make sure the tag isn't empty.
                 $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $tag, $commentEnd);
                 if ($string === false || $tokens[$string]['line'] !== $tokens[$tag]['line']) {
                     $error = 'Content missing for @see tag in member variable comment';
                     $phpcsFile->addError($error, $tag, 'EmptySees');
                 }
             } else {
                 //ONGR We allow other property tags.
                 //                $error = '%s tag is not allowed in member variable comment';
                 //                $data  = array($tokens[$tag]['content']);
                 //                $phpcsFile->addWarning($error, $tag, 'TagNotAllowed', $data);
             }
         }
         //end if
     }
     //end foreach
     // The @var tag is the only one we require.
     if ($foundVar === null) {
         $error = 'Missing @var tag in member variable comment';
         $phpcsFile->addError($error, $commentEnd, 'MissingVar');
         return;
     }
     $firstTag = $tokens[$commentStart]['comment_tags'][0];
     if ($foundVar !== null && $tokens[$firstTag]['content'] !== '@var') {
         $error = 'The @var tag must be the first tag in a member variable comment';
         $phpcsFile->addError($error, $foundVar, 'VarOrder');
     }
     // Make sure the tag isn't empty and has the correct padding.
     $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $foundVar, $commentEnd);
     if ($string === false || $tokens[$string]['line'] !== $tokens[$foundVar]['line']) {
         $error = 'Content missing for @var tag in member variable comment';
         $phpcsFile->addError($error, $foundVar, 'EmptyVar');
         return;
     }
     $varType = $tokens[$foundVar + 2]['content'];
     //
     $suggestedType = PHP_CodeSniffer::suggestType($varType);
     if ($varType === 'bool') {
         $varType = 'boolean';
     } elseif ($varType === 'int') {
         $varType = 'integer';
     }
     if ($varType !== $suggestedType && strpos($varType, $suggestedType . ' ') === false) {
         $error = 'Expected "%s" but found "%s" for @var tag in member variable comment';
         $data = array($suggestedType, $varType);
         $phpcsFile->addError($error, $foundVar + 2, 'IncorrectVarType', $data);
     }
     //Ongr
     $comment = trim(preg_replace('/^array\\(\\s*([^\\s^=^>]*)(\\s*=>\\s*(.*))?\\s*\\)/i', '', $varType, 1, $count));
     if (!$count) {
         $space = strpos($comment, ' ');
         if ($space === false) {
             return;
         }
         $comment = substr($comment, $space + 1);
     }
     if ($comment === '') {
         return;
     }
     if (substr($comment, 0, 1) == '$') {
         $this->currentFile->addError('Class field docs should not contain field name', $foundVar);
         return;
     }
     if (!in_array(substr($comment, -1, 1), ['.', '?', '!'])) {
         $this->currentFile->addError('Variable comments must end in full-stops, exclamation marks, or question marks', $foundVar, 'VariableComment');
     }
     $firstLetter = substr($comment, 0, 1);
     if (strtoupper($firstLetter) !== $firstLetter) {
         $this->currentFile->addError('Variable comments must must start with a capital letter', $foundVar, 'VariableComment');
     }
 }
 /**
  * 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->currentFile->getMethodParameters($this->_functionToken);
     $params = $this->commentParser->getParams();
     $foundParams = array();
     if (empty($params) === false) {
         // Parameters must appear immediately after the comment.
         if ($params[0]->getOrder() !== 2) {
             $error = 'Parameters must appear immediately after the comment';
             $errorPos = $params[0]->getLine() + $commentStart;
             $this->currentFile->addError($error, $errorPos, 'SpacingBeforeParams');
         }
         $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() !== ' ') {
                 $error = 'Expected 1 space before variable type';
                 $this->currentFile->addError($error, $errorPos, 'SpacingBeforeParamType');
             }
             $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 ]';
             // Variable must be one of the supported standard type.
             $typeNames = explode('|', $param->getType());
             foreach ($typeNames as $typeName) {
                 $suggestedName = PHP_CodeSniffer::suggestType($typeName);
                 if ($typeName !== $suggestedName) {
                     $error = 'Expected "%s"; found "%s" for %s at position %s';
                     $data = array($suggestedName, $typeName, $paramName, $pos);
                     $this->currentFile->addError($error, $errorPos, 'IncorrectParamVarName', $data);
                 }
                 //end if
             }
             //end foreach
             // 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 = 'ParamNameNoMatch';
                     $data = array($paramName, $realName, $pos);
                     $error = 'Doc comment for var %s does not match ';
                     if (strtolower($paramName) === strtolower($realName)) {
                         $error .= 'case of ';
                         $code = 'ParamNameNoCaseMatch';
                     }
                     $error .= 'actual variable name %s at position %s';
                     $this->currentFile->addError($error, $errorPos, $code, $data);
                 }
             } else {
                 // We must have an extra parameter comment.
                 $error = 'Superfluous doc comment at position ' . $pos;
                 $this->currentFile->addError($error, $errorPos, 'ExtraParamComment');
             }
             if ($param->getVarName() === '') {
                 $error = 'Missing parameter name at position ' . $pos;
                 $this->currentFile->addError($error, $errorPos, 'MissingParamName');
             }
             if ($param->getType() === '') {
                 $error = 'Missing type at position ' . $pos;
                 $this->currentFile->addError($error, $errorPos, 'MissingParamType');
             }
             if ($paramComment !== '') {
                 // Param comments must start with a capital letter
                 $firstChar = $paramComment[0];
                 if (preg_match('|[A-Z]|', $firstChar) === 0) {
                     $error = 'Param comment must start with a capital letter';
                     $this->currentFile->addError($error, $errorPos, 'ParamCommentNotCapital');
                 }
             }
             $previousParam = $param;
         }
         //end foreach
         if ($spaceBeforeVar !== 1 && $spaceBeforeVar !== 10000 && $spaceBeforeComment !== 10000) {
             $error = 'Expected 1 space after the longest type';
             $this->currentFile->addError($error, $longestType, 'SpacingAfterLongType');
         }
         if ($spaceBeforeComment !== 1 && $spaceBeforeComment !== 10000) {
             $error = 'Expected 1 space after the longest variable name';
             $this->currentFile->addError($error, $longestVar, 'SpacingAfterLongName');
         }
     }
     //end if
     $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;
         }
         $error = 'Doc comment for "%s" missing';
         $data = array($neededParam);
         $this->currentFile->addError($error, $errorPos, 'MissingParamTag', $data);
     }
 }
 /**
  * Process the function parameter comments
  *
  * @param  integer $commentStart The position in the stack where the comment started
  * @param  integer $commentEnd   The position in the stack where the comment ended
  * @return void
  */
 protected function _processParams($commentStart, $commentEnd)
 {
     $realParams = $this->_currentFile->getMethodParameters($this->_functionToken);
     $params = $this->_commentParser->getParams();
     $foundParams = array();
     if (empty($params) === false) {
         $isSpecialMethod = ($this->_methodName === '__construct' or $this->_methodName === '__destruct');
         if (substr_count($params[count($params) - 1]->getWhitespaceAfter(), $this->_currentFile->eolChar) !== 1 and $isSpecialMethod === false) {
             $errorPos = $params[count($params) - 1]->getLine() + $commentStart;
             $this->_currentFile->addError("No empty line after last parameter comment allowed", $errorPos + 1, 'EmptyLineLastParameterFunctionComment');
         }
         // Parameters must appear immediately after the comment
         if ($params[0]->getOrder() !== 2) {
             $errorPos = $params[0]->getLine() + $commentStart;
             $this->_currentFile->addError("Parameters must appear immediately after the comment", $errorPos, 'ParameterAfterCommentFunctionComment');
         }
         $previousParam = null;
         $spaceBeforeVar = 10000;
         $spaceBeforeComment = 10000;
         $longestType = 0;
         $longestVar = 0;
         if (count($this->_commentParser->getThrows()) !== 0) {
             $isSpecialMethod = false;
         }
         foreach ($params as $param) {
             $paramComment = trim($param->getComment());
             $errorPos = $param->getLine() + $commentStart;
             if ($isSpecialMethod === true and $param->getWhitespaceBeforeType() !== ' ') {
                 $this->_currentFile->addError("Expected 1 space before variable type", $errorPos, 'OneSpaceVariableFunctionComment');
             }
             $spaceCount = substr_count($param->getWhitespaceBeforeVarName(), ' ');
             if ($spaceCount < $spaceBeforeVar) {
                 $spaceBeforeVar = $spaceCount;
                 $longestType = $errorPos;
             }
             $spaceCount = substr_count($param->getWhitespaceBeforeComment(), ' ');
             if ($spaceCount < $spaceBeforeComment and $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';
                 // Check to see if the parameters align properly
                 if ($param->alignsVariableWith($previousParam) === false) {
                     $this->_currentFile->addError("The variable names for parameters {$previousName} (" . ($pos - 1) . ") and {$paramName} ({$pos}) do not align", $errorPos, 'VariablesNamesNotAlignFunctionComment');
                 }
                 if ($param->alignsCommentWith($previousParam) === false) {
                     $this->_currentFile->addError("The comments for parameters {$previousName} (" . ($pos - 1) . ") and {$paramName} ({$pos}) do not align", $errorPos, 'CommentsNotAlignFunctionComment');
                 }
             }
             // Variable must be one of the supported standard type
             $typeNames = explode('|', $param->getType());
             foreach ($typeNames as $typeName) {
                 $suggestedName = PHP_CodeSniffer::suggestType($typeName);
                 if ($typeName !== $suggestedName) {
                     $this->_currentFile->addError("Expected {$suggestedName} found {$typeName} for {$paramName} at position {$pos}", $errorPos, 'ExpectedFoundFunctionComment');
                     continue;
                 }
                 if (count($typeNames) !== 1) {
                     continue;
                 }
                 // Check type hint for array and custom type
                 $suggestedTypeHint = '';
                 if (strpos($suggestedName, 'array') !== false) {
                     $suggestedTypeHint = 'array';
                 } else {
                     if (in_array($typeName, PHP_CodeSniffer::$allowedTypes) === false) {
                         $suggestedTypeHint = $suggestedName;
                     }
                 }
                 if ($suggestedTypeHint !== '' and isset($realParams[$pos - 1]) === true) {
                     $typeHint = $realParams[$pos - 1]['type_hint'];
                     if ($typeHint === '') {
                         $this->_currentFile->addError("Type hint {$suggestedTypeHint} missing for {$paramName} at position {$pos}", $commentEnd + 2, 'TypehintMissingFunctionComment');
                     } else {
                         if ($typeHint !== $suggestedTypeHint) {
                             $this->_currentFile->addError("Expected type hint {$suggestedTypeHint} found {$typeHint} for {$paramName} at position {$pos}", $commentEnd + 2, 'ExpectedTypehintFoundFunctionComment');
                         }
                     }
                 } else {
                     if ($suggestedTypeHint === '' and isset($realParams[$pos - 1]) === true) {
                         $typeHint = $realParams[$pos - 1]['type_hint'];
                         if ($typeHint !== '') {
                             $this->_currentFile->addError("Unknown type hint {$typeHint} found for {$paramName} at position {$pos}", $commentEnd + 2, 'UnknowTypehintFoundFunctionComment');
                         }
                     }
                 }
             }
             // 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 !== $param->getVarName()) {
                     $this->_currentFile->addError("Doc comment var {$paramName} does not match actual variable name {$realName} at position {$pos}", $errorPos, 'DoccommentNotMatchFunctionComment');
                 }
             } else {
                 // We must have an extra parameter comment
                 $this->_currentFile->addError("Superfluous doc comment at position {$pos}", $errorPos, 'SuperfluousDoccommentFunctionComment');
             }
             if ($param->getVarName() === '') {
                 $this->_currentFile->addError("Missing parameter name at position {$pos}", $errorPos, 'MissingParameterFunctionComment');
             }
             if ($param->getType() === '') {
                 $this->_currentFile->addError("Missing type at position {$pos}", $errorPos, 'MissingTypeFunctionComment');
             }
             if ($paramComment === '') {
                 $this->_currentFile->addError("Missing comment for param {$paramName} at position {$pos}", $errorPos, 'MissingCommentParamFunctionComment');
             } else {
                 // Check if optional params include (Optional) within their description
                 $functionBegin = $this->_currentFile->findNext(array(T_FUNCTION), $commentStart);
                 $functionName = $this->_currentFile->findNext(array(T_STRING), $functionBegin);
                 $openBracket = $this->_tokens[$functionBegin]['parenthesis_opener'];
                 $closeBracket = $this->_tokens[$functionBegin]['parenthesis_closer'];
                 $nextParam = $this->_currentFile->findNext(T_VARIABLE, $openBracket + 1, $closeBracket);
                 while ($nextParam !== false) {
                     $nextToken = $this->_currentFile->findNext(T_WHITESPACE, $nextParam + 1, $closeBracket + 1, true);
                     if ($nextToken === false and $this->_tokens[$nextParam + 1]['code'] === T_CLOSE_PARENTHESIS) {
                         break;
                     }
                     $nextCode = $this->_tokens[$nextToken]['code'];
                     $arg = $this->_tokens[$nextParam]['content'];
                     if ($nextCode === T_EQUAL and $paramName === $arg) {
                         if (substr($paramComment, 0, 11) !== '(Optional) ') {
                             $this->_currentFile->addError("Le commentaire pour le parametre {$paramName} doit commencer avec \"(Optional)\"", $errorPos, 'OptionalParamStartFunctionComment');
                         }
                     }
                     $nextParam = $this->_currentFile->findNext(T_VARIABLE, $nextParam + 1, $closeBracket);
                 }
             }
             $previousParam = $param;
         }
         if ($spaceBeforeVar !== 1 and $spaceBeforeVar !== 10000 and $spaceBeforeComment !== 10000) {
             $this->_currentFile->addError("1 espace attendu après la définition du type pour les paramètres", $longestType, 'OneSpaceLongestTypeFunctionComment');
         }
         if ($spaceBeforeComment !== 1 and $spaceBeforeComment !== 10000) {
             $this->_currentFile->addError("1 espace attendu après le nom de la variable pour les paramètres", $longestVar, 'OneSpaceLongestTypeFunctionComment');
         }
     }
     $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;
         }
         $this->_currentFile->addError("Commentaire de fonction manquant ({$neededParam})", $errorPos, 'DoccommentMissingFunctionComment');
     }
 }
Ejemplo n.º 14
0
 /**
  * Called to process class member vars.
  *
  * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  *
  * @param int                  $stackPtr  The position of the current token in the stack passed in $tokens.
  *
  * @return void
  *
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     $commentToken = array(T_COMMENT, T_DOC_COMMENT_CLOSE_TAG);
     $commentEnd = $phpcsFile->findPrevious($commentToken, $stackPtr);
     if ($commentEnd === false) {
         $phpcsFile->addError('Missing member variable doc comment', $stackPtr, 'Missing');
         return;
     }
     // Make sure the comment we have found belongs to us.
     $commentFor = $phpcsFile->findNext(array(T_VARIABLE, T_CLASS, T_INTERFACE, T_FUNCTION), $commentEnd + 1);
     if ($commentFor !== $stackPtr) {
         $phpcsFile->addError('Missing member variable doc comment', $stackPtr, 'Missing');
         return;
     }
     if ($tokens[$commentEnd]['code'] === T_COMMENT) {
         $phpcsFile->addError('Member variable doc comment must be doc block', $commentEnd, 'NotDocBlock');
         return;
     } elseif ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG) {
         return;
     }
     $commentStart = $tokens[$commentEnd]['comment_opener'];
     $comment = strtolower($phpcsFile->getTokensAsString($commentStart, $commentEnd - $commentStart));
     // Accept inheriting of comments to be sufficient.
     if (strpos($comment, '@inheritdoc') !== false) {
         return;
     }
     // Add well known types phpcs does not know about.
     $previous = PHP_CodeSniffer::$allowedTypes;
     PHP_CodeSniffer::$allowedTypes[] = 'int';
     PHP_CodeSniffer::$allowedTypes[] = 'bool';
     $foundVar = null;
     foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
         if ($tokens[$tag]['content'] === '@var') {
             if ($foundVar !== null) {
                 $phpcsFile->addError('Only one @var tag is allowed in a member variable comment', $tag, 'DuplicateVar');
             } else {
                 $foundVar = $tag;
             }
         } elseif ($tokens[$tag]['content'] === '@see') {
             // Make sure the tag isn't empty.
             $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $tag, $commentEnd);
             if ($string === false || $tokens[$string]['line'] !== $tokens[$tag]['line']) {
                 $error = 'Content missing for @see tag in member variable comment';
                 $phpcsFile->addError($error, $tag, 'EmptySees');
             }
         } elseif ($tokens[$tag]['content'] === '@deprecated') {
             $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $tag, $commentEnd);
             if ($string === false || $tokens[$string]['line'] !== $tokens[$tag]['line']) {
                 $error = 'Content missing for @deprecated tag in member variable comment';
                 $phpcsFile->addError($error, $tag, 'EmptyDeprecated');
             }
         }
     }
     // The @var tag is the only one we require.
     if ($foundVar === null) {
         $error = 'Missing @var tag in member variable comment';
         $phpcsFile->addError($error, $commentEnd, 'MissingVar');
         return;
     }
     $firstTag = $tokens[$commentStart]['comment_tags'][0];
     if ($foundVar !== null && $tokens[$firstTag]['content'] !== '@var') {
         $error = 'The @var tag must be the first tag in a member variable comment';
         $phpcsFile->addError($error, $foundVar, 'VarOrder');
     }
     // Make sure the tag isn't empty and has the correct padding.
     $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $foundVar, $commentEnd);
     if ($string === false || $tokens[$string]['line'] !== $tokens[$foundVar]['line']) {
         $error = 'Content missing for @var tag in member variable comment';
         $phpcsFile->addError($error, $foundVar, 'EmptyVar');
         return;
     }
     $varType = $tokens[$foundVar + 2]['content'];
     $suggestedType = PHP_CodeSniffer::suggestType($varType);
     if ($varType !== $suggestedType) {
         $error = 'Expected "%s" but found "%s" for @var tag in member variable comment';
         $data = array($suggestedType, $varType);
         $phpcsFile->addError($error, $foundVar + 2, 'IncorrectVarType', $data);
     }
     PHP_CodeSniffer::$allowedTypes = $previous;
     $this->checkShortComment($phpcsFile, $commentStart, $commentEnd);
 }
 /**
  * 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->currentFile->getMethodParameters($this->_functionToken);
     $params = $this->commentParser->getParams();
     $foundParams = array();
     if (empty($params) === false) {
         if (substr_count($params[count($params) - 1]->getWhitespaceAfter(), $this->currentFile->eolChar) !== 2) {
             $error = 'Last parameter comment requires a blank newline after it';
             $errorPos = $params[count($params) - 1]->getLine() + $commentStart;
             $this->currentFile->addError($error, $errorPos, 'SpacingAfterParams');
         }
         // Parameters must appear immediately after the comment.
         if ($params[0]->getOrder() !== 2) {
             $error = 'Parameters must appear immediately after the comment';
             $errorPos = $params[0]->getLine() + $commentStart;
             $this->currentFile->addError($error, $errorPos, 'SpacingBeforeParams');
         }
         $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() !== ' ') {
                 $error = 'Expected 1 space before variable type';
                 $this->currentFile->addError($error, $errorPos, 'SpacingBeforeParamType');
             }
             $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';
                 // Check to see if the parameters align properly.
                 if ($param->alignsVariableWith($previousParam) === false) {
                     $error = 'The variable names for parameters %s (%s) and %s (%s) do not align';
                     $data = array($previousName, $pos - 1, $paramName, $pos);
                     $this->currentFile->addError($error, $errorPos, 'ParameterNamesNotAligned', $data);
                 }
                 if ($param->alignsCommentWith($previousParam) === false) {
                     $error = 'The comments for parameters %s (%s) and %s (%s) do not align';
                     $data = array($previousName, $pos - 1, $paramName, $pos);
                     $this->currentFile->addError($error, $errorPos, 'ParameterCommentsNotAligned', $data);
                 }
             }
             // Variable must be one of the supported standard type.
             $typeNames = explode('|', $param->getType());
             foreach ($typeNames as $typeName) {
                 $suggestedName = PHP_CodeSniffer::suggestType($typeName);
                 if ($typeName !== $suggestedName) {
                     $error = 'Expected "%s"; found "%s" for %s at position %s';
                     $data = array($suggestedName, $typeName, $paramName, $pos);
                     $this->currentFile->addError($error, $errorPos, 'IncorrectParamVarName', $data);
                 } else {
                     if (count($typeNames) === 1) {
                         // Check type hint for array and custom type.
                         $suggestedTypeHint = '';
                         if (strpos($suggestedName, 'array') !== false) {
                             $suggestedTypeHint = 'array';
                         } else {
                             if (in_array($typeName, PHP_CodeSniffer::$allowedTypes) === false) {
                                 $suggestedTypeHint = $suggestedName;
                             }
                         }
                         if ($suggestedTypeHint !== '' && isset($realParams[$pos - 1]) === true) {
                             $typeHint = $realParams[$pos - 1]['type_hint'];
                             if ($typeHint === '') {
                                 $error = 'Type hint "%s" missing for %s at position %s';
                                 $data = array($suggestedTypeHint, $paramName, $pos);
                                 $this->currentFile->addError($error, $commentEnd + 2, 'TypeHintMissing', $data);
                             } else {
                                 if ($typeHint !== $suggestedTypeHint) {
                                     $error = 'Expected type hint "%s"; found "%s" for %s at position %s';
                                     $data = array($suggestedTypeHint, $typeHint, $paramName, $pos);
                                     $this->currentFile->addError($error, $commentEnd + 2, 'IncorrectTypeHint', $data);
                                 }
                             }
                         } else {
                             if ($suggestedTypeHint === '' && isset($realParams[$pos - 1]) === true) {
                                 $typeHint = $realParams[$pos - 1]['type_hint'];
                                 if ($typeHint !== '') {
                                     $error = 'Unknown type hint "%s" found for %s at position %s';
                                     $data = array($typeHint, $paramName, $pos);
                                     $this->currentFile->addError($error, $commentEnd + 2, 'InvalidTypeHint', $data);
                                 }
                             }
                         }
                     }
                 }
                 //end if
             }
             //end foreach
             // 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 = 'ParamNameNoMatch';
                     $data = array($paramName, $realName, $pos);
                     $error = 'Doc comment for var %s does not match ';
                     if (strtolower($paramName) === strtolower($realName)) {
                         $error .= 'case of ';
                         $code = 'ParamNameNoCaseMatch';
                     }
                     $error .= 'actual variable name %s at position %s';
                     $this->currentFile->addError($error, $errorPos, $code, $data);
                 }
             } else {
                 if (substr($paramName, -4) !== ',...') {
                     // We must have an extra parameter comment.
                     $error = 'Superfluous doc comment at position ' . $pos;
                     $this->currentFile->addError($error, $errorPos, 'ExtraParamComment');
                 }
             }
             if ($param->getVarName() === '') {
                 $error = 'Missing parameter name at position ' . $pos;
                 $this->currentFile->addError($error, $errorPos, 'MissingParamName');
             }
             if ($param->getType() === '') {
                 $error = 'Missing type at position ' . $pos;
                 $this->currentFile->addError($error, $errorPos, 'MissingParamType');
             }
             if ($paramComment === '') {
                 $error = 'Missing comment for param "%s" at position %s';
                 $data = array($paramName, $pos);
                 $this->currentFile->addError($error, $errorPos, 'MissingParamComment', $data);
             } else {
                 // Param comments must start with a capital letter and
                 // end with the full stop.
                 $firstChar = $paramComment[0];
                 if (preg_match('|[A-Z]|', $firstChar) === 0) {
                     $error = 'Param comment must start with a capital letter';
                     $this->currentFile->addError($error, $errorPos, 'ParamCommentNotCapital');
                 }
                 $lastChar = $paramComment[strlen($paramComment) - 1];
                 if ($lastChar !== '.') {
                     $error = 'Param comment must end with a full stop';
                     $this->currentFile->addError($error, $errorPos, 'ParamCommentFullStop');
                 }
             }
             $previousParam = $param;
         }
         //end foreach
         if ($spaceBeforeVar !== 1 && $spaceBeforeVar !== 10000 && $spaceBeforeComment !== 10000) {
             $error = 'Expected 1 space after the longest type';
             $this->currentFile->addError($error, $longestType, 'SpacingAfterLongType');
         }
         if ($spaceBeforeComment !== 1 && $spaceBeforeComment !== 10000) {
             $error = 'Expected 1 space after the longest variable name';
             $this->currentFile->addError($error, $longestVar, 'SpacingAfterLongName');
         }
     }
     //end if
     $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;
         }
         $error = 'Doc comment for "%s" missing';
         $data = array($neededParam);
         $this->currentFile->addError($error, $errorPos, 'MissingParamTag', $data);
     }
 }
 /**
  * Process the return comment of this function comment.
  *
  * @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 processReturn($commentStart, $commentEnd)
 {
     // Skip constructor and destructor.
     $className = '';
     if ($this->_classToken !== null) {
         $className = $this->currentFile->getDeclarationName($this->_classToken);
         $className = strtolower(ltrim($className, '_'));
     }
     $methodName = strtolower(ltrim($this->_methodName, '_'));
     if ($methodName === '') {
         $methodName = $this->_methodName;
     }
     $isSpecialMethod = $this->_methodName === '__construct' || $this->_methodName === '__destruct';
     $return = $this->commentParser->getReturn();
     if ($isSpecialMethod === false && $methodName !== $className) {
         if ($return !== null) {
             $tagOrder = $this->commentParser->getTagOrders();
             $index = array_keys($tagOrder, 'return');
             $errorPos = $commentStart + $return->getLine();
             $content = trim($return->getRawContent());
             if (count($index) > 1) {
                 $error = 'Only 1 @return tag is allowed in function comment';
                 $this->currentFile->addError($error, $errorPos, 'DuplicateReturn');
                 return;
             }
             $since = array_keys($tagOrder, 'since');
             if (count($since) === 1 && $this->_tagIndex !== 0) {
                 $this->_tagIndex++;
                 if ($index[0] !== $this->_tagIndex) {
                     $error = 'The @return tag is in the wrong order; the tag follows @see (if used)';
                     $this->currentFile->addError($error, $errorPos, 'ReturnOrder');
                 }
             }
             if (empty($content) === true) {
                 $error = 'Return type missing for @return tag in function comment';
                 $this->currentFile->addError($error, $errorPos, 'MissingReturnType');
             } else {
                 // Check return type (can be multiple, separated by '|').
                 $typeNames = explode('|', $content);
                 $suggestedNames = array();
                 foreach ($typeNames as $i => $typeName) {
                     $suggestedName = PHP_CodeSniffer::suggestType($typeName);
                     if (in_array($suggestedName, $suggestedNames) === false) {
                         $suggestedNames[] = $suggestedName;
                     }
                 }
                 $suggestedType = implode('|', $suggestedNames);
                 if ($content !== $suggestedType) {
                     $error = 'Function return type "%s" is invalid';
                     $data = array($content);
                     $this->currentFile->addError($error, $errorPos, 'InvalidReturn', $data);
                 }
                 $tokens = $this->currentFile->getTokens();
                 // If the return type is void, make sure there is
                 // no return statement in the function.
                 if ($content === 'void') {
                     if (isset($tokens[$this->_functionToken]['scope_closer']) === true) {
                         $endToken = $tokens[$this->_functionToken]['scope_closer'];
                         $tokens = $this->currentFile->getTokens();
                         for ($returnToken = $this->_functionToken; $returnToken < $endToken; $returnToken++) {
                             if ($tokens[$returnToken]['code'] === T_CLOSURE) {
                                 $returnToken = $tokens[$returnToken]['scope_closer'];
                                 continue;
                             }
                             if ($tokens[$returnToken]['code'] === T_RETURN) {
                                 break;
                             }
                         }
                         if ($returnToken !== $endToken) {
                             // If the function is not returning anything, just
                             // exiting, then there is no problem.
                             $semicolon = $this->currentFile->findNext(T_WHITESPACE, $returnToken + 1, null, true);
                             if ($tokens[$semicolon]['code'] !== T_SEMICOLON) {
                                 $error = 'Function return type is void, but function contains return statement';
                                 $this->currentFile->addError($error, $errorPos, 'InvalidReturnVoid');
                             }
                         }
                     }
                 } else {
                     if ($content !== 'mixed') {
                         // If return type is not void, there needs to be a
                         // returns statement somewhere in the function that
                         // returns something.
                         if (isset($tokens[$this->_functionToken]['scope_closer']) === true) {
                             $endToken = $tokens[$this->_functionToken]['scope_closer'];
                             $returnToken = $this->currentFile->findNext(T_RETURN, $this->_functionToken, $endToken);
                             if ($returnToken === false) {
                                 $error = 'Function return type is not void, but function has no return statement';
                                 $this->currentFile->addError($error, $errorPos, 'InvalidNoReturn');
                             } else {
                                 $semicolon = $this->currentFile->findNext(T_WHITESPACE, $returnToken + 1, null, true);
                                 if ($tokens[$semicolon]['code'] === T_SEMICOLON) {
                                     $error = 'Function return type is not void, but function is returning void here';
                                     $this->currentFile->addError($error, $returnToken, 'InvalidReturnNotVoid');
                                 }
                             }
                         }
                     }
                 }
                 //end if
                 $spacing = substr_count($return->getWhitespaceBeforeValue(), ' ');
                 if ($spacing !== 1) {
                     $error = '@return tag indented incorrectly; expected 1 space but found %s';
                     $data = array($spacing);
                     $this->currentFile->addError($error, $errorPos, 'ReturnIndent', $data);
                 }
             }
             //end if
         } else {
             $error = 'Missing @return tag in function comment';
             $this->currentFile->addError($error, $commentEnd, 'MissingReturn');
         }
         //end if
     } else {
         // No return tag for constructor and destructor.
         if ($return !== null) {
             $errorPos = $commentStart + $return->getLine();
             $error = '@return tag is not required for constructor and destructor';
             $this->currentFile->addError($error, $errorPos, 'ReturnNotRequired');
         }
     }
     //end if
 }
 /**
  * Process the function parameter comments
  *
  * @param  integer $commentStart The position in the stack where the comment started
  * @param  integer $commentEnd   The position in the stack where the comment ended
  * @return void
  */
 protected function _processParams($commentStart, $commentEnd)
 {
     $realParams = $this->_currentFile->getMethodParameters($this->_functionToken);
     $params = $this->_commentParser->getParams();
     $foundParams = array();
     if (empty($params) === false) {
         $isSpecialMethod = ($this->_methodName === '__construct' or $this->_methodName === '__destruct');
         if (substr_count($params[count($params) - 1]->getWhitespaceAfter(), $this->_currentFile->eolChar) !== 1 and $isSpecialMethod === false) {
             $error = 'No empty line after last parameter comment allowed';
             $errorPos = $params[count($params) - 1]->getLine() + $commentStart;
             $this->_currentFile->addError($error, $errorPos + 1);
         }
         // Parameters must appear immediately after the comment
         if ($params[0]->getOrder() !== 2) {
             $error = 'Parameters must appear immediately after the comment';
             $errorPos = $params[0]->getLine() + $commentStart;
             $this->_currentFile->addError($error, $errorPos);
         }
         $previousParam = null;
         $spaceBeforeVar = 10000;
         $spaceBeforeComment = 10000;
         $longestType = 0;
         $longestVar = 0;
         if (count($this->_commentParser->getThrows()) !== 0) {
             $isSpecialMethod = false;
         }
         foreach ($params as $param) {
             $paramComment = trim($param->getComment());
             $errorPos = $param->getLine() + $commentStart;
             // Make sure that there is only one or two space before the var type
             if ($isSpecialMethod === false and $param->getWhitespaceBeforeType() !== '  ') {
                 $error = 'Expected 2 spaces before variable type';
                 $this->_currentFile->addError($error, $errorPos);
             }
             if ($isSpecialMethod === true and $param->getWhitespaceBeforeType() !== ' ') {
                 $error = 'Expected 1 space before variable type';
                 $this->_currentFile->addError($error, $errorPos);
             }
             $spaceCount = substr_count($param->getWhitespaceBeforeVarName(), ' ');
             if ($spaceCount < $spaceBeforeVar) {
                 $spaceBeforeVar = $spaceCount;
                 $longestType = $errorPos;
             }
             $spaceCount = substr_count($param->getWhitespaceBeforeComment(), ' ');
             if ($spaceCount < $spaceBeforeComment and $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';
                 // Check to see if the parameters align properly
                 if ($param->alignsVariableWith($previousParam) === false) {
                     $error = 'The variable names for parameters ' . $previousName . ' (' . ($pos - 1) . ') and ' . $paramName . ' (' . $pos . ') do not align';
                     $this->_currentFile->addError($error, $errorPos);
                 }
                 if ($param->alignsCommentWith($previousParam) === false) {
                     $error = 'The comments for parameters ' . $previousName . ' (' . ($pos - 1) . ') and ' . $paramName . ' (' . $pos . ') do not align';
                     $this->_currentFile->addError($error, $errorPos);
                 }
             }
             // Variable must be one of the supported standard type
             $typeNames = explode('|', $param->getType());
             foreach ($typeNames as $typeName) {
                 $suggestedName = PHP_CodeSniffer::suggestType($typeName);
                 if ($typeName !== $suggestedName) {
                     $error = "Expected \"{$suggestedName}\"; found \"{$typeName}\" for {$paramName} at position {$pos}";
                     $this->_currentFile->addError($error, $errorPos);
                     continue;
                 }
                 if (count($typeNames) !== 1) {
                     continue;
                 }
                 // Check type hint for array and custom type
                 $suggestedTypeHint = '';
                 if (strpos($suggestedName, 'array') !== false) {
                     $suggestedTypeHint = 'array';
                 } else {
                     if (in_array($typeName, PHP_CodeSniffer::$allowedTypes) === false) {
                         $suggestedTypeHint = $suggestedName;
                     }
                 }
                 if ($suggestedTypeHint !== '' and isset($realParams[$pos - 1]) === true) {
                     $typeHint = $realParams[$pos - 1]['type_hint'];
                     if ($typeHint === '') {
                         $error = "Type hint \"{$suggestedTypeHint}\" missing for {$paramName} at position {$pos}";
                         $this->_currentFile->addError($error, $commentEnd + 2);
                     } else {
                         if ($typeHint !== $suggestedTypeHint) {
                             $error = "Expected type hint \"{$suggestedTypeHint}\"; found \"{$typeHint}\"" . " for {$paramName} at position {$pos}";
                             $this->_currentFile->addError($error, $commentEnd + 2);
                         }
                     }
                 } else {
                     if ($suggestedTypeHint === '' and isset($realParams[$pos - 1]) === true) {
                         $typeHint = $realParams[$pos - 1]['type_hint'];
                         if ($typeHint !== '') {
                             $error = "Unknown type hint \"{$typeHint}\" found for {$paramName} at position {$pos}";
                             $this->_currentFile->addError($error, $commentEnd + 2);
                         }
                     }
                 }
             }
             // 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 !== $param->getVarName()) {
                     $error = 'Doc comment var "' . $paramName;
                     $error .= '" does not match actual variable name "' . $realName;
                     $error .= '" at position ' . $pos;
                     $this->_currentFile->addError($error, $errorPos);
                 }
             } else {
                 // We must have an extra parameter comment
                 $error = 'Superfluous doc comment at position ' . $pos;
                 $this->_currentFile->addError($error, $errorPos);
             }
             if ($param->getVarName() === '') {
                 $error = 'Missing parameter name at position ' . $pos;
                 $this->_currentFile->addError($error, $errorPos);
             }
             if ($param->getType() === '') {
                 $error = 'Missing type at position ' . $pos;
                 $this->_currentFile->addError($error, $errorPos);
             }
             if ($paramComment === '') {
                 $error = 'Missing comment for param "' . $paramName . '" at position ' . $pos;
                 $this->_currentFile->addError($error, $errorPos);
             } else {
                 // Param comments must start with a capital letter
                 $firstChar = $paramComment[0];
                 if (preg_match('|[A-Z]|', $firstChar) === 0 and $firstChar !== '(') {
                     $error = 'Param comment must start with a capital letter';
                     $this->_currentFile->addError($error, $errorPos);
                 }
                 // Check if optional params include (Optional) within their description
                 $functionBegin = $this->_currentFile->findNext(array(T_FUNCTION), $commentStart);
                 $functionName = $this->_currentFile->findNext(array(T_STRING), $functionBegin);
                 $openBracket = $this->_tokens[$functionBegin]['parenthesis_opener'];
                 $closeBracket = $this->_tokens[$functionBegin]['parenthesis_closer'];
                 $nextParam = $this->_currentFile->findNext(T_VARIABLE, $openBracket + 1, $closeBracket);
                 while ($nextParam !== false) {
                     $nextToken = $this->_currentFile->findNext(T_WHITESPACE, $nextParam + 1, $closeBracket + 1, true);
                     if ($nextToken === false and $this->_tokens[$nextParam + 1]['code'] === T_CLOSE_PARENTHESIS) {
                         break;
                     }
                     $nextCode = $this->_tokens[$nextToken]['code'];
                     $arg = $this->_tokens[$nextParam]['content'];
                     if ($nextCode === T_EQUAL and $paramName === $arg) {
                         if (substr($paramComment, 0, 11) !== '(Optional) ') {
                             $error = "Optional param comment for '{$paramName}' must start with '(Optional)'";
                             $this->_currentFile->addError($error, $errorPos);
                         } else {
                             if (preg_match('|[A-Z]|', $paramComment[11]) === 0) {
                                 $error = 'Param comment must start with a capital letter';
                                 $this->_currentFile->addError($error, $errorPos);
                             }
                         }
                     }
                     $nextParam = $this->_currentFile->findNext(T_VARIABLE, $nextParam + 1, $closeBracket);
                 }
             }
             $previousParam = $param;
         }
         if ($spaceBeforeVar !== 1 and $spaceBeforeVar !== 10000 and $spaceBeforeComment !== 10000) {
             $error = 'Expected 1 space after the longest type';
             $this->_currentFile->addError($error, $longestType);
         }
         if ($spaceBeforeComment !== 1 and $spaceBeforeComment !== 10000) {
             $error = 'Expected 1 space after the longest variable name';
             $this->_currentFile->addError($error, $longestVar);
         }
     }
     $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;
         }
         $error = 'Doc comment for "' . $neededParam . '" missing';
         $this->_currentFile->addError($error, $errorPos);
     }
 }
 /**
  * PHP Codesniffer dislikes certain types like int and bool, we make them available here.
  *
  * @param string $typeName The type name to translate.
  *
  * @return array
  */
 protected function translateSuggestedType($typeName)
 {
     $suggestedName = PHP_CodeSniffer::suggestType($typeName);
     switch ($typeName) {
         case 'int':
         case 'bool':
             return $typeName;
         default:
     }
     return $suggestedName;
 }
Ejemplo n.º 19
0
 /**
  * Process the return comment of this function comment.
  *
  * @param PHP_CodeSniffer_File $phpcsFile    The file being scanned.
  * @param int                  $stackPtr     The position of the current token
  *                                           in the stack passed in $tokens.
  * @param int                  $commentStart The position in the stack where the comment started.
  *
  * @return void
  */
 protected function processReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart)
 {
     $tokens = $phpcsFile->getTokens();
     // Skip constructor and destructor.
     $className = '';
     foreach ($tokens[$stackPtr]['conditions'] as $condPtr => $condition) {
         if ($condition === T_CLASS || $condition === T_INTERFACE) {
             $className = $phpcsFile->getDeclarationName($condPtr);
             $className = strtolower(ltrim($className, '_'));
         }
     }
     $methodName = $phpcsFile->getDeclarationName($stackPtr);
     $isSpecialMethod = $methodName === '__construct' || $methodName === '__destruct';
     if ($methodName !== '_') {
         $methodName = strtolower(ltrim($methodName, '_'));
     }
     $return = null;
     foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
         if ($tokens[$tag]['content'] === '@return') {
             if ($return !== null) {
                 $error = 'Only 1 @return tag is allowed in a function comment';
                 $phpcsFile->addError($error, $tag, 'DuplicateReturn');
                 return;
             }
             $return = $tag;
         }
     }
     //  find out if this function actually returns anything
     $functionHasReturn = false;
     if (isset($tokens[$stackPtr]['scope_closer']) === true) {
         $endToken = $tokens[$stackPtr]['scope_closer'];
         for ($returnToken = $stackPtr; $returnToken < $endToken; $returnToken++) {
             if ($tokens[$returnToken]['code'] === T_CLOSURE) {
                 $returnToken = $tokens[$returnToken]['scope_closer'];
                 continue;
             }
             if ($tokens[$returnToken]['code'] === T_RETURN) {
                 $functionHasReturn = true;
                 break;
             }
         }
     }
     if ($isSpecialMethod === false && $methodName !== $className) {
         if ($return !== null) {
             $content = $tokens[$return + 2]['content'];
             if (empty($content) === true || $tokens[$return + 2]['code'] !== T_DOC_COMMENT_STRING) {
                 if ($functionHasReturn) {
                     $error = 'Return type missing for @return tag in function comment';
                     $phpcsFile->addError($error, $return, 'MissingReturnType');
                 } else {
                     $error = 'Invalid @return tag, function does not return anything';
                     $phpcsFile->addError($error, $return, 'MissingReturnType');
                 }
             } else {
                 // Check return type (can be multiple, separated by '|').
                 $typeNames = explode('|', $content);
                 $suggestedNames = array();
                 foreach ($typeNames as $i => $typeName) {
                     $suggestedName = PHP_CodeSniffer::suggestType($typeName);
                     if (in_array($suggestedName, $suggestedNames) === false) {
                         $suggestedNames[] = $suggestedName;
                     }
                 }
                 $suggestedType = implode('|', $suggestedNames);
                 if ($content !== $suggestedType) {
                     $error = 'Function return type "%s" is invalid';
                     $error = 'Expected "%s" but found "%s" for function return type';
                     $data = array($suggestedType, $content);
                     $phpcsFile->addError($error, $return, 'InvalidReturn', $data);
                 }
                 // If the return type is void, make sure there is
                 // no return statement in the function.
                 if ($content === 'void') {
                     if (isset($tokens[$stackPtr]['scope_closer']) === true) {
                         $endToken = $tokens[$stackPtr]['scope_closer'];
                         for ($returnToken = $stackPtr; $returnToken < $endToken; $returnToken++) {
                             if ($tokens[$returnToken]['code'] === T_CLOSURE) {
                                 $returnToken = $tokens[$returnToken]['scope_closer'];
                                 continue;
                             }
                             if ($tokens[$returnToken]['code'] === T_RETURN) {
                                 break;
                             }
                         }
                         if ($returnToken !== $endToken) {
                             // If the function is not returning anything, just
                             // exiting, then there is no problem.
                             $semicolon = $phpcsFile->findNext(T_WHITESPACE, $returnToken + 1, null, true);
                             if ($tokens[$semicolon]['code'] !== T_SEMICOLON) {
                                 $error = 'Function return type is void, but function contains return statement';
                                 $phpcsFile->addError($error, $return, 'InvalidReturnVoid');
                             }
                         }
                     }
                     //end if
                 } else {
                     if ($content !== 'mixed') {
                         // If return type is not void, there needs to be a return statement
                         // somewhere in the function that returns something.
                         if (isset($tokens[$stackPtr]['scope_closer']) === true) {
                             $endToken = $tokens[$stackPtr]['scope_closer'];
                             $returnToken = $phpcsFile->findNext(T_RETURN, $stackPtr, $endToken);
                             if ($returnToken === false) {
                                 $error = 'Invalid @return tag, function has no return statement';
                                 $phpcsFile->addError($error, $return, 'InvalidNoReturn');
                             } else {
                                 $semicolon = $phpcsFile->findNext(T_WHITESPACE, $returnToken + 1, null, true);
                                 if ($tokens[$semicolon]['code'] === T_SEMICOLON) {
                                     $error = 'Superflous or Invalid @return tag, function returns void';
                                     $phpcsFile->addError($error, $returnToken, 'InvalidReturnNotVoid');
                                 }
                             }
                         }
                     }
                 }
                 //end if
             }
             //end if
         } else {
             if ($functionHasReturn) {
                 $content = $tokens[$return + 2]['content'];
                 if (trim($content) != '') {
                     $error = 'Missing @return tag in function comment';
                     $phpcsFile->addError($error, $tokens[$commentStart]['comment_closer'], 'MissingReturn');
                 }
             }
         }
         //end if
     } else {
         // No return tag for constructor and destructor.
         if ($return !== null) {
             $error = '@return tag is not required for constructor and destructor';
             $phpcsFile->addError($error, $return, 'ReturnNotRequired');
         }
     }
     //end if
 }
 /**
  * Process the return comment of this function comment.
  *
  * Extends PEAR.Commenting.FunctionComment.processReturn to exclude @return tag requirements for constructors and destructors and
  * to enforce alignment of the doc blocks.
  *
  * @param   PHP_CodeSniffer_File  $phpcsFile     The file being scanned.
  * @param   integer               $stackPtr      The position of the current token in the stack passed in $tokens.
  * @param   integer               $commentStart  The position in the stack where the comment started.
  *
  * @return  void
  *
  * @todo    Reinstate the check on the alignment of the tag
  */
 protected function processReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart)
 {
     $tokens = $phpcsFile->getTokens();
     $methodName = $phpcsFile->getDeclarationName($stackPtr);
     $isSpecialMethod = $methodName === '__construct' || $methodName === '__destruct';
     $return = null;
     foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
         if ($tokens[$tag]['content'] === '@return') {
             // Joomla Standard - Constructors and destructors should not have a @return tag
             if ($isSpecialMethod) {
                 $error = 'Constructor and destructor comments must not have a @return tag';
                 $phpcsFile->addError($error, $tag, 'UselessReturn');
                 return;
             } elseif ($return !== null) {
                 $error = 'Only 1 @return tag is allowed in a function comment';
                 $phpcsFile->addError($error, $tag, 'DuplicateReturn');
                 return;
             }
             $return = $tag;
         }
     }
     if ($isSpecialMethod === true) {
         return;
     }
     if ($return !== null) {
         $content = $tokens[$return + 2]['content'];
         if (empty($content) === true || $tokens[$return + 2]['code'] !== T_DOC_COMMENT_STRING) {
             $error = 'Return type missing for @return tag in function comment';
             $phpcsFile->addError($error, $return, 'MissingReturnType');
         } else {
             // Check return type (can have multiple return types separated by '|').
             $typeNames = explode('|', $content);
             $suggestedNames = array();
             foreach ($typeNames as $i => $typeName) {
                 $suggestedName = PHP_CodeSniffer::suggestType($typeName);
                 if (in_array($suggestedName, $suggestedNames) === false) {
                     $suggestedNames[] = $suggestedName;
                 }
             }
             $suggestedType = implode('|', $suggestedNames);
             if ($content !== $suggestedType) {
                 $error = 'Expected "%s" but found "%s" for function return type';
                 $data = array($suggestedType, $content);
                 $fix = $phpcsFile->addFixableError($error, $return, 'InvalidReturn', $data);
                 if ($fix === true) {
                     $phpcsFile->fixer->replaceToken($return + 2, $suggestedType);
                 }
             }
             /*
              * If return type is not void, there needs to be a return statement
              * somewhere in the function that returns something.
              * Skip this check for mixed return types.
              */
             if (!in_array($content, array('void', 'mixed'))) {
                 if (isset($tokens[$stackPtr]['scope_closer']) === true) {
                     $endToken = $tokens[$stackPtr]['scope_closer'];
                     $returnToken = $phpcsFile->findNext(array(T_RETURN, T_YIELD), $stackPtr, $endToken);
                     if ($returnToken === false) {
                         $error = 'Function return type is not void, but function has no return statement';
                         $phpcsFile->addError($error, $return, 'InvalidNoReturn');
                     } else {
                         $semicolon = $phpcsFile->findNext(T_WHITESPACE, $returnToken + 1, null, true);
                         if ($tokens[$semicolon]['code'] === T_SEMICOLON) {
                             $error = 'Function return type is not void, but function is returning void here';
                             $phpcsFile->addError($error, $returnToken, 'InvalidReturnNotVoid');
                         }
                     }
                 }
             }
         }
     } else {
         $error = 'Missing @return tag in function comment';
         $phpcsFile->addError($error, $tokens[$commentStart]['comment_closer'], 'MissingReturn');
     }
 }