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 |
||
9 | class ForeignKey extends Analyse implements AnalyseInterface |
||
10 | { |
||
11 | /** |
||
12 | * @var string The description for fields with invalid foreign keys. |
||
13 | */ |
||
14 | const ERROR_INVALID_FOREIGN_KEY = 'There are <strong>%d</strong> fields that have invalid foreign keys:'; |
||
15 | |||
16 | /** |
||
17 | * @var string The name of the datapackage the current foreign key references. |
||
18 | */ |
||
19 | private $dataPackage; |
||
20 | |||
21 | /** |
||
22 | * @var object The validator. |
||
23 | */ |
||
24 | private $validator; |
||
25 | |||
26 | /** |
||
27 | * @var object The current foreign key being analysed. |
||
28 | */ |
||
29 | private $foreignKey; |
||
30 | |||
31 | /** |
||
32 | * @var array The position of the foreign key columns in the CSV file. |
||
33 | */ |
||
34 | private $csvPositions = []; |
||
35 | |||
36 | /** |
||
37 | * @var string The current foreign key field having it's CSV file position found. |
||
38 | */ |
||
39 | private $csvFieldName; |
||
40 | |||
41 | /** |
||
42 | * @var int The current CSV row being analysed. |
||
43 | */ |
||
44 | private $currentCsvRow; |
||
45 | |||
46 | |||
47 | |||
48 | /** |
||
49 | * Validate that any specified foreign key constraints have been met. |
||
50 | * |
||
51 | * @return boolean Does the data meet the foreign key constraints. |
||
52 | * |
||
53 | * @throws \Exception if a foreign key other than postgresql is specified. |
||
54 | */ |
||
55 | 80 | public function validate() |
|
89 | |||
90 | |||
91 | /** |
||
92 | * Get the package of the specified foreign key. |
||
93 | * |
||
94 | * @return string The package for the foreign key. |
||
95 | */ |
||
96 | 79 | private function getForeignKeyPackage() |
|
101 | |||
102 | |||
103 | /** |
||
104 | * Check that the data package for the current foreign key is a valid package type. |
||
105 | * |
||
106 | * @return boolean Whether the data package is valid |
||
107 | */ |
||
108 | 79 | private function checkValidDataPackageType() |
|
112 | |||
113 | |||
114 | /** |
||
115 | * Handle an invalid data package being referenced in a foreign key. |
||
116 | * |
||
117 | * @return void |
||
118 | * |
||
119 | * @throws \Exception if the data package is not valid. |
||
120 | */ |
||
121 | 7 | private function handleInvalidDataPackageType() |
|
127 | |||
128 | |||
129 | /** |
||
130 | * Set the validator property. |
||
131 | * The type of validator is taken from the type of datapackage. |
||
132 | * |
||
133 | * @return void |
||
134 | */ |
||
135 | 72 | private function setValidator() |
|
139 | |||
140 | |||
141 | /** |
||
142 | * Ensure that the "field" and "reference field" properties of the foreign key are both arrays. |
||
143 | * |
||
144 | * @return void |
||
145 | */ |
||
146 | 72 | private function setFieldArrays() |
|
151 | |||
152 | |||
153 | /** |
||
154 | * Loop through the CSV fields listed in the foreign |
||
155 | * key and build up a list of CSV positions these relate to. |
||
156 | * |
||
157 | * @return boolean Whether any CSV field positions have been found. |
||
158 | * |
||
159 | * @throws \Exception if a foreign key field is not defined in the schema file. |
||
160 | * @throws \Exception if a multi-field foreign key field is not in the CSV file. |
||
161 | */ |
||
162 | 72 | private function getForeignKeyCsvPositions() |
|
188 | |||
189 | |||
190 | /** |
||
191 | * Analyse and check if each CSV row has a valid foreign key. |
||
192 | * |
||
193 | * @return bool Whether all the CSV rows are valid. |
||
194 | */ |
||
195 | 72 | private function validateCsvRows() |
|
221 | |||
222 | |||
223 | /** |
||
224 | * Handle a CSV row having an invalid foreign key. |
||
225 | * |
||
226 | * @param string $csvValueHash The foreign key hash that couldn't be matched. |
||
227 | * |
||
228 | * @return void |
||
229 | */ |
||
230 | 1 | View Code Duplication | private function handleInvalidForeignKey($csvValueHash) |
239 | } |
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.