Complex classes like Formatter 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 Formatter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | trait Formatter |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Accepts the same format as the date() function. |
||
| 16 | * |
||
| 17 | * @see date(), \DateTime |
||
| 18 | * |
||
| 19 | * @param $format |
||
| 20 | * |
||
| 21 | * @return string |
||
| 22 | */ |
||
| 23 | public function format($format) |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Return the value of the format character. |
||
| 38 | * |
||
| 39 | * @param string $name of the field |
||
| 40 | * |
||
| 41 | * @return string |
||
| 42 | */ |
||
| 43 | protected function getValueOfFormatCharacter($name) |
||
| 52 | |||
| 53 | /** |
||
| 54 | * (01-30) Day of the month, 2 digits with leading zeros |
||
| 55 | * |
||
| 56 | * @return string |
||
| 57 | */ |
||
| 58 | public function getDayTwoDigit() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * (ሰኞ-እሑድ) A full textual representation of the day of the week |
||
| 67 | * |
||
| 68 | * return string |
||
| 69 | */ |
||
| 70 | public function getTextualDay() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * (ሰኞ-እሑ) A textual representation of a day, two letters |
||
| 77 | * |
||
| 78 | * return string |
||
| 79 | */ |
||
| 80 | public function getTextualDayShort() |
||
| 86 | |||
| 87 | /** |
||
| 88 | * (መስከረም-ጳጉሜን) A full textual representation of a month |
||
| 89 | * |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | public function getTextualMonth() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * (መስ - ጳጉ) A short textual representation of a month, two letters |
||
| 99 | * |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | public function getTextualMonthShort() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * (1-13) Numeric representation of a month, without leading zeros |
||
| 111 | * |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | public function getMonth() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * (01-13) Numeric representation of a month, with leading zeros |
||
| 121 | * |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | public function getMonthTwoDigit() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * (5,6 or 30) Number of days in the given month |
||
| 133 | * |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | public function getDaysInMonth() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * (1 or 0) Whether it's a leap year |
||
| 143 | * |
||
| 144 | * @return string |
||
| 145 | */ |
||
| 146 | public function getLeapYearString() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * (1000-9999) A full numeric representation of a year, 4 digits mostly |
||
| 153 | * |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | public function getYear() |
||
| 160 | |||
| 161 | public function getYearShort() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Return the amharic equivalent of AM & PM. |
||
| 170 | * |
||
| 171 | * (እኩለ፡ሌሊት-ምሽት) |
||
| 172 | * |
||
| 173 | * It suppose to format 'Ante meridiem' and 'Post meridiem' |
||
| 174 | * But we Ethiopians classify the day in <b>ten</b> parts |
||
| 175 | * and we don't have Uppercase and Lowercase letters |
||
| 176 | * |
||
| 177 | * @link http://web.archive.org/web/20140331152859/http://ethiopic.org/Calendars/ |
||
| 178 | * |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | public function getTimeOfDay() |
||
| 226 | |||
| 227 | public function getOrthodoxDay() |
||
| 231 | |||
| 232 | public function getTextualEra() |
||
| 236 | |||
| 237 | public function getOrthodoxYear() |
||
| 241 | |||
| 242 | } |
||
| 243 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: