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 |
||
32 | class Expr extends \Doctrine\MongoDB\Query\Expr |
||
33 | { |
||
34 | /** |
||
35 | * The DocumentManager instance for this query |
||
36 | * |
||
37 | * @var DocumentManager |
||
38 | */ |
||
39 | private $dm; |
||
40 | |||
41 | /** |
||
42 | * The ClassMetadata instance for the document being queried |
||
43 | * |
||
44 | * @var ClassMetadata |
||
45 | */ |
||
46 | private $class; |
||
47 | |||
48 | 213 | public function __construct(DocumentManager $dm) |
|
52 | |||
53 | 211 | public function setClassMetadata(ClassMetadata $class) |
|
57 | |||
58 | /** |
||
59 | * Checks that the value of the current field is a reference to the supplied document. |
||
60 | */ |
||
61 | 9 | public function references($document) |
|
62 | { |
||
63 | 9 | if ($this->currentField) { |
|
64 | try { |
||
65 | 9 | $mapping = $this->class->getFieldMapping($this->currentField); |
|
66 | 9 | } catch (MappingException $e) { |
|
67 | 2 | if (empty($this->class->discriminatorMap)) { |
|
68 | throw $e; |
||
69 | } |
||
70 | 2 | foreach ($this->class->discriminatorMap as $child) { |
|
71 | 2 | $childClass = $this->dm->getClassMetadata($child); |
|
72 | 2 | if ($childClass->hasAssociation($this->currentField)) { |
|
73 | 1 | $mapping = $childClass->getFieldMapping($this->currentField); |
|
74 | 1 | break; |
|
75 | } |
||
76 | 2 | } |
|
77 | 2 | if ( ! isset($mapping) || $mapping === null) { |
|
78 | 1 | throw MappingException::mappingNotFoundInClassNorDescendants($this->class->name, $this->currentField); |
|
79 | } |
||
80 | } |
||
81 | 8 | $dbRef = $this->dm->createDBRef($document, $mapping); |
|
82 | |||
83 | 8 | if (isset($mapping['simple']) && $mapping['simple']) { |
|
84 | 4 | $this->query[$mapping['name']] = $dbRef; |
|
85 | 4 | View Code Duplication | } else { |
|
|||
86 | 6 | $keys = array('ref' => true, 'id' => true, 'db' => true); |
|
87 | |||
88 | 6 | if (isset($mapping['targetDocument'])) { |
|
89 | 4 | unset($keys['ref'], $keys['db']); |
|
90 | 4 | } |
|
91 | |||
92 | 6 | foreach ($keys as $key => $value) { |
|
93 | 6 | $this->query[$this->currentField . '.$' . $key] = $dbRef['$' . $key]; |
|
94 | 6 | } |
|
95 | } |
||
96 | 8 | } else { |
|
97 | $dbRef = $this->dm->createDBRef($document); |
||
98 | $this->query = $dbRef; |
||
99 | } |
||
100 | |||
101 | 8 | return $this; |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * Checks that the current field includes a reference to the supplied document. |
||
106 | */ |
||
107 | 2 | public function includesReferenceTo($document) |
|
133 | |||
134 | 202 | public function getQuery() |
|
140 | |||
141 | 185 | public function getNewObj() |
|
147 | } |
||
148 |
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.