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 |
||
23 | class WriteQuery extends AbstractEntityValueQuery implements WriteQueryInterface |
||
24 | { |
||
25 | const EVENT_BEFORE = 'write.before'; |
||
26 | const EVENT_AFTER = 'write.after'; |
||
27 | |||
28 | use GetTypeTrait; |
||
29 | |||
30 | protected $instance; |
||
31 | |||
32 | protected $values = []; |
||
33 | |||
34 | /** |
||
35 | * Sets field names which values will be written |
||
36 | * |
||
37 | * @param array $fields |
||
38 | * |
||
39 | * @return $this |
||
40 | */ |
||
41 | View Code Duplication | public function values($fields = []) |
|
59 | |||
60 | /** |
||
61 | * Adds field which value will be written |
||
62 | * |
||
63 | * @param string $field |
||
64 | * |
||
65 | * @return $this |
||
66 | */ |
||
67 | public function value($field) |
||
73 | |||
74 | /** |
||
75 | * Assigns value to query |
||
76 | * |
||
77 | * @param FieldInterface $field |
||
78 | */ |
||
79 | protected function assignValue(FieldInterface $field) |
||
83 | |||
84 | |||
85 | /** |
||
86 | * Executes query |
||
87 | * After execution query is reset |
||
88 | * |
||
89 | * @return mixed |
||
90 | */ |
||
91 | View Code Duplication | public function execute() |
|
106 | |||
107 | /** |
||
108 | * Returns current query string |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | public function getSQL() |
||
116 | |||
117 | /** |
||
118 | * @return InsertQuery|UpdateQuery |
||
119 | */ |
||
120 | protected function buildQuery() |
||
132 | |||
133 | /** |
||
134 | * Returns true if entity exists database |
||
135 | * |
||
136 | * @return int |
||
137 | */ |
||
138 | protected function checkIfEntityExists() |
||
154 | |||
155 | /** |
||
156 | * Returns array with bound values and their placeholders as keys |
||
157 | * |
||
158 | * @return array |
||
159 | */ |
||
160 | public function binds() |
||
164 | |||
165 | /** |
||
166 | * Resets adapter |
||
167 | * |
||
168 | * @return $this |
||
169 | */ |
||
170 | public function reset() |
||
177 | } |
||
178 |
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.