Complex classes like Recorder 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 Recorder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Recorder extends Component {
|
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var mixed |
||
| 17 | */ |
||
| 18 | protected $config = []; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | protected $absolutelyDontHandle = [ |
||
| 24 | \Tylercd100\LERN\Exceptions\RecorderFailedException::class, |
||
| 25 | ]; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The constructor |
||
| 29 | */ |
||
| 30 | 54 | public function __construct() {
|
|
| 33 | |||
| 34 | /** |
||
| 35 | * Records an Exception to the database |
||
| 36 | * @param Exception $e The exception you want to record |
||
| 37 | * @return false|ExceptionModel |
||
| 38 | * @throws RecorderFailedException |
||
| 39 | */ |
||
| 40 | 15 | public function record(Exception $e) |
|
| 41 | {
|
||
| 42 | 15 | if ($this->shouldntHandle($e)) {
|
|
| 43 | 3 | return false; |
|
| 44 | } |
||
| 45 | |||
| 46 | $opts = [ |
||
| 47 | 12 | 'class' => get_class($e), |
|
| 48 | 12 | 'file' => $e->getFile(), |
|
| 49 | 12 | 'line' => $e->getLine(), |
|
| 50 | 12 | 'code' => (is_int($e->getCode()) ? $e->getCode() : 0), |
|
| 51 | 12 | 'message' => $e->getMessage(), |
|
| 52 | 12 | 'trace' => $e->getTraceAsString(), |
|
| 53 | ]; |
||
| 54 | |||
| 55 | 12 | $configDependant = array_keys($this->config['collect']); |
|
| 56 | |||
| 57 | try {
|
||
| 58 | 12 | foreach ($configDependant as $key) {
|
|
| 59 | 12 | if ($this->canCollect($key)) {
|
|
| 60 | 9 | $value = $this->collect($key, $e); |
|
| 61 | 9 | if ($value !== null) {
|
|
| 62 | 12 | $opts[$key] = $value; |
|
| 63 | } |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | 12 | $class = config('lern.recorder.model');
|
|
| 68 | 12 | $class = !empty($class) ? $class : ExceptionModel::class; |
|
| 69 | |||
| 70 | 12 | $model = new $class(); |
|
| 71 | 12 | foreach($opts as $key => $value) {
|
|
| 72 | 12 | $model->{$key} = $value;
|
|
| 73 | } |
||
| 74 | |||
| 75 | 12 | $model->save(); |
|
| 76 | 9 | return $model; |
|
| 77 | 3 | } catch (Exception $e) {
|
|
| 78 | 3 | $code = (is_int($e->getCode()) ? $e->getCode() : 0); |
|
| 79 | 3 | throw new RecorderFailedException($e->getMessage(), $code, $e); |
|
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Checks the config to see if you can collect certain information |
||
| 85 | * @param string $type the config value you want to check |
||
| 86 | * @return boolean |
||
| 87 | */ |
||
| 88 | 12 | private function canCollect($type) {
|
|
| 89 | 12 | if (!empty($this->config) && !empty($this->config['collect']) && !empty($this->config['collect'][$type])) {
|
|
| 90 | 9 | return $this->config['collect'][$type] === true; |
|
| 91 | } |
||
| 92 | 3 | return false; |
|
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param string $key |
||
| 97 | * @param Exception $e |
||
| 98 | * @return array|int|null|string |
||
| 99 | * @throws Exception |
||
| 100 | */ |
||
| 101 | 9 | protected function collect($key, Exception $e = null) {
|
|
| 122 | |||
| 123 | /** |
||
| 124 | * Gets the ID of the User that is logged in |
||
| 125 | * @return integer|null The ID of the User or Null if not logged in |
||
| 126 | */ |
||
| 127 | 9 | protected function getUserId() {
|
|
| 135 | |||
| 136 | /** |
||
| 137 | * Gets the Method of the Request |
||
| 138 | * @return string|null Possible values are null or GET, POST, DELETE, PUT, etc... |
||
| 139 | */ |
||
| 140 | 9 | protected function getMethod() {
|
|
| 148 | |||
| 149 | /** |
||
| 150 | * Gets the input data of the Request |
||
| 151 | * @return array|null The Input data or null |
||
| 152 | */ |
||
| 153 | 12 | protected function getData() {
|
|
| 161 | |||
| 162 | /** |
||
| 163 | * Gets the URL of the Request |
||
| 164 | * @return string|null Returns a URL string or null |
||
| 165 | */ |
||
| 166 | 9 | protected function getUrl() {
|
|
| 174 | |||
| 175 | /** |
||
| 176 | * Returns the IP from the request |
||
| 177 | * |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | protected function getIp() {
|
||
| 183 | |||
| 184 | /** |
||
| 185 | * Gets the status code of the Exception |
||
| 186 | * @param Exception $e The Exception to check |
||
| 187 | * @return string|integer The status code value |
||
| 188 | */ |
||
| 189 | 9 | protected function getStatusCode(Exception $e) {
|
|
| 196 | |||
| 197 | /** |
||
| 198 | * This function will remove all keys from an array recursively as defined in the config file |
||
| 199 | * @param array $data The array to remove keys from |
||
| 200 | * @return void |
||
| 201 | */ |
||
| 202 | 18 | protected function excludeKeys(array $data) {
|
|
| 214 | } |
||
| 215 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: