View source
<?php
namespace Drupal\Sniffs\Commenting;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
class FunctionCommentSniff implements Sniff {
public static $invalidTypes = [
'Array' => 'array',
'array()' => 'array',
'[]' => 'array',
'boolean' => 'bool',
'Boolean' => 'bool',
'integer' => 'int',
'str' => 'string',
'stdClass' => 'object',
'\\stdClass' => 'object',
'number' => 'int',
'String' => 'string',
'type' => 'mixed',
'NULL' => 'null',
'FALSE' => 'false',
'TRUE' => 'true',
'Bool' => 'bool',
'Int' => 'int',
'Integer' => 'int',
'TRUEFALSE' => 'bool',
];
public $allowedTypes = [
'array',
'mixed',
'object',
'resource',
'callable',
];
public function register() {
return [
T_FUNCTION,
];
}
public function process(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile
->getTokens();
$find = Tokens::$methodPrefixes;
$find[] = T_WHITESPACE;
$commentEnd = $phpcsFile
->findPrevious($find, $stackPtr - 1, null, true);
$beforeCommentEnd = $phpcsFile
->findPrevious(Tokens::$emptyTokens, $commentEnd - 1, null, true);
if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG && $tokens[$commentEnd]['code'] !== T_COMMENT || $beforeCommentEnd !== false && $tokens[$beforeCommentEnd]['line'] === $tokens[$commentEnd]['line']) {
$fix = $phpcsFile
->addFixableError('Missing function doc comment', $stackPtr, 'Missing');
if ($fix === true) {
$before = $phpcsFile
->findNext(T_WHITESPACE, $commentEnd + 1, $stackPtr + 1, true);
$phpcsFile->fixer
->addContentBefore($before, "/**\n *\n */\n");
}
return;
}
if ($tokens[$commentEnd]['code'] === T_COMMENT) {
$fix = $phpcsFile
->addFixableError('You must use "/**" style comments for a function comment', $stackPtr, 'WrongStyle');
if ($fix === true) {
$phpcsFile->fixer
->beginChangeset();
$comment = '';
for ($i = $commentEnd; $tokens[$i]['code'] === T_COMMENT; $i--) {
$comment = ' *' . ltrim($tokens[$i]['content'], '/* ') . $comment;
$phpcsFile->fixer
->replaceToken($i, '');
}
$phpcsFile->fixer
->replaceToken($commentEnd, "/**\n" . rtrim($comment, "*/\n") . "\n */\n");
$phpcsFile->fixer
->endChangeset();
}
return;
}
$commentStart = $tokens[$commentEnd]['comment_opener'];
foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
if ($tokens[$tag]['content'] === '@file') {
$fix = $phpcsFile
->addFixableError('Missing function doc comment', $stackPtr, 'Missing');
if ($fix === true) {
$before = $phpcsFile
->findNext(T_WHITESPACE, $commentEnd + 1, $stackPtr + 1, true);
$phpcsFile->fixer
->addContentBefore($before, "/**\n *\n */\n");
}
return;
}
if ($tokens[$tag]['content'] === '@see') {
$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 function comment';
$phpcsFile
->addError($error, $tag, 'EmptySees');
}
}
}
if ($tokens[$commentEnd]['line'] !== $tokens[$stackPtr]['line'] - 1) {
$error = 'There must be no blank lines after the function comment';
$fix = $phpcsFile
->addFixableError($error, $commentEnd, 'SpacingAfter');
if ($fix === true) {
$phpcsFile->fixer
->replaceToken($commentEnd + 1, '');
}
}
$this
->processReturn($phpcsFile, $stackPtr, $commentStart);
$this
->processThrows($phpcsFile, $stackPtr, $commentStart);
$this
->processParams($phpcsFile, $stackPtr, $commentStart);
$this
->processSees($phpcsFile, $stackPtr, $commentStart);
}
protected function processReturn(File $phpcsFile, $stackPtr, $commentStart) {
$tokens = $phpcsFile
->getTokens();
$className = '';
foreach ($tokens[$stackPtr]['conditions'] as $condPtr => $condition) {
if ($condition === T_CLASS || $condition === T_INTERFACE) {
$className = $phpcsFile
->getDeclarationName($condPtr);
$className = strtolower(ltrim($className, '_'));
}
}
$methodName = $phpcsFile
->getDeclarationName($stackPtr);
$isSpecialMethod = $methodName === '__construct' || $methodName === '__destruct';
$methodName = strtolower(ltrim($methodName, '_'));
$return = null;
$end = $stackPtr;
foreach ($tokens[$commentStart]['comment_tags'] as $pos => $tag) {
if ($tokens[$tag]['content'] === '@return') {
if ($return !== null) {
$error = 'Only 1 @return tag is allowed in a function comment';
$phpcsFile
->addError($error, $tag, 'DuplicateReturn');
return;
}
$return = $tag;
if (isset($tokens[$commentStart]['comment_tags'][$pos + 1]) === true) {
$skipTags = [
'@code',
'@endcode',
];
$skipPos = $pos + 1;
while (isset($tokens[$commentStart]['comment_tags'][$skipPos]) === true && in_array($tokens[$commentStart]['comment_tags'][$skipPos], $skipTags) === true) {
$skipPos++;
}
$end = $tokens[$commentStart]['comment_tags'][$skipPos];
}
else {
$end = $tokens[$commentStart]['comment_closer'];
}
}
}
$type = null;
if ($isSpecialMethod === false) {
if ($return !== null) {
$type = trim($tokens[$return + 2]['content']);
if (empty($type) === true || $tokens[$return + 2]['code'] !== T_DOC_COMMENT_STRING) {
$error = 'Return type missing for @return tag in function comment';
$phpcsFile
->addError($error, $return, 'MissingReturnType');
}
else {
if (strpos($type, ' ') === false) {
$typeNames = explode('|', $type);
$suggestedNames = [];
$hasNull = false;
foreach ($typeNames as $i => $typeName) {
if (strtolower($typeName) === 'null') {
$hasNull = true;
}
$suggestedName = static::suggestType($typeName);
if (in_array($suggestedName, $suggestedNames) === false) {
$suggestedNames[] = $suggestedName;
}
}
$suggestedType = implode('|', $suggestedNames);
if ($type !== $suggestedType) {
$error = 'Expected "%s" but found "%s" for function return type';
$data = [
$suggestedType,
$type,
];
$fix = $phpcsFile
->addFixableError($error, $return, 'InvalidReturn', $data);
if ($fix === true) {
$content = $suggestedType;
$phpcsFile->fixer
->replaceToken($return + 2, $content);
}
}
if ($type === 'void') {
$error = 'If there is no return value for a function, there must not be a @return tag.';
$phpcsFile
->addError($error, $return, 'VoidReturn');
}
else {
if ($type !== 'mixed') {
if (isset($tokens[$stackPtr]['scope_closer']) === true) {
$endToken = $tokens[$stackPtr]['scope_closer'];
$foundReturnToken = false;
$searchStart = $stackPtr;
$foundNonVoidReturn = false;
do {
$returnToken = $phpcsFile
->findNext([
T_RETURN,
T_YIELD,
T_YIELD_FROM,
], $searchStart, $endToken);
if ($returnToken === false && $foundReturnToken === false) {
$error = '@return doc comment specified, but function has no return statement';
$phpcsFile
->addError($error, $return, 'InvalidNoReturn');
}
else {
if ($returnToken !== false) {
$foundReturnToken = true;
$semicolon = $phpcsFile
->findNext(T_WHITESPACE, $returnToken + 1, null, true);
if ($tokens[$semicolon]['code'] === T_SEMICOLON) {
if ($hasNull === false) {
$error = 'Function return type is not void, but function is returning void here';
$phpcsFile
->addError($error, $returnToken, 'InvalidReturnNotVoid');
}
}
else {
$foundNonVoidReturn = true;
}
$searchStart = $returnToken + 1;
}
}
} while ($returnToken !== false);
if ($foundNonVoidReturn === false && $foundReturnToken === true) {
$error = 'Function return type is not void, but function does not have a non-void return statement';
$phpcsFile
->addError($error, $return, 'InvalidReturnNotVoid');
}
}
}
}
}
}
$comment = '';
for ($i = $return + 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[] = [
'comment' => $tokens[$i]['content'],
'token' => $i,
'indent' => $indent,
];
if ($indent < 3) {
$error = 'Return comment indentation must be 3 spaces, found %s spaces';
$fix = $phpcsFile
->addFixableError($error, $i, 'ReturnCommentIndentation', [
$indent,
]);
if ($fix === true) {
$phpcsFile->fixer
->replaceToken($i - 1, ' ');
}
}
}
}
if (empty($commentLines[0]['indent']) === false && $commentLines[0]['indent'] > 3) {
$error = 'Return comment indentation must be 3 spaces, found %s spaces';
$fix = $phpcsFile
->addFixableError($error, $commentLines[0]['token'] - 1, 'ReturnCommentIndentation', [
$commentLines[0]['indent'],
]);
if ($fix === true) {
$phpcsFile->fixer
->replaceToken($commentLines[0]['token'] - 1, ' ');
}
}
if ($comment === '' && $type !== '$this' && $type !== 'static') {
if (strpos($type, ' ') !== false) {
$error = 'Description for the @return value must be on the next line';
}
else {
$error = 'Description for the @return value is missing';
}
$phpcsFile
->addError($error, $return, 'MissingReturnComment');
}
else {
if (strpos($type, ' ') !== false) {
if (preg_match('/^([^\\s]+)[\\s]+(\\$[^\\s]+)[\\s]*$/', $type, $matches) === 1) {
$error = 'Return type must not contain variable name "%s"';
$data = [
$matches[2],
];
$fix = $phpcsFile
->addFixableError($error, $return + 2, 'ReturnVarName', $data);
if ($fix === true) {
$phpcsFile->fixer
->replaceToken($return + 2, $matches[1]);
}
}
else {
$error = 'Return type "%s" must not contain spaces';
$data = [
$type,
];
$phpcsFile
->addError($error, $return, 'ReturnTypeSpaces', $data);
}
}
}
}
}
}
protected function processThrows(File $phpcsFile, $stackPtr, $commentStart) {
$tokens = $phpcsFile
->getTokens();
foreach ($tokens[$commentStart]['comment_tags'] as $pos => $tag) {
if ($tokens[$tag]['content'] !== '@throws') {
continue;
}
if ($tokens[$tag + 2]['code'] !== T_DOC_COMMENT_STRING) {
$error = 'Exception type missing for @throws tag in function comment';
$phpcsFile
->addError($error, $tag, 'InvalidThrows');
}
else {
if (isset($tokens[$commentStart]['comment_tags'][$pos + 1]) === true) {
$end = $tokens[$commentStart]['comment_tags'][$pos + 1];
}
else {
$end = $tokens[$commentStart]['comment_closer'];
}
$comment = '';
$throwStart = null;
for ($i = $tag + 3; $i < $end; $i++) {
if ($tokens[$i]['code'] === T_DOC_COMMENT_STRING) {
if ($throwStart === null) {
$throwStart = $i;
}
$indent = 0;
if ($tokens[$i - 1]['code'] === T_DOC_COMMENT_WHITESPACE) {
$indent = strlen($tokens[$i - 1]['content']);
}
$comment .= ' ' . $tokens[$i]['content'];
if ($indent < 3) {
$error = 'Throws comment indentation must be 3 spaces, found %s spaces';
$phpcsFile
->addError($error, $i, 'TrhowsCommentIndentation', [
$indent,
]);
}
}
}
$comment = trim($comment);
if ($comment === '') {
if (str_word_count($tokens[$tag + 2]['content'], 0, '\\_') > 1) {
$error = '@throws comment must be on the next line';
$phpcsFile
->addError($error, $tag, 'ThrowsComment');
}
return;
}
$firstChar = $comment[0];
if (strtoupper($firstChar) !== $firstChar) {
$error = '@throws tag comment must start with a capital letter';
$phpcsFile
->addError($error, $throwStart, 'ThrowsNotCapital');
}
$lastChar = substr($comment, -1);
if (in_array($lastChar, [
'.',
'!',
'?',
]) === false) {
$error = '@throws tag comment must end with a full stop';
$phpcsFile
->addError($error, $throwStart, 'ThrowsNoFullStop');
}
}
}
}
protected function processParams(File $phpcsFile, $stackPtr, $commentStart) {
$tokens = $phpcsFile
->getTokens();
$params = [];
$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 = [];
if ($tokens[$tag + 2]['code'] === T_DOC_COMMENT_STRING) {
$matches = [];
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 && ($typeLen > 0 || preg_match('/[^\\s]+[\\s]+[^\\s]+/', $matches[4]) === 1)) {
$comment = $matches[4];
$error = 'Parameter comment must be on the next line';
$fix = $phpcsFile
->addFixableError($error, $tag + 2, 'ParamCommentNewLine');
if ($fix === true) {
$parts = $matches;
unset($parts[0]);
$parts[3] = "\n * ";
$phpcsFile->fixer
->replaceToken($tag + 2, implode('', $parts));
}
}
if (isset($matches[2]) === true) {
$var = $matches[2];
}
else {
$var = '';
}
if (substr($var, -1) === '.') {
$error = 'Doc comment parameter name "%s" must not end with a dot';
$fix = $phpcsFile
->addFixableError($error, $tag + 2, 'ParamNameDot', [
$var,
]);
if ($fix === true) {
$content = $type . ' ' . substr($var, 0, -1);
$phpcsFile->fixer
->replaceToken($tag + 2, $content);
}
continue;
}
$varLen = strlen($var);
if ($varLen > $maxVar) {
$maxVar = $varLen;
}
if (isset($tokens[$commentStart]['comment_tags'][$pos + 1]) === true) {
$skipTags = [
'@code',
'@endcode',
'@link',
];
$skipPos = $pos;
while (isset($tokens[$commentStart]['comment_tags'][$skipPos + 1]) === true) {
$skipPos++;
if (in_array($tokens[$tokens[$commentStart]['comment_tags'][$skipPos]]['content'], $skipTags) === false && $tokens[$tokens[$commentStart]['comment_tags'][$skipPos]]['column'] === $tokens[$tag]['column']) {
break;
}
}
if ($tokens[$tokens[$commentStart]['comment_tags'][$skipPos]]['column'] === $tokens[$tag]['column'] + 2) {
$end = $tokens[$commentStart]['comment_closer'];
}
else {
$end = $tokens[$commentStart]['comment_tags'][$skipPos];
}
}
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']);
if ($tokens[$i - 2]['code'] === T_DOC_COMMENT_TAG) {
$indent = 0;
if ($tokens[$i - 3]['code'] === T_DOC_COMMENT_WHITESPACE) {
$indent = strlen($tokens[$i - 3]['content']);
}
}
}
$comment .= ' ' . $tokens[$i]['content'];
$commentLines[] = [
'comment' => $tokens[$i]['content'],
'token' => $i,
'indent' => $indent,
];
if ($indent < 3) {
$error = 'Parameter comment indentation must be 3 spaces, found %s spaces';
$fix = $phpcsFile
->addFixableError($error, $i, 'ParamCommentIndentation', [
$indent,
]);
if ($fix === true) {
$phpcsFile->fixer
->replaceToken($i - 1, ' ');
}
}
}
}
if (empty($commentLines[0]['indent']) === false && $commentLines[0]['indent'] > 3) {
$error = 'Parameter comment indentation must be 3 spaces, found %s spaces';
$fix = $phpcsFile
->addFixableError($error, $commentLines[0]['token'] - 1, 'ParamCommentIndentation', [
$commentLines[0]['indent'],
]);
if ($fix === true) {
$phpcsFile->fixer
->replaceToken($commentLines[0]['token'] - 1, ' ');
}
}
if ($comment === '') {
$error = 'Missing parameter comment';
$phpcsFile
->addError($error, $tag, 'MissingParamComment');
$commentLines[] = [
'comment' => '',
];
}
$variableArguments = false;
if ($tokens[$tag + 2]['content'] === '...' || substr($tokens[$tag + 2]['content'], -3) === '...' && count(explode(' ', $tokens[$tag + 2]['content'])) === 2) {
$variableArguments = true;
}
if ($typeLen === 0) {
$error = 'Missing parameter type';
if (isset($matches[4]) === true && preg_match('/[^\\s]+[\\s]+[^\\s]+/', $matches[4]) === 0) {
$fix = $phpcsFile
->addFixableError($error, $tag, 'MissingParamType');
if ($fix === true) {
$phpcsFile->fixer
->replaceToken($tag + 2, $matches[4] . ' ' . $var);
}
}
else {
$phpcsFile
->addError($error, $tag, 'MissingParamType');
}
}
if (empty($matches[2]) === true && $variableArguments === false) {
$error = 'Missing parameter name';
$phpcsFile
->addError($error, $tag, 'MissingParamName');
}
}
else {
$error = 'Missing parameter type';
$phpcsFile
->addError($error, $tag, 'MissingParamType');
}
$params[] = [
'tag' => $tag,
'type' => $type,
'var' => $var,
'comment' => $comment,
'commentLines' => $commentLines,
'type_space' => $typeSpace,
'var_space' => $varSpace,
];
}
$realParams = $phpcsFile
->getMethodParameters($stackPtr);
$foundParams = [];
$checkPos = 0;
foreach ($params as $pos => $param) {
if ($param['var'] === '') {
continue;
}
$foundParams[] = $param['var'];
if ($param['type'] === '') {
continue;
}
$matched = false;
$realName = '<undefined>';
while (isset($realParams[$checkPos]) === true) {
$realName = $realParams[$checkPos]['name'];
if ($realName === $param['var'] || $realParams[$checkPos]['pass_by_reference'] === true && '&' . $realName === $param['var']) {
$matched = true;
break;
}
$checkPos++;
}
$typeNames = explode('|', $param['type']);
$suggestedNames = [];
foreach ($typeNames as $i => $typeName) {
$suggestedNames[] = static::suggestType($typeName);
}
$suggestedType = implode('|', $suggestedNames);
if (preg_match('/(\\s+)\\.{3}$/', $param['type'], $matches) === 1) {
$param['type_space'] = strlen($matches[1]);
$param['type'] = preg_replace('/\\s+\\.{3}$/', '', $param['type']);
}
if (preg_match('/\\s/', $param['type']) === 1) {
$error = 'Parameter type "%s" must not contain spaces';
$data = [
$param['type'],
];
$phpcsFile
->addError($error, $param['tag'], 'ParamTypeSpaces', $data);
}
else {
if ($param['type'] !== $suggestedType) {
$error = 'Expected "%s" but found "%s" for parameter type';
$data = [
$suggestedType,
$param['type'],
];
$fix = $phpcsFile
->addFixableError($error, $param['tag'], 'IncorrectParamVarName', $data);
if ($fix === true) {
$content = $suggestedType;
$content .= str_repeat(' ', $param['type_space']);
$content .= $param['var'];
$phpcsFile->fixer
->replaceToken($param['tag'] + 2, $content);
}
}
}
$suggestedName = '';
$typeName = '';
if (count($typeNames) === 1) {
$typeName = $param['type'];
$suggestedName = static::suggestType($typeName);
}
if (count($typeNames) === 1 && $typeName === $suggestedName) {
$suggestedTypeHint = '';
if (strpos($suggestedName, 'array') !== false) {
$suggestedTypeHint = 'array';
}
else {
if (strpos($suggestedName, 'callable') !== false) {
$suggestedTypeHint = 'callable';
}
else {
if (substr($suggestedName, -2) === '[]') {
$suggestedTypeHint = 'array';
}
else {
if ($suggestedName === 'object') {
$suggestedTypeHint = '';
}
else {
if (in_array($typeName, $this->allowedTypes) === false) {
$suggestedTypeHint = $suggestedName;
}
}
}
}
}
if ($suggestedTypeHint !== '' && isset($realParams[$checkPos]) === true) {
$typeHint = $realParams[$checkPos]['type_hint'];
if ($typeHint === '' && in_array($suggestedTypeHint, [
'string',
'int',
'float',
'bool',
]) === false) {
$error = 'Type hint "%s" missing for %s';
$data = [
$suggestedTypeHint,
$param['var'],
];
$phpcsFile
->addError($error, $stackPtr, 'TypeHintMissing', $data);
}
else {
if ($typeHint !== $suggestedTypeHint && $typeHint !== '') {
$nameParts = explode('\\', $suggestedTypeHint);
$lastPart = end($nameParts);
if ($lastPart !== $typeHint && $this
->isAliasedType($typeHint, $suggestedTypeHint, $phpcsFile) === false) {
$error = 'Expected type hint "%s"; found "%s" for %s';
$data = [
$lastPart,
$typeHint,
$param['var'],
];
$phpcsFile
->addError($error, $stackPtr, 'IncorrectTypeHint', $data);
}
}
}
}
else {
if ($suggestedTypeHint === '' && isset($realParams[$checkPos]) === true) {
$typeHint = $realParams[$checkPos]['type_hint'];
if ($typeHint !== '' && $typeHint !== 'stdClass' && $typeHint !== '\\stdClass' && $typeHint !== 'object') {
$error = 'Unknown type hint "%s" found for %s';
$data = [
$typeHint,
$param['var'],
];
$phpcsFile
->addError($error, $stackPtr, 'InvalidTypeHint', $data);
}
}
}
}
$spaces = 1;
if ($param['type_space'] !== $spaces) {
$error = 'Expected %s spaces after parameter type; %s found';
$data = [
$spaces,
$param['type_space'],
];
$fix = $phpcsFile
->addFixableError($error, $param['tag'], 'SpacingAfterParamType', $data);
if ($fix === true) {
$phpcsFile->fixer
->beginChangeset();
$content = $param['type'];
$content .= str_repeat(' ', $spaces);
$content .= $param['var'];
$content .= str_repeat(' ', $param['var_space']);
$phpcsFile->fixer
->replaceToken($param['tag'] + 2, $content);
foreach ($param['commentLines'] as $lineNum => $line) {
if ($lineNum === 0 || $param['commentLines'][$lineNum]['indent'] === 0) {
continue;
}
$newIndent = $param['commentLines'][$lineNum]['indent'] + $spaces - $param['type_space'];
$phpcsFile->fixer
->replaceToken($param['commentLines'][$lineNum]['token'] - 1, str_repeat(' ', $newIndent));
}
$phpcsFile->fixer
->endChangeset();
}
}
if ($matched === false) {
if ($checkPos >= $pos) {
$code = 'ParamNameNoMatch';
$data = [
$param['var'],
$realName,
];
$error = 'Doc comment for parameter %s does not match ';
if (strtolower($param['var']) === strtolower($realName)) {
$error .= 'case of ';
$code = 'ParamNameNoCaseMatch';
}
$error .= 'actual variable name %s';
$phpcsFile
->addError($error, $param['tag'], $code, $data);
$checkPos = $pos - 1;
}
else {
if (substr($param['var'], -4) !== ',...') {
$error = 'Superfluous parameter comment';
$phpcsFile
->addError($error, $param['tag'], 'ExtraParamComment');
}
}
}
$checkPos++;
if ($param['comment'] === '') {
continue;
}
if (isset($param['commentLines'][0]['comment']) === true) {
$firstChar = $param['commentLines'][0]['comment'];
}
else {
$firstChar = $param['comment'];
}
if (preg_match('|\\p{Lu}|u', $firstChar) === 0) {
$error = 'Parameter comment must start with a capital letter';
if (isset($param['commentLines'][0]['token']) === true) {
$commentToken = $param['commentLines'][0]['token'];
}
else {
$commentToken = $param['tag'];
}
$phpcsFile
->addError($error, $commentToken, 'ParamCommentNotCapital');
}
$lastChar = substr($param['comment'], -1);
if (in_array($lastChar, [
'.',
'!',
'?',
')',
]) === false) {
$error = 'Parameter comment must end with a full stop';
if (empty($param['commentLines']) === true) {
$commentToken = $param['tag'] + 2;
}
else {
$lastLine = end($param['commentLines']);
$commentToken = $lastLine['token'];
}
if ($this
->isInCodeExample($phpcsFile, $commentToken, $param['tag']) === false) {
$fix = $phpcsFile
->addFixableError($error, $commentToken, 'ParamCommentFullStop');
if ($fix === true) {
$phpcsFile->fixer
->addContent($commentToken, '.');
}
}
}
}
if ($tokens[$stackPtr]['level'] > 0 && empty($foundParams) === false) {
foreach ($realParams as $realParam) {
$realParamKeyName = $realParam['name'];
if (in_array($realParamKeyName, $foundParams) === false && ($realParam['pass_by_reference'] === true && in_array("&{$realParamKeyName}", $foundParams) === true) === false) {
$error = 'Parameter %s is not described in comment';
$phpcsFile
->addError($error, $commentStart, 'ParamMissingDefinition', [
$realParam['name'],
]);
}
}
}
}
protected function processSees(File $phpcsFile, $stackPtr, $commentStart) {
$tokens = $phpcsFile
->getTokens();
foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
if ($tokens[$tag]['content'] !== '@see') {
continue;
}
if ($tokens[$tag + 2]['code'] === T_DOC_COMMENT_STRING) {
$comment = $tokens[$tag + 2]['content'];
if (strpos($comment, ' ') !== false) {
$error = 'The @see reference should not contain any additional text';
$phpcsFile
->addError($error, $tag, 'SeeAdditionalText');
continue;
}
if (preg_match('/[\\.!\\?]$/', $comment) === 1) {
$error = 'Trailing punctuation for @see references is not allowed.';
$fix = $phpcsFile
->addFixableError($error, $tag, 'SeePunctuation');
if ($fix === true) {
$content = substr($comment, 0, -1);
$phpcsFile->fixer
->replaceToken($tag + 2, $content);
}
}
}
}
}
public static function suggestType($type) {
if (isset(static::$invalidTypes[$type]) === true) {
return static::$invalidTypes[$type];
}
if ($type === '$this') {
return $type;
}
$type = preg_replace('/[^a-zA-Z0-9_\\\\[\\]]/', '', $type);
return $type;
}
protected function isAliasedType($typeHint, $suggestedTypeHint, File $phpcsFile) {
$tokens = $phpcsFile
->getTokens();
$usePtr = 0;
while ($usePtr !== false) {
$usePtr = $phpcsFile
->findNext(T_USE, $usePtr + 1);
if ($usePtr === false) {
return false;
}
if (empty($tokens[$usePtr]['conditions']) === false) {
continue;
}
$originalClass = $phpcsFile
->findNext(Tokens::$emptyTokens, $usePtr + 1, null, true);
if ($originalClass === false || $tokens[$originalClass]['code'] !== T_STRING && $tokens[$originalClass]['code'] !== T_NS_SEPARATOR) {
continue;
}
$originalClassName = '';
while (in_array($tokens[$originalClass]['code'], [
T_STRING,
T_NS_SEPARATOR,
]) === true) {
$originalClassName .= $tokens[$originalClass]['content'];
$originalClass++;
}
if (ltrim($originalClassName, '\\') !== ltrim($suggestedTypeHint, '\\')) {
continue;
}
$asPtr = $phpcsFile
->findNext(Tokens::$emptyTokens, $originalClass + 1, null, true);
if ($asPtr === false || $tokens[$asPtr]['code'] !== T_AS) {
continue;
}
$aliasPtr = $phpcsFile
->findNext(Tokens::$emptyTokens, $asPtr + 1, null, true);
if ($aliasPtr === false || $tokens[$aliasPtr]['code'] !== T_STRING || $tokens[$aliasPtr]['content'] !== $typeHint) {
continue;
}
return true;
}
}
protected function isInCodeExample(File $phpcsFile, $stackPtr, $commentStart) {
$tokens = $phpcsFile
->getTokens();
if (strpos($tokens[$stackPtr]['content'], '@code') !== false) {
return true;
}
$prevTag = $phpcsFile
->findPrevious([
T_DOC_COMMENT_TAG,
], $stackPtr - 1, $commentStart);
if ($prevTag === false) {
return false;
}
if ($tokens[$prevTag]['content'] === '@code') {
return true;
}
return false;
}
}