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:
Complex classes like DatetimeOffsetFix 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 DatetimeOffsetFix, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class DatetimeOffsetFix extends JobHandler |
||
| 27 | { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Key for the option used to track which models have been processed when doing the batches. |
||
| 31 | */ |
||
| 32 | const MODELS_TO_PROCESS_OPTION_KEY = 'ee_models_processed_for_datetime_offset_fix'; |
||
| 33 | |||
| 34 | |||
| 35 | const COUNT_OF_MODELS_PROCESSED = 'ee_count_of_ee_models_processed_for_datetime_offset_fixed'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Key for the option used to track what the current offset is that will be applied when this tool is executed. |
||
| 39 | */ |
||
| 40 | const OFFSET_TO_APPLY_OPTION_KEY = 'ee_datetime_offset_fix_offset_to_apply'; |
||
| 41 | |||
| 42 | |||
| 43 | const OPTION_KEY_OFFSET_RANGE_START_DATE = 'ee_datetime_offset_start_date_range'; |
||
| 44 | |||
| 45 | |||
| 46 | const OPTION_KEY_OFFSET_RANGE_END_DATE = 'ee_datetime_offset_end_date_range'; |
||
| 47 | |||
| 48 | |||
| 49 | /** |
||
| 50 | * String labelling the datetime offset fix type for change-log entries. |
||
| 51 | */ |
||
| 52 | const DATETIME_OFFSET_FIX_CHANGELOG_TYPE = 'datetime_offset_fix'; |
||
| 53 | |||
| 54 | |||
| 55 | /** |
||
| 56 | * String labelling a datetime offset fix error for change-log entries. |
||
| 57 | */ |
||
| 58 | const DATETIME_OFFSET_FIX_CHANGELOG_ERROR_TYPE = 'datetime_offset_fix_error'; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var EEM_Base[] |
||
| 62 | */ |
||
| 63 | protected $models_with_datetime_fields = array(); |
||
| 64 | |||
| 65 | |||
| 66 | /** |
||
| 67 | * Performs any necessary setup for starting the job. This is also a good |
||
| 68 | * place to setup the $job_arguments which will be used for subsequent HTTP requests |
||
| 69 | * when continue_job will be called |
||
| 70 | * |
||
| 71 | * @param JobParameters $job_parameters |
||
| 72 | * @return JobStepResponse |
||
| 73 | * @throws EE_Error |
||
| 74 | * @throws InvalidArgumentException |
||
| 75 | * @throws InvalidDataTypeException |
||
| 76 | * @throws InvalidInterfaceException |
||
| 77 | */ |
||
| 78 | public function create_job(JobParameters $job_parameters) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Performs another step of the job |
||
| 91 | * |
||
| 92 | * @param JobParameters $job_parameters |
||
| 93 | * @param int $batch_size |
||
| 94 | * @return JobStepResponse |
||
| 95 | * @throws EE_Error |
||
| 96 | * @throws InvalidArgumentException |
||
| 97 | * @throws InvalidDataTypeException |
||
| 98 | * @throws InvalidInterfaceException |
||
| 99 | */ |
||
| 100 | public function continue_job(JobParameters $job_parameters, $batch_size = 50) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Performs any clean-up logic when we know the job is completed |
||
| 126 | * |
||
| 127 | * @param JobParameters $job_parameters |
||
| 128 | * @return JobStepResponse |
||
| 129 | * @throws BatchRequestException |
||
| 130 | */ |
||
| 131 | public function cleanup_job(JobParameters $job_parameters) |
||
| 143 | |||
| 144 | |||
| 145 | /** |
||
| 146 | * Contains the logic for processing a model and applying the datetime offset to affected fields on that model. |
||
| 147 | * @param string $model_class_name |
||
| 148 | * @throws EE_Error |
||
| 149 | */ |
||
| 150 | protected function processModel($model_class_name) |
||
| 207 | |||
| 208 | |||
| 209 | /** |
||
| 210 | * Does the query on each $inner_query individually. |
||
| 211 | * |
||
| 212 | * @param string $query |
||
| 213 | * @param array $inner_query |
||
| 214 | * @param DbSafeDateTime|null $start_date_range |
||
| 215 | * @param DbSafeDateTime|null $end_date_range |
||
| 216 | * @return array An array of any errors encountered and the fields they were for. |
||
| 217 | */ |
||
| 218 | private function doQueryForEachField($query, array $inner_query, $start_date_range, $end_date_range) |
||
| 250 | |||
| 251 | |||
| 252 | /** |
||
| 253 | * Performs the query for all fields within the inner_query |
||
| 254 | * |
||
| 255 | * @param string $query |
||
| 256 | * @param array $inner_query |
||
| 257 | * @return false|int |
||
| 258 | */ |
||
| 259 | private function doQueryForAllFields($query, array $inner_query) |
||
| 265 | |||
| 266 | |||
| 267 | /** |
||
| 268 | * Records a changelog entry using the given information. |
||
| 269 | * |
||
| 270 | * @param EEM_Base $model |
||
| 271 | * @param float $offset |
||
| 272 | * @param EE_Table_Base $table |
||
| 273 | * @param EE_Model_Field_Base[] $model_fields_affected |
||
| 274 | * @param string $error_message If present then there was an error so let's record that instead. |
||
| 275 | * @throws EE_Error |
||
| 276 | */ |
||
| 277 | private function recordChangeLog( |
||
| 325 | |||
| 326 | |||
| 327 | /** |
||
| 328 | * Returns an array of models that have datetime fields. |
||
| 329 | * This array is added to a short lived transient cache to keep having to build this list to a minimum. |
||
| 330 | * |
||
| 331 | * @return array an array of model class names. |
||
| 332 | * @throws EE_Error |
||
| 333 | * @throws InvalidDataTypeException |
||
| 334 | * @throws InvalidInterfaceException |
||
| 335 | * @throws InvalidArgumentException |
||
| 336 | */ |
||
| 337 | private function getModelsWithDatetimeFields() |
||
| 356 | |||
| 357 | |||
| 358 | /** |
||
| 359 | * This simply records the models that have been processed with our tracking option. |
||
| 360 | * @param array $models_to_set array of model class names. |
||
| 361 | */ |
||
| 362 | private function setModelsToProcess($models_to_set) |
||
| 366 | |||
| 367 | |||
| 368 | /** |
||
| 369 | * Used to keep track of how many models have been processed for the batch |
||
| 370 | * @param $count |
||
| 371 | */ |
||
| 372 | private function updateCountOfModelsProcessed($count = 1) |
||
| 377 | |||
| 378 | |||
| 379 | /** |
||
| 380 | * Retrieve the tracked number of models processed between requests. |
||
| 381 | * @return int |
||
| 382 | */ |
||
| 383 | private function getCountOfModelsProcessed() |
||
| 387 | |||
| 388 | |||
| 389 | /** |
||
| 390 | * Returns the models that are left to process. |
||
| 391 | * @return array an array of model class names. |
||
| 392 | */ |
||
| 393 | private function getModelsToProcess() |
||
| 400 | |||
| 401 | |||
| 402 | /** |
||
| 403 | * Used to record the offset that will be applied to dates and times for EE_Datetime_Field columns. |
||
| 404 | * @param float $offset |
||
| 405 | */ |
||
| 406 | public static function updateOffset($offset) |
||
| 410 | |||
| 411 | |||
| 412 | /** |
||
| 413 | * Used to retrieve the saved offset that will be applied to dates and times for EE_Datetime_Field columns. |
||
| 414 | * |
||
| 415 | * @return float |
||
| 416 | */ |
||
| 417 | public static function getOffset() |
||
| 421 | |||
| 422 | |||
| 423 | /** |
||
| 424 | * Used to set the saved offset range start date. |
||
| 425 | * @param DbSafeDateTime|null $start_date |
||
| 426 | */ |
||
| 427 | View Code Duplication | public static function updateStartDateRange(DbSafeDateTime $start_date = null) |
|
| 434 | |||
| 435 | |||
| 436 | /** |
||
| 437 | * Used to get the saved offset range start date. |
||
| 438 | * @return DbSafeDateTime|null |
||
| 439 | */ |
||
| 440 | View Code Duplication | public static function getStartDateRange() |
|
| 454 | |||
| 455 | |||
| 456 | |||
| 457 | /** |
||
| 458 | * Used to set the saved offset range end date. |
||
| 459 | * @param DbSafeDateTime|null $end_date |
||
| 460 | */ |
||
| 461 | View Code Duplication | public static function updateEndDateRange(DbSafeDateTime $end_date = null) |
|
| 468 | |||
| 469 | |||
| 470 | /** |
||
| 471 | * Used to get the saved offset range end date. |
||
| 472 | * @return DbSafeDateTime|null |
||
| 473 | */ |
||
| 474 | View Code Duplication | public static function getEndDateRange() |
|
| 487 | } |
||
| 488 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.