/**
  * Process the function parameter comments.
  *
  * @param int $commentStart The position in the stack where
  *                          the comment started.
  *
  * @return void
  */
 protected function processParams($commentStart)
 {
     $realParams = $this->currentFile->getMethodParameters($this->_functionToken);
     $params = $this->commentParser->getParams();
     $foundParams = array();
     if (empty($params) === false) {
         $lastParm = count($params) - 1;
         if (substr_count($params[$lastParm]->getWhitespaceAfter(), $this->currentFile->eolChar) !== 2) {
             $error = 'Last parameter comment requires a blank newline after it';
             $errorPos = $params[$lastParm]->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;
         /** @var PHP_CodeSniffer_CommentParser_ParameterElement $param */
         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 (strlen($param->getWhitespaceBeforeType()) < 1) {
                 $error = 'Expected at least 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);
                 }
             }
             //end if
             // 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 === '') {
                 $error = 'Missing comment for param "%s" at position %s';
                 $data = array($paramName, $pos);
                 $this->currentFile->addError($error, $errorPos, 'MissingParamComment', $data);
             }
             $previousParam = $param;
         }
         if ($spaceBeforeVar > 4 && $spaceBeforeVar !== 10000 && $spaceBeforeComment !== 10000) {
             $error = 'Expected no more spaces than 1 tab after the longest type';
             $this->currentFile->addError($error, $longestType, 'SpacingAfterLongType');
         }
         if ($spaceBeforeComment > 4 && $spaceBeforeComment !== 10000) {
             $error = 'Expected no more spaces than 1 tab after the longest variable name';
             $this->currentFile->addError($error, $longestVar, 'SpacingAfterLongName');
         }
     }
     $realNames = array();
     foreach ($realParams as $realParam) {
         $realNames[] = $realParam['name'];
     }
     // Report and 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 int $commentStart The position in the stack where
  *                          the comment started.
  * @param int $commentEnd   The position in the stack where
  *                          the comment ended.
  *
  * @return void
  */
 protected function processParams($commentStart, $commentEnd)
 {
     $realParams = $this->helper->getCurrentFile()->getMethodParameters($this->_functionToken);
     $params = $this->commentParser->getParams();
     $foundParams = [];
     if (empty($params) === false) {
         $subStrCount = substr_count($params[count($params) - 1]->getWhitespaceAfter(), $this->helper->getCurrentFile()->eolChar);
         if ($subStrCount !== 2) {
             $errorPos = $params[count($params) - 1]->getLine() + $commentStart;
             $this->helper->addMessage($errorPos, M2_Sniffs_Annotations_Helper::SPACING_AFTER_PARAMS);
         }
         // Parameters must appear immediately after the comment.
         if ($params[0]->getOrder() !== 2) {
             $errorPos = $params[0]->getLine() + $commentStart;
             $this->helper->addMessage($errorPos, M2_Sniffs_Annotations_Helper::SPACING_BEFORE_PARAMS);
         }
         $previousParam = null;
         $spaceBeforeVar = 10000;
         $spaceBeforeComment = 10000;
         $longestType = 0;
         $longestVar = 0;
         foreach ($params as $param) {
             $paramComment = trim($param->getComment());
             $errorPos = $param->getLine() + $commentStart;
             // Make sure that there is only one space before the var type.
             if ($param->getWhitespaceBeforeType() !== ' ') {
                 $this->helper->addMessage($errorPos, M2_Sniffs_Annotations_Helper::SPACING_BEFORE_PARAM_TYPE);
             }
             $spaceCount = substr_count($param->getWhitespaceBeforeVarName(), ' ');
             if ($spaceCount < $spaceBeforeVar) {
                 $spaceBeforeVar = $spaceCount;
                 $longestType = $errorPos;
             }
             $spaceCount = substr_count($param->getWhitespaceBeforeComment(), ' ');
             if ($spaceCount < $spaceBeforeComment && $paramComment !== '') {
                 $spaceBeforeComment = $spaceCount;
                 $longestVar = $errorPos;
             }
             // Make sure they are in the correct order, and have the correct name.
             $pos = $param->getPosition();
             $paramName = $param->getVarName() !== '' ? $param->getVarName() : '[ UNKNOWN ]';
             if ($previousParam !== null) {
                 $previousName = $previousParam->getVarName() !== '' ? $previousParam->getVarName() : 'UNKNOWN';
             }
             // Variable must be one of the supported standard type.
             $typeNames = explode('|', $param->getType());
             foreach ($typeNames as $typeName) {
                 $suggestedName = $this->helper->suggestType($typeName);
                 if ($typeName !== $suggestedName) {
                     $data = [$suggestedName, $typeName, $paramName, $pos];
                     $this->helper->addMessage($errorPos, M2_Sniffs_Annotations_Helper::INCORRECT_PARAM_VAR_NAME, $data);
                 } elseif ($this->helper->isAmbiguous($typeName, $matches)) {
                     // Warn about ambiguous types ie array or mixed
                     $data = [$matches[1], $paramName, ' at position ' . $pos . ' is NOT recommended'];
                     $this->helper->addMessage($commentEnd + 2, M2_Sniffs_Annotations_Helper::AMBIGUOUS_TYPE, $data);
                 } elseif (count($typeNames) === 1) {
                     // Check type hint for array and custom type.
                     $suggestedTypeHint = '';
                     if (strpos($suggestedName, 'array') !== false) {
                         $suggestedTypeHint = 'array';
                     } elseif (strpos($suggestedName, 'callable') !== false) {
                         $suggestedTypeHint = 'callable';
                     } elseif (in_array($typeName, $this->helper->getAllowedTypes()) === false) {
                         $suggestedTypeHint = $suggestedName;
                     } else {
                         $suggestedTypeHint = $this->helper->suggestType($typeName);
                     }
                     if ($suggestedTypeHint !== '' && isset($realParams[$pos - 1]) === true) {
                         $typeHint = $realParams[$pos - 1]['type_hint'];
                         if ($typeHint === '') {
                             $data = [$suggestedTypeHint, $paramName, $pos];
                             $this->helper->addMessage($commentEnd + 2, M2_Sniffs_Annotations_Helper::TYPE_HINT_MISSING, $data);
                         } elseif ($typeHint !== $suggestedTypeHint) {
                             $data = [$suggestedTypeHint, $typeHint, $paramName, $pos];
                             $this->helper->addMessage($commentEnd + 2, M2_Sniffs_Annotations_Helper::INCORRECT_TYPE_HINT, $data);
                         }
                     } elseif ($suggestedTypeHint === '' && isset($realParams[$pos - 1]) === true) {
                         $typeHint = $realParams[$pos - 1]['type_hint'];
                         if ($typeHint !== '') {
                             $data = [$typeHint, $paramName, $pos];
                             $this->helper->addMessage($commentEnd + 2, M2_Sniffs_Annotations_Helper::INVALID_TYPE_HINT, $data);
                         }
                     }
                 }
             }
             // Make sure the names of the parameter comment matches the
             // actual parameter.
             if (isset($realParams[$pos - 1]) === true) {
                 $realName = $realParams[$pos - 1]['name'];
                 $foundParams[] = $realName;
                 // Append ampersand to name if passing by reference.
                 if ($realParams[$pos - 1]['pass_by_reference'] === true) {
                     $realName = '&' . $realName;
                 }
                 if ($realName !== $paramName) {
                     $code = M2_Sniffs_Annotations_Helper::PARAM_NAME_NO_MATCH;
                     $data = [$paramName, $realName, $pos];
                     if (strtolower($paramName) === strtolower($realName)) {
                         $code = M2_Sniffs_Annotations_Helper::PARAM_NAME_NO_CASE_MATCH;
                     }
                     $this->helper->addMessage($errorPos, $code, $data);
                 }
             } elseif (substr($paramName, -4) !== ',...') {
                 // We must have an extra parameter comment.
                 $this->helper->addMessage($errorPos, M2_Sniffs_Annotations_Helper::EXTRA_PARAM_COMMENT, [$pos]);
             }
             if ($param->getVarName() === '') {
                 $this->helper->addMessage($errorPos, M2_Sniffs_Annotations_Helper::MISSING_PARAM_NAME, [$pos]);
             }
             if ($param->getType() === '') {
                 $this->helper->addMessage($errorPos, M2_Sniffs_Annotations_Helper::MISSING_PARAM_TYPE, [$pos]);
             }
             if ($paramComment === '') {
                 $data = [$paramName, $pos];
                 $this->helper->addMessage($errorPos, M2_Sniffs_Annotations_Helper::MISSING_PARAM_COMMENT, $data);
             } else {
                 // Param comments must start with a capital letter and
                 // end with the full stop.
                 $firstChar = $paramComment[0];
                 if (preg_match('|\\p{Lu}|u', $firstChar) === 0) {
                     $this->helper->addMessage($errorPos, M2_Sniffs_Annotations_Helper::PARAM_COMMENT_NOT_CAPITAL);
                 }
                 $lastChar = $paramComment[strlen($paramComment) - 1];
                 if ($lastChar !== '.') {
                     $this->helper->addMessage($errorPos, M2_Sniffs_Annotations_Helper::PARAM_COMMENT_FULL_STOP);
                 }
             }
             $previousParam = $param;
         }
         if ($spaceBeforeVar !== 1 && $spaceBeforeVar !== 10000 && $spaceBeforeComment !== 10000) {
             $this->helper->addMessage($longestType, M2_Sniffs_Annotations_Helper::SPACING_AFTER_LONG_TYPE);
         }
         if ($spaceBeforeComment !== 1 && $spaceBeforeComment !== 10000) {
             $this->helper->addMessage($longestVar, M2_Sniffs_Annotations_Helper::SPACING_AFTER_LONG_NAME);
         }
     }
     $realNames = [];
     foreach ($realParams as $realParam) {
         $realNames[] = $realParam['name'];
     }
     // Report missing comments.
     $diff = array_diff($realNames, $foundParams);
     foreach ($diff as $neededParam) {
         if (count($params) !== 0) {
             $errorPos = $params[count($params) - 1]->getLine() + $commentStart;
         } else {
             $errorPos = $commentStart;
         }
         $data = [$neededParam];
         $this->helper->addMessage($errorPos, M2_Sniffs_Annotations_Helper::MISSING_PARAM_TAG, $data);
     }
 }
Esempio n. 3
0
 /**
  * {@inheritdoc}
  */
 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     if ($this->should_ignore_use($phpcsFile, $stackPtr) === true) {
         return;
     }
     $tokens = $phpcsFile->getTokens();
     $class_name_start = $phpcsFile->findNext(array(T_NS_SEPARATOR, T_STRING), $stackPtr + 1);
     $find = array(T_NS_SEPARATOR, T_STRING, T_WHITESPACE);
     $class_name_end = $phpcsFile->findNext($find, $stackPtr + 1, null, true);
     $aliasing_as_position = $phpcsFile->findNext(T_AS, $class_name_end, null, false, null, true);
     if ($aliasing_as_position !== false) {
         $alias_position = $phpcsFile->findNext(T_STRING, $aliasing_as_position, null, false, null, true);
         $class_name_short = $tokens[$alias_position]['content'];
         $class_name_full = $phpcsFile->getTokensAsString($class_name_start, $class_name_end - $class_name_start - 1);
     } else {
         $class_name_full = $phpcsFile->getTokensAsString($class_name_start, $class_name_end - $class_name_start);
         $class_name_short = $tokens[$class_name_end - 1]['content'];
     }
     $ok = false;
     // Checks in simple statements (new, instanceof and extends)
     foreach (array(T_INSTANCEOF, T_NEW, T_EXTENDS) as $keyword) {
         $old_simple_statement = $stackPtr;
         while (($simple_statement = $phpcsFile->findNext($keyword, $old_simple_statement + 1)) !== false) {
             $old_simple_statement = $simple_statement;
             $simple_class_name_start = $phpcsFile->findNext(array(T_NS_SEPARATOR, T_STRING), $simple_statement + 1);
             $simple_class_name_end = $phpcsFile->findNext($find, $simple_statement + 1, null, true);
             $simple_class_name = trim($phpcsFile->getTokensAsString($simple_class_name_start, $simple_class_name_end - $simple_class_name_start));
             $ok = $this->check($phpcsFile, $simple_class_name, $class_name_full, $class_name_short, $simple_statement) ? true : $ok;
         }
     }
     // Checks paamayim nekudotayim
     $old_paamayim_nekudotayim = $stackPtr;
     while (($paamayim_nekudotayim = $phpcsFile->findNext(T_PAAMAYIM_NEKUDOTAYIM, $old_paamayim_nekudotayim + 1)) !== false) {
         $old_paamayim_nekudotayim = $paamayim_nekudotayim;
         $paamayim_nekudotayim_class_name_start = $phpcsFile->findPrevious($find, $paamayim_nekudotayim - 1, null, true);
         $paamayim_nekudotayim_class_name_end = $paamayim_nekudotayim - 1;
         $paamayim_nekudotayim_class_name = trim($phpcsFile->getTokensAsString($paamayim_nekudotayim_class_name_start + 1, $paamayim_nekudotayim_class_name_end - $paamayim_nekudotayim_class_name_start));
         $ok = $this->check($phpcsFile, $paamayim_nekudotayim_class_name, $class_name_full, $class_name_short, $paamayim_nekudotayim) ? true : $ok;
     }
     // Checks in implements
     $old_implements = $stackPtr;
     while (($implements = $phpcsFile->findNext(T_IMPLEMENTS, $old_implements + 1)) !== false) {
         $old_implements = $implements;
         $old_implemented_class = $implements;
         while (($implemented_class = $phpcsFile->findNext(T_STRING, $old_implemented_class + 1, null, false, null, true)) !== false) {
             $old_implemented_class = $implemented_class;
             $implements_class_name_start = $phpcsFile->findNext(array(T_NS_SEPARATOR, T_STRING), $implemented_class - 1);
             $implements_class_name_end = $phpcsFile->findNext($find, $implemented_class - 1, null, true);
             $implements_class_name = trim($phpcsFile->getTokensAsString($implements_class_name_start, $implements_class_name_end - $implements_class_name_start));
             $ok = $this->check($phpcsFile, $implements_class_name, $class_name_full, $class_name_short, $implements) ? true : $ok;
         }
     }
     // Checks in type hinting
     $old_function_declaration = $stackPtr;
     while (($function_declaration = $phpcsFile->findNext(T_FUNCTION, $old_function_declaration + 1)) !== false) {
         $old_function_declaration = $function_declaration;
         // Check docblocks
         $find = array(T_COMMENT, T_DOC_COMMENT, T_CLASS, T_FUNCTION, T_OPEN_TAG);
         $comment_end = $phpcsFile->findPrevious($find, $function_declaration - 1);
         if ($comment_end !== false) {
             if (!$tokens[$comment_end]['code'] !== T_DOC_COMMENT) {
                 $comment_start = $phpcsFile->findPrevious(T_DOC_COMMENT, $comment_end - 1, null, true) + 1;
                 $comment = $phpcsFile->getTokensAsString($comment_start, $comment_end - $comment_start + 1);
                 try {
                     $comment_parser = new PHP_CodeSniffer_CommentParser_FunctionCommentParser($comment, $phpcsFile);
                     $comment_parser->parse();
                     // Check @param
                     foreach ($comment_parser->getParams() as $param) {
                         $type = $param->getType();
                         $types = explode('|', str_replace('[]', '', $type));
                         foreach ($types as $type) {
                             $ok = $this->check($phpcsFile, $type, $class_name_full, $class_name_short, $param->getLine() + $comment_start) ? true : $ok;
                         }
                     }
                     // Check @return
                     $return = $comment_parser->getReturn();
                     if ($return !== null) {
                         $type = $return->getValue();
                         $types = explode('|', str_replace('[]', '', $type));
                         foreach ($types as $type) {
                             $ok = $this->check($phpcsFile, $type, $class_name_full, $class_name_short, $return->getLine() + $comment_start) ? true : $ok;
                         }
                     }
                 } catch (PHP_CodeSniffer_CommentParser_ParserException $e) {
                     $line = $e->getLineWithinComment() + $comment_start;
                     $phpcsFile->addError($e->getMessage(), $line, 'FailedParse');
                 }
             }
         }
         // Check type hint
         $params = $phpcsFile->getMethodParameters($function_declaration);
         foreach ($params as $param) {
             $ok = $this->check($phpcsFile, $param['type_hint'], $class_name_full, $class_name_short, $function_declaration) ? true : $ok;
         }
     }
     if (!$ok) {
         $error = 'There must not be unused USE statements.';
         $phpcsFile->addError($error, $stackPtr, 'Unused');
     }
 }