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 |
||
| 17 | class Wordlift_Batch_Analysis_Adapter { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * A {@link Wordlift_Log_Service} instance. |
||
| 21 | * |
||
| 22 | * @since 3.17.0 |
||
| 23 | * @access private |
||
| 24 | * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
||
| 25 | */ |
||
| 26 | private $log; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The {@link Wordlift_Batch_Analysis_Service} instance. |
||
| 30 | * |
||
| 31 | * @since 3.17.0 |
||
| 32 | * @access private |
||
| 33 | * @var \Wordlift_Batch_Analysis_Service $batch_analysis_service The {@link Wordlift_Batch_Analysis_Service} instance. |
||
| 34 | */ |
||
| 35 | private $batch_analysis_service; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Wordlift_Batch_Analysis_Adapter constructor. |
||
| 39 | * |
||
| 40 | * @since 3.14.2 |
||
| 41 | * |
||
| 42 | * @param \Wordlift_Batch_Analysis_Service $batch_analysis_service |
||
| 43 | */ |
||
| 44 | public function __construct( $batch_analysis_service ) { |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Submit the posts for batch analysis. |
||
| 59 | * |
||
| 60 | * @since 3.14.2 |
||
| 61 | */ |
||
| 62 | public function submit() { |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Submit the posts for batch analysis. |
||
| 80 | * |
||
| 81 | * @since 3.14.2 |
||
| 82 | */ |
||
| 83 | public function submit_posts() { |
||
| 106 | |||
| 107 | public function complete() { |
||
| 114 | |||
| 115 | |||
| 116 | /** |
||
| 117 | * A helper function to create the parameters from the $_REQUEST. |
||
| 118 | * |
||
| 119 | * @since 3.17.0 |
||
| 120 | * |
||
| 121 | * @return array An array or parameters. |
||
| 122 | */ |
||
| 123 | private static function create_params_from_request() { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Cancel the batch analysis for the specified post. |
||
| 153 | * |
||
| 154 | * @since 3.14.0 |
||
| 155 | */ |
||
| 156 | View Code Duplication | public function cancel() { |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Clear warnings for the specified post. |
||
| 174 | * |
||
| 175 | * @since 3.14.0 |
||
| 176 | */ |
||
| 177 | View Code Duplication | public function clear_warning() { |
|
| 192 | |||
| 193 | } |
||
| 194 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: