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 |
||
27 | trait TranslatableRepositoryTrait |
||
28 | { |
||
29 | use RepositoryTrait { |
||
30 | __construct as private constructFromRepositoryTrait; |
||
31 | createQueryBuilder as private createQueryBuilderFromRepositoryTrait; |
||
32 | getProperty as private getPropertyFromRepositoryTrait; |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @var LocaleContextInterface |
||
37 | */ |
||
38 | private $localeContext; |
||
39 | |||
40 | /** |
||
41 | * @var string[] |
||
42 | */ |
||
43 | private $cache; |
||
44 | |||
45 | /** |
||
46 | * @param EntityManager $em |
||
47 | * @param ClassMetadata $class |
||
48 | * @param ResourceInterface $resource |
||
49 | * @param LocaleContextInterface $localeContext |
||
50 | */ |
||
51 | public function __construct( |
||
52 | $em, |
||
53 | ClassMetadata $class, |
||
54 | ResourceInterface $resource, |
||
55 | LocaleContextInterface $localeContext |
||
56 | ) { |
||
57 | $this->constructFromRepositoryTrait($em, $class, $resource); |
||
|
|||
58 | |||
59 | $this->localeContext = $localeContext; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | public function createQueryBuilder($alias = null, $indexBy = null) |
||
66 | { |
||
67 | $queryBuilder = $this->createQueryBuilderFromRepositoryTrait($alias, $indexBy); |
||
68 | $queryBuilder |
||
69 | ->addSelect($alias = $this->getTranslationAlias($queryBuilder)) |
||
70 | ->leftJoin($this->getProperty('translations', $queryBuilder), $alias); |
||
71 | |||
72 | return $queryBuilder; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | public function createQueryBuilderForCollection($alias = null, $indexBy = null) |
||
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | public function getProperty($property, $root = null) |
||
97 | { |
||
98 | if ($this->cache === null) { |
||
99 | $translatableFields = $this->getClassMetadata()->getFieldNames(); |
||
100 | $translationFields = $this->getEntityManager() |
||
101 | ->getClassMetadata($this->getClassMetadata()->getAssociationMapping('translations')['targetEntity']) |
||
102 | ->getFieldNames(); |
||
103 | |||
104 | $this->cache = array_diff($translationFields, $translatableFields); |
||
105 | } |
||
106 | |||
107 | if (($pos = strpos($property, '.')) !== false) { |
||
108 | |||
109 | } |
||
110 | |||
111 | if (in_array($property, $this->cache, true)) { |
||
112 | $root = $this->getTranslationAlias($root); |
||
113 | } |
||
114 | |||
115 | return $this->getPropertyFromRepositoryTrait($property, $root); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param QueryBuilder|string|null $root |
||
120 | * |
||
121 | * @return string |
||
122 | */ |
||
123 | private function getTranslationAlias($root) |
||
127 | |||
128 | /** |
||
129 | * @param QueryBuilder|string|null $root |
||
130 | * |
||
131 | * @return string |
||
132 | */ |
||
133 | private function getRootAlias($root) |
||
141 | |||
142 | /** |
||
143 | * @return string[] |
||
144 | */ |
||
145 | View Code Duplication | private function getLocales() |
|
155 | } |
||
156 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.