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:
Complex classes like RelationManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RelationManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class RelationManager |
||
| 8 | { |
||
| 9 | private $relations; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * |
||
| 13 | * @var \Sokil\Mongo\Document |
||
| 14 | */ |
||
| 15 | private $document; |
||
| 16 | |||
| 17 | private $resolvedRelationIds = array(); |
||
| 18 | |||
| 19 | public function __construct(Document $document = null) |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Check if relation with specified name configured |
||
| 27 | * @param string $name |
||
| 28 | * @return boolean |
||
| 29 | */ |
||
| 30 | public function isRelationExists($name) |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Get related documents |
||
| 37 | * @param string $relationName name of relation |
||
| 38 | */ |
||
| 39 | public function getRelated($relationName) |
||
| 160 | |||
| 161 | public function addRelation($relationName, Document $document) |
||
| 162 | { |
||
| 163 | if (!$this->isRelationExists($relationName)) { |
||
| 164 | throw new \Exception('Relation "' . $relationName . '" not configured'); |
||
| 165 | } |
||
| 166 | |||
| 167 | $relation = $this->relations[$relationName]; |
||
| 168 | |||
| 169 | list($relationType, $relatedCollectionName, $field) = $relation; |
||
| 170 | |||
| 171 | $relatedCollection = $this->document |
||
| 172 | ->getCollection() |
||
| 173 | ->getDatabase() |
||
| 174 | ->getCollection($relatedCollectionName); |
||
| 175 | |||
| 176 | if (!$relatedCollection->hasDocument($document)) { |
||
| 177 | throw new \Sokil\Mongo\Exception('Document must belongs to related collection'); |
||
| 178 | } |
||
| 179 | |||
| 180 | switch ($relationType) { |
||
| 181 | case Document::RELATION_BELONGS: |
||
| 182 | if (!$document->isStored()) { |
||
| 183 | throw new \Sokil\Mongo\Exception(sprintf( |
||
| 184 | 'Document %s must be saved before adding relation', |
||
| 185 | get_class($document) |
||
| 186 | )); |
||
| 187 | } |
||
| 188 | $this->document->set($field, $document->getId()); |
||
| 189 | break; |
||
| 190 | |||
| 191 | View Code Duplication | case Document::RELATION_HAS_ONE: |
|
| 192 | if (!$this->document->isStored()) { |
||
| 193 | throw new \Sokil\Mongo\Exception( |
||
| 194 | 'Document ' . get_class($this) . ' must be saved before adding relation' |
||
| 195 | ); |
||
| 196 | } |
||
| 197 | $document->set($field, $this->document->getId())->save(); |
||
| 198 | break; |
||
| 199 | |||
| 200 | View Code Duplication | case Document::RELATION_HAS_MANY: |
|
| 201 | if (!$this->document->isStored()) { |
||
| 202 | throw new \Sokil\Mongo\Exception( |
||
| 203 | 'Document ' . get_class($this) . ' must be saved before adding relation' |
||
| 204 | ); |
||
| 205 | } |
||
| 206 | $document->set($field, $this->document->getId())->save(); |
||
| 207 | break; |
||
| 208 | |||
| 209 | View Code Duplication | case Document::RELATION_MANY_MANY: |
|
| 210 | $isRelationListStoredInternally = isset($relation[3]) && $relation[3]; |
||
| 211 | if ($isRelationListStoredInternally) { |
||
| 212 | $this->document->push($field, $document->getId())->save(); |
||
| 213 | } else { |
||
| 214 | $document->push($field, $this->document->getId())->save(); |
||
| 215 | } |
||
| 216 | break; |
||
| 217 | |||
| 218 | default: |
||
| 219 | throw new \Sokil\Mongo\Exception( |
||
| 220 | 'Unsupported relation type "' . $relationType . '" when resolve relation "' . $relationName . '"' |
||
| 221 | ); |
||
| 222 | } |
||
| 223 | |||
| 224 | return $this; |
||
| 225 | } |
||
| 226 | |||
| 227 | public function removeRelation($relationName, Document $document = null) |
||
| 288 | } |
||
| 289 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: