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() |
||
| 123 | { |
||
| 124 | // Check for $carbonatedTimezone property in model. |
||
| 125 | if ($this->ensureProperty($this, 'carbonatedTimezone')) { |
||
| 126 | return (string) $this->carbonatedTimezone; |
||
| 127 | } // If not, check for an authenticated user with a $timezone property. |
||
| 128 | elseif (class_exists(\Auth::class) && \Auth::check() && \Auth::user()->timezone) { |
||
| 129 | return (string) \Auth::user()->timezone; |
||
| 130 | } |
||
| 131 | |||
| 132 | // Otherwise use same timezone as database. |
||
| 133 | return $this->databaseTimezone(); |
||
| 134 | } |
||
| 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() |
||
| 218 | { |
||
| 219 | // Check for $databaseTimezone property in model. |
||
| 220 | if ($this->ensureProperty($this, 'databaseTimezone')) { |
||
| 221 | return (string) $this->databaseTimezone; |
||
| 222 | } |
||
| 223 | |||
| 224 | // Otherwise use app's timezone configuration. |
||
| 225 | return (string) function_exists('config') ? config('app.timezone') : 'UTC'; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Store and return carbon instances for reuse. |
||
| 230 | * |
||
| 231 | * @return object |
||
| 232 | */ |
||
| 233 | protected function carbonInstances() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Return a clone of $this object and modify it's accessors. |
||
| 274 | * |
||
| 275 | * @return $this |
||
| 276 | */ |
||
| 277 | public function getWithCarbonAttribute() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Access and format for front end. |
||
| 291 | * |
||
| 292 | * @param string $key |
||
| 293 | * @param bool $json |
||
| 294 | * |
||
| 295 | * @return string |
||
| 296 | */ |
||
| 297 | public function carbonatedAccessor($key, $json = false) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Mutate to a storable value for database. |
||
| 327 | * |
||
| 328 | * @param string $key |
||
| 329 | * @param mixed $value |
||
| 330 | * |
||
| 331 | * @return string |
||
| 332 | */ |
||
| 333 | public function carbonatedMutator($key, $value) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Check if request is some type of JSON (purposefully more lenient than current Request::isJson() helper). |
||
| 370 | * |
||
| 371 | * @return bool |
||
| 372 | */ |
||
| 373 | public static function requestIsJson() |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Override default getDates() to allow created_at and updated_at handling by carbonated. |
||
| 380 | * |
||
| 381 | * @return array |
||
| 382 | */ |
||
| 383 | public function getDates() |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Override default freshTimestamp() to be more explicit in setting timezone for storage. |
||
| 390 | * |
||
| 391 | * @return \Carbon\Carbon |
||
| 392 | */ |
||
| 393 | public function freshTimestamp() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Override default toArray() to include our own accessors. |
||
| 402 | * |
||
| 403 | * @param bool $useJsonAccessors |
||
| 404 | * |
||
| 405 | * @return array |
||
| 406 | */ |
||
| 407 | public function toArray($useJsonAccessors = false) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Override default jsonSerialize() to set $useJsonAccessors parameter. |
||
| 425 | * |
||
| 426 | * @return array |
||
| 427 | */ |
||
| 428 | public function jsonSerialize() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Override default getAttributeValue() to include our own accessors. |
||
| 435 | * |
||
| 436 | * @param string $key |
||
| 437 | * |
||
| 438 | * @return mixed |
||
| 439 | */ |
||
| 440 | public function getAttributeValue($key) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Override default setAttribute() to include our own mutators. |
||
| 468 | * |
||
| 469 | * @param string $key |
||
| 470 | * @param mixed $value |
||
| 471 | * |
||
| 472 | * @return void |
||
| 473 | */ |
||
| 474 | public function setAttribute($key, $value) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @param object $carbonInstances |
||
| 498 | */ |
||
| 499 | public function setCarbonInstances($carbonInstances) |
||
| 506 | |||
| 507 | private function ensureProperty($instance, $propertyName) |
||
| 520 | |||
| 521 | private function useLocalizedFormats() |
||
| 527 | } |
||
| 528 |