Complex classes like Date often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Date, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Date { |
||
| 9 | |||
| 10 | private $year = '00'; |
||
| 11 | private $month = '00'; |
||
| 12 | private $day = '00'; |
||
| 13 | private $hour = '00'; |
||
| 14 | private $minute = '00'; |
||
| 15 | private $second = '00'; |
||
| 16 | static $monthNames = array(0 => "00", "Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"); |
||
| 17 | static $zeroDate = '0000-00-00 00:00:00'; |
||
| 18 | |||
| 19 | const UNITS = array( |
||
| 20 | 31536000 => ['ano', 'anos'], |
||
| 21 | 2592000 => ['mês', 'mêses'], |
||
| 22 | 604800 => ['semana', 'semanas'], |
||
| 23 | 86400 => ['dia', 'dias'], |
||
| 24 | 3600 => ['hora', 'horas'], |
||
| 25 | 60 => ['minuto', 'minutos'], |
||
| 26 | 1 => ['segundo', 'segundos'] |
||
| 27 | ); |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Inicia a data/hora de acordo com a string informada |
||
| 31 | * @param string |
||
| 32 | * @example new Data('01/01/1980 18:00'); |
||
| 33 | */ |
||
| 34 | public function __construct($stringDate = null) { |
||
| 39 | |||
| 40 | /* METODOS DE ACESSO */ |
||
| 41 | |||
| 42 | public function getYear() { |
||
| 45 | |||
| 46 | public function getMonth() { |
||
| 49 | |||
| 50 | public function getDay() { |
||
| 53 | |||
| 54 | public function getHour() { |
||
| 57 | |||
| 58 | public function getMinute() { |
||
| 61 | |||
| 62 | public function getSecond() { |
||
| 65 | |||
| 66 | public static function getZeroDate() { |
||
| 69 | |||
| 70 | public function setYear($year) { |
||
| 73 | |||
| 74 | public function setMonth($month) { |
||
| 77 | |||
| 78 | public function setDay($day) { |
||
| 81 | |||
| 82 | public function setHour($hour) { |
||
| 85 | |||
| 86 | public function setMinute($minute) { |
||
| 89 | |||
| 90 | public function setSecond($second) { |
||
| 93 | |||
| 94 | public function getMonthName() { |
||
| 97 | |||
| 98 | public function getMonthAbbre() { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Inicia o objeto de acordo com a data informada |
||
| 104 | * @param string $stringDate |
||
| 105 | */ |
||
| 106 | public function initDateTime($stringDate = null) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Inicia o Dia/Mes/Ano |
||
| 117 | * @param string $stringDateOnly |
||
| 118 | */ |
||
| 119 | private function initDate($stringDateOnly) { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Inicia a Hora:Minuto:Segundo |
||
| 137 | * @param string $stringTimeOnly |
||
| 138 | */ |
||
| 139 | private function initTime($stringTimeOnly) { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Retorna a data no padrao HTML BRASILEIRO: "Dia/Mês/Ano Hora:Minuto:Segundo" |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function toHtml() { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Retorna a data no padrao SQL DATETIME: "Ano-Mês-Dia Hora:Minuto:Segundo" |
||
| 157 | * @return string |
||
| 158 | */ |
||
| 159 | public function toSql() { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Retorna a data com o formato padrão 'd/m/y' |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | public function __toString() { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Retorna a data no formato humano |
||
| 177 | * @return string (ex: 4 horas atrás), (ex: daqui a 5 dias) |
||
| 178 | */ |
||
| 179 | public function toHumanFormat() { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Retorna a data de acordo com o formato escolhido. Ex: [d/m/y h:i:s] , [y-m-d] , [y-m-d h:i:s] , etc |
||
| 197 | * @param string $format |
||
| 198 | * @return string Data no formato desejado |
||
| 199 | */ |
||
| 200 | public function format($format = 'd/m/y') { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Valida a data |
||
| 212 | * @return boolean retorna True se data é valida |
||
| 213 | */ |
||
| 214 | public function isValid() { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Retorna true se é uma data de nascimento válida |
||
| 223 | * @return boolean |
||
| 224 | */ |
||
| 225 | public function isValidBirthday() { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Soma a quantidade de dias informada |
||
| 237 | * @param int $days |
||
| 238 | */ |
||
| 239 | public function sumDays($days) { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Soma a quantidade de meses informada |
||
| 245 | * @param int $months |
||
| 246 | */ |
||
| 247 | public function sumMonths($months) { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Soma a quantidade de anos informada |
||
| 253 | * @param int $years |
||
| 254 | */ |
||
| 255 | public function sumYears($years) { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Soma a quantidade de tempo informada |
||
| 261 | * @param int $time Unidade |
||
| 262 | * @param string $tipo [days,month,years] |
||
| 263 | */ |
||
| 264 | public function sumTime($time, $tipo = 'days') { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Retorna a diferença em Segundos entre duas datas |
||
| 273 | * @param Data $date1 |
||
| 274 | * @param Data $date2 |
||
| 275 | * @return float Diferença em Segundos |
||
| 276 | */ |
||
| 277 | public static function secondsDiff(Data $date1, Data $date2) { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Retorna a diferença em Dias entre duas datas |
||
| 287 | * @param Data $date1 |
||
| 288 | * @param Data $date2 |
||
| 289 | * @return float Diferença em Dias |
||
| 290 | */ |
||
| 291 | public static function daysDiff(Data $date1, Data $date2) { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Retorna a data em unidade de tempo Humana |
||
| 298 | * @param string $dateInSeconds |
||
| 299 | * @return string |
||
| 300 | */ |
||
| 301 | protected function getHumanUnits($dateInSeconds) { |
||
| 314 | |||
| 315 | } |
||
| 316 |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: