Complex classes like Carbonated 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 Carbonated, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | trait Carbonated |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Store carbon instances for reuse. |
||
| 12 | * |
||
| 13 | * @var object |
||
| 14 | */ |
||
| 15 | protected $carbonInstances; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Indicate whether accessors should be overridden to return carbon instances. |
||
| 19 | * |
||
| 20 | * @var bool |
||
| 21 | */ |
||
| 22 | protected $returnCarbon = false; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Get the attributes that should be handled as carbonated timestamps. |
||
| 26 | * |
||
| 27 | * @return array |
||
| 28 | */ |
||
| 29 | public function carbonatedTimestamps() |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Get the attributes that should be handled as carbonated dates. |
||
| 39 | * |
||
| 40 | * @return array |
||
| 41 | */ |
||
| 42 | public function carbonatedDates() |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Get the attributes that should be handled as carbonated times. |
||
| 49 | * |
||
| 50 | * @return array |
||
| 51 | */ |
||
| 52 | public function carbonatedTimes() |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Get all attributes that should be handled by carbonated. |
||
| 59 | * |
||
| 60 | * @return array |
||
| 61 | */ |
||
| 62 | public function carbonatedAttributes() |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Get carbonated attribute type. |
||
| 69 | * |
||
| 70 | * @param string $key |
||
| 71 | * |
||
| 72 | * @return string |
||
| 73 | */ |
||
| 74 | public function carbonatedAttributeType($key) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Get the intended timestamp format for view output. |
||
| 89 | * |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | public function carbonatedTimestampFormat() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Get the intended date format for view output. |
||
| 99 | * |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | public function carbonatedDateFormat() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Get the intended date format for view output. |
||
| 109 | * |
||
| 110 | * @return string |
||
| 111 | */ |
||
| 112 | public function carbonatedTimeFormat() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Get the intended timezone for view output. |
||
| 119 | * |
||
| 120 | * @return string |
||
| 121 | */ |
||
| 122 | public function carbonatedTimezone() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Get the intended timestamp for json output. |
||
| 138 | * |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | public function jsonTimestampFormat() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Get the intended date for json output. |
||
| 148 | * |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function jsonDateFormat() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Get the intended time for json output. |
||
| 158 | * |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | public function jsonTimeFormat() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Get the intended timezone for json output. |
||
| 168 | * |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | public function jsonTimezone() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Get the intended database format for timestamp storage. |
||
| 184 | * |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | public function databaseTimestampFormat() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Get the intended database format for date storage. |
||
| 194 | * |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | public function databaseDateFormat() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Get the intended database format for time storage. |
||
| 204 | * |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | public function databaseTimeFormat() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Get the intended timezone for database storage. |
||
| 214 | * |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | public function databaseTimezone() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Store and return carbon instances for reuse. |
||
| 230 | * |
||
| 231 | * @return object |
||
| 232 | */ |
||
| 233 | protected function carbonInstances() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Return a clone of $this object and modify it's accessors. |
||
| 271 | * |
||
| 272 | * @return $this |
||
| 273 | */ |
||
| 274 | public function getWithCarbonAttribute() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Access and format for front end. |
||
| 288 | * |
||
| 289 | * @param string $key |
||
| 290 | * @param bool $json |
||
| 291 | * |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | public function carbonatedAccessor($key, $json = false) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Mutate to a storable value for database. |
||
| 320 | * |
||
| 321 | * @param string $key |
||
| 322 | * @param mixed $value |
||
| 323 | * |
||
| 324 | * @return string |
||
| 325 | */ |
||
| 326 | public function carbonatedMutator($key, $value) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Check if request is some type of JSON (purposefully more lenient than current Request::isJson() helper). |
||
| 363 | * |
||
| 364 | * @return bool |
||
| 365 | */ |
||
| 366 | public static function requestIsJson() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Override default getDates() to allow created_at and updated_at handling by carbonated. |
||
| 373 | * |
||
| 374 | * @return array |
||
| 375 | */ |
||
| 376 | public function getDates() |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Override default freshTimestamp() to be more explicit in setting timezone for storage. |
||
| 383 | * |
||
| 384 | * @return \Carbon\Carbon |
||
| 385 | */ |
||
| 386 | public function freshTimestamp() |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Override default toArray() to include our own accessors. |
||
| 395 | * |
||
| 396 | * @param bool $useJsonAccessors |
||
| 397 | * |
||
| 398 | * @return array |
||
| 399 | */ |
||
| 400 | public function toArray($useJsonAccessors = false) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Override default jsonSerialize() to set $useJsonAccessors parameter. |
||
| 418 | * |
||
| 419 | * @return array |
||
| 420 | */ |
||
| 421 | public function jsonSerialize() |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Override default getAttributeValue() to include our own accessors. |
||
| 428 | * |
||
| 429 | * @param string $key |
||
| 430 | * |
||
| 431 | * @return mixed |
||
| 432 | */ |
||
| 433 | public function getAttributeValue($key) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Override default setAttribute() to include our own mutators. |
||
| 461 | * |
||
| 462 | * @param string $key |
||
| 463 | * @param mixed $value |
||
| 464 | * |
||
| 465 | * @return void |
||
| 466 | */ |
||
| 467 | public function setAttribute($key, $value) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @param object $carbonInstances |
||
| 491 | */ |
||
| 492 | public function setCarbonInstances($carbonInstances) |
||
| 499 | |||
| 500 | private function ensureProperty($instance, $propertyName) |
||
| 513 | |||
| 514 | private function useLocalizedFormats() |
||
| 520 | } |
||
| 521 |
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: