Complex classes like ErrorHandlerTrait 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 ErrorHandlerTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | trait ErrorHandlerTrait |
||
25 | { |
||
26 | /** |
||
27 | * @var string The url of the error api without trailing slash. Make sure you have installed the error api |
||
28 | * module on the requested api url (https://luya.io/guide/module/luyadev---luya-module-errorapi). |
||
29 | * |
||
30 | * An example when using the erroapi module, the url could look like this `https://luya.io/errorapi`. |
||
31 | */ |
||
32 | public $api; |
||
33 | |||
34 | /** |
||
35 | * @var boolean Enable the transfer of exceptions to the defined `$api` server. |
||
36 | */ |
||
37 | public $transferException = false; |
||
38 | |||
39 | /** |
||
40 | * @var \Curl\Curl|null The curl object from the last error api call. |
||
41 | * @since 1.0.5 |
||
42 | */ |
||
43 | public $lastTransferCall; |
||
44 | |||
45 | /** |
||
46 | * @var array An array of exceptions which are whitelisted and therefore NOT TRANSFERED. Whitelisted exception |
||
47 | * are basically expected application logic which does not need to report informations to the developer, as the |
||
48 | * application works as expect. |
||
49 | * @since 1.0.5 |
||
50 | */ |
||
51 | public $whitelist = [ |
||
52 | 'yii\base\InvalidRouteException', |
||
53 | 'yii\web\NotFoundHttpException', |
||
54 | 'yii\web\ForbiddenHttpException', |
||
55 | 'yii\web\MethodNotAllowedHttpException', |
||
56 | 'yii\web\UnauthorizedHttpException', |
||
57 | 'yii\web\BadRequestHttpException', |
||
58 | ]; |
||
59 | |||
60 | /** |
||
61 | * @var array |
||
62 | * @since 1.0.6 |
||
63 | */ |
||
64 | public $sensitiveKeys = ['password', 'pwd', 'pass', 'passwort', 'pw', 'token', 'hash', 'authorization']; |
||
65 | |||
66 | /** |
||
67 | * Send a custom message to the api server event its not related to an exception. |
||
68 | * |
||
69 | * Sometimes you just want to pass informations from your application, this method allows you to transfer |
||
70 | * a message to the error api server. |
||
71 | * |
||
72 | * Example of sending a message |
||
73 | * |
||
74 | * ```php |
||
75 | * Yii::$app->errorHandler->transferMessage('Something went wrong here!', __FILE__, __LINE__); |
||
76 | * ``` |
||
77 | * |
||
78 | * @param string $message The message you want to send to the error api server. |
||
79 | * @param string $file The you are currently send the message (use __FILE__) |
||
80 | * @param string $line The line you want to submit (use __LINE__) |
||
81 | * @return bool|null |
||
82 | */ |
||
83 | public function transferMessage($message, $file = __FILE__, $line = __LINE__) |
||
91 | |||
92 | /** |
||
93 | * Returns whether a given exception is whitelisted or not. |
||
94 | * |
||
95 | * If an exception is whitelisted or in the liste of whitelisted exception |
||
96 | * the exception content won't be transmitted to the error api. |
||
97 | * |
||
98 | * @param mixed $exception |
||
99 | * @return boolean Returns true if whitelisted, or false if not and can therefore be transmitted. |
||
100 | * @since 1.0.21 |
||
101 | */ |
||
102 | public function isExceptionWhitelisted($exception) |
||
114 | |||
115 | /** |
||
116 | * Send the array data to the api server. |
||
117 | * |
||
118 | * @param array $data The array to be sent to the server. |
||
119 | * @return boolean|null true/false if data has been sent to the api successfull or not, null if the transfer is disabled. |
||
120 | */ |
||
121 | private function apiServerSendData(array $data) |
||
133 | |||
134 | /** |
||
135 | * @inheritdoc |
||
136 | */ |
||
137 | public function renderException($exception) |
||
145 | |||
146 | /** |
||
147 | * Get an readable array to transfer from an exception |
||
148 | * |
||
149 | * @param mixed $exception Exception object |
||
150 | * @return array An array with transformed exception data |
||
151 | */ |
||
152 | public function getExceptionArray($exception) |
||
219 | |||
220 | /** |
||
221 | * Build trace array from exception. |
||
222 | * |
||
223 | * @param object $exception |
||
224 | * @return array |
||
225 | */ |
||
226 | private function buildTrace($exception) |
||
235 | |||
236 | /** |
||
237 | * Build the array trace item with file context. |
||
238 | * |
||
239 | * @param array $item |
||
240 | * @return array |
||
241 | */ |
||
242 | private function buildTraceItem(array $item) |
||
296 | } |
||
297 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.