| Conditions | 12 |
| Paths | 10 |
| Total Lines | 56 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 74 | public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 75 | { |
||
| 76 | if ($this->bowOutEarly() === true) { |
||
| 77 | return; |
||
| 78 | } |
||
| 79 | |||
| 80 | $tokens = $phpcsFile->getTokens(); |
||
| 81 | $functionName = $tokens[$stackPtr]['content']; |
||
| 82 | $functionNameLc = strtolower($functionName); |
||
| 83 | |||
| 84 | // Bow out if not one of the functions we're targetting. |
||
| 85 | if (isset($this->functions[$functionNameLc]) === false) { |
||
| 86 | return; |
||
| 87 | } |
||
| 88 | |||
| 89 | // Get the first parameter in the function call as that should contain the regex(es). |
||
| 90 | $firstParam = $this->getFunctionCallParameter($phpcsFile, $stackPtr, 1); |
||
| 91 | if ($firstParam === false) { |
||
| 92 | return; |
||
| 93 | } |
||
| 94 | |||
| 95 | // Differentiate between an array of patterns passed and a single pattern. |
||
| 96 | $nextNonEmpty = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, $firstParam['start'], ($firstParam['end'] + 1), true); |
||
| 97 | if ($nextNonEmpty !== false && ($tokens[$nextNonEmpty]['code'] === T_ARRAY || $tokens[$nextNonEmpty]['code'] === T_OPEN_SHORT_ARRAY)) { |
||
| 98 | $arrayValues = $this->getFunctionCallParameters($phpcsFile, $nextNonEmpty); |
||
| 99 | if ($functionName === 'preg_replace_callback_array') { |
||
| 100 | // For preg_replace_callback_array(), the patterns will be in the array keys. |
||
| 101 | foreach ($arrayValues as $value) { |
||
| 102 | $hasKey = $phpcsFile->findNext(T_DOUBLE_ARROW, $value['start'], ($value['end'] + 1)); |
||
| 103 | if ($hasKey === false) { |
||
| 104 | continue; |
||
| 105 | } |
||
| 106 | |||
| 107 | $value['end'] = ($hasKey - 1); |
||
| 108 | $value['raw'] = trim($phpcsFile->getTokensAsString($value['start'], ($hasKey - $value['start']))); |
||
| 109 | $this->processRegexPattern($value, $phpcsFile, $value['end'], $functionName); |
||
| 110 | } |
||
| 111 | |||
| 112 | } else { |
||
| 113 | // Otherwise, the patterns will be in the array values. |
||
| 114 | foreach ($arrayValues as $value) { |
||
| 115 | $hasKey = $phpcsFile->findNext(T_DOUBLE_ARROW, $value['start'], ($value['end'] + 1)); |
||
| 116 | if ($hasKey !== false) { |
||
| 117 | $value['start'] = ($hasKey + 1); |
||
| 118 | $value['raw'] = trim($phpcsFile->getTokensAsString($value['start'], (($value['end'] + 1) - $value['start']))); |
||
| 119 | } |
||
| 120 | |||
| 121 | $this->processRegexPattern($value, $phpcsFile, $value['end'], $functionName); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | } else { |
||
| 126 | $this->processRegexPattern($firstParam, $phpcsFile, $stackPtr, $functionName); |
||
| 127 | } |
||
| 128 | |||
| 129 | }//end process() |
||
| 130 | |||
| 237 |