| Conditions | 11 |
| Paths | 25 |
| Total Lines | 38 |
| Code Lines | 24 |
| 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 |
||
| 31 | public function resolveCloudId($cloudId) { |
||
| 32 | // TODO magic here to get the url and user instead of just splitting on @ |
||
| 33 | |||
| 34 | if (!$this->isValidCloudId($cloudId)) { |
||
| 35 | throw new \InvalidArgumentException('Invalid cloud id'); |
||
| 36 | } |
||
| 37 | |||
| 38 | // Find the first character that is not allowed in user names |
||
| 39 | $id = $this->fixRemoteURL($cloudId); |
||
| 40 | $posSlash = strpos($id, '/'); |
||
| 41 | $posColon = strpos($id, ':'); |
||
| 42 | |||
| 43 | if ($posSlash === false && $posColon === false) { |
||
| 44 | $invalidPos = strlen($id); |
||
| 45 | } else if ($posSlash === false) { |
||
| 46 | $invalidPos = $posColon; |
||
| 47 | } else if ($posColon === false) { |
||
| 48 | $invalidPos = $posSlash; |
||
| 49 | } else { |
||
| 50 | $invalidPos = min($posSlash, $posColon); |
||
| 51 | } |
||
| 52 | |||
| 53 | // Find the last @ before $invalidPos |
||
| 54 | $pos = $lastAtPos = 0; |
||
| 55 | while ($lastAtPos !== false && $lastAtPos <= $invalidPos) { |
||
| 56 | $pos = $lastAtPos; |
||
| 57 | $lastAtPos = strpos($id, '@', $pos + 1); |
||
| 58 | } |
||
| 59 | |||
| 60 | if ($pos !== false) { |
||
| 61 | $user = substr($id, 0, $pos); |
||
| 62 | $remote = substr($id, $pos + 1); |
||
| 63 | if (!empty($user) && !empty($remote)) { |
||
| 64 | return new CloudId($id, $user, $remote); |
||
| 65 | } |
||
| 66 | } |
||
| 67 | throw new \InvalidArgumentException('Invalid cloud id'); |
||
| 68 | } |
||
| 69 | |||
| 110 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.