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 |
||
47 | class SubQuery implements NodeInterface |
||
48 | { |
||
49 | private $subQuery; |
||
50 | |||
51 | /** |
||
52 | * Returns the list of subQuery statements. |
||
53 | * |
||
54 | * @return Select |
||
55 | */ |
||
56 | public function getSubQuery() |
||
57 | { |
||
58 | return $this->subQuery; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Sets the list of subQuery statements. |
||
63 | * |
||
64 | * @param Select $subQuery |
||
65 | */ |
||
66 | public function setSubQuery(Select $subQuery) |
||
67 | { |
||
68 | $this->subQuery = $subQuery; |
||
69 | } |
||
70 | |||
71 | private $alias; |
||
72 | |||
73 | /** |
||
74 | * Returns the alias. |
||
75 | * |
||
76 | * @return string |
||
77 | */ |
||
78 | public function getAlias() |
||
79 | { |
||
80 | return $this->alias; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Sets the alias. |
||
85 | * |
||
86 | * @param string $alias |
||
87 | */ |
||
88 | public function setAlias($alias) |
||
89 | { |
||
90 | $this->alias = $alias; |
||
91 | } |
||
92 | |||
93 | private $joinType; |
||
94 | |||
95 | /** |
||
96 | * Returns the join type. |
||
97 | * |
||
98 | * @return string |
||
99 | */ |
||
100 | public function getJoinType() |
||
101 | { |
||
102 | return $this->joinType; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Sets the join type (JOIN, LEFT JOIN, RIGHT JOIN, etc...). |
||
107 | * |
||
108 | * @param string $joinType |
||
109 | */ |
||
110 | public function setJoinType($joinType) |
||
111 | { |
||
112 | $this->joinType = $joinType; |
||
113 | } |
||
114 | |||
115 | private $refClause; |
||
116 | |||
117 | /** |
||
118 | * Returns the list of refClause statements. |
||
119 | * |
||
120 | * @return NodeInterface[] |
||
121 | */ |
||
122 | public function getRefClause() |
||
123 | { |
||
124 | return $this->refClause; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Sets the list of refClause statements. |
||
129 | * |
||
130 | * @param NodeInterface[] $refClause |
||
131 | */ |
||
132 | public function setRefClause($refClause) |
||
133 | { |
||
134 | $this->refClause = $refClause; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Returns a Mouf instance descriptor describing this object. |
||
139 | * |
||
140 | * @param MoufManager $moufManager |
||
141 | * |
||
142 | * @return MoufInstanceDescriptor |
||
143 | */ |
||
144 | View Code Duplication | public function toInstanceDescriptor(MoufManager $moufManager) |
|
|
|||
145 | { |
||
146 | $instanceDescriptor = $moufManager->createInstance(get_called_class()); |
||
147 | $instanceDescriptor->getProperty('subQuery')->setValue($this->subQuery->toInstanceDescriptor($moufManager)); |
||
148 | $instanceDescriptor->getProperty('alias')->setValue($this->alias); |
||
149 | $instanceDescriptor->getProperty('joinType')->setValue($this->joinType); |
||
150 | $instanceDescriptor->getProperty('refClause')->setValue(NodeFactory::nodeToInstanceDescriptor($this->refClause, $moufManager)); |
||
151 | |||
152 | return $instanceDescriptor; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Walks the tree of nodes, calling the visitor passed in parameter. |
||
157 | * |
||
158 | * @param VisitorInterface $visitor |
||
159 | */ |
||
160 | View Code Duplication | public function walk(VisitorInterface $visitor) |
|
161 | { |
||
162 | $node = $this; |
||
163 | $result = $visitor->enterNode($node); |
||
164 | if ($result instanceof NodeInterface) { |
||
165 | $node = $result; |
||
166 | } |
||
167 | if ($result !== NodeTraverser::DONT_TRAVERSE_CHILDREN) { |
||
168 | $result2 = $this->subQuery->walk($visitor); |
||
169 | if ($result2 === NodeTraverser::REMOVE_NODE) { |
||
170 | return NodeTraverser::REMOVE_NODE; |
||
171 | } elseif ($result2 instanceof NodeInterface) { |
||
172 | $this->subQuery = $result2; |
||
173 | } |
||
174 | } |
||
175 | |||
176 | return $visitor->leaveNode($node); |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Renders the object as a SQL string. |
||
181 | * |
||
182 | * @param Connection $dbConnection |
||
183 | * @param array $parameters |
||
184 | * @param number $indent |
||
185 | * @param int $conditionsMode |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY) |
||
206 | } |
||
207 |
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.