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 |
||
| 7 | abstract class BaseCalendar |
||
| 8 | { |
||
| 9 | protected $vacancies = []; |
||
| 10 | |||
| 11 | protected $service = null; |
||
| 12 | |||
| 13 | protected $duration = null; |
||
| 14 | |||
| 15 | protected $date = null; |
||
| 16 | |||
| 17 | protected $time = null; |
||
| 18 | |||
| 19 | protected $timezone = 'UTC'; |
||
| 20 | |||
| 21 | public function __construct($vacancies, $timezone = 'UTC') |
||
| 27 | |||
| 28 | public function timezone($timezone = null) |
||
| 36 | |||
| 37 | public function forService($service = null) |
||
| 43 | |||
| 44 | public function forDate($date) |
||
| 50 | |||
| 51 | public function atTime($time, $timezone = null) |
||
| 61 | |||
| 62 | public function withDuration($duration) |
||
| 68 | |||
| 69 | public function getUTCDateTime() |
||
| 73 | |||
| 74 | final protected function date() |
||
| 78 | |||
| 79 | View Code Duplication | public function find() |
|
| 97 | |||
| 98 | View Code Duplication | protected function prepare() |
|
| 110 | } |
||
| 111 |
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.