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 |
||
52 | class InsertUpdateTable extends AbstractTarget implements TargetInterface |
||
53 | { |
||
54 | /** |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $table_records; |
||
58 | |||
59 | /** |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $configuration; |
||
63 | |||
64 | /** |
||
65 | * @var string |
||
66 | */ |
||
67 | protected $firstTime = "1"; |
||
68 | |||
69 | /** |
||
70 | * @var Strategy |
||
71 | */ |
||
72 | protected $strategy; |
||
73 | |||
74 | /** |
||
75 | * @return array |
||
76 | */ |
||
77 | View Code Duplication | public function getConfiguration() |
|
86 | |||
87 | /** |
||
88 | * @param \HDNET\Importr\Domain\Model\Strategy $strategy |
||
89 | * |
||
90 | * @return void |
||
91 | */ |
||
92 | public function start(Strategy $strategy) |
||
96 | |||
97 | /** |
||
98 | * Process every entry in the .csv |
||
99 | * |
||
100 | * @param array $entry |
||
101 | * |
||
102 | * @return int|void |
||
103 | */ |
||
104 | public function processEntry(array $entry) |
||
135 | |||
136 | /** |
||
137 | * |
||
138 | * Fetch all records from the target table, where the PID equals the PID specified |
||
139 | * in the target section of the strategy |
||
140 | * |
||
141 | * @return void |
||
142 | */ |
||
143 | public function getRecords($selectFields) |
||
156 | |||
157 | /** |
||
158 | * Insert record into the target table which you have specified in the target section of the strategy |
||
159 | * |
||
160 | * @param array $entry |
||
161 | * |
||
162 | * @return void |
||
163 | */ |
||
164 | public function insertRecord(array $entry) |
||
180 | |||
181 | /** |
||
182 | * Update a record in the target table which you have specified in the |
||
183 | * target section of the strategy (don't update the password) |
||
184 | * |
||
185 | * @param array $entry |
||
186 | * |
||
187 | * @return void |
||
188 | */ |
||
189 | public function updateRecord(array $entry) |
||
207 | |||
208 | /** |
||
209 | * This function creates a duplicate of a associative array and optionally removes |
||
210 | * any entries which are also elements of a second array |
||
211 | * |
||
212 | * @param array $arr |
||
213 | * @param array $exclude_arr |
||
214 | * |
||
215 | * @return array |
||
216 | */ |
||
217 | public function duplicateArray(array $arr, array $exclude_arr = null) |
||
242 | |||
243 | public function end() |
||
246 | } |
||
247 |
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.