Complex classes like Notification 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 Notification, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Notification extends AbstractPlugin |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * Namespace for info notifications |
||
| 30 | */ |
||
| 31 | const NAMESPACE_INFO = 'info'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Namespace for success notifications |
||
| 35 | */ |
||
| 36 | const NAMESPACE_SUCCESS = 'success'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Namespace for warning notifications |
||
| 40 | */ |
||
| 41 | const NAMESPACE_WARNING = 'warning'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Namespace for error notifications |
||
| 45 | */ |
||
| 46 | const NAMESPACE_ERROR = 'error'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var Container |
||
| 50 | */ |
||
| 51 | protected $container; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var Manager |
||
| 55 | */ |
||
| 56 | protected $sessionManager; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | protected $notifications = []; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var bool |
||
| 65 | */ |
||
| 66 | protected $isAdded = false; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $customMethods = [ |
||
| 72 | 'add', |
||
| 73 | 'get', |
||
| 74 | 'getcurrent', |
||
| 75 | 'has', |
||
| 76 | 'hascurrent' |
||
| 77 | ]; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param Manager $manager |
||
| 81 | * @return $this |
||
| 82 | */ |
||
| 83 | 4 | public function setSessionManager(Manager $manager) |
|
| 89 | |||
| 90 | /** |
||
| 91 | * @return Manager |
||
| 92 | */ |
||
| 93 | 4 | public function getSessionManager() |
|
| 101 | |||
| 102 | /** |
||
| 103 | * @return Container |
||
| 104 | */ |
||
| 105 | 4 | public function getContainer() |
|
| 116 | |||
| 117 | /** |
||
| 118 | * @param $type |
||
| 119 | * @param $message |
||
| 120 | * @param int $hops |
||
| 121 | * @return $this |
||
| 122 | */ |
||
| 123 | 5 | public function add($type, $message, $hops = 1) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * @param $message |
||
| 144 | * @return $this |
||
| 145 | */ |
||
| 146 | 1 | public function addInfo($message) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * @param $message |
||
| 153 | * @return $this |
||
| 154 | */ |
||
| 155 | 1 | public function addSuccess($message) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * @param $message |
||
| 162 | * @return $this |
||
| 163 | */ |
||
| 164 | 1 | public function addWarning($message) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * @param $message |
||
| 171 | * @return $this |
||
| 172 | */ |
||
| 173 | 1 | public function addError($message) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * @param $type |
||
| 180 | * @param $message |
||
| 181 | * @return $this |
||
| 182 | */ |
||
| 183 | 2 | public function __invoke($type = null, $message = null) |
|
| 191 | |||
| 192 | /** |
||
| 193 | * @param $namespace |
||
| 194 | * @return array |
||
| 195 | */ |
||
| 196 | 4 | public function get($namespace) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * @return array |
||
| 207 | */ |
||
| 208 | 1 | public function getAll() |
|
| 214 | |||
| 215 | /** |
||
| 216 | * @return array |
||
| 217 | */ |
||
| 218 | 1 | public function getInfo() |
|
| 222 | |||
| 223 | /** |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | 1 | public function getSuccess() |
|
| 230 | |||
| 231 | /** |
||
| 232 | * @return array |
||
| 233 | */ |
||
| 234 | 1 | public function getWarning() |
|
| 238 | |||
| 239 | /** |
||
| 240 | * @return array |
||
| 241 | */ |
||
| 242 | 1 | public function getError() |
|
| 246 | |||
| 247 | /** |
||
| 248 | * @param $namespace |
||
| 249 | * @return bool |
||
| 250 | */ |
||
| 251 | 3 | public function has($namespace) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * @return bool |
||
| 260 | */ |
||
| 261 | 1 | public function hasInfo() |
|
| 265 | |||
| 266 | /** |
||
| 267 | * @return bool |
||
| 268 | */ |
||
| 269 | 1 | public function hasSuccess() |
|
| 273 | |||
| 274 | /** |
||
| 275 | * @return bool |
||
| 276 | */ |
||
| 277 | 1 | public function hasWarning() |
|
| 281 | |||
| 282 | /** |
||
| 283 | * @return bool |
||
| 284 | */ |
||
| 285 | 1 | public function hasError() |
|
| 289 | |||
| 290 | /** |
||
| 291 | * @param $namespace |
||
| 292 | * @return array|mixed |
||
| 293 | */ |
||
| 294 | 3 | public function getCurrent($namespace) |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Get all current notifications |
||
| 307 | * |
||
| 308 | * @return array |
||
| 309 | */ |
||
| 310 | 1 | public function getAllCurrent() |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Get info notifications added during this request |
||
| 324 | * |
||
| 325 | * @return array|mixed |
||
| 326 | */ |
||
| 327 | 1 | public function getCurrentInfo() |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Get success notifications added during this request |
||
| 334 | * |
||
| 335 | * @return array|mixed |
||
| 336 | */ |
||
| 337 | 1 | public function getCurrentSuccess() |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Get warning notifications added during this request |
||
| 344 | * |
||
| 345 | * @return array|mixed |
||
| 346 | */ |
||
| 347 | 1 | public function getCurrentWarning() |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Get error notifications added during this request |
||
| 354 | * |
||
| 355 | * @return array|mixed |
||
| 356 | */ |
||
| 357 | 1 | public function getCurrentError() |
|
| 361 | |||
| 362 | /** |
||
| 363 | * @param $namespace |
||
| 364 | * @return bool |
||
| 365 | */ |
||
| 366 | 2 | public function hasCurrent($namespace) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Check if info notifications have been added during this request |
||
| 375 | * |
||
| 376 | * @return bool |
||
| 377 | */ |
||
| 378 | 1 | public function hasCurrentInfo() |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Check if success notifications have been added during this request |
||
| 385 | * |
||
| 386 | * @return bool |
||
| 387 | */ |
||
| 388 | 1 | public function hasCurrentSuccess() |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Check if warning notifications have been added during this request |
||
| 395 | * |
||
| 396 | * @return bool |
||
| 397 | */ |
||
| 398 | 1 | public function hasCurrentWarning() |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Check if error notifications have been added during this request |
||
| 405 | * |
||
| 406 | * @return bool |
||
| 407 | */ |
||
| 408 | 1 | public function hasCurrentError() |
|
| 412 | |||
| 413 | /** |
||
| 414 | * @param $name |
||
| 415 | * @param $arguments |
||
| 416 | * @return mixed |
||
| 417 | */ |
||
| 418 | 7 | public function __call($name, $arguments) |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Clear notifications from container |
||
| 452 | */ |
||
| 453 | 4 | protected function getNotificationsFromContainer() |
|
| 469 | |||
| 470 | /** |
||
| 471 | * @param $namespaces |
||
| 472 | */ |
||
| 473 | 4 | protected function clearNotificationsFromContainer($namespaces) |
|
| 482 | |||
| 483 | /** |
||
| 484 | * Clear notifications from container if namespace is provided or clear all notifications |
||
| 485 | * added during this request if namespace is not provided. |
||
| 486 | * |
||
| 487 | * @param null $namespace |
||
| 488 | * @return bool |
||
| 489 | */ |
||
| 490 | 3 | public function clearCurrent($namespace = null) |
|
| 509 | |||
| 510 | /** |
||
| 511 | * Clear notifications from previous request by provided namespace or clear all |
||
| 512 | * notifications if namespace is not provided |
||
| 513 | * |
||
| 514 | * @param null $namespace |
||
| 515 | * @return bool |
||
| 516 | */ |
||
| 517 | 3 | public function clear($namespace = null) |
|
| 536 | } |