Conditions | 12 |
Paths | 99 |
Total Lines | 48 |
Code Lines | 26 |
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 |
||
107 | private function determineProfileLink( |
||
108 | int $userId = null, |
||
109 | string $userName = null, |
||
110 | string $class = '', |
||
111 | string $imagePath = '', |
||
112 | int $maxLength = 0, |
||
113 | string $title = '' |
||
114 | ): string { |
||
115 | if (!isset($userId) && !isset($userName)) { |
||
116 | throw new InvalidArgumentException(); |
||
117 | } |
||
118 | /** @var User $user */ |
||
119 | if (null !== $userId) { |
||
120 | $user = $this->userRepository->find($userId); |
||
121 | } else { |
||
122 | $user = $this->userRepository->findOneBy(['uname' => $userName]); |
||
123 | } |
||
124 | if (!$user) { |
||
125 | return $userId . $userName; // one or the other is empty |
||
126 | } |
||
127 | |||
128 | $userDisplayName = $this->profileHelper->getDisplayName($user->getUid()); |
||
129 | if (!$userDisplayName) { |
||
130 | $userDisplayName = $user->getUname(); |
||
131 | } |
||
132 | |||
133 | $class = !empty($class) ? ' class="' . htmlspecialchars($class, ENT_QUOTES) . '"' : ''; |
||
134 | |||
135 | if (!empty($imagePath)) { |
||
136 | $show = '<img src="' . htmlspecialchars($imagePath, ENT_QUOTES) . '" alt="' . htmlspecialchars($userDisplayName, ENT_QUOTES) . '" />'; |
||
137 | } elseif (0 < $maxLength) { |
||
138 | // truncate the user name to $maxLength chars |
||
139 | $length = mb_strlen($userDisplayName); |
||
140 | $truncEnd = ($maxLength > $length) ? $length : $maxLength; |
||
141 | $show = htmlspecialchars(s($userDisplayName)->slice(0, $truncEnd)->toString(), ENT_QUOTES); |
||
142 | } else { |
||
143 | $show = htmlspecialchars($userDisplayName, ENT_QUOTES); |
||
144 | } |
||
145 | $href = $this->profileHelper->getProfileUrl($user->getUid()); |
||
146 | if ('#' === $href) { |
||
147 | return $userDisplayName; |
||
148 | } |
||
149 | |||
150 | if (empty($title)) { |
||
151 | $title = $this->translator->trans('Profile') . ': ' . $userDisplayName; |
||
152 | } |
||
153 | |||
154 | return '<a' . $class . ' title="' . htmlspecialchars($title, ENT_QUOTES) . '" href="' . $href . '">' . $show . '</a>'; |
||
155 | } |
||
157 |