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 |
||
15 | class Recorder extends Component { |
||
16 | |||
17 | /** |
||
18 | * @var mixed |
||
19 | */ |
||
20 | protected $config = []; |
||
21 | |||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $absolutelyDontHandle = [ |
||
26 | \Tylercd100\LERN\Exceptions\RecorderFailedException::class, |
||
27 | ]; |
||
28 | |||
29 | /** |
||
30 | * The constructor |
||
31 | */ |
||
32 | 57 | public function __construct() { |
|
35 | |||
36 | /** |
||
37 | * Records an Exception to the database |
||
38 | * @param Exception $e The exception you want to record |
||
39 | * @return false|ExceptionModel |
||
40 | * @throws RecorderFailedException |
||
41 | */ |
||
42 | 18 | public function record(Exception $e) |
|
43 | { |
||
44 | 18 | if ($this->shouldntHandle($e)) { |
|
45 | 6 | return false; |
|
46 | } |
||
47 | |||
48 | $opts = [ |
||
49 | 15 | 'class' => get_class($e), |
|
50 | 15 | 'file' => $e->getFile(), |
|
51 | 15 | 'line' => $e->getLine(), |
|
52 | 15 | 'code' => (is_int($e->getCode()) ? $e->getCode() : 0), |
|
53 | 15 | 'message' => $e->getMessage(), |
|
54 | 15 | 'trace' => $e->getTraceAsString(), |
|
55 | ]; |
||
56 | |||
57 | 15 | $configDependant = array_keys($this->config['collect']); |
|
58 | |||
59 | try { |
||
60 | 15 | foreach ($configDependant as $key) { |
|
61 | 15 | if ($this->canCollect($key)) { |
|
62 | 12 | $value = $this->collect($key, $e); |
|
63 | 12 | if ($value !== null) { |
|
64 | 15 | $opts[$key] = $value; |
|
65 | } |
||
66 | } |
||
67 | } |
||
68 | |||
69 | 15 | $class = config('lern.recorder.model'); |
|
70 | 15 | $class = !empty($class) ? $class : ExceptionModel::class; |
|
71 | |||
72 | 15 | $model = new $class(); |
|
73 | 15 | foreach($opts as $key => $value) { |
|
74 | 15 | $model->{$key} = $value; |
|
75 | } |
||
76 | |||
77 | 15 | $model->save(); |
|
78 | |||
79 | 12 | Cache::forever($this->getCacheKey($e), Carbon::now()); |
|
80 | |||
81 | 12 | return $model; |
|
82 | 3 | } catch (Exception $e) { |
|
83 | 3 | $code = (is_int($e->getCode()) ? $e->getCode() : 0); |
|
84 | 3 | throw new RecorderFailedException($e->getMessage(), $code, $e); |
|
85 | } |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Checks the config to see if you can collect certain information |
||
90 | * @param string $type the config value you want to check |
||
91 | * @return boolean |
||
92 | */ |
||
93 | 15 | private function canCollect($type) { |
|
94 | 15 | if (!empty($this->config) && !empty($this->config['collect']) && !empty($this->config['collect'][$type])) { |
|
95 | 12 | return $this->config['collect'][$type] === true; |
|
96 | } |
||
97 | 3 | return false; |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param string $key |
||
102 | * @param Exception $e |
||
103 | * @return array|int|null|string |
||
104 | * @throws Exception |
||
105 | */ |
||
106 | 12 | protected function collect($key, Exception $e = null) { |
|
127 | |||
128 | /** |
||
129 | * Gets the ID of the User that is logged in |
||
130 | * @return integer|null The ID of the User or Null if not logged in |
||
131 | */ |
||
132 | 12 | protected function getUserId() { |
|
140 | |||
141 | /** |
||
142 | * Gets the Method of the Request |
||
143 | * @return string|null Possible values are null or GET, POST, DELETE, PUT, etc... |
||
144 | */ |
||
145 | 12 | protected function getMethod() { |
|
153 | |||
154 | /** |
||
155 | * Gets the input data of the Request |
||
156 | * @return array|null The Input data or null |
||
157 | */ |
||
158 | 15 | protected function getData() { |
|
166 | |||
167 | /** |
||
168 | * Gets the URL of the Request |
||
169 | * @return string|null Returns a URL string or null |
||
170 | */ |
||
171 | 12 | protected function getUrl() { |
|
179 | |||
180 | /** |
||
181 | * Returns the IP from the request |
||
182 | * |
||
183 | * @return string |
||
184 | */ |
||
185 | protected function getIp() { |
||
188 | |||
189 | /** |
||
190 | * Gets the status code of the Exception |
||
191 | * @param Exception $e The Exception to check |
||
192 | * @return string|integer The status code value |
||
193 | */ |
||
194 | 12 | protected function getStatusCode(Exception $e) { |
|
201 | |||
202 | /** |
||
203 | * This function will remove all keys from an array recursively as defined in the config file |
||
204 | * @param array $data The array to remove keys from |
||
205 | * @return void |
||
206 | */ |
||
207 | 21 | protected function excludeKeys(array $data) { |
|
219 | } |
||
220 |
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: