Conditions | 11 |
Paths | 25 |
Total Lines | 40 |
Lines | 21 |
Ratio | 52.5 % |
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 |
||
62 | 233 | public function splitUserRemote($address) { |
|
63 | 4 | if (\strpos($address, '@') === false) { |
|
64 | 4 | $hint = $this->l->t('Invalid Federated Cloud ID'); |
|
65 | throw new HintException('Invalid Federated Cloud ID', $hint); |
||
66 | } |
||
67 | |||
68 | 229 | // Find the first character that is not allowed in user names |
|
69 | 229 | $id = \str_replace('\\', '/', $address); |
|
70 | 229 | $posSlash = \strpos($id, '/'); |
|
71 | $posColon = \strpos($id, ':'); |
||
72 | 229 | ||
73 | 18 | View Code Duplication | if ($posSlash === false && $posColon === false) { |
|
|||
74 | 229 | $invalidPos = \strlen($id); |
|
75 | 5 | } elseif ($posSlash === false) { |
|
76 | 211 | $invalidPos = $posColon; |
|
77 | 41 | } elseif ($posColon === false) { |
|
78 | 41 | $invalidPos = $posSlash; |
|
79 | 165 | } else { |
|
80 | $invalidPos = \min($posSlash, $posColon); |
||
81 | } |
||
82 | |||
83 | 229 | // Find the last @ before $invalidPos |
|
84 | 229 | $pos = $lastAtPos = 0; |
|
85 | 229 | View Code Duplication | while ($lastAtPos !== false && $lastAtPos <= $invalidPos) { |
86 | 229 | $pos = $lastAtPos; |
|
87 | 229 | $lastAtPos = \strpos($id, '@', $pos + 1); |
|
88 | } |
||
89 | 229 | ||
90 | 229 | View Code Duplication | if ($pos !== false) { |
91 | 229 | $user = \substr($id, 0, $pos); |
|
92 | 229 | $remote = \substr($id, $pos + 1); |
|
93 | 229 | $remote = $this->fixRemoteURL($remote); |
|
94 | 225 | if (!empty($user) && !empty($remote)) { |
|
95 | return [$user, $remote]; |
||
96 | 4 | } |
|
97 | } |
||
98 | 4 | ||
99 | 4 | $hint = $this->l->t('Invalid Federated Cloud ID'); |
|
100 | throw new HintException('Invalid Federated Cloud ID', $hint); |
||
101 | } |
||
102 | |||
156 |
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.