Conditions | 16 |
Paths | 20 |
Total Lines | 118 |
Code Lines | 69 |
Lines | 51 |
Ratio | 43.22 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
84 | public function getRelated($relationName) |
||
85 | { |
||
86 | // check if relation exists |
||
87 | if (!$this->isRelationExists($relationName)) { |
||
88 | throw new \Sokil\Mongo\Exception('Relation with name "'.$relationName.'" not found'); |
||
89 | } |
||
90 | |||
91 | // get relation metadata |
||
92 | $relation = $this->relations[$relationName]; |
||
93 | |||
94 | $relationType = $relation[0]; |
||
95 | $targetCollectionName = $relation[1]; |
||
96 | |||
97 | // get target collection |
||
98 | $foreignCollection = $this->document |
||
99 | ->getCollection() |
||
100 | ->getDatabase() |
||
101 | ->getCollection($targetCollectionName); |
||
102 | |||
103 | // check if relation already resolved |
||
104 | View Code Duplication | if (isset($this->resolvedRelationIds[$relationName])) { |
|
105 | if (is_array($this->resolvedRelationIds[$relationName])) { |
||
106 | // has_many, many_many |
||
107 | return $foreignCollection->getDocuments($this->resolvedRelationIds[$relationName]); |
||
108 | } else { |
||
109 | //has_one, belongs |
||
110 | return $foreignCollection->getDocument($this->resolvedRelationIds[$relationName]); |
||
111 | } |
||
112 | } |
||
113 | |||
114 | switch ($relationType) { |
||
115 | |||
116 | View Code Duplication | case Document::RELATION_HAS_ONE: |
|
117 | $foreignKey = $relation[2]; |
||
118 | |||
119 | $documentDbRef = self::generateDbRef($this->document); |
||
120 | |||
121 | $document = $foreignCollection |
||
122 | ->find() |
||
123 | ->where($foreignKey.'.$id', $this->extractMongoIdFromDbRef($documentDbRef)) |
||
124 | ->findOne(); |
||
125 | |||
126 | if ($document) { |
||
127 | $this->resolvedRelationIds[$relationName] = (string)$document->getId(); |
||
128 | } |
||
129 | |||
130 | return $document; |
||
131 | |||
132 | View Code Duplication | case Document::RELATION_BELONGS: |
|
133 | $document = null; |
||
134 | |||
135 | $localKey = $relation[2]; |
||
136 | |||
137 | $dbRef = $this->document->get($localKey); |
||
138 | $document = $foreignCollection->getDocument($this->extractMongoIdFromDbRef($dbRef)); |
||
139 | |||
140 | if ($document) { |
||
141 | $this->resolvedRelationIds[$relationName] = (string)$document->getId(); |
||
142 | } |
||
143 | |||
144 | return $document; |
||
145 | |||
146 | View Code Duplication | case Document::RELATION_HAS_MANY: |
|
147 | $foreignKey = $relation[2]; |
||
148 | |||
149 | $documentDbRef = self::generateDbRef($this->document); |
||
150 | $documents = $foreignCollection |
||
151 | ->find() |
||
152 | ->where($foreignKey.'.$id', $this->extractMongoIdFromDbRef($documentDbRef)) |
||
153 | ->findAll(); |
||
154 | |||
155 | foreach ($documents as $document) { |
||
156 | $this->resolvedRelationIds[$relationName][] = (string)$document->getId(); |
||
157 | } |
||
158 | |||
159 | return $documents; |
||
160 | |||
161 | case Document::RELATION_MANY_MANY: |
||
162 | $isRelationListStoredInternally = isset($relation[3]) && $relation[3]; |
||
163 | $reference = $relation[2]; |
||
164 | |||
165 | if ($isRelationListStoredInternally) { |
||
166 | // relation list stored in this document |
||
167 | $relatedDbRefsList = $this->document->get($reference); |
||
168 | |||
169 | if (!$relatedDbRefsList) { |
||
170 | return array(); |
||
171 | } |
||
172 | $relationsMongoIds = array(); |
||
173 | foreach ($relatedDbRefsList as $relatedDbRef) { |
||
174 | $relationsMongoIds[] = $this->extractMongoIdFromDbRef($relatedDbRef); |
||
175 | } |
||
176 | |||
177 | $documents = $foreignCollection |
||
178 | ->find() |
||
179 | ->whereIn('_id', $relationsMongoIds) |
||
180 | ->findAll(); |
||
181 | |||
182 | } else { |
||
183 | // relation list stored in external document |
||
184 | $documents = $foreignCollection |
||
185 | ->find() |
||
186 | ->where($reference.'.$id', $this->document->getId()) |
||
187 | ->findAll(); |
||
188 | } |
||
189 | |||
190 | foreach ($documents as $document) { |
||
191 | $this->resolvedRelationIds[$relationName][] = (string)$document->getId(); |
||
192 | } |
||
193 | |||
194 | return $documents; |
||
195 | |||
196 | default: |
||
197 | throw new \Sokil\Mongo\Exception( |
||
198 | 'Unsupported relation type "'.$relationType.'" when resolve relation "'.$relationName.'"' |
||
199 | ); |
||
200 | } |
||
201 | } |
||
202 | |||
355 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: