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 | * The constructor |
||
22 | */ |
||
23 | 48 | public function __construct() { |
|
26 | |||
27 | /** |
||
28 | * Records an Exception to the database |
||
29 | * @param Exception $e The exception you want to record |
||
30 | * @return false|ExceptionModel |
||
31 | * @throws RecorderFailedException |
||
32 | */ |
||
33 | 9 | public function record(Exception $e) |
|
75 | |||
76 | /** |
||
77 | * Checks the config to see if you can collect certain information |
||
78 | * @param string $type the config value you want to check |
||
79 | * @return boolean |
||
80 | */ |
||
81 | 6 | private function canCollect($type) { |
|
87 | |||
88 | /** |
||
89 | * @param string $key |
||
90 | * @param Exception $e |
||
91 | * @return array|int|null|string |
||
92 | * @throws Exception |
||
93 | */ |
||
94 | 3 | protected function collect($key, Exception $e = null) { |
|
115 | |||
116 | /** |
||
117 | * Gets the ID of the User that is logged in |
||
118 | * @return integer|null The ID of the User or Null if not logged in |
||
119 | */ |
||
120 | 3 | protected function getUserId() { |
|
128 | |||
129 | /** |
||
130 | * Gets the Method of the Request |
||
131 | * @return string|null Possible values are null or GET, POST, DELETE, PUT, etc... |
||
132 | */ |
||
133 | 3 | protected function getMethod() { |
|
141 | |||
142 | /** |
||
143 | * Gets the input data of the Request |
||
144 | * @return array|null The Input data or null |
||
145 | */ |
||
146 | 6 | protected function getData() { |
|
154 | |||
155 | /** |
||
156 | * Gets the URL of the Request |
||
157 | * @return string|null Returns a URL string or null |
||
158 | */ |
||
159 | 3 | protected function getUrl() { |
|
167 | |||
168 | /** |
||
169 | * Returns the IP from the request |
||
170 | * |
||
171 | * @return string |
||
172 | */ |
||
173 | protected function getIp() { |
||
176 | |||
177 | /** |
||
178 | * Gets the status code of the Exception |
||
179 | * @param Exception $e The Exception to check |
||
180 | * @return string|integer The status code value |
||
181 | */ |
||
182 | 3 | protected function getStatusCode(Exception $e) { |
|
189 | |||
190 | /** |
||
191 | * This function will remove all keys from an array recursively as defined in the config file |
||
192 | * @param array $data The array to remove keys from |
||
193 | * @return void |
||
194 | */ |
||
195 | 12 | protected function excludeKeys(array $data) { |
|
207 | } |
||
208 |
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: