| Conditions | 21 |
| Paths | 40 |
| Total Lines | 121 |
| Code Lines | 74 |
| Lines | 29 |
| Ratio | 23.97 % |
| Changes | 0 | ||
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 |
||
| 39 | public function getRelated($relationName) |
||
| 40 | { |
||
| 41 | // check if relation exists |
||
| 42 | if (!$this->isRelationExists($relationName)) { |
||
| 43 | throw new \Sokil\Mongo\Exception('Relation with name "' . $relationName . '" not found'); |
||
| 44 | } |
||
| 45 | |||
| 46 | // get relation metadata |
||
| 47 | $relation = $this->relations[$relationName]; |
||
| 48 | |||
| 49 | $relationType = $relation[0]; |
||
| 50 | $targetCollectionName = $relation[1]; |
||
| 51 | |||
| 52 | // get target collection |
||
| 53 | $foreignCollection = $this->document |
||
| 54 | ->getCollection() |
||
| 55 | ->getDatabase() |
||
| 56 | ->getCollection($targetCollectionName); |
||
| 57 | |||
| 58 | // check if relation already resolved |
||
| 59 | if (isset($this->resolvedRelationIds[$relationName])) { |
||
| 60 | if (is_array($this->resolvedRelationIds[$relationName])) { |
||
| 61 | // has_many, many_many |
||
| 62 | return $foreignCollection->getDocuments($this->resolvedRelationIds[$relationName]); |
||
| 63 | } else { |
||
| 64 | //has_one, belongs |
||
| 65 | return $foreignCollection->getDocument($this->resolvedRelationIds[$relationName]); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | switch ($relationType) { |
||
| 70 | View Code Duplication | case Document::RELATION_HAS_ONE: |
|
| 71 | $localKey = isset($relation['localKey']) ? $relation['localKey'] : '_id'; |
||
| 72 | $foreignKey = $relation[2]; |
||
| 73 | |||
| 74 | $document = $foreignCollection |
||
| 75 | ->find() |
||
| 76 | ->where($foreignKey, $this->document->get($localKey)) |
||
| 77 | ->findOne(); |
||
| 78 | |||
| 79 | if ($document) { |
||
| 80 | $this->resolvedRelationIds[$relationName] = (string) $document->getId(); |
||
| 81 | } |
||
| 82 | |||
| 83 | return $document; |
||
| 84 | |||
| 85 | case Document::RELATION_BELONGS: |
||
| 86 | $localKey = $relation[2]; |
||
| 87 | $foreignKey = isset($relation['foreignKey']) ? $relation['foreignKey'] : '_id'; |
||
| 88 | |||
| 89 | if ($foreignKey === '_id') { |
||
| 90 | $document = $foreignCollection->getDocument($this->document->get($localKey)); |
||
| 91 | } else { |
||
| 92 | $document = $foreignCollection |
||
| 93 | ->find() |
||
| 94 | ->where($foreignKey, $this->document->get($localKey)) |
||
| 95 | ->findOne(); |
||
| 96 | } |
||
| 97 | |||
| 98 | if ($document) { |
||
| 99 | $this->resolvedRelationIds[$relationName] = (string) $document->getId(); |
||
| 100 | } |
||
| 101 | |||
| 102 | return $document; |
||
| 103 | |||
| 104 | View Code Duplication | case Document::RELATION_HAS_MANY: |
|
| 105 | $localKey = isset($relation['localKey']) ? $relation['localKey'] : '_id'; |
||
| 106 | $foreignKey = $relation[2]; |
||
| 107 | |||
| 108 | $documents = $foreignCollection |
||
| 109 | ->find() |
||
| 110 | ->where($foreignKey, $this->document->get($localKey)) |
||
| 111 | ->findAll(); |
||
| 112 | |||
| 113 | foreach ($documents as $document) { |
||
| 114 | $this->resolvedRelationIds[$relationName][] = (string) $document->getId(); |
||
| 115 | } |
||
| 116 | |||
| 117 | return $documents; |
||
| 118 | |||
| 119 | case Document::RELATION_MANY_MANY: |
||
| 120 | $isRelationListStoredInternally = isset($relation[3]) && $relation[3]; |
||
| 121 | if ($isRelationListStoredInternally) { |
||
| 122 | // relation list stored in this document |
||
| 123 | $localKey = $relation[2]; |
||
| 124 | $foreignKey = isset($relation['foreignKey']) ? $relation['foreignKey'] : '_id'; |
||
| 125 | ; |
||
| 126 | |||
| 127 | $relatedIdList = $this->document->get($localKey); |
||
| 128 | if (!$relatedIdList) { |
||
| 129 | return array(); |
||
| 130 | } |
||
| 131 | |||
| 132 | $documents = $foreignCollection |
||
| 133 | ->find() |
||
| 134 | ->whereIn($foreignKey, $relatedIdList) |
||
| 135 | ->findAll(); |
||
| 136 | } else { |
||
| 137 | // relation list stored in external document |
||
| 138 | $localKey = isset($relation['localKey']) ? $relation['localKey'] : '_id'; |
||
| 139 | ; |
||
| 140 | $foreignKey = $relation[2]; |
||
| 141 | |||
| 142 | $documents = $foreignCollection |
||
| 143 | ->find() |
||
| 144 | ->where($foreignKey, $this->document->get($localKey)) |
||
| 145 | ->findAll(); |
||
| 146 | } |
||
| 147 | |||
| 148 | foreach ($documents as $document) { |
||
| 149 | $this->resolvedRelationIds[$relationName][] = (string) $document->getId(); |
||
| 150 | } |
||
| 151 | |||
| 152 | return $documents; |
||
| 153 | |||
| 154 | default: |
||
| 155 | throw new \Sokil\Mongo\Exception( |
||
| 156 | 'Unsupported relation type "' . $relationType . '" when resolve relation "' . $relationName . '"' |
||
| 157 | ); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 289 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: