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 |
||
27 | abstract class TypeConverter implements ConverterInterface |
||
28 | { |
||
29 | protected $class_name; |
||
30 | |||
31 | /** |
||
32 | * getTypeClassName |
||
33 | * |
||
34 | * Return the type class name |
||
35 | * |
||
36 | * @return string |
||
37 | */ |
||
38 | abstract protected function getTypeClassName(); |
||
39 | |||
40 | /** |
||
41 | * __construct |
||
42 | * |
||
43 | * Set the type class name. |
||
44 | * |
||
45 | * @param string $class_name |
||
46 | */ |
||
47 | public function __construct($class_name = null) |
||
55 | |||
56 | /** |
||
57 | * fromPg |
||
58 | * |
||
59 | * @see ConverterInterface |
||
60 | */ |
||
61 | public function fromPg($data, $type, Session $session) |
||
71 | |||
72 | /** |
||
73 | * toPg |
||
74 | * |
||
75 | * @see ConverterInterface |
||
76 | */ |
||
77 | View Code Duplication | public function toPg($data, $type, Session $session) |
|
85 | |||
86 | /** |
||
87 | * toPgStandardFormat |
||
88 | * |
||
89 | * @see ConverterInterface |
||
90 | */ |
||
91 | View Code Duplication | public function toPgStandardFormat($data, $type, Session $session) |
|
99 | |||
100 | /** |
||
101 | * checkData |
||
102 | * |
||
103 | * Check if data is suitable for Pg conversion. If not an attempt is made |
||
104 | * to build the object from the given definition. |
||
105 | * |
||
106 | * @param mixed $data |
||
107 | * @return object |
||
108 | */ |
||
109 | public function checkData($data) |
||
119 | |||
120 | /** |
||
121 | * createObjectFrom |
||
122 | * |
||
123 | * Create a range object from a given definition. If the object creation |
||
124 | * fails, an exception is thrown. |
||
125 | * |
||
126 | * @param mixed $data |
||
127 | * @return BaseRange |
||
128 | * @throws ConverterException |
||
129 | */ |
||
130 | protected function createObjectFrom($data) |
||
144 | } |
||
145 |
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.