function InlineDocCommentDeclarationSniff::checkVariable
*
Parameters
list<Annotation<VarTagValueNode>> $annotations:
1 call to InlineDocCommentDeclarationSniff::checkVariable()
- InlineDocCommentDeclarationSniff::process in vendor/
slevomat/ coding-standard/ SlevomatCodingStandard/ Sniffs/ Commenting/ InlineDocCommentDeclarationSniff.php - * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *
File
-
vendor/
slevomat/ coding-standard/ SlevomatCodingStandard/ Sniffs/ Commenting/ InlineDocCommentDeclarationSniff.php, line 233
Class
Namespace
SlevomatCodingStandard\Sniffs\CommentingCode
private function checkVariable(File $phpcsFile, array $annotations, int $docCommentOpenerPointer, int $docCommentCloserPointer) : void {
$tokens = $phpcsFile->getTokens();
$checkedTokens = [
T_VARIABLE,
T_FOREACH,
T_WHILE,
T_LIST,
T_OPEN_SHORT_ARRAY,
T_CLOSURE,
T_FN,
];
$variableNames = [];
foreach ($annotations as $variableAnnotation) {
if ($variableAnnotation->isInvalid()) {
continue;
}
$variableName = $variableAnnotation->getValue()->variableName;
if ($variableName === '') {
continue;
}
$variableNames[] = $variableName;
}
$improveCodePointer = function (int $codePointer) use ($phpcsFile, $tokens, $checkedTokens, $variableNames) : int {
$shouldSearchClosure = false;
if (!in_array($tokens[$codePointer]['code'], $checkedTokens, true)) {
$shouldSearchClosure = true;
}
elseif ($tokens[$codePointer]['code'] === T_VARIABLE && (!$this->isAssignment($phpcsFile, $codePointer) || !in_array($tokens[$codePointer]['content'], $variableNames, true))) {
$shouldSearchClosure = true;
}
if (!$shouldSearchClosure) {
return $codePointer;
}
$closurePointer = TokenHelper::findNext($phpcsFile, [
T_CLOSURE,
T_FN,
], $codePointer + 1);
if ($closurePointer !== null && $tokens[$codePointer]['line'] === $tokens[$closurePointer]['line']) {
return $closurePointer;
}
return $codePointer;
};
$firstPointerOnNextLine = TokenHelper::findFirstNonWhitespaceOnNextLine($phpcsFile, $docCommentCloserPointer);
$codePointerAfter = $firstPointerOnNextLine;
while ($codePointerAfter !== null && $tokens[$codePointerAfter]['code'] === T_DOC_COMMENT_OPEN_TAG) {
$codePointerAfter = TokenHelper::findFirstNonWhitespaceOnNextLine($phpcsFile, $codePointerAfter + 1);
}
if ($codePointerAfter !== null) {
if ($tokens[$codePointerAfter]['code'] === T_STATIC) {
$codePointerAfter = TokenHelper::findNextEffective($phpcsFile, $codePointerAfter + 1);
}
$codePointerAfter = $improveCodePointer($codePointerAfter);
}
$codePointerBefore = TokenHelper::findFirstNonWhitespaceOnPreviousLine($phpcsFile, $docCommentOpenerPointer);
while ($codePointerBefore !== null && $tokens[$codePointerBefore]['code'] === T_DOC_COMMENT_OPEN_TAG) {
$codePointerBefore = TokenHelper::findFirstNonWhitespaceOnPreviousLine($phpcsFile, $codePointerBefore - 1);
}
if ($codePointerBefore !== null) {
$codePointerBefore = $improveCodePointer($codePointerBefore);
}
foreach ($annotations as $variableAnnotation) {
if ($variableAnnotation->isInvalid()) {
continue;
}
$variableName = $variableAnnotation->getValue()->variableName;
if ($variableName === '') {
continue;
}
$missingVariableErrorParameters = [
sprintf('Missing variable %s before or after the documentation comment.', $variableName),
$docCommentOpenerPointer,
self::CODE_MISSING_VARIABLE,
];
$noAssignmentErrorParameters = [
sprintf('No assignment to %s variable before or after the documentation comment.', $variableName),
$docCommentOpenerPointer,
self::CODE_NO_ASSIGNMENT,
];
if ($this->allowAboveNonAssignment && $firstPointerOnNextLine !== null) {
for ($i = $firstPointerOnNextLine; $i < count($tokens); $i++) {
if ($tokens[$i]['line'] > $tokens[$firstPointerOnNextLine]['line']) {
break;
}
if ($tokens[$i]['code'] !== T_VARIABLE) {
continue;
}
if ($tokens[$i]['content'] === $variableName) {
return;
}
}
}
foreach ([
1 => $codePointerBefore,
2 => $codePointerAfter,
] as $tryNo => $codePointer) {
if ($codePointer === null || !in_array($tokens[$codePointer]['code'], $checkedTokens, true)) {
if ($tryNo === 2) {
$phpcsFile->addError(...$missingVariableErrorParameters);
}
continue;
}
if ($tokens[$codePointer]['code'] === T_VARIABLE) {
if ($tokens[$codePointer]['content'] !== '$this' && !$this->isAssignment($phpcsFile, $codePointer)) {
if ($tryNo === 2) {
$phpcsFile->addError(...$noAssignmentErrorParameters);
}
continue;
}
if ($variableName !== $tokens[$codePointer]['content']) {
if ($tryNo === 2) {
$phpcsFile->addError(...$missingVariableErrorParameters);
}
continue;
}
}
elseif ($tokens[$codePointer]['code'] === T_LIST) {
$listParenthesisOpener = TokenHelper::findNextEffective($phpcsFile, $codePointer + 1);
$variablePointerInList = TokenHelper::findNextContent($phpcsFile, T_VARIABLE, $variableName, $listParenthesisOpener + 1, $tokens[$listParenthesisOpener]['parenthesis_closer']);
if ($variablePointerInList === null) {
if ($tryNo === 2) {
$phpcsFile->addError(...$missingVariableErrorParameters);
}
continue;
}
}
elseif ($tokens[$codePointer]['code'] === T_OPEN_SHORT_ARRAY) {
$pointerAfterList = TokenHelper::findNextEffective($phpcsFile, $tokens[$codePointer]['bracket_closer'] + 1);
if ($tokens[$pointerAfterList]['code'] !== T_EQUAL) {
if ($tryNo === 2) {
$phpcsFile->addError(...$noAssignmentErrorParameters);
}
continue;
}
$variablePointerInList = TokenHelper::findNextContent($phpcsFile, T_VARIABLE, $variableName, $codePointer + 1, $tokens[$codePointer]['bracket_closer']);
if ($variablePointerInList === null) {
if ($tryNo === 2) {
$phpcsFile->addError(...$missingVariableErrorParameters);
}
continue;
}
}
elseif (in_array($tokens[$codePointer]['code'], [
T_CLOSURE,
T_FN,
], true)) {
$parameterPointer = TokenHelper::findNextContent($phpcsFile, T_VARIABLE, $variableName, $tokens[$codePointer]['parenthesis_opener'] + 1, $tokens[$codePointer]['parenthesis_closer']);
if ($parameterPointer === null) {
if ($tryNo === 2) {
$phpcsFile->addError(...$missingVariableErrorParameters);
}
continue;
}
}
else {
if ($tokens[$codePointer]['code'] === T_WHILE) {
$variablePointerInWhile = TokenHelper::findNextContent($phpcsFile, T_VARIABLE, $variableName, $tokens[$codePointer]['parenthesis_opener'] + 1, $tokens[$codePointer]['parenthesis_closer']);
if ($variablePointerInWhile === null) {
if ($tryNo === 2) {
$phpcsFile->addError(...$missingVariableErrorParameters);
}
continue;
}
$pointerAfterVariableInWhile = TokenHelper::findNextEffective($phpcsFile, $variablePointerInWhile + 1);
if ($tokens[$pointerAfterVariableInWhile]['code'] !== T_EQUAL) {
if ($tryNo === 2) {
$phpcsFile->addError(...$noAssignmentErrorParameters);
}
continue;
}
}
else {
$asPointer = TokenHelper::findNext($phpcsFile, T_AS, $tokens[$codePointer]['parenthesis_opener'] + 1, $tokens[$codePointer]['parenthesis_closer']);
$variablePointerInForeach = TokenHelper::findNextContent($phpcsFile, T_VARIABLE, $variableName, $asPointer + 1, $tokens[$codePointer]['parenthesis_closer']);
if ($variablePointerInForeach === null) {
if ($tryNo === 2) {
$phpcsFile->addError(...$missingVariableErrorParameters);
}
continue;
}
}
}
// No error, don't check second $codePointer
continue 2;
}
}
}