| Conditions | 11 |
| Paths | 35 |
| Total Lines | 63 |
| 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 |
||
| 141 | protected function inlineUserTag($excerpt) |
||
| 142 | { |
||
| 143 | if (!$this->enableUserTagParser) { |
||
| 144 | return null; |
||
| 145 | } |
||
| 146 | |||
| 147 | $text = &$excerpt['text']; |
||
| 148 | $start = strpos($text, '@'); |
||
| 149 | |||
| 150 | if ($this->isSingleWord($excerpt)) { |
||
| 151 | if (!isset($text[$start + 1])) { |
||
| 152 | return null; |
||
| 153 | } |
||
| 154 | |||
| 155 | $exitChar = $text[$start + 1] === '{' ? '}' : ":,.\'\n "; // <-- space at the end |
||
| 156 | $end = $this->strpos($text, $exitChar, $start); |
||
| 157 | |||
| 158 | if ($end === false) { |
||
| 159 | $end = mb_strlen($text) - $start; |
||
| 160 | } |
||
| 161 | |||
| 162 | $length = $end - $start; |
||
| 163 | $start += 1; |
||
| 164 | $end -= 1; |
||
| 165 | |||
| 166 | if ($exitChar == '}') { |
||
| 167 | $start += 1; |
||
| 168 | $end -= 1; |
||
| 169 | |||
| 170 | $length += 1; |
||
| 171 | } |
||
| 172 | |||
| 173 | $name = substr($text, $start, $end); |
||
| 174 | |||
| 175 | // user name ends with ")" -- we strip if login is within bracket |
||
| 176 | if (strlen($name) > 0 && $name[mb_strlen($name) - 1] === ')' && mb_strpos($name, '(') === false) { |
||
| 177 | $name = mb_substr($name, 0, -1); |
||
| 178 | $length -= 1; |
||
| 179 | } |
||
| 180 | |||
| 181 | $user = $this->user->findByName($name); |
||
| 182 | |||
| 183 | $replacement = [ |
||
| 184 | 'extent' => $length, |
||
| 185 | 'element' => [ |
||
| 186 | 'name' => 'a', |
||
| 187 | 'text' => '@' . $name |
||
| 188 | ] |
||
| 189 | ]; |
||
| 190 | |||
| 191 | if ($user) { |
||
| 192 | $replacement['element']['attributes'] = [ |
||
| 193 | 'href' => route('profile', [$user->id]), |
||
| 194 | 'data-user-id' => $user->id, |
||
| 195 | 'class' => 'mention' |
||
| 196 | ]; |
||
| 197 | } else { |
||
| 198 | $replacement['element']['name'] = 'strong'; |
||
| 199 | } |
||
| 200 | |||
| 201 | return $replacement; |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 246 |