| Conditions | 33 |
| Paths | 1896 |
| Total Lines | 98 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 55 | public function process(File $phpcsFile, $stackPtr) |
||
| 56 | { |
||
| 57 | $tokens = $phpcsFile->getTokens(); |
||
| 58 | |||
| 59 | $ignoreBefore = false; |
||
| 60 | $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
||
| 61 | if ($tokens[$prev]['code'] === T_END_HEREDOC || $tokens[$prev]['code'] === T_END_NOWDOC) { |
||
| 62 | // Spacing before must be preserved due to the here/nowdoc closing tag. |
||
| 63 | $ignoreBefore = true; |
||
| 64 | } |
||
| 65 | |||
| 66 | $this->spacing = (int) $this->spacing; |
||
| 67 | |||
| 68 | if ($ignoreBefore === false) { |
||
| 69 | if ($tokens[($stackPtr - 1)]['code'] !== T_WHITESPACE) { |
||
| 70 | $before = 0; |
||
| 71 | } else { |
||
| 72 | if ($tokens[($stackPtr - 2)]['line'] !== $tokens[$stackPtr]['line']) { |
||
| 73 | $before = 'newline'; |
||
| 74 | } else { |
||
| 75 | $before = $tokens[($stackPtr - 1)]['length']; |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | $phpcsFile->recordMetric($stackPtr, 'Spacing before string concat', $before); |
||
| 80 | } |
||
| 81 | |||
| 82 | if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) { |
||
| 83 | $after = 0; |
||
| 84 | } else { |
||
| 85 | if ($tokens[($stackPtr + 2)]['line'] !== $tokens[$stackPtr]['line']) { |
||
| 86 | $after = 'newline'; |
||
| 87 | } else { |
||
| 88 | $after = $tokens[($stackPtr + 1)]['length']; |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | $phpcsFile->recordMetric($stackPtr, 'Spacing after string concat', $after); |
||
| 93 | |||
| 94 | if (($ignoreBefore === true |
||
| 95 | || $before === $this->spacing |
||
| 96 | || ($before === 'newline' |
||
| 97 | && $this->ignoreNewlines === true)) |
||
| 98 | && ($after === $this->spacing |
||
| 99 | || ($after === 'newline' |
||
| 100 | && $this->ignoreNewlines === true)) |
||
| 101 | ) { |
||
| 102 | return; |
||
| 103 | } |
||
| 104 | |||
| 105 | if ($this->spacing === 0) { |
||
| 106 | $message = 'Concat operator must not be surrounded by spaces'; |
||
| 107 | $data = []; |
||
| 108 | } else { |
||
| 109 | if ($this->spacing > 1) { |
||
| 110 | $message = 'Concat operator must be surrounded by %s spaces'; |
||
| 111 | } else { |
||
| 112 | $message = 'Concat operator must be surrounded by a single space'; |
||
| 113 | } |
||
| 114 | |||
| 115 | $data = [$this->spacing]; |
||
| 116 | } |
||
| 117 | |||
| 118 | $fix = $phpcsFile->addFixableError($message, $stackPtr, 'PaddingFound', $data); |
||
| 119 | |||
| 120 | if ($fix === true) { |
||
| 121 | $padding = str_repeat(' ', $this->spacing); |
||
| 122 | if ($ignoreBefore === false && ($before !== 'newline' || $this->ignoreNewlines === false)) { |
||
| 123 | if ($tokens[($stackPtr - 1)]['code'] === T_WHITESPACE) { |
||
| 124 | $phpcsFile->fixer->beginChangeset(); |
||
| 125 | $phpcsFile->fixer->replaceToken(($stackPtr - 1), $padding); |
||
| 126 | if ($this->spacing === 0 |
||
| 127 | && ($tokens[($stackPtr - 2)]['code'] === T_LNUMBER |
||
| 128 | || $tokens[($stackPtr - 2)]['code'] === T_DNUMBER) |
||
| 129 | ) { |
||
| 130 | $phpcsFile->fixer->replaceToken(($stackPtr - 2), '('.$tokens[($stackPtr - 2)]['content'].')'); |
||
| 131 | } |
||
| 132 | |||
| 133 | $phpcsFile->fixer->endChangeset(); |
||
| 134 | } else if ($this->spacing > 0) { |
||
| 135 | $phpcsFile->fixer->addContent(($stackPtr - 1), $padding); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | if ($after !== 'newline' || $this->ignoreNewlines === false) { |
||
| 140 | if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) { |
||
| 141 | $phpcsFile->fixer->beginChangeset(); |
||
| 142 | $phpcsFile->fixer->replaceToken(($stackPtr + 1), $padding); |
||
| 143 | if ($this->spacing === 0 |
||
| 144 | && ($tokens[($stackPtr + 2)]['code'] === T_LNUMBER |
||
| 145 | || $tokens[($stackPtr + 2)]['code'] === T_DNUMBER) |
||
| 146 | ) { |
||
| 147 | $phpcsFile->fixer->replaceToken(($stackPtr + 2), '('.$tokens[($stackPtr + 2)]['content'].')'); |
||
| 148 | } |
||
| 149 | |||
| 150 | $phpcsFile->fixer->endChangeset(); |
||
| 151 | } else if ($this->spacing > 0) { |
||
| 152 | $phpcsFile->fixer->addContent($stackPtr, $padding); |
||
| 153 | } |
||
| 161 |