Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
33 | class AddressHandler { |
||
34 | |||
35 | /** @var IL10N */ |
||
36 | private $l; |
||
37 | |||
38 | /** @var IURLGenerator */ |
||
39 | private $urlGenerator; |
||
40 | |||
41 | /** |
||
42 | * AddressHandler constructor. |
||
43 | * |
||
44 | * @param IURLGenerator $urlGenerator |
||
45 | * @param IL10N $il10n |
||
46 | 256 | */ |
|
47 | public function __construct( |
||
54 | |||
55 | /** |
||
56 | * split user and remote from federated cloud id |
||
57 | * |
||
58 | * @param string $address federated share address |
||
59 | * @return array [user, remoteURL] |
||
60 | * @throws HintException |
||
61 | 233 | */ |
|
62 | 233 | public function splitUserRemote($address) { |
|
102 | |||
103 | /** |
||
104 | * @param string $uid |
||
105 | * |
||
106 | * @return Address |
||
107 | 9 | */ |
|
108 | 9 | public function getLocalUserFederatedAddress($uid) { |
|
112 | |||
113 | /** |
||
114 | * generate remote URL part of federated ID |
||
115 | * |
||
116 | * @return string url of the current server |
||
117 | */ |
||
118 | public function generateRemoteURL() { |
||
122 | 24 | ||
123 | 24 | /** |
|
124 | * remove protocol from URL |
||
125 | 24 | * |
|
126 | * @param string $url |
||
127 | 11 | * @return string |
|
128 | 11 | */ |
|
129 | 11 | public function removeProtocolFromUrl($url) { |
|
133 | 11 | ||
134 | 11 | /** |
|
135 | 11 | * Strips away a potential file names and trailing slashes: |
|
136 | 11 | * - http://localhost |
|
137 | * - http://localhost/ |
||
138 | 11 | * - http://localhost/index.php |
|
139 | 6 | * - http://localhost/index.php/s/{shareToken} |
|
140 | * |
||
141 | 5 | * all return: http://localhost |
|
142 | * |
||
143 | 18 | * @param string $remote |
|
144 | * @return string |
||
145 | */ |
||
146 | View Code Duplication | protected function fixRemoteURL($remote) { |
|
155 | } |
||
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.