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 | * Constructor. |
||
| 71 | * |
||
| 72 | * @param Database $db |
||
| 73 | * @param Server $server |
||
| 74 | * @param LoggerInterace $logger |
||
| 75 | * @param iterable $config |
||
| 76 | */ |
||
| 77 | public function __construct(Database $db, Server $server, LoggerInterface $logger, ?Iterable $config = null) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Set options. |
||
| 87 | * |
||
| 88 | * @param iterable $config |
||
| 89 | * |
||
| 90 | * @return Notifier |
||
| 91 | */ |
||
| 92 | public function setOptions(?Iterable $config = null): self |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Send notification. |
||
| 116 | * |
||
| 117 | * @param iterable $receiver |
||
| 118 | * @param User $sender |
||
| 119 | * @param string $subject |
||
| 120 | * @param string $body |
||
| 121 | * @param array $context |
||
| 122 | * |
||
| 123 | * @return bool |
||
| 124 | */ |
||
| 125 | public function notify(Iterable $receiver, ?User $sender, MessageInterface $message, array $context = []): bool |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Has adapter. |
||
| 150 | * |
||
| 151 | * @param string $name |
||
| 152 | * |
||
| 153 | * @return bool |
||
| 154 | */ |
||
| 155 | public function hasAdapter(string $name): bool |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Inject adapter. |
||
| 162 | * |
||
| 163 | * @param AdapterInterface $adapter |
||
| 164 | * @param string $name |
||
| 165 | * |
||
| 166 | * @return Notifier |
||
| 167 | */ |
||
| 168 | public function injectAdapter(AdapterInterface $adapter, ?string $name = null): self |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Get adapter. |
||
| 189 | * |
||
| 190 | * @param string $name |
||
| 191 | * |
||
| 192 | * @return AdapterInterface |
||
| 193 | */ |
||
| 194 | public function getAdapter(string $name): AdapterInterface |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Get adapters. |
||
| 205 | * |
||
| 206 | * @param array $adapters |
||
| 207 | * |
||
| 208 | * @return AdapterInterface[] |
||
| 209 | */ |
||
| 210 | public function getAdapters(array $adapters = []): array |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Add notification. |
||
| 228 | * |
||
| 229 | * @param array $receiver |
||
| 230 | * @param User $user |
||
| 231 | * @param MessageInterface $message |
||
| 232 | * @param array $context |
||
| 233 | * |
||
| 234 | * @return ObjectId |
||
| 235 | */ |
||
| 236 | public function postNotification(User $receiver, ?User $sender, MessageInterface $message, array $context = []): ObjectId |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Get notifications. |
||
| 256 | * |
||
| 257 | * @param User $user |
||
| 258 | * @param int $offset |
||
| 259 | * @param int $limit |
||
| 260 | * @param int $total |
||
| 261 | * |
||
| 262 | * @return iterable |
||
| 263 | */ |
||
| 264 | public function getNotifications(User $user, ?int $offset = null, ?int $limit = null, ?int &$total = null): Iterable |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Get notification. |
||
| 277 | * |
||
| 278 | * @param ObjectId $id |
||
| 279 | * |
||
| 280 | * @return array |
||
| 281 | */ |
||
| 282 | public function getNotification(ObjectId $id): array |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Get notifications. |
||
| 298 | * |
||
| 299 | * @param ObjectId $id |
||
| 300 | * |
||
| 301 | * @return bool |
||
| 302 | */ |
||
| 303 | public function deleteNotification(ObjectId $id): bool |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Throttle subscriptions. |
||
| 323 | */ |
||
| 324 | public function throttleSubscriptions(NodeInterface $node, array $user): Notifier |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Get subscription. |
||
| 343 | */ |
||
| 344 | public function getSubscription(NodeInterface $node, User $user): ?array |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Get subscriptions. |
||
| 356 | */ |
||
| 357 | public function getSubscriptions(NodeInterface $node): Iterable |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Subscribe to node updates. |
||
| 368 | * |
||
| 369 | * @param NodeInterface $node |
||
| 370 | * @param bool $subscribe |
||
| 371 | * @param bool $exclude_me |
||
| 372 | * @param bool $recursive |
||
| 373 | * |
||
| 374 | * @return bool |
||
| 375 | */ |
||
| 376 | public function subscribeNode(NodeInterface $node, bool $subscribe = true, bool $exclude_me = true, bool $recursive = false): bool |
||
| 444 | } |
||
| 445 |
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.