1<?php 2/** 3 * Squiz_Sniffs_WhiteSpace_ObjectOperatorSpacingSniff. 4 * 5 * PHP version 5 6 * 7 * @category PHP 8 * @package PHP_CodeSniffer 9 * @author Greg Sherwood <gsherwood@squiz.net> 10 * @author Marc McIntyre <mmcintyre@squiz.net> 11 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) 12 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence 13 * @link http://pear.php.net/package/PHP_CodeSniffer 14 */ 15 16/** 17 * Squiz_Sniffs_WhiteSpace_ObjectOperatorSpacingSniff. 18 * 19 * Ensure there is no whitespace before/after an object operator. 20 * 21 * @category PHP 22 * @package PHP_CodeSniffer 23 * @author Greg Sherwood <gsherwood@squiz.net> 24 * @author Marc McIntyre <mmcintyre@squiz.net> 25 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) 26 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence 27 * @version Release: @package_version@ 28 * @link http://pear.php.net/package/PHP_CodeSniffer 29 */ 30class Squiz_Sniffs_WhiteSpace_ObjectOperatorSpacingSniff implements PHP_CodeSniffer_Sniff 31{ 32 33 /** 34 * Allow newlines instead of spaces. 35 * 36 * @var boolean 37 */ 38 public $ignoreNewlines = false; 39 40 41 /** 42 * Returns an array of tokens this test wants to listen for. 43 * 44 * @return array 45 */ 46 public function register() 47 { 48 return array(T_OBJECT_OPERATOR); 49 50 }//end register() 51 52 53 /** 54 * Processes this test, when one of its tokens is encountered. 55 * 56 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. 57 * @param int $stackPtr The position of the current token 58 * in the stack passed in $tokens. 59 * 60 * @return void 61 */ 62 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) 63 { 64 $tokens = $phpcsFile->getTokens(); 65 if ($tokens[($stackPtr - 1)]['code'] !== T_WHITESPACE) { 66 $before = 0; 67 } else { 68 if ($tokens[($stackPtr - 2)]['line'] !== $tokens[$stackPtr]['line']) { 69 $before = 'newline'; 70 } else { 71 $before = $tokens[($stackPtr - 1)]['length']; 72 } 73 } 74 75 if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) { 76 $after = 0; 77 } else { 78 if ($tokens[($stackPtr + 2)]['line'] !== $tokens[$stackPtr]['line']) { 79 $after = 'newline'; 80 } else { 81 $after = $tokens[($stackPtr + 1)]['length']; 82 } 83 } 84 85 $phpcsFile->recordMetric($stackPtr, 'Spacing before object operator', $before); 86 $phpcsFile->recordMetric($stackPtr, 'Spacing after object operator', $after); 87 88 $this->checkSpacingBeforeOperator($phpcsFile, $stackPtr, $before); 89 $this->checkSpacingAfterOperator($phpcsFile, $stackPtr, $after); 90 91 }//end process() 92 93 94 /** 95 * Check the spacing before the operator. 96 * 97 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. 98 * @param int $stackPtr The position of the current token 99 * in the stack passed in $tokens. 100 * @param mixed $before The number of spaces found before the 101 * operator or the string 'newline'. 102 * 103 * @return boolean true if there was no error, false otherwise. 104 */ 105 protected function checkSpacingBeforeOperator(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $before) 106 { 107 if ($before !== 0 108 && ($before !== 'newline' || $this->ignoreNewlines === false) 109 ) { 110 $error = 'Space found before object operator'; 111 $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Before'); 112 if ($fix === true) { 113 $phpcsFile->fixer->replaceToken(($stackPtr - 1), ''); 114 } 115 116 return false; 117 } 118 119 return true; 120 121 }//end checkSpacingBeforeOperator() 122 123 124 /** 125 * Check the spacing after the operator. 126 * 127 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. 128 * @param int $stackPtr The position of the current token 129 * in the stack passed in $tokens. 130 * @param mixed $after The number of spaces found after the 131 * operator or the string 'newline'. 132 * 133 * @return boolean true if there was no error, false otherwise. 134 */ 135 protected function checkSpacingAfterOperator(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $after) 136 { 137 if ($after !== 0 138 && ($after !== 'newline' || $this->ignoreNewlines === false) 139 ) { 140 $error = 'Space found after object operator'; 141 $fix = $phpcsFile->addFixableError($error, $stackPtr, 'After'); 142 if ($fix === true) { 143 $phpcsFile->fixer->replaceToken(($stackPtr + 1), ''); 144 } 145 146 return false; 147 } 148 149 return true; 150 151 }//end checkSpacingAfterOperator() 152 153 154}//end class 155