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 |
||
| 16 | class RpcClient implements QueueProducerInterface |
||
| 17 | { |
||
| 18 | private $connectionName; |
||
| 19 | private $connectionManager; |
||
| 20 | private $fromName; |
||
| 21 | private $queueName; |
||
| 22 | private $response; |
||
| 23 | private $logOutput; |
||
| 24 | private $errOutput; |
||
| 25 | private $correlationId; |
||
| 26 | private $callbackQueue; |
||
| 27 | |||
| 28 | public function __construct($queueName, ConnectionManager $manager, $fromName, $connectionName = 'default') |
||
| 29 | { |
||
| 30 | $this->connectionName = $connectionName; |
||
| 31 | $this->queueName = $queueName; |
||
| 32 | $this->fromName = $fromName; |
||
| 33 | $this->connectionManager = $manager; |
||
| 34 | $this->logOutput = fopen('php://stdout', 'a+'); |
||
| 35 | $this->errOutput = fopen('php://stderr', 'a+'); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param AMQPMessage $rep |
||
| 40 | */ |
||
| 41 | public function onResponse(AMQPMessage $rep) |
||
| 42 | { |
||
| 43 | if ($rep->get('correlation_id') === $this->correlationId) { |
||
| 44 | $this->response = $rep->getBody(); |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | public function createCallbackQueue(CmobiAMQPChannel $channel, $expire, $sufix, $corralationId = null) |
||
| 49 | { |
||
| 50 | $this->correlationId = is_null($corralationId) ? $this->generateCorrelationId() : $corralationId; |
||
| 51 | $queueBag = new RpcQueueBag( |
||
| 52 | sprintf( |
||
| 53 | 'callback_to_%s_from_%s_%s', |
||
| 54 | $this->getQueueName(), |
||
| 55 | $this->getFromName(), |
||
| 56 | $sufix |
||
| 57 | ) |
||
| 58 | ); |
||
| 59 | $queueBag->setArguments([ |
||
| 60 | 'x-expires' => ['I', $expire], |
||
| 61 | ]); |
||
| 62 | list($callbackQueue) = $channel->queueDeclare($queueBag->getQueueDeclare()); |
||
| 63 | $this->callbackQueue = $callbackQueue; |
||
| 64 | $consumeQueueBag = new RpcQueueBag($callbackQueue); |
||
| 65 | |||
| 66 | $channel->basicConsume( |
||
| 67 | $consumeQueueBag->getQueueConsume(), |
||
| 68 | [$this, 'onResponse'] |
||
| 69 | ); |
||
| 70 | |||
| 71 | return $callbackQueue; |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param $data |
||
| 76 | * @param int $expire |
||
| 77 | * @param int $priority |
||
| 78 | * @throws QueueNotFoundException |
||
| 79 | * @throws \Cmobi\RabbitmqBundle\Connection\Exception\NotFoundAMQPConnectionFactoryException |
||
| 80 | */ |
||
| 81 | public function publish($data, $expire = self::DEFAULT_TTL, $priority = self::PRIORITY_LOW) |
||
| 82 | { |
||
| 83 | $this->response = null; |
||
| 84 | $connection = $this->connectionManager->getConnection($this->connectionName); |
||
| 85 | $channel = $connection->channel(); |
||
| 86 | |||
| 87 | if (! $this->queueHasExists($channel)) { |
||
| 88 | throw new QueueNotFoundException("Queue $this->queueName not declared."); |
||
| 89 | } |
||
| 90 | $sufix = Uuid::uuid4()->toString() . microtime(); |
||
| 91 | $this->createCallbackQueue($channel, $expire, $sufix); |
||
| 92 | $msg = new CmobiAMQPMessage( |
||
| 93 | (string) $data, |
||
| 94 | [ |
||
| 95 | 'correlation_id' => $this->correlationId, |
||
| 96 | 'reply_to' => $this->callbackQueue, |
||
| 97 | 'priority' => $priority, |
||
| 98 | ] |
||
| 99 | ); |
||
| 100 | $channel->basic_publish($msg, '', $this->getQueueName()); |
||
| 101 | |||
| 102 | while (! $this->response) { |
||
| 103 | try { |
||
| 104 | $channel->wait(null, 0, ($expire / 1000)); |
||
|
|
|||
| 105 | } catch (\Exception $e) { |
||
| 106 | fwrite($this->errOutput, $e->getMessage()); |
||
| 107 | break; |
||
| 108 | } |
||
| 109 | } |
||
| 110 | $channel->close(); |
||
| 111 | $connection->close(); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @return bool |
||
| 116 | */ |
||
| 117 | /** |
||
| 118 | * @param CmobiAMQPChannel $channel |
||
| 119 | * @return bool |
||
| 120 | */ |
||
| 121 | View Code Duplication | public function queueHasExists(CmobiAMQPChannel $channel) |
|
| 122 | { |
||
| 123 | try { |
||
| 124 | $channel->queue_declare($this->queueName, true); |
||
| 125 | } catch (\Exception $e) { |
||
| 126 | return false; |
||
| 127 | } |
||
| 128 | |||
| 129 | return true; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | public function getQueueName() |
||
| 136 | { |
||
| 137 | return $this->queueName; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | public function getFromName() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @todo unecessary method set, its only exists to run tests whitout stay jailed in infinite while waiting response. |
||
| 150 | * |
||
| 151 | * @param $content |
||
| 152 | */ |
||
| 153 | public function setResponse($content) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | public function getResponse() |
||
| 165 | |||
| 166 | /** @return string */ |
||
| 167 | public function generateCorrelationId() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | public function getCurrentCorrelationId() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @return string |
||
| 182 | */ |
||
| 183 | public function getExchange() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @return string |
||
| 190 | */ |
||
| 191 | public function getExchangeType() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @return ConnectionManager |
||
| 198 | */ |
||
| 199 | public function getConnectionManager() |
||
| 200 | { |
||
| 201 | return $this->connectionManager; |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param CmobiAMQPConnectionInterface $connection |
||
| 206 | * @param $expire |
||
| 207 | * @param $sufix |
||
| 208 | * @param $corralationId |
||
| 209 | * @return CmobiAMQPConnectionInterface |
||
| 210 | */ |
||
| 211 | public function forceReconnect(CmobiAMQPConnectionInterface $connection, $expire, $sufix, $corralationId) |
||
| 229 | } |
||
| 230 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: