| Conditions | 14 |
| Paths | 91 |
| Total Lines | 41 |
| 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 |
||
| 85 | public function getAvatar($userId = null, array $parameters = []): string |
||
| 86 | { |
||
| 87 | $userEntity = $this->findUser($userId); |
||
| 88 | if (!$userEntity) { |
||
| 89 | throw new \InvalidArgumentException('Invalid userId provided'); |
||
| 90 | } |
||
| 91 | |||
| 92 | $avatarPath = $this->avatarImagePath; |
||
| 93 | $userAttributes = $userEntity->getAttributes(); |
||
| 94 | $key = $this->prefix . ':avatar'; |
||
| 95 | $avatar = $userAttributes[$key] ? $userAttributes[$key]->getValue() : $this->avatarDefaultImage; |
||
| 96 | |||
| 97 | $avatarUrl = ''; |
||
| 98 | if (!in_array($avatar, ['blank.gif', 'blank.jpg'], true)) { |
||
| 99 | if (isset($avatar) && !empty($avatar) && $avatar !== $this->avatarDefaultImage && file_exists($this->projectDir . '/' . $avatarPath . '/' . $avatar)) { |
||
| 100 | $request = $this->requestStack->getCurrentRequest(); |
||
| 101 | if (null !== $request) { |
||
| 102 | $avatarPath = s($avatarPath)->after('public/')->toString(); |
||
| 103 | $avatarUrl = $request->getSchemeAndHttpHost() . $request->getBasePath() . '/' . $avatarPath . '/' . $avatar; |
||
| 104 | } |
||
| 105 | } elseif (true === $this->gravatarEnabled) { |
||
| 106 | $parameters = $this->squareSize($parameters); |
||
| 107 | $avatarUrl = $this->gravatarHelper->getGravatarUrl($userEntity->getEmail(), $parameters); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | if (empty($avatarUrl)) { |
||
| 112 | // e.g. blank.gif or empty avatars |
||
| 113 | return ''; |
||
| 114 | } |
||
| 115 | |||
| 116 | if (!isset($parameters['class'])) { |
||
| 117 | $parameters['class'] = 'img-fluid img-thumbnail'; |
||
| 118 | } |
||
| 119 | $attributes = ' class="' . str_replace('"', '', htmlspecialchars($parameters['class'])) . '"'; |
||
| 120 | $attributes .= isset($parameters['width']) ? ' width="' . (int) $parameters['width'] . '"' : ''; |
||
| 121 | $attributes .= isset($parameters['height']) ? ' height="' . (int) $parameters['height'] . '"' : ''; |
||
| 122 | |||
| 123 | $result = '<img src="' . str_replace('"', '', htmlspecialchars($avatarUrl)) . '" title="' . str_replace('"', '', htmlspecialchars($userEntity->getUsername())) . '" alt="' . str_replace('"', '', htmlspecialchars($userEntity->getUsername())) . '"' . $attributes . ' />'; |
||
| 124 | |||
| 125 | return $result; |
||
| 126 | } |
||
| 178 |