Complex classes like Analyse often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Analyse, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Analyse extends Base implements AnalyseInterface |
||
12 | { |
||
13 | /** |
||
14 | * @var string The description for missing mandatory columns. |
||
15 | */ |
||
16 | const ERROR_REQUIRED_COLUMN_MISSING = '<strong>%d</strong> required column(s) missing:'; |
||
17 | |||
18 | /** |
||
19 | * @var string The description for CSV columns that are not in the schema. |
||
20 | */ |
||
21 | const ERROR_UNSPECIFIED_COLUMN = '<strong>%d</strong> unexpected column(s):'; |
||
22 | |||
23 | /** |
||
24 | * @var string The description for rows with missing columns. |
||
25 | */ |
||
26 | const ERROR_INCORRECT_COLUMN_COUNT = 'There are the wrong number of columns'; |
||
27 | |||
28 | /** |
||
29 | * @var string The description for rows with missing columns. |
||
30 | */ |
||
31 | const ERROR_REQUIRED_FIELD_MISSING_DATA = 'There are <strong>%d</strong> required fields with missing data:'; |
||
32 | |||
33 | /** |
||
34 | * @var string The format validation type. |
||
35 | */ |
||
36 | const VALIDATION_TYPE_FORMAT = 'Format'; |
||
37 | |||
38 | /** |
||
39 | * @var string The foreign key validation type. |
||
40 | */ |
||
41 | const VALIDATION_TYPE_FOREIGN_KEY = 'ForeignKey'; |
||
42 | |||
43 | /** |
||
44 | * @access protected |
||
45 | * |
||
46 | * @var boolean Should the analysis stop when an error is found. |
||
47 | */ |
||
48 | protected $stopIfInvalid; |
||
49 | |||
50 | /** |
||
51 | * @access protected |
||
52 | * |
||
53 | * @var array Statistics relating to the file analysis. |
||
54 | */ |
||
55 | protected static $statistics = [ |
||
56 | 'rows_with_errors' => [], |
||
57 | 'percent_rows_with_errors' => 0, |
||
58 | 'rows_analysed' => 0 |
||
59 | ]; |
||
60 | |||
61 | /** |
||
62 | * @access protected |
||
63 | * @static |
||
64 | * |
||
65 | * @var array Error messages. |
||
66 | */ |
||
67 | protected static $errors = []; |
||
68 | |||
69 | |||
70 | /** |
||
71 | * Analyse the specified file against the loaded schema. |
||
72 | * |
||
73 | * @access public |
||
74 | * |
||
75 | * @param boolean $stopIfInvalid Should the analysis stop when the file is found to be invalid. |
||
76 | * The default is false. |
||
77 | * |
||
78 | * @return boolean true if the file passes the validation and false if not. |
||
79 | */ |
||
80 | 30 | public function validate($stopIfInvalid = false) |
|
117 | |||
118 | |||
119 | /** |
||
120 | * Get the statistics about the file analysis. |
||
121 | * |
||
122 | * @access public |
||
123 | * |
||
124 | * @return array The statistics. |
||
125 | */ |
||
126 | 10 | public function getStatistics() |
|
136 | |||
137 | |||
138 | /** |
||
139 | * Validate that all mandatory columns are present. |
||
140 | * |
||
141 | * @access private |
||
142 | * |
||
143 | * @return boolean Are all mandatory columns present. |
||
144 | */ |
||
145 | 30 | private function validateMandatoryColumns() |
|
164 | |||
165 | |||
166 | /** |
||
167 | * Check that there are no columns in the CSV that are not specified in the schema. |
||
168 | * |
||
169 | * @access private |
||
170 | * |
||
171 | * @return boolean Are all the CSV columns specified in the schema. |
||
172 | */ |
||
173 | 28 | private function validateUnspecifiedColumns() |
|
190 | |||
191 | |||
192 | /** |
||
193 | * Check if the specified column is mandatory. |
||
194 | * |
||
195 | * @access protected |
||
196 | * |
||
197 | * @param object $schemaColumn The schema column object to examine. |
||
198 | * |
||
199 | * @return boolean Whether the column is mandatory. |
||
200 | */ |
||
201 | 30 | protected function isColumnMandatory($schemaColumn) |
|
208 | |||
209 | |||
210 | /** |
||
211 | * Load and instantiate the specified validator. |
||
212 | * |
||
213 | * @access protected |
||
214 | * |
||
215 | * @param string $validationType The type of validator to load. |
||
216 | * @param string $type The type being validated. |
||
217 | * For formats this will be the field type. |
||
218 | * For foreign keys this will be the datapackage type |
||
219 | * |
||
220 | * @return object The validation object. Throws an exception on error. |
||
221 | * |
||
222 | * @throws \Exception if the validator file couldn't be loaded. |
||
223 | * @throws \Exception if the validator class definition couldn't be found. |
||
224 | */ |
||
225 | 28 | protected function instantiateValidator($validationType, $type) |
|
250 | |||
251 | |||
252 | /** |
||
253 | * Check if the file was found to be valid. |
||
254 | * This checks for any validation errors. |
||
255 | * |
||
256 | * @access private |
||
257 | * |
||
258 | * @return boolean Is the file valid. |
||
259 | */ |
||
260 | 30 | private function isFileValid() |
|
264 | |||
265 | |||
266 | /** |
||
267 | * Return all errors. |
||
268 | * |
||
269 | * @access public |
||
270 | * |
||
271 | * @return array The error messages. |
||
272 | */ |
||
273 | 2 | public function getErrors() |
|
285 | |||
286 | |||
287 | /** |
||
288 | * Add an error message. |
||
289 | * |
||
290 | * @access protected |
||
291 | * |
||
292 | * @param string $type The type of error. |
||
293 | * @param string $error The error message (or field). |
||
294 | * |
||
295 | * @return void |
||
296 | */ |
||
297 | 11 | protected function setError($type, $error) |
|
305 | |||
306 | |||
307 | /** |
||
308 | * Add the row number of a row with an error to the analysis statistics. |
||
309 | * |
||
310 | * @access protected |
||
311 | * |
||
312 | * @param int $row_number The position of the row with the error in the CSV file. |
||
313 | * |
||
314 | * @return void |
||
315 | */ |
||
316 | 9 | protected function setErrorRowStatistic($row_number) |
|
320 | |||
321 | |||
322 | /** |
||
323 | * Clean the rows with errors statistic. |
||
324 | * This removes duplicated records where the same row has had multiple errors. |
||
325 | * |
||
326 | * @access private |
||
327 | * |
||
328 | * @return void |
||
329 | */ |
||
330 | 10 | private function cleanErrorRowStatistic() |
|
334 | |||
335 | |||
336 | /** |
||
337 | * Get the percentage of analysed rows that have had a error with them. |
||
338 | * |
||
339 | * @access private |
||
340 | * |
||
341 | * @return int The percentage. |
||
342 | */ |
||
343 | 10 | private function getErrorRowPercent() |
|
347 | } |
||
348 |