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 |
||
24 | trait EntityRepositoryTrait |
||
25 | { |
||
26 | /** |
||
27 | * {@inheritdoc} |
||
28 | */ |
||
29 | public function persist(PersistableInterface $object) |
||
33 | |||
34 | /** |
||
35 | * {@inheritdoc} |
||
36 | */ |
||
37 | public function add(PersistableInterface $object) |
||
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | public function flush() |
||
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | View Code Duplication | public function remove(PersistableInterface $object) |
|
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | public function getPaginatedByCriteria(Criteria $criteria, array $sorting = [], PaginationData $paginationData = null) |
||
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | public function getQueryByCriteria(Criteria $criteria, array $sorting, string $alias): QueryBuilder |
||
88 | |||
89 | /** |
||
90 | * @param QueryBuilder $queryBuilder |
||
91 | * @param Criteria $criteria |
||
92 | */ |
||
93 | public function applyLimiting(QueryBuilder $queryBuilder, Criteria $criteria) |
||
102 | |||
103 | /** |
||
104 | * @param $queryBuilder |
||
105 | * @param PaginationData $paginationData |
||
106 | * |
||
107 | * @return PaginationInterface |
||
108 | */ |
||
109 | protected function getPaginator($queryBuilder, PaginationData $paginationData) |
||
115 | |||
116 | /** |
||
117 | * @param QueryBuilder $queryBuilder |
||
118 | * @param Criteria $criteria |
||
119 | * @param string $alias |
||
120 | */ |
||
121 | protected function applyCriteria(QueryBuilder $queryBuilder, Criteria $criteria, string $alias) |
||
143 | |||
144 | /** |
||
145 | * @param QueryBuilder $queryBuilder |
||
146 | * @param array $sorting |
||
147 | * @param string $alias |
||
148 | */ |
||
149 | protected function applySorting(QueryBuilder $queryBuilder, array $sorting, string $alias) |
||
157 | |||
158 | /** |
||
159 | * @param string $name |
||
160 | * @param string $alias |
||
161 | * |
||
162 | * @return string |
||
163 | */ |
||
164 | protected function getPropertyName(string $name, string $alias) |
||
172 | } |
||
173 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: