1 | <?php |
||
27 | * @package PHP_CodeSniffer |
||
28 | * @author Peter Philipp <[email protected]> |
||
29 | * @author Alexander Obuhovich <[email protected]> |
||
30 | * @license https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause |
||
31 | * @link https://github.com/aik099/CodingStandard |
||
32 | */ |
||
33 | class SpaceOperatorSniff implements Sniff |
||
34 | { |
||
35 | |||
36 | |||
37 | /** |
||
38 | * Returns an array of tokens this test wants to listen for. |
||
39 | * |
||
40 | * @return array |
||
41 | */ |
||
42 | 1 | public function register() |
|
46 | |||
47 | |||
48 | /** |
||
49 | * Processes this test, when one of its tokens is encountered. |
||
50 | * |
||
51 | * @param File $phpcsFile The file being scanned. |
||
52 | * @param int $stackPtr The position of the current token in the |
||
53 | * stack passed in $tokens. |
||
54 | * |
||
55 | * @return void |
||
56 | */ |
||
57 | 1 | public function process(File $phpcsFile, $stackPtr) |
|
58 | { |
||
59 | // Only cover places, that are not handled by "Squiz.WhiteSpace.OperatorSpacing" sniff. |
||
60 | 1 | $tokens = $phpcsFile->getTokens(); |
|
61 | |||
62 | 1 | if ($tokens[($stackPtr - 1)]['code'] !== T_WHITESPACE) { |
|
63 | 1 | return; |
|
64 | } |
||
65 | |||
66 | 1 | if ($tokens[($stackPtr - 2)]['line'] !== $tokens[$stackPtr]['line']) { |
|
67 | 1 | $found = 'newline'; |
|
68 | 1 | } else { |
|
69 | 1 | $found = $tokens[($stackPtr - 1)]['length']; |
|
70 | } |
||
71 | |||
72 | 1 | if (isset($phpcsFile->fixer) === true) { |
|
73 | 1 | $phpcsFile->recordMetric($stackPtr, 'Space before operator', $found); |
|
74 | 1 | } |
|
75 | |||
76 | 1 | if ($found !== 1) { |
|
77 | 1 | $error = 'Expected 1 space before "%s"; %s found'; |
|
78 | $data = array( |
||
79 | 1 | $tokens[$stackPtr]['content'], |
|
80 | 1 | $found, |
|
81 | 1 | ); |
|
82 | 1 | $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingBefore', $data); |
|
83 | |||
84 | 1 | if ($fix === true) { |
|
85 | 1 | $phpcsFile->fixer->replaceToken(($stackPtr - 1), ' '); |
|
86 | 1 | } |
|
87 | 1 | } |
|
88 | 1 | }//end process() |
|
90 |