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 |
||
| 18 | abstract class AbstractDoctrineRepository implements RepositoryInterface |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var Connection |
||
| 22 | */ |
||
| 23 | protected $connection; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var ResolverInterface |
||
| 27 | */ |
||
| 28 | protected $resolver; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var ModelCacheInterface |
||
| 32 | */ |
||
| 33 | protected $cache; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var LoggerInterface |
||
| 37 | */ |
||
| 38 | protected $logger; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param Connection $connection |
||
| 42 | * @param ResolverInterface $resolver |
||
| 43 | * @param ModelCacheInterface|null $cache |
||
| 44 | * @param LoggerInterface|null $logger |
||
| 45 | */ |
||
| 46 | public function __construct( |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param string $id |
||
| 60 | * |
||
| 61 | * @return ModelInterface|null |
||
| 62 | */ |
||
| 63 | public function find(string $id) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param array $criteria |
||
| 93 | * |
||
| 94 | * @return null|ModelInterface |
||
| 95 | */ |
||
| 96 | public function findOneBy(array $criteria, array $orderBy = null) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param array $criteria |
||
| 114 | * |
||
| 115 | * @return ModelInterface[]|array |
||
| 116 | */ |
||
| 117 | public function findBy(array $criteria, array $orderBy = null, int $limit = null, int $offset = null): array |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param QueryBuilder $qb |
||
| 154 | * @param array $criteria |
||
| 155 | */ |
||
| 156 | protected function addCriteriaToQueryBuilder(QueryBuilder $qb, array $criteria) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param QueryBuilder $qb |
||
| 166 | * @param array|null $orderBy |
||
| 167 | */ |
||
| 168 | protected function addOrderByToQueryBuilder(QueryBuilder $qb, array $orderBy = null) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param ModelInterface $model |
||
| 181 | */ |
||
| 182 | public function persist(ModelInterface $model) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param ModelInterface $model |
||
| 213 | */ |
||
| 214 | public function remove(ModelInterface $model) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param string $id |
||
| 240 | * @param array $row |
||
| 241 | */ |
||
| 242 | View Code Duplication | protected function insert(string $id, array $row) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * @param string $id |
||
| 256 | * @param array $row |
||
| 257 | */ |
||
| 258 | View Code Duplication | protected function update(string $id, array $row) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * @param ModelCollectionInterface $modelCollection |
||
| 272 | */ |
||
| 273 | private function persistRelatedModels(ModelCollectionInterface $modelCollection) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param ModelInterface $model |
||
| 292 | */ |
||
| 293 | private function persistRelatedModel(ModelInterface $model) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param ModelCollectionInterface $modelCollection |
||
| 300 | */ |
||
| 301 | private function removeRelatedModels(ModelCollectionInterface $modelCollection) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param ModelInterface $model |
||
| 310 | */ |
||
| 311 | private function removeRelatedModel(ModelInterface $model) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @param array $row |
||
| 318 | * |
||
| 319 | * @return ModelInterface |
||
| 320 | */ |
||
| 321 | abstract protected function fromPersistence(array $row): ModelInterface; |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @return string |
||
| 325 | */ |
||
| 326 | abstract protected function getTable(): string; |
||
| 327 | } |
||
| 328 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.