function RequireSingleLineConditionSniff::process
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *
Parameters
int $controlStructurePointer:
Overrides Sniff::process
File
-
vendor/
slevomat/ coding-standard/ SlevomatCodingStandard/ Sniffs/ ControlStructures/ RequireSingleLineConditionSniff.php, line 28
Class
Namespace
SlevomatCodingStandard\Sniffs\ControlStructuresCode
public function process(File $phpcsFile, $controlStructurePointer) : void {
$this->maxLineLength = SniffSettingsHelper::normalizeInteger($this->maxLineLength);
if ($this->shouldBeSkipped($phpcsFile, $controlStructurePointer)) {
return;
}
$tokens = $phpcsFile->getTokens();
$parenthesisOpenerPointer = $tokens[$controlStructurePointer]['parenthesis_opener'];
$parenthesisCloserPointer = $tokens[$controlStructurePointer]['parenthesis_closer'];
if ($tokens[$parenthesisOpenerPointer]['line'] === $tokens[$parenthesisCloserPointer]['line']) {
return;
}
if (TokenHelper::findNext($phpcsFile, TokenHelper::$inlineCommentTokenCodes, $parenthesisOpenerPointer + 1, $parenthesisCloserPointer) !== null) {
return;
}
$lineStart = $this->getLineStart($phpcsFile, $parenthesisOpenerPointer);
$condition = $this->getCondition($phpcsFile, $parenthesisOpenerPointer, $parenthesisCloserPointer);
$lineEnd = $this->getLineEnd($phpcsFile, $parenthesisCloserPointer);
$lineLength = strlen($lineStart . $condition . $lineEnd);
$isSimpleCondition = TokenHelper::findNext($phpcsFile, Tokens::$booleanOperators, $parenthesisOpenerPointer + 1, $parenthesisCloserPointer) === null;
if (!$this->shouldReportError($lineLength, $isSimpleCondition)) {
return;
}
$fix = $phpcsFile->addFixableError(sprintf('Condition of "%s" should be placed on a single line.', $this->getControlStructureName($phpcsFile, $controlStructurePointer)), $controlStructurePointer, self::CODE_REQUIRED_SINGLE_LINE_CONDITION);
if (!$fix) {
return;
}
$phpcsFile->fixer
->beginChangeset();
$phpcsFile->fixer
->addContent($parenthesisOpenerPointer, $condition);
FixerHelper::removeBetween($phpcsFile, $parenthesisOpenerPointer, $parenthesisCloserPointer);
$phpcsFile->fixer
->endChangeset();
}