| Conditions | 10 |
| Paths | 5 |
| Total Lines | 53 |
| 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 |
||
| 48 | protected function atUserLink(string $string): string |
||
| 49 | { |
||
| 50 | $tags = []; |
||
| 51 | |||
| 52 | /* |
||
| 53 | * - '\pP' all unicode punctuation marks |
||
| 54 | * - '<' if nl2br has taken place whatchout for <br /> linebreaks |
||
| 55 | */ |
||
| 56 | $hasTags = preg_match_all('/(\s|^)@([^\s\pP<]+)/m', $string, $tags); |
||
| 57 | if (!$hasTags) { |
||
| 58 | return $string; |
||
| 59 | } |
||
| 60 | |||
| 61 | // would be cleaner to pass userlist by value, but for performance reasons |
||
| 62 | // we only query the db if we actually have any @ tags |
||
| 63 | $users = $this->_sOptions->get('UserList')->get(); |
||
| 64 | sort($users); |
||
| 65 | $names = []; |
||
| 66 | if (empty($tags[2]) === false) { |
||
| 67 | $tags = $tags[2]; |
||
| 68 | foreach ($tags as $tag) { |
||
| 69 | if (in_array($tag, $users)) { |
||
| 70 | $names[$tag] = 1; |
||
| 71 | } else { |
||
| 72 | $continue = 0; |
||
| 73 | foreach ($users as $user) { |
||
| 74 | if (mb_strpos($user, $tag) === 0) { |
||
| 75 | $names[$user] = 1; |
||
| 76 | $continue = true; |
||
| 77 | } |
||
| 78 | if ($continue === false) { |
||
| 79 | break; |
||
| 80 | } elseif ($continue !== 0) { |
||
| 81 | $continue = false; |
||
| 82 | } |
||
| 83 | } |
||
| 84 | } |
||
| 85 | } |
||
| 86 | } |
||
| 87 | krsort($names); |
||
| 88 | $baseUrl = $this->_sOptions->get('webroot') . $this->_sOptions->get('atBaseUrl'); |
||
| 89 | foreach ($names as $name => $v) { |
||
| 90 | $title = urlencode($name); |
||
| 91 | $link = $this->_url( |
||
| 92 | $baseUrl . $title, |
||
| 93 | "@$name", |
||
| 94 | false |
||
| 95 | ); |
||
| 96 | $string = str_replace("@$name", $link, $string); |
||
| 97 | } |
||
| 98 | |||
| 99 | return $string; |
||
| 100 | } |
||
| 101 | |||
| 192 |