comparison vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Semantics/FunctionCall.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
1 <?php 1 <?php
2 /** 2 /**
3 * Drupal_Sniffs_Semantics_FunctionCall. 3 * \Drupal\Sniffs\Semantics\FunctionCall.
4 * 4 *
5 * @category PHP 5 * @category PHP
6 * @package PHP_CodeSniffer 6 * @package PHP_CodeSniffer
7 * @link http://pear.php.net/package/PHP_CodeSniffer 7 * @link http://pear.php.net/package/PHP_CodeSniffer
8 */ 8 */
9
10 namespace Drupal\Sniffs\Semantics;
11
12 use PHP_CodeSniffer\Files\File;
13 use PHP_CodeSniffer\Sniffs\Sniff;
14 use PHP_CodeSniffer\Util\Tokens;
9 15
10 /** 16 /**
11 * Helper class to sniff for specific function calls. 17 * Helper class to sniff for specific function calls.
12 * 18 *
13 * @category PHP 19 * @category PHP
14 * @package PHP_CodeSniffer 20 * @package PHP_CodeSniffer
15 * @link http://pear.php.net/package/PHP_CodeSniffer 21 * @link http://pear.php.net/package/PHP_CodeSniffer
16 */ 22 */
17 abstract class Drupal_Sniffs_Semantics_FunctionCall implements PHP_CodeSniffer_Sniff 23 abstract class FunctionCall implements Sniff
18 { 24 {
19 25
20 /** 26 /**
21 * The currently processed file. 27 * The currently processed file.
22 * 28 *
23 * @var PHP_CodeSniffer_File 29 * @var \PHP_CodeSniffer\Files\File
24 */ 30 */
25 protected $phpcsFile; 31 protected $phpcsFile;
26 32
27 /** 33 /**
28 * The token position of the function call. 34 * The token position of the function call.
74 80
75 81
76 /** 82 /**
77 * Processes this test, when one of its tokens is encountered. 83 * Processes this test, when one of its tokens is encountered.
78 * 84 *
79 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. 85 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
80 * @param int $stackPtr The position of the current token 86 * @param int $stackPtr The position of the current token
81 * in the stack passed in $tokens. 87 * in the stack passed in $tokens.
82 * 88 *
83 * @return void 89 * @return void
84 */ 90 */
85 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) 91 public function process(File $phpcsFile, $stackPtr)
86 { 92 {
87 $tokens = $phpcsFile->getTokens(); 93 $tokens = $phpcsFile->getTokens();
88 $functionName = $tokens[$stackPtr]['content']; 94 $functionName = $tokens[$stackPtr]['content'];
89 if (in_array($functionName, $this->registerFunctionNames()) === false) { 95 if (in_array($functionName, $this->registerFunctionNames()) === false) {
90 // Not interested in this function. 96 // Not interested in this function.
94 if ($this->isFunctionCall($phpcsFile, $stackPtr) === false) { 100 if ($this->isFunctionCall($phpcsFile, $stackPtr) === false) {
95 return; 101 return;
96 } 102 }
97 103
98 // Find the next non-empty token. 104 // Find the next non-empty token.
99 $openBracket = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true); 105 $openBracket = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
100 106
101 $this->phpcsFile = $phpcsFile; 107 $this->phpcsFile = $phpcsFile;
102 $this->functionCall = $stackPtr; 108 $this->functionCall = $stackPtr;
103 $this->openBracket = $openBracket; 109 $this->openBracket = $openBracket;
104 $this->closeBracket = $tokens[$openBracket]['parenthesis_closer']; 110 $this->closeBracket = $tokens[$openBracket]['parenthesis_closer'];
110 116
111 117
112 /** 118 /**
113 * Checks if this is a function call. 119 * Checks if this is a function call.
114 * 120 *
115 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. 121 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
116 * @param int $stackPtr The position of the current token 122 * @param int $stackPtr The position of the current token
117 * in the stack passed in $tokens. 123 * in the stack passed in $tokens.
118 * 124 *
119 * @return bool 125 * @return bool
120 */ 126 */
121 protected function isFunctionCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr) 127 protected function isFunctionCall(File $phpcsFile, $stackPtr)
122 { 128 {
123 $tokens = $phpcsFile->getTokens(); 129 $tokens = $phpcsFile->getTokens();
124 // Find the next non-empty token. 130 // Find the next non-empty token.
125 $openBracket = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true); 131 $openBracket = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
126 132
127 if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) { 133 if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) {
128 // Not a function call. 134 // Not a function call.
129 return false; 135 return false;
130 } 136 }
133 // Not a function call. 139 // Not a function call.
134 return false; 140 return false;
135 } 141 }
136 142
137 // Find the previous non-empty token. 143 // Find the previous non-empty token.
138 $search = PHP_CodeSniffer_Tokens::$emptyTokens; 144 $search = Tokens::$emptyTokens;
139 $search[] = T_BITWISE_AND; 145 $search[] = T_BITWISE_AND;
140 $previous = $phpcsFile->findPrevious($search, ($stackPtr - 1), null, true); 146 $previous = $phpcsFile->findPrevious($search, ($stackPtr - 1), null, true);
141 if ($tokens[$previous]['code'] === T_FUNCTION) { 147 if ($tokens[$previous]['code'] === T_FUNCTION) {
142 // It's a function definition, not a function call. 148 // It's a function definition, not a function call.
143 return false; 149 return false;
173 return $this->arguments[$number]; 179 return $this->arguments[$number];
174 } 180 }
175 181
176 $tokens = $this->phpcsFile->getTokens(); 182 $tokens = $this->phpcsFile->getTokens();
177 // Start token of the first argument. 183 // Start token of the first argument.
178 $start = $this->phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($this->openBracket + 1), null, true); 184 $start = $this->phpcsFile->findNext(Tokens::$emptyTokens, ($this->openBracket + 1), null, true);
179 if ($start === $this->closeBracket) { 185 if ($start === $this->closeBracket) {
180 // Function call has no arguments, so return false. 186 // Function call has no arguments, so return false.
181 return false; 187 return false;
182 } 188 }
183 189
184 // End token of the last argument. 190 // End token of the last argument.
185 $end = $this->phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($this->closeBracket - 1), null, true); 191 $end = $this->phpcsFile->findPrevious(Tokens::$emptyTokens, ($this->closeBracket - 1), null, true);
186 $lastArgEnd = $end; 192 $lastArgEnd = $end;
187 $nextSeperator = $this->openBracket; 193 $nextSeperator = $this->openBracket;
188 $counter = 1; 194 $counter = 1;
189 while (($nextSeperator = $this->phpcsFile->findNext(T_COMMA, ($nextSeperator + 1), $this->closeBracket)) !== false) { 195 while (($nextSeperator = $this->phpcsFile->findNext(T_COMMA, ($nextSeperator + 1), $this->closeBracket)) !== false) {
190 // Make sure the comma belongs directly to this function call, 196 // Make sure the comma belongs directly to this function call,
194 if ($lastBracket !== $this->closeBracket) { 200 if ($lastBracket !== $this->closeBracket) {
195 continue; 201 continue;
196 } 202 }
197 203
198 // Update the end token of the current argument. 204 // Update the end token of the current argument.
199 $end = $this->phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($nextSeperator - 1), null, true); 205 $end = $this->phpcsFile->findPrevious(Tokens::$emptyTokens, ($nextSeperator - 1), null, true);
200 // Save the calculated findings for the current argument. 206 // Save the calculated findings for the current argument.
201 $this->arguments[$counter] = array( 207 $this->arguments[$counter] = array(
202 'start' => $start, 208 'start' => $start,
203 'end' => $end, 209 'end' => $end,
204 ); 210 );
205 if ($counter === $number) { 211 if ($counter === $number) {
206 break; 212 break;
207 } 213 }
208 214
209 $counter++; 215 $counter++;
210 $start = $this->phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($nextSeperator + 1), null, true); 216 $start = $this->phpcsFile->findNext(Tokens::$emptyTokens, ($nextSeperator + 1), null, true);
211 $end = $lastArgEnd; 217 $end = $lastArgEnd;
212 }//end while 218 }//end while
213 219
214 // If the counter did not reach the passed number something is wrong. 220 // If the counter did not reach the passed number something is wrong.
215 if ($counter !== $number) { 221 if ($counter !== $number) {