Complex classes like RedirectTrait 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 RedirectTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | trait RedirectTrait |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var int Redirect HTTP status code |
||
| 17 | */ |
||
| 18 | protected $redirectStatus; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Set HTTP redirect status code. |
||
| 22 | * |
||
| 23 | * @param int $redirectStatus Redirect HTTP status code |
||
| 24 | * |
||
| 25 | * @return self |
||
| 26 | */ |
||
| 27 | public function redirect($redirectStatus = 302) |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Returns a redirect response. |
||
| 40 | * |
||
| 41 | * @param int $redirectStatus |
||
| 42 | * @param UriInterface $uri |
||
| 43 | * @param ResponseInterface $response |
||
| 44 | */ |
||
| 45 | protected static function getRedirectResponse($redirectStatus, UriInterface $uri, ResponseInterface $response) |
||
| 52 | } |
||
| 53 |