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 | 33 | 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 | 3 | public function record(Exception $e) |
|
| 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 | 3 | private function canCollect($type) {
|
|
| 94 | |||
| 95 | /** |
||
| 96 | * @param string $key |
||
| 97 | * @param Exception $e |
||
| 98 | * @return array|int|null|string |
||
| 99 | * @throws Exception |
||
| 100 | */ |
||
| 101 | 3 | 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 | 3 | 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 | 3 | protected function getMethod() {
|
|
| 148 | |||
| 149 | /** |
||
| 150 | * Gets the input data of the Request |
||
| 151 | * @return array|null The Input data or null |
||
| 152 | */ |
||
| 153 | 3 | protected function getData() {
|
|
| 161 | |||
| 162 | /** |
||
| 163 | * Gets the URL of the Request |
||
| 164 | * @return string|null Returns a URL string or null |
||
| 165 | */ |
||
| 166 | 3 | 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 | 3 | 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 | 3 | 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: