本文整理汇总了PHP中PHP_CodeSniffer_File::getMethodParameters方法的典型用法代码示例。如果您正苦于以下问题:PHP PHP_CodeSniffer_File::getMethodParameters方法的具体用法?PHP PHP_CodeSniffer_File::getMethodParameters怎么用?PHP PHP_CodeSniffer_File::getMethodParameters使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PHP_CodeSniffer_File
的用法示例。
在下文中一共展示了PHP_CodeSniffer_File::getMethodParameters方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: process
/**
* Processes this test, when one of its tokens is encountered.
*
* @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 process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
foreach ($phpcsFile->getMethodParameters($stackPtr) as $param) {
if (in_array($param, $this->forbiddenParameterNames) === true) {
$error = "[PHP 5.4] {$param} is not a valid parameter name.";
$phpcsFile->addError($error, $stackPtr);
}
}
}
示例2: testDefaultValues
/**
* Verify default value parsing.
*
* @return void
*/
public function testDefaultValues()
{
$expected = array();
$expected[0] = array('name' => '$var1', 'default' => '1', 'pass_by_reference' => false, 'type_hint' => '');
$expected[1] = array('name' => '$var2', 'default' => "'value'", 'pass_by_reference' => false, 'type_hint' => '');
$start = $this->_phpcsFile->numTokens - 1;
$function = $this->_phpcsFile->findPrevious(T_COMMENT, $start, null, false, '/* testDefaultValues */');
$found = $this->_phpcsFile->getMethodParameters($function + 2);
$this->assertSame($expected, $found);
}
示例3: process
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$paramsInfo = $phpcsFile->getMethodParameters($stackPtr);
// Check each param to see if it has a type hint we need to change
foreach ($paramsInfo as $paramInfo) {
if (isset($paramInfo['type_hint'])) {
$classNamePtr = $paramInfo['type_hint'];
$className = $tokens[$classNamePtr]['content'];
$varPtr = $paramInfo['var'];
$this->_variableTypes->setVariableType($varPtr, $className, $phpcsFile, null, $stackPtr);
}
}
}
示例4: process
public function process(CodeSnifferFile $file, $stackPtr)
{
$methodName = $file->getDeclarationName($stackPtr);
if ($methodName !== '__construct' && strpos($methodName, 'set') !== 0) {
return;
}
$methodParameters = $file->getMethodParameters($stackPtr);
foreach ($methodParameters as $methodParameter) {
$this->verifyParameterName($file, $stackPtr, $methodName, $methodParameter);
}
$propertyAssignments = $this->getPropertyAssignments($file, $stackPtr);
foreach ($methodParameters as $methodParameter) {
$this->verifyPropertyName($file, $stackPtr, $methodName, $methodParameter, $propertyAssignments);
}
}
示例5: process
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$functionName = $phpcsFile->getDeclarationName($stackPtr);
if ($this->startsWith($functionName, $this->prefixes, $this->exclude)) {
$paramsQty = count($phpcsFile->getMethodParameters($stackPtr));
if ($paramsQty < self::PARAMS_QTY) {
$phpcsFile->addWarning('Plugin ' . $functionName . ' function must have at least two parameters.', $stackPtr);
}
$tokens = $phpcsFile->getTokens();
$hasReturn = false;
foreach ($tokens as $currToken) {
if ($currToken['code'] == T_RETURN && isset($currToken['conditions'][$stackPtr])) {
$hasReturn = true;
break;
}
}
if (!$hasReturn) {
$phpcsFile->addError('Plugin ' . $functionName . ' function must return value.', $stackPtr);
}
}
}
示例6: process
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
// Find the actual name of the class
if ($tokens[$stackPtr]['code'] == T_FUNCTION) {
// Get information about the function parameters
$paramsInfo = $phpcsFile->getMethodParameters($stackPtr);
// Check each param to see if it has a type hint we need to change
foreach ($paramsInfo as $paramInfo) {
if (isset($paramInfo['type_hint'])) {
$classNamePtr = $paramInfo['type_hint'];
$this->checkClassNamePtr($classNamePtr, $phpcsFile);
}
}
} else {
if ($tokens[$stackPtr]['code'] == T_PAAMAYIM_NEKUDOTAYIM) {
$classNamePtr = $phpcsFile->findPrevious(T_STRING, $stackPtr);
$this->checkClassNamePtr($classNamePtr, $phpcsFile);
} else {
$classNamePtr = $phpcsFile->findNext(T_STRING, $stackPtr);
$this->checkClassNamePtr($classNamePtr, $phpcsFile);
}
}
}
示例7: processParams
/**
* 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) {
// There must be an empty line before the parameter block.
if (substr_count($params[0]->getWhitespaceBefore(), $this->currentFile->eolChar) < 2) {
$error = 'There must be an empty line before the parameter block';
$errorPos = $params[0]->getLine() + $commentStart;
$this->currentFile->addError($error, $errorPos, 'SpacingBeforeParams');
}
$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');
}
$checkPos = 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');
}
// Make sure they are in the correct order,
// and have the correct name.
$pos = $param->getPosition();
$paramName = '[ UNKNOWN ]';
if ($param->getVarName() !== '') {
$paramName = $param->getVarName();
}
// Make sure the names of the parameter comment matches the
// actual parameter.
$matched = false;
// Parameter documentation can be ommitted for some parameters, so
// we have to search the rest for a match.
while (isset($realParams[$checkPos]) === true) {
$realName = $realParams[$checkPos]['name'];
$expectedParamName = $realName;
$isReference = $realParams[$checkPos]['pass_by_reference'];
// Append ampersand to name if passing by reference.
if ($isReference === true && substr($paramName, 0, 1) === '&') {
$expectedParamName = '&' . $realName;
}
if ($expectedParamName === $paramName) {
$matched = true;
break;
}
$checkPos++;
}
if ($matched === false && $paramName !== '...') {
if ($checkPos >= $pos) {
$code = 'ParamNameNoMatch';
$data = array($paramName, $realParams[$pos - 1]['name'], $pos);
$error = 'Doc comment for var %s does not match ';
if (strtolower($paramName) === strtolower($realParams[$pos - 1]['name'])) {
$error .= 'case of ';
$code = 'ParamNameNoCaseMatch';
}
$error .= 'actual variable name %s at position %s';
$this->currentFile->addError($error, $errorPos, $code, $data);
// Reset the parameter position to check for following
// parameters.
$checkPos = $pos - 1;
} else {
// We must have an extra parameter comment.
$error = 'Superfluous doc comment at position ' . $pos;
$this->currentFile->addError($error, $errorPos, 'ExtraParamComment');
}
//end if
}
//end if
$checkPos++;
if ($param->getVarName() === '') {
$error = 'Missing parameter name at position ' . $pos;
$this->currentFile->addError($error, $errorPos, 'MissingParamName');
}
if ($param->getType() === '') {
$error = 'Missing parameter type at position ' . $pos;
$this->currentFile->addError($error, $errorPos, 'MissingParamType');
}
if (in_array($param->getType(), array('unknown_type', '<type>', 'type')) === true) {
$error = 'Expected a valid @param data type, but found %s';
$data = array($param->getType());
$this->currentFile->addError($error, $errorPos, 'InvalidParamType', $data);
}
if (isset($this->invalidTypes[$param->getType()]) === true) {
$error = 'Invalid @param data type, expected %s but found %s';
$data = array($this->invalidTypes[$param->getType()], $param->getType());
//.........这里部分代码省略.........
示例8: _checkArguments
/**
* Checks the number of method arguments.
*
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
* @param int $stackPtr The position in the stack where
* the token was found.
* @param string $methodName The name of the method.
* @param int $countArgs Number of required arguments.
*
* @return void
*/
private function _checkArguments(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $methodName, $countArgs)
{
$methodArguments = $phpcsFile->getMethodParameters($stackPtr);
// Warning for non-public declaration
if ($countArgs !== count($methodArguments)) {
$error = sprintf('Wrong argument count of magic method "%s"! Must take exactly %d argument(s).', $methodName, $countArgs);
$phpcsFile->addError($error, $stackPtr);
}
}
示例9: 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[4]) === true) {
$error = 'Parameter comment must be on the next line';
$phpcsFile->addError($error, $tag + 2, 'ParamCommentNewLine');
}
$var = isset($matches[2]) ? $matches[2] : '';
$varLen = strlen($var);
if ($varLen > $maxVar) {
$maxVar = $varLen;
}
// 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);
if ($indent < 3) {
$error = 'Parameter comment indentation must be 3 spaces, found %s spaces';
$phpcsFile->addError($error, $i, 'ParamCommentIndentation', array($indent));
}
}
}
if ($comment == '') {
$error = 'Missing parameter comment';
$phpcsFile->addError($error, $tag, 'MissingParamComment');
$commentLines[] = array('comment' => '');
}
//end if
// Allow the "..." @param doc for a variable number of parameters.
if (isset($matches[2]) === false && $tokens[$tag + 2]['content'] !== '...') {
if ($tokens[$tag + 2]['content'][0] === '$' || $tokens[$tag + 2]['content'][0] === '&') {
$error = 'Missing parameter type';
$phpcsFile->addError($error, $tag, 'MissingParamType');
} 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();
$checkPos = 0;
foreach ($params as $pos => $param) {
// If the type is empty, the whole line is empty.
if ($param['type'] === '') {
continue;
}
if ($param['var'] === '') {
continue;
//.........这里部分代码省略.........
示例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)
{
$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 = '';
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];
// 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) {
$comment .= ' ' . $tokens[$i]['content'];
}
}
} else {
$error = 'Missing parameter comment';
$phpcsFile->addError($error, $tag, 'MissingParamComment');
}
} 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, 'type_space' => $typeSpace, 'var_space' => $varSpace);
}
//end foreach
$realParams = $phpcsFile->getMethodParameters($stackPtr);
$foundParams = array();
foreach ($params as $pos => $param) {
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->enabled === true) {
$content = $param['type'];
$content .= str_repeat(' ', $spaces);
$content .= $param['var'];
$content .= str_repeat(' ', $param['var_space']);
$content .= $param['comment'];
$phpcsFile->fixer->replaceToken($param['tag'] + 2, $content);
}
}
// Make sure the param name is correct.
if (isset($realParams[$pos]) === true) {
$realName = $realParams[$pos]['name'];
// Append ampersand to name if passing by reference.
if ($realParams[$pos]['pass_by_reference'] === true) {
$realName = '&' . $realName;
//.........这里部分代码省略.........
示例11: process
/**
* Processes this test, when one of its tokens is encountered.
*
* @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 process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];
// Skip broken function declarations.
if (isset($token['scope_opener']) === false || isset($token['parenthesis_opener']) === false) {
return;
}
$params = array();
foreach ($phpcsFile->getMethodParameters($stackPtr) as $param) {
$params[$param['name']] = $stackPtr;
}
$next = ++$token['scope_opener'];
$end = --$token['scope_closer'];
$foundContent = false;
for (; $next <= $end; ++$next) {
$token = $tokens[$next];
$code = $token['code'];
// Ignorable tokens.
if (in_array($code, PHP_CodeSniffer_Tokens::$emptyTokens) === true) {
continue;
}
if ($foundContent === false) {
// A throw statement as the first content indicates an interface method.
if ($code === T_THROW) {
return;
}
// A return statement as the first content indicates an interface method.
if ($code === T_RETURN) {
$tmp = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $next + 1, null, true);
if ($tmp === false) {
return;
}
// There is a return.
if ($tokens[$tmp]['code'] === T_SEMICOLON) {
return;
}
$tmp = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $tmp + 1, null, true);
if ($tmp !== false && $tokens[$tmp]['code'] === T_SEMICOLON) {
// There is a return <token>.
return;
}
}
//end if
}
//end if
$foundContent = true;
if ($code === T_VARIABLE && isset($params[$token['content']]) === true) {
unset($params[$token['content']]);
} else {
if ($code === T_DOLLAR) {
$nextToken = $phpcsFile->findNext(T_WHITESPACE, $next + 1, null, true);
if ($tokens[$nextToken]['code'] === T_OPEN_CURLY_BRACKET) {
$nextToken = $phpcsFile->findNext(T_WHITESPACE, $nextToken + 1, null, true);
if ($tokens[$nextToken]['code'] === T_STRING) {
$varContent = '$' . $tokens[$nextToken]['content'];
if (isset($params[$varContent]) === true) {
unset($params[$varContent]);
}
}
}
} else {
if ($code === T_DOUBLE_QUOTED_STRING || $code === T_START_HEREDOC || $code === T_START_NOWDOC) {
// Tokenize strings that can contain variables.
// Make sure the string is re-joined if it occurs over multiple lines.
$validTokens = array(T_HEREDOC, T_NOWDOC, T_END_HEREDOC, T_END_NOWDOC, T_DOUBLE_QUOTED_STRING);
$validTokens = array_merge($validTokens, PHP_CodeSniffer_Tokens::$emptyTokens);
$content = $token['content'];
for ($i = $next + 1; $i <= $end; $i++) {
if (in_array($tokens[$i]['code'], $validTokens) === true) {
$content .= $tokens[$i]['content'];
$next++;
} else {
break;
}
}
$stringTokens = token_get_all(sprintf('<?php %s;?>', $content));
foreach ($stringTokens as $stringPtr => $stringToken) {
if (is_array($stringToken) === false) {
continue;
}
$varContent = '';
if ($stringToken[0] === T_DOLLAR_OPEN_CURLY_BRACES) {
$varContent = '$' . $stringTokens[$stringPtr + 1][1];
} else {
if ($stringToken[0] === T_VARIABLE) {
$varContent = $stringToken[1];
}
}
if ($varContent !== '' && isset($params[$varContent]) === true) {
unset($params[$varContent]);
//.........这里部分代码省略.........
示例12: 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'];
//.........这里部分代码省略.........
示例13: 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 {
//.........这里部分代码省略.........
示例14: process
/**
* Processes this test, when one of its tokens is encountered.
*
* @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 process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];
// Skip broken function declarations.
if (isset($token['scope_opener']) === false || isset($token['parenthesis_opener']) === false) {
return;
}
$params = array();
foreach ($phpcsFile->getMethodParameters($stackPtr) as $param) {
$params[$param['name']] = $stackPtr;
}
$next = ++$token['scope_opener'];
$end = --$token['scope_closer'];
$emptyBody = true;
for (; $next <= $end; ++$next) {
$token = $tokens[$next];
$code = $token['code'];
// Ingorable tokens.
if (in_array($code, PHP_CodeSniffer_Tokens::$emptyTokens) === true) {
continue;
} else {
if ($code === T_THROW && $emptyBody === true) {
// Throw statement and an empty body indicate an interface method.
return;
} else {
if ($code === T_RETURN && $emptyBody === true) {
// Return statement and an empty body indicate an interface method.
$tmp = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $next + 1, null, true);
if ($tmp === false) {
return;
}
// There is a return.
if ($tokens[$tmp]['code'] === T_SEMICOLON) {
return;
}
$tmp = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $tmp + 1, null, true);
// There is a return <token>.
if ($tmp !== false && $tokens[$tmp]['code'] === T_SEMICOLON) {
return;
}
}
}
}
//end if
$emptyBody = false;
if ($code === T_VARIABLE && isset($params[$token['content']]) === true) {
unset($params[$token['content']]);
} else {
if ($code === T_DOUBLE_QUOTED_STRING || $code === T_HEREDOC) {
// Tokenize strings that can contain variables.
// Make sure the string is re-joined if it occurs over multiple lines.
$string = $token['content'];
for ($i = $next + 1; $i <= $end; $i++) {
if ($tokens[$i]['code'] === $code) {
$string .= $tokens[$i]['content'];
$next++;
}
}
$strTokens = token_get_all(sprintf('<?php %s;?>', $string));
foreach ($strTokens as $tok) {
if (is_array($tok) === false || $tok[0] !== T_VARIABLE) {
continue;
}
if (isset($params[$tok[1]]) === true) {
unset($params[$tok[1]]);
}
}
}
}
//end if
}
//end for
if ($emptyBody === false && count($params) > 0) {
foreach ($params as $paramName => $position) {
$error = 'The method parameter %s is never used';
$data = array($paramName);
$phpcsFile->addWarning($error, $position, 'Found', $data);
}
}
}
示例15: processParams
/**
* Process the function parameter comments.
*
* @param int $commentStart The position in the stack where
* the comment started.
*
* @return void
*/
protected function processParams($commentStart, $commentEnd, $tagElements)
{
$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($this->getReqPrefix('REQ.PHP.4.1.20') . $error, $errorPos);
}
// 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($this->getReqPrefix('REQ.PHP.4.1.21') . $error, $errorPos);
}
$previousParam = null;
$spaceBeforeVar = 10000;
$spaceBeforeComment = 10000;
$longestType = 0;
$longestVar = 0;
foreach ($params as $param) {
$paramComment = trim($param->getComment());
$errorPos = $param->getLine() + $commentStart;
// Check type
$r = $this->checkType($param->getType(), $this->allowedParamTypes, 'param');
if (true !== $r) {
$this->currentFile->addError($this->getReqPrefix('REQ.PHP.3.5.15') . $r, $errorPos);
}
// 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($this->getReqPrefix('REQ.PHP.4.1.22') . $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($this->getReqPrefix('REQ.PHP.4.1.23') . $error, $errorPos);
}
if ($param->alignsCommentWith($previousParam) === false) {
$error = 'The comments for parameters ' . $previousName . ' (' . ($pos - 1) . ') and ' . $paramName . ' (' . $pos . ') do not align';
$this->currentFile->addError($this->getReqPrefix('REQ.PHP.4.1.24') . $error, $errorPos);
}
}
//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 !== $param->getVarName()) {
$error = 'Doc comment var "' . $paramName;
$error .= '" does not match actual variable name "' . $realName;
$error .= '" at position ' . $pos;
$this->currentFile->addError($this->getReqPrefix('REQ.PHP.4.1.25') . $error, $errorPos);
}
if (isset($realParams[$pos - 1]['default']) && !preg_match('/ OPTIONAL$/Ss', $paramComment)) {
$this->currentFile->addError($this->getReqPrefix('REQ.PHP.3.5.18') . 'Переменная "' . $paramName . '" опциональная, но служебного тэга OPTIONAL ее комментарий не имеет', $errorPos);
}
} else {
// We must have an extra parameter comment.
$error = 'Superfluous doc comment at position ' . $pos;
$this->currentFile->addError($this->getReqPrefix('REQ.PHP.4.1.27') . $error, $errorPos);
}
if ($param->getVarName() === '') {
$error = 'Missing parameter name at position ' . $pos;
$this->currentFile->addError($this->getReqPrefix('REQ.PHP.4.1.26') . $error, $errorPos);
}
if ($param->getType() === '') {
$error = 'Missing type at position ' . $pos;
//.........这里部分代码省略.........