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 |
||
| 30 | class TryCatchTask extends SubTask |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * @var \Netresearch\Kite\Task |
||
| 34 | */ |
||
| 35 | protected $catchTask; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var bool If the next fetched task should be the catch task |
||
| 39 | */ |
||
| 40 | protected $catch = false; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Configure the options |
||
| 44 | * |
||
| 45 | * @return array |
||
| 46 | */ |
||
| 47 | View Code Duplication | protected function configureVariables() |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Set a variable or it's value |
||
| 64 | * |
||
| 65 | * @param string $name The name |
||
| 66 | * @param mixed $value The value |
||
| 67 | * |
||
| 68 | * @return void |
||
| 69 | */ |
||
| 70 | public function offsetSet($name, $value) |
||
| 78 | |||
| 79 | |||
| 80 | /** |
||
| 81 | * Set task as catchTask if $this->catch |
||
| 82 | * |
||
| 83 | * @param \Netresearch\Kite\Task $task The task |
||
| 84 | * |
||
| 85 | * @return $this|mixed $this or the task return value when this is running |
||
| 86 | */ |
||
| 87 | public function addTask(Task $task) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Fetch the next task as that task to execute when an exception occured while |
||
| 99 | * executing the other tasks |
||
| 100 | * |
||
| 101 | * @return $this |
||
| 102 | */ |
||
| 103 | public function onCatch() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Run an array of tasks |
||
| 114 | * |
||
| 115 | * @return mixed The ran tasks return value |
||
| 116 | */ |
||
| 117 | public function run() |
||
| 132 | } |
||
| 133 | ?> |
||
| 134 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.