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