Complex classes like ActivityManager 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 ActivityManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class ActivityManager |
||
| 26 | { |
||
| 27 | /** Max char length of saved content data */ |
||
| 28 | const CONTENT_MAX_LENGTH = 2048; |
||
| 29 | |||
| 30 | /** @var bool If log is enabled */ |
||
| 31 | private $enabled = false; |
||
| 32 | |||
| 33 | /** @var Request $request */ |
||
| 34 | private $request; |
||
| 35 | |||
| 36 | /** @var array */ |
||
| 37 | private $configurations; |
||
| 38 | |||
| 39 | /** @var AuditTracking */ |
||
| 40 | private $document; |
||
| 41 | |||
| 42 | /** @var array Events that shall be stored */ |
||
| 43 | private $events = []; |
||
| 44 | |||
| 45 | /** @var string */ |
||
| 46 | private $globalRequestLocation = ''; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * DBActivityListener constructor. |
||
| 50 | * |
||
| 51 | * @param RequestStack $requestStack Sf request data |
||
| 52 | * @param AuditTracking $document DocumentCollection for event |
||
| 53 | */ |
||
| 54 | 4 | public function __construct( |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Set permission and access configuration |
||
| 64 | * |
||
| 65 | * @param array $configurations key value config |
||
| 66 | * @return void |
||
| 67 | */ |
||
| 68 | 4 | public function setConfiguration(array $configurations) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Return casted value from configuration. |
||
| 78 | * |
||
| 79 | * @param string $key Configuration key |
||
| 80 | * @param string $cast Type of object is expected to be returned |
||
| 81 | * @return int|string|bool|array |
||
| 82 | * @throws ParameterNotFoundException |
||
| 83 | */ |
||
| 84 | 2 | public function getConfigValue($key, $cast = 'string') |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Check if this the Call has to be logged |
||
| 102 | * |
||
| 103 | * @return bool |
||
| 104 | */ |
||
| 105 | 4 | private function runTracking() |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Incoming request done by user |
||
| 139 | * @param Request $request sf response priority 1 |
||
| 140 | * @return void |
||
| 141 | */ |
||
| 142 | public function registerRequestEvent(Request $request) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * The response returned to user |
||
| 179 | * |
||
| 180 | * @param Response $response sf response |
||
| 181 | * @return void |
||
| 182 | */ |
||
| 183 | public function registerResponseEvent(Response $response) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Capture possible un-handled exceptions in php |
||
| 222 | * |
||
| 223 | * @param \Exception $exception The exception thrown in service. |
||
| 224 | * @return void |
||
| 225 | */ |
||
| 226 | public function registerExceptionEvent(\Exception $exception) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Any database events, update, save or delete |
||
| 251 | * |
||
| 252 | * Available $event->getCollection() would give you the full object. |
||
| 253 | * |
||
| 254 | * @param ModelEvent $event Document object changed |
||
| 255 | * @return void |
||
| 256 | */ |
||
| 257 | public function registerDocumentModelEvent(ModelEvent $event) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Parse and extract customer header links |
||
| 288 | * |
||
| 289 | * @param string $strHeaderLink sf header links |
||
| 290 | * @param string $extract desired key to be found |
||
| 291 | * @return string |
||
| 292 | */ |
||
| 293 | 2 | private function extractHeaderLink($strHeaderLink, $extract = 'self') |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Get events AuditTracking |
||
| 312 | * |
||
| 313 | * @return array |
||
| 314 | */ |
||
| 315 | public function getEvents() |
||
| 319 | } |
||
| 320 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.