function AbstractControlStructureSpacing::checkLinesBefore
3 calls to AbstractControlStructureSpacing::checkLinesBefore()
- AbstractControlStructureSpacing::process in vendor/
slevomat/ coding-standard/ SlevomatCodingStandard/ Sniffs/ ControlStructures/ AbstractControlStructureSpacing.php - * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *
- JumpStatementsSpacingSniff::checkLinesBefore in vendor/
slevomat/ coding-standard/ SlevomatCodingStandard/ Sniffs/ ControlStructures/ JumpStatementsSpacingSniff.php - JumpStatementsSpacingSniff::checkLinesBefore in vendor/
slevomat/ coding-standard/ SlevomatCodingStandard/ Sniffs/ ControlStructures/ JumpStatementsSpacingSniff.php
1 method overrides AbstractControlStructureSpacing::checkLinesBefore()
- JumpStatementsSpacingSniff::checkLinesBefore in vendor/
slevomat/ coding-standard/ SlevomatCodingStandard/ Sniffs/ ControlStructures/ JumpStatementsSpacingSniff.php
File
-
vendor/
slevomat/ coding-standard/ SlevomatCodingStandard/ Sniffs/ ControlStructures/ AbstractControlStructureSpacing.php, line 129
Class
- AbstractControlStructureSpacing
- @internal
Namespace
SlevomatCodingStandard\Sniffs\ControlStructuresCode
protected function checkLinesBefore(File $phpcsFile, int $controlStructurePointer) : void {
$tokens = $phpcsFile->getTokens();
if (in_array($tokens[$controlStructurePointer]['code'], [
T_CASE,
T_DEFAULT,
], true)) {
$pointerBefore = TokenHelper::findPreviousEffective($phpcsFile, $controlStructurePointer - 1);
if ($tokens[$pointerBefore]['code'] === T_COLON) {
return;
}
}
$nonWhitespacePointerBefore = TokenHelper::findPreviousNonWhitespace($phpcsFile, $controlStructurePointer - 1);
$controlStructureStartPointer = $controlStructurePointer;
$pointerBefore = $nonWhitespacePointerBefore;
$pointerToCheckFirst = $pointerBefore;
if (in_array($tokens[$nonWhitespacePointerBefore]['code'], Tokens::$commentTokens, true)) {
$effectivePointerBefore = TokenHelper::findPreviousEffective($phpcsFile, $pointerBefore - 1);
if ($tokens[$effectivePointerBefore]['line'] === $tokens[$nonWhitespacePointerBefore]['line']) {
$pointerToCheckFirst = $effectivePointerBefore;
}
elseif ($tokens[$nonWhitespacePointerBefore]['line'] + 1 === $tokens[$controlStructurePointer]['line']) {
if ($tokens[$effectivePointerBefore]['line'] !== $tokens[$nonWhitespacePointerBefore]['line']) {
$controlStructureStartPointer = array_key_exists('comment_opener', $tokens[$nonWhitespacePointerBefore]) ? $tokens[$nonWhitespacePointerBefore]['comment_opener'] : CommentHelper::getMultilineCommentStartPointer($phpcsFile, $nonWhitespacePointerBefore);
$pointerBefore = TokenHelper::findPreviousNonWhitespace($phpcsFile, $controlStructureStartPointer - 1);
}
$pointerToCheckFirst = $pointerBefore;
}
}
$isFirstControlStructure = in_array($tokens[$pointerToCheckFirst]['code'], [
T_OPEN_CURLY_BRACKET,
T_COLON,
], true);
$whitespaceBefore = '';
if ($tokens[$pointerBefore]['code'] === T_OPEN_TAG) {
$whitespaceBefore .= substr($tokens[$pointerBefore]['content'], strlen('<?php'));
}
$hasCommentWithLineEndBefore = in_array($tokens[$pointerBefore]['code'], TokenHelper::$inlineCommentTokenCodes, true) && substr($tokens[$pointerBefore]['content'], -strlen($phpcsFile->eolChar)) === $phpcsFile->eolChar;
if ($hasCommentWithLineEndBefore) {
$whitespaceBefore .= $phpcsFile->eolChar;
}
if ($pointerBefore + 1 !== $controlStructurePointer) {
$whitespaceBefore .= TokenHelper::getContent($phpcsFile, $pointerBefore + 1, $controlStructureStartPointer - 1);
}
$requiredLinesCountBefore = $isFirstControlStructure ? $this->getLinesCountBeforeFirst($phpcsFile, $controlStructurePointer) : $this->getLinesCountBefore();
$actualLinesCountBefore = substr_count($whitespaceBefore, $phpcsFile->eolChar) - 1;
if ($requiredLinesCountBefore === $actualLinesCountBefore) {
return;
}
$fix = $phpcsFile->addFixableError(sprintf('Expected %d line%s before "%s", found %d.', $requiredLinesCountBefore, $requiredLinesCountBefore === 1 ? '' : 's', $tokens[$controlStructurePointer]['content'], $actualLinesCountBefore), $controlStructurePointer, $isFirstControlStructure ? self::CODE_INCORRECT_LINES_COUNT_BEFORE_FIRST_CONTROL_STRUCTURE : self::CODE_INCORRECT_LINES_COUNT_BEFORE_CONTROL_STRUCTURE);
if (!$fix) {
return;
}
$endOfLineBeforePointer = TokenHelper::findPreviousContent($phpcsFile, T_WHITESPACE, $phpcsFile->eolChar, $controlStructureStartPointer - 1);
$phpcsFile->fixer
->beginChangeset();
if ($tokens[$pointerBefore]['code'] === T_OPEN_TAG) {
$phpcsFile->fixer
->replaceToken($pointerBefore, '<?php');
}
if ($endOfLineBeforePointer !== null) {
FixerHelper::removeBetweenIncluding($phpcsFile, $pointerBefore + 1, $endOfLineBeforePointer);
}
$linesToAdd = $hasCommentWithLineEndBefore ? $requiredLinesCountBefore - 1 : $requiredLinesCountBefore;
for ($i = 0; $i <= $linesToAdd; $i++) {
$phpcsFile->fixer
->addNewline($pointerBefore);
}
$phpcsFile->fixer
->endChangeset();
}