View source
<?php
class Drupal_Sniffs_Formatting_SpaceInlineIfSniff implements PHP_CodeSniffer_Sniff {
public function register() {
return array(
T_INLINE_THEN,
T_INLINE_ELSE,
);
}
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile
->getTokens();
$operator = $tokens[$stackPtr]['content'];
$previous = $phpcsFile
->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
if ($tokens[$previous]['code'] === T_INLINE_THEN) {
if ($previous !== $stackPtr - 1) {
$error = 'There must be no space between ? and :';
$phpcsFile
->addError($error, $stackPtr, 'SpaceInlineElse');
}
if ($tokens[$stackPtr + 1]['code'] !== T_WHITESPACE) {
$error = "Expected 1 space after \"{$operator}\"; 0 found";
$phpcsFile
->addError($error, $stackPtr, 'NoSpaceAfter');
}
else {
if (strlen($tokens[$stackPtr + 1]['content']) !== 1) {
$found = strlen($tokens[$stackPtr + 1]['content']);
$error = 'Expected 1 space after "%s"; %s found';
$data = array(
$operator,
$found,
);
$phpcsFile
->addError($error, $stackPtr, 'SpacingAfter', $data);
}
}
return;
}
$next = $phpcsFile
->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
if ($tokens[$next]['code'] === T_INLINE_ELSE) {
if ($tokens[$stackPtr - 1]['code'] !== T_WHITESPACE) {
$error = "Expected 1 space before \"{$operator}\"; 0 found";
$phpcsFile
->addError($error, $stackPtr, 'NoSpaceBefore');
}
else {
if (strlen($tokens[$stackPtr - 1]['content']) !== 1) {
$found = strlen($tokens[$stackPtr - 1]['content']);
$error = 'Expected 1 space before "%s"; %s found';
$data = array(
$operator,
$found,
);
$phpcsFile
->addError($error, $stackPtr, 'SpacingBefore', $data);
}
}
return;
}
$sniff = new Squiz_Sniffs_WhiteSpace_OperatorSpacingSniff();
$sniff
->process($phpcsFile, $stackPtr);
}
}