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 |
||
33 | class Lookup extends BaseStage\Lookup |
||
34 | { |
||
35 | /** |
||
36 | * @var DocumentManager |
||
37 | */ |
||
38 | private $dm; |
||
39 | |||
40 | /** |
||
41 | * @var ClassMetadata |
||
42 | */ |
||
43 | private $class; |
||
44 | |||
45 | /** |
||
46 | * @var ClassMetadata |
||
47 | */ |
||
48 | private $targetClass; |
||
49 | |||
50 | /** |
||
51 | * @param Builder $builder |
||
52 | * @param string $from |
||
53 | * @param DocumentManager $documentManager |
||
54 | * @param ClassMetadata $class |
||
55 | */ |
||
56 | 13 | public function __construct(Builder $builder, $from, DocumentManager $documentManager, ClassMetadata $class) |
|
63 | |||
64 | /** |
||
65 | * @param string $from |
||
66 | * @return $this |
||
67 | */ |
||
68 | 13 | View Code Duplication | public function from($from) |
92 | |||
93 | /** |
||
94 | * @param string $fieldName |
||
95 | * @return $this |
||
96 | * @throws MappingException |
||
97 | */ |
||
98 | 9 | private function fromReference($fieldName) |
|
99 | { |
||
100 | 9 | if (! $this->class->hasReference($fieldName)) { |
|
101 | MappingException::referenceMappingNotFound($this->class->name, $fieldName); |
||
102 | } |
||
103 | |||
104 | 9 | $referenceMapping = $this->class->getFieldMapping($fieldName); |
|
105 | 9 | $this->targetClass = $this->dm->getClassMetadata($referenceMapping['targetDocument']); |
|
106 | 9 | if ($this->targetClass->isSharded()) { |
|
107 | 1 | throw MappingException::cannotUseShardedCollectionInLookupStages($this->targetClass->name); |
|
108 | } |
||
109 | |||
110 | 8 | parent::from($this->targetClass->getCollection()); |
|
111 | |||
112 | 8 | if ($referenceMapping['isOwningSide']) { |
|
113 | 4 | View Code Duplication | switch ($referenceMapping['storeAs']) { |
114 | case ClassMetadataInfo::REFERENCE_STORE_AS_ID: |
||
115 | case ClassMetadataInfo::REFERENCE_STORE_AS_REF: |
||
116 | 4 | $referencedFieldName = ClassMetadataInfo::getReferenceFieldName($referenceMapping['storeAs'], $referenceMapping['name']); |
|
117 | 4 | break; |
|
118 | |||
119 | default: |
||
120 | throw MappingException::cannotLookupDbRefReference($this->class->name, $fieldName); |
||
121 | } |
||
122 | |||
123 | $this |
||
124 | 4 | ->foreignField('_id') |
|
125 | 4 | ->localField($referencedFieldName); |
|
126 | } else { |
||
127 | 4 | View Code Duplication | if (isset($referenceMapping['repositoryMethod']) || ! isset($referenceMapping['mappedBy'])) { |
128 | throw MappingException::repositoryMethodLookupNotAllowed($this->class->name, $fieldName); |
||
129 | } |
||
130 | |||
131 | 4 | $mappedByMapping = $this->targetClass->getFieldMapping($referenceMapping['mappedBy']); |
|
132 | 4 | View Code Duplication | switch ($mappedByMapping['storeAs']) { |
133 | case ClassMetadataInfo::REFERENCE_STORE_AS_ID: |
||
134 | case ClassMetadataInfo::REFERENCE_STORE_AS_REF: |
||
135 | 4 | $referencedFieldName = ClassMetadataInfo::getReferenceFieldName($mappedByMapping['storeAs'], $mappedByMapping['name']); |
|
136 | 4 | break; |
|
137 | |||
138 | default: |
||
139 | throw MappingException::cannotLookupDbRefReference($this->class->name, $fieldName); |
||
140 | } |
||
141 | |||
142 | $this |
||
143 | 4 | ->localField('_id') |
|
144 | 4 | ->foreignField($referencedFieldName); |
|
145 | } |
||
146 | |||
147 | 8 | return $this; |
|
148 | } |
||
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | */ |
||
153 | 11 | public function localField($localField) |
|
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | 11 | public function foreignField($foreignField) |
|
165 | |||
166 | 11 | protected function prepareFieldName($fieldName, ClassMetadata $class = null) |
|
174 | |||
175 | /** |
||
176 | * @return \Doctrine\ODM\MongoDB\Persisters\DocumentPersister |
||
177 | */ |
||
178 | 11 | private function getDocumentPersister(ClassMetadata $class) |
|
182 | } |
||
183 |
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.