Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like PredisAdapter 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 PredisAdapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class PredisAdapter implements QueueAdapterInterface, SchedulerAdapterInterface |
||
| 17 | { |
||
| 18 | |||
| 19 | private $client; |
||
| 20 | |||
| 21 | public function __construct(Client $client) |
||
| 25 | |||
| 26 | public function queueCommand(string $queueName, string $id, string $serialized) |
||
| 27 | { |
||
| 28 | if (!$this->storeCommandAndCheckIfIdReserved($queueName, $id, $serialized)) { |
||
| 29 | View Code Duplication | $this->client->pipeline(function (ClientContextInterface $client) use ($queueName, $id) { |
|
|
|
|||
| 30 | self::cReserveCommandId($client, $queueName, $id); |
||
| 31 | self::cUpdateCommandStatus($client, $queueName, $id, self::STATUS_QUEUED); |
||
| 32 | $client->lpush(":{$queueName}:queue", [ $id ]); |
||
| 33 | }); |
||
| 34 | } |
||
| 35 | } |
||
| 36 | |||
| 37 | public function awaitCommand(string $queueName, int $timeout = null): ReceivedCommand |
||
| 38 | { |
||
| 39 | $stopwatchStart = time(); |
||
| 40 | $this->client->ping(); |
||
| 41 | try { |
||
| 42 | $id = $this->client->brpoplpush(":{$queueName}:queue", ":{$queueName}:consuming", $timeout ?? 0); |
||
| 43 | } catch (ConnectionException $e) { |
||
| 44 | $id = null; |
||
| 45 | } |
||
| 46 | if (!$id) { |
||
| 47 | if ($timeout !== null) { |
||
| 48 | $timeout = time() - $stopwatchStart - $timeout; |
||
| 49 | if ($timeout <= 0) { |
||
| 50 | throw new TimeoutException; |
||
| 51 | } |
||
| 52 | } |
||
| 53 | return $this->awaitCommand($queueName, $timeout); |
||
| 54 | } |
||
| 55 | /* @var $id string */ |
||
| 56 | View Code Duplication | list(, $serialized) = $this->client->pipeline(function (ClientContextInterface $client) use ($queueName, $id) { |
|
| 57 | self::cUpdateCommandStatus($client, $queueName, $id, self::STATUS_IN_PROGRESS); |
||
| 58 | self::cRetrieveCommand($client, $queueName, $id); |
||
| 59 | self::cReleaseReservedCommandIds($client, $queueName, [ $id ]); |
||
| 60 | }); |
||
| 61 | return new ReceivedCommand($queueName, $id, $serialized); |
||
| 62 | } |
||
| 63 | |||
| 64 | public function getCommandStatus(string $queueName, string $id): string |
||
| 68 | |||
| 69 | public function setCommandCompleted(string $queueName, string $id) |
||
| 75 | |||
| 76 | public function setCommandFailed(string $queueName, string $id) |
||
| 82 | |||
| 83 | public function putQueue(string $queueName) |
||
| 89 | |||
| 90 | public function getQueueNames(): array |
||
| 94 | |||
| 95 | public function getQueuedCount(string $queueName): int |
||
| 99 | |||
| 100 | public function getQueuedIds(string $queueName, int $offset = 0, int $limit = 10): array |
||
| 104 | |||
| 105 | public function getConsumingCount(string $queueName): int |
||
| 109 | |||
| 110 | public function getConsumingIds(string $queueName, int $offset = 0, int $limit = 10): array |
||
| 114 | |||
| 115 | public function readCommand(string $queueName, string $id): string |
||
| 123 | |||
| 124 | public function clearQueue(string $queueName) |
||
| 125 | { |
||
| 128 | |||
| 129 | public function deleteQueue(string $queueName) |
||
| 136 | |||
| 137 | public function purgeCommand(string $queueName, string $id) |
||
| 149 | |||
| 150 | public function scheduleCommand(string $queueName, string $id, string $serialized, \DateTime $dateTime) |
||
| 163 | |||
| 164 | public function cancelScheduledCommand(string $queueName, string $id) |
||
| 168 | |||
| 169 | public function clearSchedule(array $queueNames = null, \DateTime $start = null, \DateTime $end = null) |
||
| 195 | |||
| 196 | public function receiveDueCommands( |
||
| 241 | |||
| 242 | private function storeCommandAndCheckIfIdReserved(string $queueName, string $id, string $serialized): bool |
||
| 253 | |||
| 254 | private static function cRetrieveCommand($client, string $queueName, string $id) |
||
| 258 | |||
| 259 | private static function cReserveCommandId($client, string $queueName, string $id) |
||
| 263 | |||
| 264 | private static function cEndCommand($client, string $queueName, string $id, string $status) |
||
| 271 | |||
| 272 | private static function cReleaseReservedCommandIds($client, string $queueName, array $ids) |
||
| 276 | |||
| 277 | private static function cUpdateCommandStatus($client, string $queueName, string $id, string $status) |
||
| 281 | |||
| 282 | private static function cAddQueue($client, string $queueName) |
||
| 286 | |||
| 287 | private static function cEmptyQueue($client, string $queueName) |
||
| 296 | |||
| 297 | private static function cDeleteQueue($client, string $queueName) |
||
| 302 | } |
||
| 303 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.