Complex classes like Manager 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 Manager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class Manager implements IManager { |
||
| 39 | /** @var EventDispatcherInterface */ |
||
| 40 | protected $dispatcher; |
||
| 41 | |||
| 42 | /** @var IApp[] */ |
||
| 43 | protected $apps; |
||
| 44 | |||
| 45 | /** @var INotifier */ |
||
| 46 | protected $notifiers; |
||
| 47 | |||
| 48 | /** @var array[] */ |
||
| 49 | protected $notifiersInfo; |
||
| 50 | |||
| 51 | /** @var \Closure[] */ |
||
| 52 | protected $appsClosures; |
||
| 53 | |||
| 54 | /** @var \Closure[] */ |
||
| 55 | protected $notifiersClosures; |
||
| 56 | |||
| 57 | /** @var \Closure[] */ |
||
| 58 | protected $notifiersInfoClosures; |
||
| 59 | |||
| 60 | /** @var IApp[] */ |
||
| 61 | protected $builtAppsHolder; |
||
| 62 | |||
| 63 | /** @var INotifier[] */ |
||
| 64 | protected $builtNotifiersHolder; |
||
| 65 | |||
| 66 | public function __construct(EventDispatcherInterface $dispatcher) { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param \Closure $service The service must implement IApp, otherwise a |
||
| 85 | * \InvalidArgumentException is thrown later |
||
| 86 | * @return null |
||
| 87 | * @since 8.2.0 |
||
| 88 | */ |
||
| 89 | public function registerApp(\Closure $service) { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param \Closure $service The service must implement INotifier, otherwise a |
||
| 96 | * \InvalidArgumentException is thrown later |
||
| 97 | * @param \Closure $info An array with the keys 'id' and 'name' containing |
||
| 98 | * the app id and the app name |
||
| 99 | * @return null |
||
| 100 | * @since 8.2.0 - Parameter $info was added in 9.0.0 |
||
| 101 | */ |
||
| 102 | public function registerNotifier(\Closure $service, \Closure $info) { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * INTERNAL USE ONLY!! This method isn't part of the IManager interface |
||
| 111 | * @internal This should only be used by the RegisterConsumerEventImpl (the real implementation). |
||
| 112 | * Do NOT use this method outside as it might not work as expected. |
||
| 113 | */ |
||
| 114 | public function registerBuiltApp(IApp $app) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * INTERNAL USE ONLY!! This method isn't part of the IManager interface |
||
| 120 | * @internal This should only be used by the RegisterNotifierEventImpl (the real implementation). |
||
| 121 | * Do NOT use this method outside as it might not work as expected. |
||
| 122 | */ |
||
| 123 | public function registerBuiltNotifier(INotifier $notifier, $id, $name) { |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @return IApp[] |
||
| 136 | */ |
||
| 137 | protected function getApps() { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @return INotifier[] |
||
| 161 | */ |
||
| 162 | protected function getNotifiers() { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @return array[] |
||
| 188 | */ |
||
| 189 | public function listNotifiers() { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @return INotification |
||
| 218 | * @since 8.2.0 |
||
| 219 | */ |
||
| 220 | public function createNotification() { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @return bool |
||
| 226 | * @since 8.2.0 |
||
| 227 | */ |
||
| 228 | public function hasNotifiers() { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param INotification $notification |
||
| 234 | * @return null |
||
| 235 | * @throws \InvalidArgumentException When the notification is not valid |
||
| 236 | * @since 8.2.0 |
||
| 237 | */ |
||
| 238 | public function notify(INotification $notification) { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param INotification $notification |
||
| 255 | * @param string $languageCode The code of the language that should be used to prepare the notification |
||
| 256 | * @return INotification |
||
| 257 | * @throws \InvalidArgumentException When the notification was not prepared by a notifier |
||
| 258 | * @since 8.2.0 |
||
| 259 | */ |
||
| 260 | public function prepare(INotification $notification, $languageCode) { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @param INotification $notification |
||
| 284 | * @return null |
||
| 285 | */ |
||
| 286 | public function markProcessed(INotification $notification) { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param INotification $notification |
||
| 296 | * @return int |
||
| 297 | */ |
||
| 298 | public function getCount(INotification $notification) { |
||
| 308 | |||
| 309 | public function removeUserNotifications($uid) { |
||
| 316 | } |
||
| 317 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..