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) |
|
57 | { |
||
58 | 13 | $this->dm = $documentManager; |
|
59 | 13 | $this->class = $class; |
|
60 | |||
61 | 13 | parent::__construct($builder, $from); |
|
62 | 11 | } |
|
63 | |||
64 | /** |
||
65 | * @param string $from |
||
66 | * @return $this |
||
67 | */ |
||
68 | 13 | View Code Duplication | public function from($from) |
|
|||
69 | { |
||
70 | // $from can either be |
||
71 | // a) a field name indicating a reference to a different document. Currently, only REFERENCE_STORE_AS_ID is supported |
||
72 | // b) a Class name |
||
73 | // c) a collection name |
||
74 | // In cases b) and c) the local and foreign fields need to be filled |
||
75 | 13 | if ($this->class->hasReference($from)) { |
|
76 | 9 | return $this->fromReference($from); |
|
77 | } |
||
78 | |||
79 | // Check if mapped class with given name exists |
||
80 | try { |
||
81 | 4 | $this->targetClass = $this->dm->getClassMetadata($from); |
|
82 | 1 | } catch (BaseMappingException $e) { |
|
83 | 1 | return parent::from($from); |
|
84 | } |
||
85 | |||
86 | 3 | if ($this->targetClass->isSharded()) { |
|
87 | 1 | throw MappingException::cannotUseShardedCollectionInLookupStages($this->targetClass->name); |
|
88 | } |
||
89 | |||
90 | 2 | return parent::from($this->targetClass->getCollection()); |
|
91 | } |
||
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 | 4 | case ClassMetadataInfo::REFERENCE_STORE_AS_ID: |
|
115 | 1 | 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 | 4 | case ClassMetadataInfo::REFERENCE_STORE_AS_ID: |
|
134 | 2 | 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.