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 |
||
| 23 | trait ErrorHandlerTrait |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var string The url of the error api without trailing slash. Make sure you have installed the error api |
||
| 27 | * module on the requested api url (https://luya.io/guide/module/luyadev---luya-module-errorapi). |
||
| 28 | * |
||
| 29 | * An example when using the erroapi module, the url could look like this `https://luya.io/errorapi`. |
||
| 30 | */ |
||
| 31 | public $api; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var boolean Enable the transfer of exceptions to the defined `$api` server. |
||
| 35 | */ |
||
| 36 | public $transferException = false; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var \Curl\Curl|null The curl object from the last error api call. |
||
| 40 | * @since 1.0.5 |
||
| 41 | */ |
||
| 42 | public $lastTransferCall; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var array An array of exceptions which are whitelisted and therefore NOT TRANSFERED. |
||
| 46 | * @since 1.0.5 |
||
| 47 | */ |
||
| 48 | public $whitelist = ['yii\web\NotFoundHttpException']; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var array |
||
| 52 | * @since 1.0.6 |
||
| 53 | */ |
||
| 54 | public $sensitiveKeys = ['password', 'pwd', 'pass', 'passwort', 'pw', 'token', 'hash', 'authorization']; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Send a custom message to the api server event its not related to an exception. |
||
| 58 | * |
||
| 59 | * Sometimes you just want to pass informations from your application, this method allows you to transfer |
||
| 60 | * a message to the error api server. |
||
| 61 | * |
||
| 62 | * Example of sending a message |
||
| 63 | * |
||
| 64 | * ```php |
||
| 65 | * Yii::$app->errorHandler->transferMessage('Something went wrong here!', __FILE__, __LINE__); |
||
| 66 | * ``` |
||
| 67 | * |
||
| 68 | * @param string $message The message you want to send to the error api server. |
||
| 69 | * @param string $file The you are currently send the message (use __FILE__) |
||
| 70 | * @param string $line The line you want to submit (use __LINE__) |
||
| 71 | * @return bool|null |
||
| 72 | */ |
||
| 73 | public function transferMessage($message, $file = __FILE__, $line = __LINE__) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Send the array data to the api server. |
||
| 84 | * |
||
| 85 | * @param array $data The array to be sent to the server. |
||
| 86 | * @return boolean|null true/false if data has been sent to the api successfull or not, null if the transfer is disabled. |
||
| 87 | */ |
||
| 88 | private function apiServerSendData(array $data) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @inheritdoc |
||
| 108 | */ |
||
| 109 | public function renderException($exception) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Get an readable array to transfer from an exception |
||
| 120 | * |
||
| 121 | * @param mixed $exception Exception object |
||
| 122 | * @return array An array with transformed exception data |
||
| 123 | */ |
||
| 124 | public function getExceptionArray($exception) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Build trace array from exception. |
||
| 194 | * |
||
| 195 | * @param object $exception |
||
| 196 | * @return array |
||
| 197 | */ |
||
| 198 | private function buildTrace($exception) |
||
| 207 | |||
| 208 | private function buildTraceItem(array $item) |
||
| 265 | } |
||
| 266 |
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.