Complex classes like HandlerTrait 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 HandlerTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | trait HandlerTrait |
||
12 | { |
||
13 | use CallableTrait; |
||
14 | |||
15 | /** |
||
16 | * @var callable|string|null The handler used |
||
17 | */ |
||
18 | protected $handler; |
||
19 | |||
20 | /** |
||
21 | * Constructor. |
||
22 | * |
||
23 | * @param callable|string|null $handler |
||
24 | */ |
||
25 | public function __construct($handler = null) |
||
31 | |||
32 | /** |
||
33 | * Set the handler. |
||
34 | * |
||
35 | * @param string|callable $handler |
||
36 | * |
||
37 | * @return self |
||
38 | */ |
||
39 | public function handler($handler) |
||
45 | |||
46 | /** |
||
47 | * Execute the target. |
||
48 | * |
||
49 | * @param RequestInterface $request |
||
50 | * @param ResponseInterface $response |
||
51 | * |
||
52 | * @return ResponseInterface |
||
53 | */ |
||
54 | protected function executeHandler(RequestInterface $request, ResponseInterface $response) |
||
58 | } |
||
59 |