Complex classes like ErrorHandler 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 ErrorHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class ErrorHandler implements LoggerAwareInterface |
||
17 | { |
||
18 | /** |
||
19 | * @var LoggerInterface |
||
20 | */ |
||
21 | protected $logger; |
||
22 | |||
23 | /** |
||
24 | * @var \Exception|\Error |
||
25 | */ |
||
26 | protected $error; |
||
27 | |||
28 | |||
29 | /** |
||
30 | * Set the logger for logging errors |
||
31 | * |
||
32 | * @param LoggerInterface $logger |
||
33 | */ |
||
34 | public function setLogger(LoggerInterface $logger) |
||
38 | |||
39 | /** |
||
40 | * Set the logger for logging errors |
||
41 | * |
||
42 | * @return LoggerInterface |
||
43 | */ |
||
44 | public function getLogger() |
||
52 | |||
53 | /** |
||
54 | * Log an error or exception |
||
55 | * |
||
56 | * @param \Exception|\Error $error |
||
57 | */ |
||
58 | public function log($error) |
||
71 | |||
72 | /** |
||
73 | * Log an error |
||
74 | * |
||
75 | * @param \Error|\ErrorException $error |
||
76 | */ |
||
77 | protected function logError($error) |
||
95 | |||
96 | /** |
||
97 | * Log an exception |
||
98 | * |
||
99 | * @param \Exception $error |
||
100 | */ |
||
101 | protected function logException(\Exception $error) |
||
112 | |||
113 | |||
114 | /** |
||
115 | * Get the caught error |
||
116 | * |
||
117 | * @return \Throwable|\Exception|\Error |
||
118 | */ |
||
119 | public function getError() |
||
123 | |||
124 | |||
125 | /** |
||
126 | * Run middleware action |
||
127 | * |
||
128 | * @param ServerRequestInterface $request |
||
129 | * @param ResponseInterface $response |
||
130 | * @param callback $next |
||
131 | * @return ResponseInterface |
||
132 | */ |
||
133 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next) |
||
155 | |||
156 | /** |
||
157 | * Handle caught error |
||
158 | * |
||
159 | * @param ResponseInterface $response |
||
160 | * @return ResponseInterface |
||
161 | */ |
||
162 | protected function handleError($response) |
||
169 | |||
170 | |||
171 | /** |
||
172 | * Get the log level for an error code |
||
173 | * |
||
174 | * @param int $code E_* error code |
||
175 | * @return string |
||
176 | */ |
||
177 | protected function getLogLevel($code = null) |
||
204 | |||
205 | /** |
||
206 | * Turn an error code into a string |
||
207 | * |
||
208 | * @param int $code |
||
209 | * @return string |
||
210 | */ |
||
211 | protected function codeToString($code) |
||
243 | } |
||
244 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.