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:
| 1 | <?php |
||
| 9 | class Client |
||
| 10 | { |
||
| 11 | /** Default queue name */ |
||
| 12 | const QUEUE = 'default'; |
||
| 13 | |||
| 14 | /** Namespace */ |
||
| 15 | protected $namespace; |
||
| 16 | |||
| 17 | /** @var \Predis\Client */ |
||
| 18 | public $redis; |
||
| 19 | |||
| 20 | /** @var Serializer */ |
||
| 21 | protected $serializer; |
||
| 22 | |||
| 23 | /** @var IdGenerator */ |
||
| 24 | protected $idGenerator; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Sidekiq job pusher init |
||
| 28 | * |
||
| 29 | * @param \Predis\Client $redis |
||
| 30 | * @param string $namespace |
||
| 31 | * @param Serializer $serializer |
||
| 32 | * @param IdGenerator $idGenerator |
||
| 33 | */ |
||
| 34 | public function __construct(\Predis\Client $redis, $namespace = null, $serializer = null, $idGenerator = null) |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Push a job |
||
| 44 | * |
||
| 45 | * @param string $class |
||
| 46 | * @param array $args |
||
| 47 | * @param bool $retry |
||
| 48 | * @param string $queue |
||
| 49 | * @return string |
||
| 50 | */ |
||
| 51 | View Code Duplication | public function push($class, $args = [], $retry = true, $queue = self::QUEUE) |
|
| 58 | |||
| 59 | /** |
||
| 60 | * Schedule a job at a certain time |
||
| 61 | * |
||
| 62 | * @param float $doAt |
||
| 63 | * @param string $class |
||
| 64 | * @param array $args |
||
| 65 | * @param bool $retry |
||
| 66 | * @param string $queue |
||
| 67 | * @return string |
||
| 68 | */ |
||
| 69 | View Code Duplication | public function schedule($doAt, $class, $args = [], $retry = true, $queue = self::QUEUE) |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Push multiple jobs to queue |
||
| 79 | * |
||
| 80 | * Format: |
||
| 81 | * $jobs = [ |
||
| 82 | * [ |
||
| 83 | * 'class' => 'SomeClass', |
||
| 84 | * 'args' => array(), |
||
| 85 | * 'retry' => false, |
||
| 86 | * 'at' => microtime(true) |
||
| 87 | * ] |
||
| 88 | * ]; |
||
| 89 | * |
||
| 90 | * @param array $jobs |
||
| 91 | * @param string $queue |
||
| 92 | * @return string |
||
| 93 | * @throws exception Exception |
||
| 94 | */ |
||
| 95 | public function pushBulk($jobs = [], $queue = self::QUEUE) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Push job to redis |
||
| 119 | * |
||
| 120 | * @param string $jobId |
||
| 121 | * @param string $class |
||
| 122 | * @param array $args |
||
| 123 | * @param string $queue |
||
| 124 | * @param bool $retry |
||
| 125 | * @param float|null $doAt |
||
| 126 | * @throws exception Exception |
||
| 127 | */ |
||
| 128 | private function atomicPush($jobId, $class, $args = [], $queue = self::QUEUE, $retry = true, $doAt = null) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param string ...$key |
||
| 150 | * @return string |
||
| 151 | */ |
||
| 152 | private function name() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @return \Predis\Client |
||
| 159 | */ |
||
| 160 | public function getRedis() |
||
| 164 | } |
||
| 165 |