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. |
||
| 47 | * @since 1.0.5 |
||
| 48 | */ |
||
| 49 | public $whitelist = [ |
||
| 50 | 'yii\base\InvalidRouteException', |
||
| 51 | 'yii\web\NotFoundHttpException', |
||
| 52 | 'yii\web\ForbiddenHttpException', |
||
| 53 | 'yii\web\MethodNotAllowedHttpException', |
||
| 54 | 'yii\web\UnauthorizedHttpException', |
||
| 55 | 'yii\web\BadRequestHttpException', |
||
| 56 | ]; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var array |
||
| 60 | * @since 1.0.6 |
||
| 61 | */ |
||
| 62 | public $sensitiveKeys = ['password', 'pwd', 'pass', 'passwort', 'pw', 'token', 'hash', 'authorization']; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Send a custom message to the api server event its not related to an exception. |
||
| 66 | * |
||
| 67 | * Sometimes you just want to pass informations from your application, this method allows you to transfer |
||
| 68 | * a message to the error api server. |
||
| 69 | * |
||
| 70 | * Example of sending a message |
||
| 71 | * |
||
| 72 | * ```php |
||
| 73 | * Yii::$app->errorHandler->transferMessage('Something went wrong here!', __FILE__, __LINE__); |
||
| 74 | * ``` |
||
| 75 | * |
||
| 76 | * @param string $message The message you want to send to the error api server. |
||
| 77 | * @param string $file The you are currently send the message (use __FILE__) |
||
| 78 | * @param string $line The line you want to submit (use __LINE__) |
||
| 79 | * @return bool|null |
||
| 80 | */ |
||
| 81 | public function transferMessage($message, $file = __FILE__, $line = __LINE__) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Returns whether a given exception is whitelisted or not. |
||
| 92 | * |
||
| 93 | * If an exception is whitelisted or in the liste of whitelisted exception |
||
| 94 | * the exception content won't be transmitted to the error api. |
||
| 95 | * |
||
| 96 | * @param mixed $exception |
||
| 97 | * @return boolean Returns true if whitelisted, or false if not and can therefore be transmitted. |
||
| 98 | * @since 1.0.21 |
||
| 99 | */ |
||
| 100 | public function isExceptionWhitelisted($exception) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Send the array data to the api server. |
||
| 115 | * |
||
| 116 | * @param array $data The array to be sent to the server. |
||
| 117 | * @return boolean|null true/false if data has been sent to the api successfull or not, null if the transfer is disabled. |
||
| 118 | */ |
||
| 119 | private function apiServerSendData(array $data) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @inheritdoc |
||
| 134 | */ |
||
| 135 | public function renderException($exception) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Get an readable array to transfer from an exception |
||
| 146 | * |
||
| 147 | * @param mixed $exception Exception object |
||
| 148 | * @return array An array with transformed exception data |
||
| 149 | */ |
||
| 150 | public function getExceptionArray($exception) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Build trace array from exception. |
||
| 220 | * |
||
| 221 | * @param object $exception |
||
| 222 | * @return array |
||
| 223 | */ |
||
| 224 | private function buildTrace($exception) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Build the array trace item with file context. |
||
| 236 | * |
||
| 237 | * @param array $item |
||
| 238 | * @return array |
||
| 239 | */ |
||
| 240 | private function buildTraceItem(array $item) |
||
| 294 | } |
||
| 295 |
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.