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