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 |
||
45 | class Jid implements Immutable |
||
46 | { |
||
47 | use Accessors; |
||
48 | |||
49 | private $_local; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | private $_domain; |
||
55 | |||
56 | /** |
||
57 | * @var string|null |
||
58 | */ |
||
59 | private $_resource; |
||
60 | |||
61 | 24 | public function __construct(string $address, string $local = null, string $resource = null) |
|
73 | |||
74 | 29 | private static function _split(string $address) |
|
80 | |||
81 | /** |
||
82 | * Validates address and throws InvalidArgumentException in case of failure. |
||
83 | * |
||
84 | * @param string $address |
||
85 | * @param string $local |
||
86 | * @param string|null $resource |
||
87 | * @return bool |
||
88 | */ |
||
89 | 43 | public static function validate(string $address, string $local = null, string $resource = null) |
|
113 | |||
114 | /** |
||
115 | * Returns if address is valid or not. |
||
116 | * |
||
117 | * @param string $address |
||
118 | * @param string $local |
||
119 | * @param string|null $resource |
||
120 | * @return bool |
||
121 | */ |
||
122 | 17 | public static function isValid(string $address, string $local = null, string $resource = null) : bool |
|
130 | |||
131 | 5 | public function __toString() : string |
|
138 | |||
139 | /** |
||
140 | * Returns the domain part of address. |
||
141 | * |
||
142 | * @return string |
||
143 | */ |
||
144 | 11 | public function getDomain() : string |
|
148 | |||
149 | /** |
||
150 | * Returns the local part of JID. |
||
151 | * |
||
152 | * @return string |
||
153 | */ |
||
154 | 8 | public function getLocal() : string |
|
158 | |||
159 | /** |
||
160 | * Returns resource part of address or null if resource is not set |
||
161 | * |
||
162 | * @return null|string |
||
163 | */ |
||
164 | 6 | public function getResource() |
|
168 | |||
169 | 4 | public function bare() |
|
173 | |||
174 | 1 | public function isBare() : bool |
|
178 | |||
179 | 1 | public function isFull() : bool |
|
183 | } |
||
184 |
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.