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 |
||
30 | class DoctrineProvider extends AbstractProvider |
||
31 | { |
||
32 | |||
33 | const DEFAULT_PERIOD = 300; |
||
34 | |||
35 | protected $em; |
||
36 | protected $repository; |
||
37 | protected static $entityName = 'Uecode\Bundle\QPushBundle\Entity\DoctrineMessage'; |
||
38 | |||
39 | /** |
||
40 | * Constructor for Provider classes |
||
41 | * |
||
42 | * @param string $name Name of the Queue the provider is for |
||
43 | * @param array $options An array of configuration options for the Queue |
||
44 | * @param mixed $client A Queue Client for the provider |
||
45 | * @param Cache $cache An instance of Doctrine\Common\Cache\Cache |
||
46 | * @param Logger $logger An instance of Symfony\Bridge\Mongolog\Logger |
||
47 | */ |
||
48 | public function __construct($name, array $options, $client, Cache $cache, Logger $logger) |
||
57 | |||
58 | /** |
||
59 | * Returns the name of the Queue that this Provider is for |
||
60 | * |
||
61 | * @return string |
||
62 | */ |
||
63 | public function getName() |
||
67 | |||
68 | /** |
||
69 | * Returns the Queue Provider name |
||
70 | * |
||
71 | * @return string |
||
72 | */ |
||
73 | public function getProvider() |
||
77 | |||
78 | /** |
||
79 | * Returns the Provider's Configuration Options |
||
80 | * |
||
81 | * @return array |
||
82 | */ |
||
83 | public function getOptions() |
||
87 | |||
88 | /** |
||
89 | * Returns the Cache service |
||
90 | * |
||
91 | * @return Cache |
||
92 | */ |
||
93 | public function getCache() |
||
97 | |||
98 | /** |
||
99 | * Returns the Logger service |
||
100 | * |
||
101 | * @return Logger |
||
102 | */ |
||
103 | public function getLogger() |
||
107 | |||
108 | /** |
||
109 | * Get repository |
||
110 | * |
||
111 | * @return array |
||
112 | */ |
||
113 | public function getRepository() |
||
121 | |||
122 | /** |
||
123 | * Creates the Queue |
||
124 | * Checks to see if the underlying table has been created or not |
||
125 | * |
||
126 | * @return bool |
||
127 | */ |
||
128 | public function create() |
||
135 | |||
136 | /** |
||
137 | * Publishes a message to the Queue |
||
138 | * |
||
139 | * This method should return a string MessageId or Response |
||
140 | * |
||
141 | * @param array $message The message to queue |
||
142 | * @param array $options An array of options that override the queue defaults |
||
143 | * |
||
144 | * @return string |
||
145 | */ |
||
146 | public function publish(array $message, array $options = []) |
||
163 | |||
164 | /** |
||
165 | * Polls the Queue for Messages |
||
166 | * |
||
167 | * Depending on the Provider, this method may keep the connection open for |
||
168 | * a configurable amount of time, to allow for long polling. In most cases, |
||
169 | * this method is not meant to be used to long poll indefinitely, but should |
||
170 | * return in reasonable amount of time |
||
171 | * |
||
172 | * @param array $options An array of options that override the queue defaults |
||
173 | * |
||
174 | * @return array |
||
175 | */ |
||
176 | public function receive(array $options = []) |
||
195 | |||
196 | /** |
||
197 | * Deletes the Queue Message |
||
198 | * |
||
199 | * @param mixed $id A message identifier or resource |
||
200 | */ |
||
201 | public function delete($id) |
||
209 | |||
210 | /** |
||
211 | * Destroys a Queue and clears any Queue related Cache |
||
212 | * |
||
213 | * @return bool |
||
214 | */ |
||
215 | public function destroy() |
||
225 | |||
226 | /** |
||
227 | * Returns a specific message |
||
228 | * |
||
229 | * @param integer $id |
||
230 | * |
||
231 | * @return Message |
||
232 | */ |
||
233 | public function getById($id) |
||
237 | |||
238 | /** |
||
239 | * Returns a query of the message queue |
||
240 | * |
||
241 | * @param array $data ['field'=>'id', 'search'=>'text', 'to'=>date, from=>date] |
||
242 | * @return Query |
||
243 | * |
||
244 | */ |
||
245 | public function findBy($data) |
||
271 | |||
272 | /* |
||
273 | * Returns an array of times and messgae counts |
||
274 | * @praram $data ['from' => date, 'to' => date, 'period' => seconds |
||
275 | * @return ['time', 'count'] |
||
276 | */ |
||
277 | |||
278 | public function counts($data) |
||
309 | |||
310 | } |
||
311 |
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.