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 Lexical extends Analyse implements AnalyseInterface |
||
10 | { |
||
11 | /** |
||
12 | * @var string The description for fields with invalid formats. |
||
13 | */ |
||
14 | const ERROR_INVALID_PATTERN = 'There are <strong>%d</strong> fields that don\'t have the correct pattern:'; |
||
15 | |||
16 | /** |
||
17 | * @var string The description for fields with invalid formats. |
||
18 | */ |
||
19 | const ERROR_INVALID_FORMAT = 'There are <strong>%d</strong> fields that don\'t have the correct format:'; |
||
20 | |||
21 | /** |
||
22 | * @var array The current CSV row being analysed. |
||
23 | */ |
||
24 | private $currentCsvRow; |
||
25 | |||
26 | /** |
||
27 | * @var int The position of the CSV column currently being analysed. |
||
28 | */ |
||
29 | private $csvColumnPosition; |
||
30 | |||
31 | /** |
||
32 | * @var int The position of the current CSV row row in the CSV file. |
||
33 | */ |
||
34 | private $rowNumber; |
||
35 | |||
36 | /** |
||
37 | * @var object The schema definition for the column currently being analysed. |
||
38 | */ |
||
39 | private $schemaColumn; |
||
40 | |||
41 | /** |
||
42 | * @var int The number of columns in the currently analysed row. |
||
43 | */ |
||
44 | private $columnCount; |
||
45 | |||
46 | /** |
||
47 | * @var int The number of columns expected in each row. |
||
48 | * This is taken from the CSV header row. |
||
49 | */ |
||
50 | private $expectedColumnCount; |
||
51 | |||
52 | /** |
||
53 | * @var string The pattern to validate the current field against. |
||
54 | */ |
||
55 | private $pattern; |
||
56 | |||
57 | /** |
||
58 | * @var string The format to validate the current field against. |
||
59 | */ |
||
60 | private $format; |
||
61 | |||
62 | /** |
||
63 | * @var bool Whether the file is valid. |
||
64 | */ |
||
65 | private $valid; |
||
66 | |||
67 | |||
68 | /** |
||
69 | * Validate that all fields are of the correct type, format and pattern. |
||
70 | * This also checks that each CSV row has the expected number of columns. |
||
71 | * |
||
72 | * @access public |
||
73 | * |
||
74 | * @return boolean Is all data lexically valid. |
||
75 | */ |
||
76 | 28 | public function validate() |
|
77 | { |
||
78 | 28 | $this->valid = true; |
|
79 | 28 | $this->rowNumber = 1; |
|
80 | |||
81 | 28 | parent::rewindFilePointerToFirstData(); |
|
1 ignored issue
–
show
|
|||
82 | |||
83 | 28 | while ($this->currentCsvRow = parent::loopThroughFileRows()) { |
|
1 ignored issue
–
show
|
|||
84 | 28 | if (!$this->checkRowHasExpectedColumnCount()) { |
|
85 | $this->handleUnexpectedColumnCount(); |
||
86 | } |
||
87 | |||
88 | 28 | for ($this->csvColumnPosition = 0; $this->csvColumnPosition < $this->columnCount; $this->csvColumnPosition++) { |
|
89 | 28 | $this->schemaColumn = $this->getSchemaColumnFromCsvColumnPosition($this->csvColumnPosition); |
|
90 | |||
91 | 28 | if (!$this->checkMandatoryColumnHasData()) { |
|
92 | 2 | $this->handleInvalidMandatoryColumn(); |
|
93 | |||
94 | 2 | if ($this->stopIfInvalid) { |
|
95 | return false; |
||
96 | } |
||
97 | 2 | } |
|
98 | |||
99 | 28 | if (!$this->validateSpecificFormat()) { |
|
100 | 8 | $this->handleInvalidFormat(); |
|
101 | |||
102 | 8 | if ($this->stopIfInvalid) { |
|
103 | return false; |
||
104 | } |
||
105 | 8 | } |
|
106 | |||
107 | 28 | if (!$this->validatePattern()) { |
|
108 | $this->handleInvalidPattern(); |
||
109 | |||
110 | if ($this->stopIfInvalid) { |
||
111 | return false; |
||
112 | } |
||
113 | } |
||
114 | 28 | } |
|
115 | |||
116 | 28 | $this->rowNumber++; |
|
117 | 28 | } |
|
118 | |||
119 | 28 | $this->setRowsAnalysedStatistic(); |
|
120 | |||
121 | 28 | return $this->valid; |
|
122 | } |
||
123 | |||
124 | |||
125 | /** |
||
126 | * Check that the specified row has the expected number of columns. |
||
127 | * The expected number of columns is the number of columns in the CSV header row. |
||
128 | * |
||
129 | * @return boolean Whether the current row has the expected number of columns. |
||
130 | */ |
||
131 | 28 | private function checkRowHasExpectedColumnCount() |
|
132 | { |
||
133 | 28 | $this->columnCount = count($this->currentCsvRow); |
|
134 | 28 | $this->expectedColumnCount = count(parent::$headerColumns); |
|
135 | |||
136 | 28 | return ($this->expectedColumnCount === $this->columnCount); |
|
137 | } |
||
138 | |||
139 | |||
140 | /** |
||
141 | * Set an error and update the application as the current row has an unexpected number of columns. |
||
142 | * |
||
143 | * @access private |
||
144 | * |
||
145 | * @return void |
||
146 | */ |
||
147 | private function handleUnexpectedColumnCount() |
||
153 | |||
154 | |||
155 | /** |
||
156 | * Check whether the current column is mandatory and if so, whether it has data in it. |
||
157 | * |
||
158 | * @access private |
||
159 | * |
||
160 | * @return boolean Whether the column has data in it. |
||
161 | */ |
||
162 | 28 | private function checkMandatoryColumnHasData() |
|
170 | |||
171 | |||
172 | /** |
||
173 | * Set an error and update the application as the current column is mandatory and has no data in it. |
||
174 | * |
||
175 | * @access private |
||
176 | * |
||
177 | * @return void |
||
178 | */ |
||
179 | 2 | View Code Duplication | private function handleInvalidMandatoryColumn() |
1 ignored issue
–
show
|
|||
180 | { |
||
181 | 2 | $errorMessage = $this->schemaColumn->name . " on row $this->rowNumber is missing."; |
|
182 | 2 | $this->setError(Analyse::ERROR_REQUIRED_FIELD_MISSING_DATA, $errorMessage); |
|
183 | 2 | $this->setErrorRowStatistic($this->rowNumber); |
|
184 | 2 | $this->valid = false; |
|
185 | 2 | } |
|
186 | |||
187 | |||
188 | /** |
||
189 | * Check that the data in the current field is of a valid format as specified in the schema for this column. |
||
190 | * This instantiates and passed the data to the format validator for this field type. |
||
191 | * |
||
192 | * @access private |
||
193 | * |
||
194 | * @return boolean Whether the current field is of a valid format. |
||
195 | */ |
||
196 | 28 | private function validateSpecificFormat() |
|
205 | |||
206 | |||
207 | /** |
||
208 | * Set an error and update the application as the current data didn't match the specified format. |
||
209 | * |
||
210 | * @access private |
||
211 | * |
||
212 | * @return void |
||
213 | */ |
||
214 | 8 | View Code Duplication | private function handleInvalidFormat() |
1 ignored issue
–
show
|
|||
215 | { |
||
216 | 8 | $errorMessage = "The data in column " . $this->schemaColumn->name ." on row $this->rowNumber doesn't "; |
|
217 | 8 | $errorMessage .= "match the required format of $this->format."; |
|
218 | 8 | $this->setError(self::ERROR_INVALID_FORMAT, $errorMessage); |
|
219 | 8 | $this->setErrorRowStatistic($this->rowNumber); |
|
220 | 8 | $this->valid = false; |
|
221 | 8 | } |
|
222 | |||
223 | |||
224 | /** |
||
225 | * Get the pattern of the specified column. |
||
226 | * |
||
227 | * @access private |
||
228 | * |
||
229 | * @return string The pattern or null if no pattern is specified. |
||
230 | */ |
||
231 | 28 | private function getColumnPattern() |
|
238 | |||
239 | |||
240 | /** |
||
241 | * Check that the input matches the specified pattern. |
||
242 | * |
||
243 | * @access private |
||
244 | * |
||
245 | * @return boolean Is the data valid. |
||
246 | */ |
||
247 | 28 | private function validatePattern() |
|
258 | |||
259 | |||
260 | /** |
||
261 | * Set an error and update the application as the current data didn't match the specified pattern. |
||
262 | * |
||
263 | * @access private |
||
264 | * |
||
265 | * @return void |
||
266 | */ |
||
267 | View Code Duplication | private function handleInvalidPattern() |
|
1 ignored issue
–
show
|
|||
268 | { |
||
269 | $errorMessage = "The data in column " . $this->schemaColumn->name . " on row $this->rowNumber doesn't "; |
||
270 | $errorMessage .= "match the required pattern of $this->pattern."; |
||
271 | $this->setError(self::ERROR_INVALID_PATTERN, $errorMessage); |
||
272 | $this->setErrorRowStatistic($this->rowNumber); |
||
273 | $this->valid = false; |
||
274 | } |
||
275 | |||
276 | |||
277 | /** |
||
278 | * Add the number of rows analysed to the statistics. |
||
279 | * |
||
280 | * @access private |
||
281 | * |
||
282 | * @return void |
||
283 | */ |
||
284 | 28 | private function setRowsAnalysedStatistic() |
|
288 | } |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.