Complex classes like CallableTrait 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 CallableTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | trait CallableTrait |
||
13 | { |
||
14 | use ArgumentsTrait; |
||
15 | |||
16 | /** |
||
17 | * Execute the callable. |
||
18 | * |
||
19 | * @param mixed $target |
||
20 | * @param RequestInterface $request |
||
21 | * @param ResponseInterface $response |
||
22 | * |
||
23 | * @return ResponseInterface |
||
24 | */ |
||
25 | protected function executeCallable($target, RequestInterface $request, ResponseInterface $response) |
||
55 | |||
56 | /** |
||
57 | * Resolves the target of the route and returns a callable. |
||
58 | * |
||
59 | * @param mixed $target |
||
60 | * @param array $construct_args |
||
61 | * |
||
62 | * @throws RuntimeException If the target is not callable |
||
63 | * |
||
64 | * @return callable |
||
65 | */ |
||
66 | protected static function getCallable($target, array $construct_args) |
||
93 | } |
||
94 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.