Complex classes like Notifier 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 Notifier, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class Notifier |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * Notifications. |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $notifications = []; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Adapter. |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $adapter = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Logger. |
||
| 43 | * |
||
| 44 | * @var LoggerInterface |
||
| 45 | */ |
||
| 46 | protected $logger; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Database. |
||
| 50 | * |
||
| 51 | * @var Database |
||
| 52 | */ |
||
| 53 | protected $db; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Server. |
||
| 57 | * |
||
| 58 | * @var Server |
||
| 59 | */ |
||
| 60 | protected $server; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Collection name. |
||
| 64 | * |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | protected $collection_name = 'notification'; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Message handler. |
||
| 71 | * |
||
| 72 | * @var TemplateHandler |
||
| 73 | */ |
||
| 74 | protected $template; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Notification throttle. |
||
| 78 | * |
||
| 79 | * @var int |
||
| 80 | */ |
||
| 81 | protected $notification_throttle = 120; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Constructor. |
||
| 85 | */ |
||
| 86 | public function __construct(Database $db, Server $server, LoggerInterface $logger, TemplateHandler $template, array $config = []) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Set config. |
||
| 97 | */ |
||
| 98 | public function setOptions(array $config = []): self |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Get notification throttle time. |
||
| 116 | */ |
||
| 117 | public function getThrottleTime(): int |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Node message factory. |
||
| 124 | */ |
||
| 125 | public function compose(string $type, array $context = []): MessageInterface |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Send notification. |
||
| 132 | */ |
||
| 133 | public function notify(iterable $receiver, ?User $sender, MessageInterface $message): bool |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Has adapter. |
||
| 154 | */ |
||
| 155 | public function hasAdapter(string $name): bool |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Inject adapter. |
||
| 162 | */ |
||
| 163 | public function injectAdapter(AdapterInterface $adapter, ?string $name = null): self |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Get adapter. |
||
| 184 | */ |
||
| 185 | public function getAdapter(string $name): AdapterInterface |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Get adapters. |
||
| 196 | */ |
||
| 197 | public function getAdapters(array $adapters = []): array |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Add notification. |
||
| 215 | */ |
||
| 216 | public function postNotification(User $receiver, ?User $sender, MessageInterface $message): ObjectId |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Get notifications. |
||
| 241 | */ |
||
| 242 | public function getNotifications(User $user, array $query = [], ?int $offset = null, ?int $limit = null, ?int &$total = null): iterable |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Get notification. |
||
| 265 | */ |
||
| 266 | public function getNotification(ObjectId $id): array |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Get notifications. |
||
| 282 | */ |
||
| 283 | public function deleteNotification(ObjectId $id): bool |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Throttle subscriptions. |
||
| 303 | */ |
||
| 304 | public function throttleSubscriptions(array $subscriptions): Notifier |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Get subscription. |
||
| 321 | */ |
||
| 322 | public function getSubscription(NodeInterface $node, User $user): ?array |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Get subscriptions. |
||
| 334 | */ |
||
| 335 | public function getSubscriptions(NodeInterface $node): iterable |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Get subscriptions. |
||
| 346 | */ |
||
| 347 | public function getAllSubscriptions(NodeInterface $node): iterable |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Subscribe to node updates. |
||
| 381 | */ |
||
| 382 | public function subscribeNode(NodeInterface $node, bool $subscribe = true, bool $exclude_me = true, bool $recursive = false, ?int $throttle = null): bool |
||
| 434 | } |
||
| 435 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.