1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
<?php
/**
* Generic_Sniffs_Formatting_MultipleStatementAlignmentSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Generic_Sniffs_Formatting_MultipleStatementAlignmentSniff.
*
* Checks alignment of assignments. If there are multiple adjacent assignments,
* it will check that the equals signs of each assignment are aligned. It will
* display a warning to advise that the signs should be aligned.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @version Release: 1.3.4
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Generic_Sniffs_Formatting_MultipleStatementAlignmentSniff implements PHP_CodeSniffer_Sniff
{
/**
* A list of tokenizers this sniff supports.
*
* @var array
*/
public $supportedTokenizers = array(
'PHP',
'JS',
);
/**
* If true, an error will be thrown; otherwise a warning.
*
* @var bool
*/
public $error = false;
/**
* The maximum amount of padding before the alignment is ignored.
*
* If the amount of padding required to align this assignment with the
* surrounding assignments exceeds this number, the assignment will be
* ignored and no errors or warnings will be thrown.
*
* @var int
*/
public $maxPadding = 1000;
/**
* If true, multi-line assignments are not checked.
*
* @var int
*/
public $ignoreMultiLine = false;
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return PHP_CodeSniffer_Tokens::$assignmentTokens;
}//end register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
// Ignore assignments used in a condition, like an IF or FOR.
if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
foreach ($tokens[$stackPtr]['nested_parenthesis'] as $start => $end) {
if (isset($tokens[$start]['parenthesis_owner']) === true) {
return;
}
}
}
/*
By this stage, it is known that there is an assignment on this line.
We only want to process the block once we reach the last assignment,
so we need to determine if there are more to follow.
*/
// The assignment may span over multiple lines, so look for the
// end of the assignment so we can check assignment blocks correctly.
$lineEnd = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1));
$nextAssign = $phpcsFile->findNext(
PHP_CodeSniffer_Tokens::$assignmentTokens,
($lineEnd + 1)
);
if ($nextAssign !== false) {
$isAssign = true;
if ($tokens[$nextAssign]['line'] === ($tokens[$lineEnd]['line'] + 1)) {
// Assignment may be in the same block as this one. Just make sure
// it is not used in a condition, like an IF or FOR.
if (isset($tokens[$nextAssign]['nested_parenthesis']) === true) {
foreach ($tokens[$nextAssign]['nested_parenthesis'] as $start => $end) {
if (isset($tokens[$start]['parenthesis_owner']) === true) {
// Not an assignment.
$isAssign = false;
break;
}
}
}
if ($isAssign === true) {
return;
}
}
}
// Getting here means that this is the last in a block of statements.
$assignments = array();
$assignments[] = $stackPtr;
$prevAssignment = $stackPtr;
$lastLine = $tokens[$stackPtr]['line'];
while (($prevAssignment = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$assignmentTokens, ($prevAssignment - 1))) !== false) {
// We are not interested in double arrows as they assign values inside
// arrays and loops and do not use the same indentation rules.
if ($tokens[$prevAssignment]['code'] === T_DOUBLE_ARROW) {
continue;
}
// The assignment's end token must be on the line directly
// above the current one to be in the same assignment block.
$lineEnd = $phpcsFile->findNext(T_SEMICOLON, ($prevAssignment + 1));
// And the end token must actually belong to this assignment.
$nextOpener = $phpcsFile->findNext(
PHP_CodeSniffer_Tokens::$scopeOpeners,
($prevAssignment + 1)
);
if ($nextOpener !== false && $nextOpener < $lineEnd) {
break;
}
if ($tokens[$lineEnd]['line'] !== ($lastLine - 1)) {
break;
}
// Make sure it is not assigned inside a condition (eg. IF, FOR).
if (isset($tokens[$prevAssignment]['nested_parenthesis']) === true) {
foreach ($tokens[$prevAssignment]['nested_parenthesis'] as $start => $end) {
if (isset($tokens[$start]['parenthesis_owner']) === true) {
break(2);
}
}
}
$assignments[] = $prevAssignment;
$lastLine = $tokens[$prevAssignment]['line'];
}//end while
$assignmentData = array();
$maxAssignmentLength = 0;
$maxVariableLength = 0;
foreach ($assignments as $assignment) {
$prev = $phpcsFile->findPrevious(
PHP_CodeSniffer_Tokens::$emptyTokens,
($assignment - 1),
null,
true
);
$endColumn = $tokens[($prev + 1)]['column'];
if ($maxVariableLength < $endColumn) {
$maxVariableLength = $endColumn;
}
if ($maxAssignmentLength < strlen($tokens[$assignment]['content'])) {
$maxAssignmentLength = strlen($tokens[$assignment]['content']);
}
$assignmentData[$assignment]
= array(
'variable_length' => $endColumn,
'assignment_length' => strlen($tokens[$assignment]['content']),
);
}//end foreach
foreach ($assignmentData as $assignment => $data) {
if ($data['assignment_length'] === $maxAssignmentLength) {
if ($data['variable_length'] === $maxVariableLength) {
// The assignment is the longest possible, so the column that
// everything has to align to is based on it.
$column = ($maxVariableLength + 1);
break;
} else {
// The assignment token is the longest out of all of the
// assignments, but the variable name is not, so the column
// the start at can go back more to cover the space
// between the variable name and the assigment operator.
$column = ($maxVariableLength - ($maxAssignmentLength - 1) + 1);
}
}
}
// Determine the actual position that each equals sign should be in.
foreach ($assignments as $assignment) {
// Actual column takes into account the length of the assignment operator.
$actualColumn = ($column + $maxAssignmentLength - strlen($tokens[$assignment]['content']));
if ($tokens[$assignment]['column'] !== $actualColumn) {
$prev = $phpcsFile->findPrevious(
PHP_CodeSniffer_Tokens::$emptyTokens,
($assignment - 1),
null,
true
);
$expected = ($actualColumn - $tokens[($prev + 1)]['column']);
if ($tokens[$assignment]['line'] !== $tokens[$prev]['line']) {
// Instead of working out how many spaces there are
// across new lines, the error message becomes more
// generic below.
$found = null;
} else {
$found = ($tokens[$assignment]['column'] - $tokens[($prev + 1)]['column']);
}
// If the expected number of spaces for alignment exceeds the
// maxPadding rule, we just check for a single space as no
// alignment is required.
if ($expected > $this->maxPadding) {
if ($found === 1) {
continue;
} else {
$expected = 1;
}
}
// Skip multi-line assignments if required.
if ($found === null && $this->ignoreMultiLine === true) {
continue;
}
$expected .= ($expected === 1) ? ' space' : ' spaces';
if ($found === null) {
$found = 'a new line';
} else {
$found .= ($found === 1) ? ' space' : ' spaces';
}
if (count($assignments) === 1) {
$type = 'Incorrect';
$error = 'Equals sign not aligned correctly; expected %s but found %s';
} else {
$type = 'NotSame';
$error = 'Equals sign not aligned with surrounding assignments; expected %s but found %s';
}
$errorData = array(
$expected,
$found,
);
if ($this->error === true) {
$phpcsFile->addError($error, $assignment, $type, $errorData);
} else {
$phpcsFile->addWarning($error, $assignment, $type.'Warning', $errorData);
}
}//end if
}//end foreach
}//end process()
}//end class
?>
|