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 namespace Gears\String\Methods; |
||
16 | trait CaseManipulators |
||
17 | { |
||
18 | /** |
||
19 | * Converts the first character of the string to lower case. |
||
20 | * |
||
21 | * @return static String with the first character of $str being lower case |
||
22 | */ |
||
23 | View Code Duplication | public function lowerCaseFirst() |
|
37 | |||
38 | /** |
||
39 | * Converts the first character of the supplied string to upper case. |
||
40 | * |
||
41 | * @return static String with the first character of $str being upper case |
||
42 | */ |
||
43 | View Code Duplication | public function upperCaseFirst() |
|
57 | |||
58 | /** |
||
59 | * Returns a case swapped version of the string. |
||
60 | * |
||
61 | * @return Stringy Object whose $str has each character's case swapped |
||
62 | */ |
||
63 | public function swapCase() |
||
84 | } |
||
85 |
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.