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 |
||
| 15 | class ChannelRepository extends EntityRepository |
||
| 16 | { |
||
| 17 | const BUFFER_SIZE = 100; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @info Check if task is running |
||
| 21 | * |
||
| 22 | * @param string $commandName |
||
| 23 | * @param int|null $integrationId |
||
| 24 | * |
||
| 25 | * @return int |
||
| 26 | */ |
||
| 27 | public function getRunningSyncJobsCount($commandName, $integrationId = null) |
||
| 28 | { |
||
| 29 | $qb = $this->getSyncJobsCountQueryBuilder($commandName, $integrationId); |
||
| 30 | $qb->andWhere('j.state=:stateName'); |
||
| 31 | $qb->setParameter('stateName', Job::STATE_RUNNING); |
||
| 32 | |||
| 33 | return (int)$qb->getQuery()->getSingleScalarResult(); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @info Check if task is pending or running |
||
| 38 | * |
||
| 39 | * @param string $commandName |
||
| 40 | * @param int|null $integrationId |
||
| 41 | * |
||
| 42 | * @return int |
||
| 43 | */ |
||
| 44 | public function getExistingSyncJobsCount($commandName, $integrationId = null) |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param string $commandName |
||
| 55 | * @param int|null $integrationId |
||
| 56 | * |
||
| 57 | * @return QueryBuilder |
||
| 58 | */ |
||
| 59 | protected function getSyncJobsCountQueryBuilder($commandName, $integrationId = null) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param string[]|string $commandName |
||
| 91 | * @param string[]|string|null $arguments |
||
| 92 | * @param string[]|string|null $states |
||
| 93 | * |
||
| 94 | * @return QueryBuilder |
||
| 95 | */ |
||
| 96 | protected function getQBSyncJobs($commandName, $arguments = null, $states = null) |
||
| 97 | { |
||
| 98 | /** @var QueryBuilder $qb */ |
||
| 99 | $qb = $this->getEntityManager() |
||
| 100 | ->getRepository('JMSJobQueueBundle:Job') |
||
| 101 | ->createQueryBuilder('j'); |
||
| 102 | |||
| 103 | if (is_array($commandName)) { |
||
| 104 | $qb->andWhere('j.command in (:commandName)'); |
||
| 105 | } else { |
||
| 106 | $qb->andWhere('j.command=:commandName'); |
||
| 107 | } |
||
| 108 | |||
| 109 | if (!empty($states)) { |
||
| 110 | if (is_array($states)) { |
||
| 111 | $qb->andWhere('j.state in (:stateName)'); |
||
| 112 | } else { |
||
| 113 | $qb->andWhere('j.state=:stateName'); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | $qb->setParameter('stateName', $states) |
||
| 118 | ->setParameter('commandName', $commandName); |
||
| 119 | |||
| 120 | if (!empty($arguments)) { |
||
| 121 | if (is_array($arguments)) { |
||
| 122 | $orX = $qb->expr()->orX(); |
||
| 123 | foreach ($arguments as $key => $argument) { |
||
| 124 | $orX->add($qb->expr()->like('cast(j.args as text)', ':args_' . $key)); |
||
| 125 | $qb->setParameter('args_' . $key, '%' . $argument . '%'); |
||
| 126 | } |
||
| 127 | |||
| 128 | $qb->andWhere($orX); |
||
| 129 | } else { |
||
| 130 | $qb->andWhere($qb->expr()->like('cast(j.args as text)', ':args')) |
||
| 131 | ->setParameter('args', '%' . $arguments . '%'); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | return $qb; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param string[]|string $commandName |
||
| 140 | * @param string[]|string|null $arguments |
||
| 141 | * @param string[]|string|null $states |
||
| 142 | * |
||
| 143 | * @return QueryBuilder |
||
| 144 | */ |
||
| 145 | View Code Duplication | protected function getQBSyncJobsCount($commandName, $arguments = null, $states = null) |
|
| 146 | { |
||
| 147 | $qb = $this->getQBSyncJobs($commandName, $arguments, $states); |
||
| 148 | $qb->select('count(j.id)'); |
||
| 149 | |||
| 150 | return $qb; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param string $commandName |
||
| 155 | * @param string[]|string|null $arguments |
||
| 156 | * @param string[]|string|null $states |
||
| 157 | * |
||
| 158 | * @return int |
||
| 159 | */ |
||
| 160 | View Code Duplication | public function getSyncJobsCount($commandName, $states = null, $arguments = null) |
|
| 161 | { |
||
| 162 | /** @var QueryBuilder $qb */ |
||
| 163 | $qb = $this->getQBSyncJobsCount($commandName, $arguments, $states); |
||
| 164 | |||
| 165 | return (int)$qb->getQuery()->getSingleScalarResult(); |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Returns latest status for integration's connector and code if it exists. |
||
| 170 | * |
||
| 171 | * @param Integration $integration |
||
| 172 | * @param string $connector |
||
| 173 | * @param int|null $code |
||
| 174 | * |
||
| 175 | * @return Status|null |
||
| 176 | */ |
||
| 177 | public function getLastStatusForConnector(Integration $integration, $connector, $code = null) |
||
| 178 | { |
||
| 179 | $queryBuilder = $this->getConnectorStatusesQueryBuilder($integration, $connector, $code); |
||
| 180 | $queryBuilder |
||
| 181 | ->setFirstResult(0) |
||
| 182 | ->setMaxResults(1); |
||
| 183 | |||
| 184 | return $queryBuilder->getQuery()->getOneOrNullResult(); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param Integration $integration |
||
| 189 | * @param string $connector |
||
| 190 | * @param int|null $code |
||
| 191 | * |
||
| 192 | * @return Status[]|\Iterator |
||
| 193 | */ |
||
| 194 | public function getConnectorStatuses(Integration $integration, $connector, $code = null) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param Integration $integration |
||
| 206 | * @param string $connector |
||
| 207 | * @param int|null $code |
||
| 208 | * |
||
| 209 | * @return QueryBuilder |
||
| 210 | */ |
||
| 211 | public function getConnectorStatusesQueryBuilder(Integration $integration, $connector, $code = null) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Returns channels that have configured transports |
||
| 231 | * Assume that they are ready for sync |
||
| 232 | * |
||
| 233 | * @param null|string $type |
||
| 234 | * @param boolean $isReadOnly |
||
| 235 | * |
||
| 236 | * @return array |
||
| 237 | */ |
||
| 238 | public function getConfiguredChannelsForSync($type = null, $isReadOnly = false) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Load instance once and precache it in property |
||
| 265 | * |
||
| 266 | * @param int $id |
||
| 267 | * |
||
| 268 | * @return Integration|bool |
||
| 269 | */ |
||
| 270 | public function getOrLoadById($id) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Adds status to integration, manual persist of newly created statuses and do flush. |
||
| 285 | * |
||
| 286 | * @deprecated 1.9.0:1.11.0 Use $this->addStatusAndFlush() instead |
||
| 287 | * |
||
| 288 | * @param Integration $integration |
||
| 289 | * @param Status $status |
||
| 290 | */ |
||
| 291 | public function addStatus(Integration $integration, Status $status) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Adds status to integration, manual persist of newly created statuses and do flush. |
||
| 298 | * |
||
| 299 | * @param Integration $integration |
||
| 300 | * @param Status $status |
||
| 301 | */ |
||
| 302 | public function addStatusAndFlush(Integration $integration, Status $status) |
||
| 313 | } |
||
| 314 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: