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 |
||
26 | abstract class EntityRepository |
||
27 | { |
||
28 | /** |
||
29 | * @var ObjectManager |
||
30 | */ |
||
31 | protected $manager; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $class; |
||
37 | |||
38 | /** |
||
39 | * @var PaginatorInterface |
||
40 | */ |
||
41 | protected $paginator; |
||
42 | |||
43 | /** |
||
44 | * @var DoctrineEntityRepository |
||
45 | */ |
||
46 | private $repository; |
||
47 | |||
48 | public function __construct(ManagerRegistry $managerRegistry, PaginatorInterface $paginator, string $class) |
||
55 | |||
56 | /** |
||
57 | * @param string $name |
||
58 | * @param array $arguments |
||
59 | * |
||
60 | * @return mixed |
||
61 | */ |
||
62 | public function __call($name, $arguments) |
||
74 | |||
75 | /** |
||
76 | * Saves the $object. |
||
77 | * |
||
78 | * @param $object |
||
79 | */ |
||
80 | public function save($object) |
||
85 | |||
86 | /** |
||
87 | * @param int $page |
||
88 | * @param int $itemsPerPage |
||
89 | * @param array $orderBy |
||
90 | * @param QueryBuilder $qb |
||
91 | * |
||
92 | * @return PaginationInterface |
||
93 | */ |
||
94 | public function findAllWithPaging($page = 1, $itemsPerPage = 100, array $orderBy = [], QueryBuilder $qb = null): PaginationInterface |
||
112 | |||
113 | /** |
||
114 | * @param string $alias |
||
115 | * |
||
116 | * @return QueryBuilder |
||
117 | */ |
||
118 | public function getQueryBuilder(string $alias): QueryBuilder |
||
122 | |||
123 | /** |
||
124 | * @return DoctrineEntityRepository |
||
125 | */ |
||
126 | protected function getRepository(): DoctrineEntityRepository |
||
134 | } |
||
135 |
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.