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 |
||
| 10 | class Harvest |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var string |
||
| 14 | */ |
||
| 15 | private $message = ''; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var mixed |
||
| 19 | */ |
||
| 20 | private $serviceApiCall = null; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @param $domain |
||
| 24 | * @param null $apiToken |
||
| 25 | */ |
||
| 26 | View Code Duplication | public function __construct($domain = null, $apiToken = null) |
|
| 37 | |||
| 38 | /** |
||
| 39 | * @param $timerId |
||
| 40 | * @return mixed |
||
| 41 | */ |
||
| 42 | View Code Duplication | public function deleteTimer($timerId = null) |
|
| 59 | |||
| 60 | /** |
||
| 61 | * @return mixed |
||
| 62 | */ |
||
| 63 | public function getLastMessage() |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param $description |
||
| 70 | * @param $projectId |
||
| 71 | * @param $taskId |
||
| 72 | * @return mixed |
||
| 73 | */ |
||
| 74 | public function startTimer($description, $projectId, $taskId) |
||
| 75 | { |
||
| 76 | $harvestId = null; |
||
| 77 | |||
| 78 | $item = [ |
||
| 79 | 'notes' => $description, |
||
| 80 | 'project_id' => $projectId, |
||
| 81 | 'task_id' => $taskId, |
||
| 82 | ]; |
||
| 83 | |||
| 84 | View Code Duplication | if ($this->serviceApiCall->send('post', 'add', ['json' => $item]) === true) { |
|
| 85 | if ($this->serviceApiCall->last('success') === true) { |
||
| 86 | $this->setMessage('timer started'); |
||
| 87 | $harvestId = $this->serviceApiCall->getData()['id']; |
||
| 88 | } else { |
||
| 89 | $this->setMessage('cannot start timer!'); |
||
| 90 | } |
||
| 91 | } else { |
||
| 92 | $this->setMessage($this->serviceApiCall->getMessage()); |
||
| 93 | } |
||
| 94 | |||
| 95 | return $harvestId; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param $timerId |
||
| 100 | * @return mixed |
||
| 101 | */ |
||
| 102 | public function stopTimer($timerId = null) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param $timerId |
||
| 126 | * @return mixed |
||
| 127 | */ |
||
| 128 | View Code Duplication | private function isTimerRunning($timerId) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * @param $message |
||
| 147 | */ |
||
| 148 | private function setMessage($message = null) |
||
| 152 | } |
||
| 153 |
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.