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 | public function validate() |
||
| 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 | private function checkRowHasExpectedColumnCount() |
||
| 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 | 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 | private function handleInvalidMandatoryColumn() |
||
| 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 | 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 | View Code Duplication | private function handleInvalidFormat() |
|
| 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 | 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 | 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() |
|
| 275 | |||
| 276 | |||
| 277 | /** |
||
| 278 | * Add the number of rows analysed to the statistics. |
||
| 279 | * |
||
| 280 | * @access private |
||
| 281 | * |
||
| 282 | * @return void |
||
| 283 | */ |
||
| 284 | 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 theSoncalls the wrong method in the parent class.