本文整理汇总了PHP中PHP_CodeSniffer::suggestType方法的典型用法代码示例。如果您正苦于以下问题:PHP PHP_CodeSniffer::suggestType方法的具体用法?PHP PHP_CodeSniffer::suggestType怎么用?PHP PHP_CodeSniffer::suggestType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PHP_CodeSniffer
的用法示例。
在下文中一共展示了PHP_CodeSniffer::suggestType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: processVar
/**
* 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
}
示例2: processParams
/**
* 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'];
//.........这里部分代码省略.........
示例3: processVar
/**
* 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');
}
}
示例4: processVar
/**
* 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
}
示例5: processParams
/**
* 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);
}
}
}
//.........这里部分代码省略.........
示例6: processMemberVar
/**
* 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);
}
}
示例7: suggestType
/**
* 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;
}
示例8: _processParams
/**
* 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
//.........这里部分代码省略.........
示例9: processVar
/**
* 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);
}
}
示例10: processParams
/**
* 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);
//.........这里部分代码省略.........
示例11: processMemberVar
/**
* 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;
}
//.........这里部分代码省略.........
示例12: processParams
/**
* 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');
}
//.........这里部分代码省略.........
示例13: _processParams
/**
* 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
//.........这里部分代码省略.........
示例14: processMemberVar
/**
* 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);
}
示例15: processParams
/**
* 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 {
//.........这里部分代码省略.........