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 |
||
| 12 | class Day{ |
||
| 13 | |||
| 14 | const TYPE_MORNING = 1; |
||
| 15 | const TYPE_DAY = 2; |
||
| 16 | const TYPE_EVENING = 3; |
||
| 17 | const TYPE_NIGHT = 4; |
||
| 18 | const TYPE_DAY_SHORT = 5; |
||
| 19 | const TYPE_NIGHT_SHORT = 6; |
||
| 20 | |||
| 21 | const HOUR_NIGHT = 6; |
||
| 22 | const HOUR_MORNING = 12; |
||
| 23 | const HOUR_DAY = 18; |
||
| 24 | const HOUR_EVENING = 23; |
||
| 25 | |||
| 26 | |||
| 27 | public $sunrise; |
||
| 28 | public $sunset; |
||
| 29 | public $moon_phase; |
||
| 30 | |||
| 31 | public $morning; |
||
| 32 | public $day; |
||
| 33 | public $evening; |
||
| 34 | public $night; |
||
| 35 | public $day_short; |
||
| 36 | public $night_short; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * |
||
| 40 | * @param $value |
||
| 41 | * @param bool $weekend |
||
| 42 | * @return bool |
||
| 43 | */ |
||
| 44 | public static function CheckWeekend($value, $weekend = false) |
||
| 45 | { |
||
| 46 | $dayNumber = date('w', strtotime($value)); |
||
| 47 | if($weekend && ($dayNumber == 0 || $dayNumber == 6)) |
||
| 48 | return true; |
||
| 49 | elseif ($weekend == false && $dayNumber == 0) |
||
|
|
|||
| 50 | return true; |
||
| 51 | else |
||
| 52 | return false; |
||
| 53 | } |
||
| 54 | |||
| 55 | public static function getDayNumber($value) { |
||
| 58 | |||
| 59 | public static function getMonthName($value) { |
||
| 63 | |||
| 64 | public static function getDayName($value) { |
||
| 65 | $str = ""; |
||
| 66 | |||
| 67 | $listDays = ['вс','пн', 'вт', 'ср', 'чт', 'пт', 'сб']; |
||
| 68 | |||
| 69 | $date = Date('d.m.Y', strtotime("+1 days")); |
||
| 70 | $match_date = date('d.m.Y', strtotime($value)); |
||
| 71 | |||
| 72 | if($date == $match_date) |
||
| 73 | $str = "Завтра"; |
||
| 74 | else { |
||
| 75 | $str = $listDays[date("w", strtotime($value))]; |
||
| 76 | |||
| 77 | //return $this->days[date('w', $date)].', '.(int)date('d',$date).' '.$this->months[date('n', $date)]; |
||
| 78 | } |
||
| 79 | |||
| 80 | |||
| 81 | return $str; |
||
| 82 | |||
| 83 | } |
||
| 84 | |||
| 85 | public function AdditionalState() |
||
| 86 | { |
||
| 87 | $arrAdditionWeather = []; |
||
| 88 | $hour = date("G"); |
||
| 89 | View Code Duplication | if ($hour > self::HOUR_EVENING OR $hour <= self::HOUR_NIGHT) { |
|
| 90 | $arrAdditionWeather["Утром"] = $this->morning; |
||
| 91 | $arrAdditionWeather["Днём"] = $this->day; |
||
| 92 | } |
||
| 93 | View Code Duplication | if ($hour > self::HOUR_NIGHT && $hour <= self::HOUR_MORNING) { |
|
| 94 | $arrAdditionWeather["Днём"] = $this->day; |
||
| 95 | $arrAdditionWeather["Вечером"] = $this->evening; |
||
| 96 | } |
||
| 97 | View Code Duplication | if ($hour > self::HOUR_MORNING && $hour <= self::HOUR_DAY) { |
|
| 98 | $arrAdditionWeather["Вечером"] = $this->evening; |
||
| 99 | $arrAdditionWeather["Ночью"] = $this->night; |
||
| 100 | } |
||
| 101 | View Code Duplication | if ($hour > self::HOUR_DAY && $hour <= self::HOUR_EVENING) { |
|
| 102 | $arrAdditionWeather["Ночью"] = $this->night; |
||
| 103 | $arrAdditionWeather["Утром"] = $this->morning; |
||
| 104 | } |
||
| 105 | |||
| 106 | return $arrAdditionWeather; |
||
| 107 | } |
||
| 108 | |||
| 109 | public function getListDetail() { |
||
| 112 | |||
| 113 | } |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.