Complex classes like Dates 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 Dates, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Dates extends DateTime |
||
9 | { |
||
10 | protected static $humainReadableI18n = [ |
||
11 | 'now' => 'Now', |
||
12 | 'since' => 'Since', |
||
13 | 'yesterday' => 'Yesterday', |
||
14 | 'the' => 'The', |
||
15 | 'at' => 'at' |
||
16 | ]; |
||
17 | |||
18 | protected static $humainReadableFormats = [ |
||
19 | 'dateSameYear' => 'm-d', |
||
20 | 'dateDifferentYear' => 'Y-m-d', |
||
21 | 'time' => 'H:i' |
||
22 | ]; |
||
23 | |||
24 | public static function getHumainReadableI18n() |
||
28 | |||
29 | public static function setHumainReadableI18nKey($key, $value) |
||
33 | |||
34 | public static function setHumainReadableI18n($value) |
||
38 | |||
39 | public static function getHumainReadableFormats() |
||
43 | |||
44 | public static function setHumainReadableFormatsKey($key, $value) |
||
48 | |||
49 | public static function setHumainReadableFormats($value) |
||
53 | |||
54 | /** |
||
55 | * Accesseur vers l'attribut $date |
||
56 | */ |
||
57 | public function getDate() |
||
61 | |||
62 | public function getYear() |
||
66 | |||
67 | public function getMonth() |
||
71 | |||
72 | public function getDay() |
||
76 | |||
77 | public function getHour() |
||
81 | |||
82 | public function getMinute() |
||
86 | |||
87 | public function getSecond() |
||
91 | |||
92 | public function getZone() |
||
96 | |||
97 | public function modify($modify) |
||
98 | { |
||
99 | $dateDepart = clone $this; |
||
100 | @parent::modify($modify); //Yeurk, but for personnal pattern, no choice |
||
101 | |||
102 | if ($dateDepart != $this) { |
||
103 | return $this; |
||
104 | } |
||
105 | |||
106 | $this->modifyOthersKeywords($modify); |
||
107 | |||
108 | return $this; |
||
109 | } |
||
110 | |||
111 | protected function getModifyOthersKeywors() |
||
138 | |||
139 | protected function modifyOthersKeywords($modify) |
||
165 | |||
166 | /** |
||
167 | * Renvoi au format pour SQL (postgresql) via un array |
||
168 | * |
||
169 | * @param bool $returnArray (default: false) Indique si on veux retourner |
||
170 | * un string ayant tout, ou un array ayant la date et l'heure séparé |
||
171 | * |
||
172 | * @return string|array Le format pour SQL |
||
173 | * Si string : aaaa-mm-jj hh:mm:ss |
||
174 | * Si array : [0]=>partie date (aaaa-mm-jj), [1]=>partie heure (hh:mm:ss) |
||
175 | */ |
||
176 | public function getSqlFormat($returnArray = false) |
||
187 | |||
188 | /** |
||
189 | * Liste tous les timezone qui existe |
||
190 | * |
||
191 | * @return array La liste des timezone possible |
||
192 | */ |
||
193 | public function lstTimeZone() |
||
197 | |||
198 | /** |
||
199 | * Liste les continents possible pour les timezones |
||
200 | * |
||
201 | * @return string[] La liste des continents |
||
202 | */ |
||
203 | public function lstTimeZoneContinent() |
||
218 | |||
219 | /** |
||
220 | * Liste des pays possible pour un continent donné |
||
221 | * |
||
222 | * @param string $continent Le continent dans lequel on veux la liste des pays |
||
223 | * |
||
224 | * @return array La liste des pays pour le continent donné |
||
225 | */ |
||
226 | public function lstTimeZonePays($continent) |
||
242 | |||
243 | public function humainReadable($returnDateAndTime = true, $toLower = false) |
||
306 | } |
||
307 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.