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 |
||
46 | class Table implements NodeInterface |
||
47 | { |
||
48 | private $database; |
||
49 | |||
50 | /** |
||
51 | * Returns the name of the database. |
||
52 | * |
||
53 | * @return mixed |
||
54 | */ |
||
55 | public function getDatabase() |
||
59 | |||
60 | /** |
||
61 | * Sets the name of the database |
||
62 | * |
||
63 | * @param mixed $database |
||
64 | */ |
||
65 | public function setDatabase($database) |
||
69 | |||
70 | private $table; |
||
71 | |||
72 | /** |
||
73 | * Returns the table name. |
||
74 | * |
||
75 | * @return string |
||
76 | */ |
||
77 | public function getTable() |
||
81 | |||
82 | /** |
||
83 | * Sets the table name. |
||
84 | * |
||
85 | * @Important |
||
86 | * |
||
87 | * @param string $table |
||
88 | */ |
||
89 | public function setTable($table) |
||
93 | |||
94 | private $alias; |
||
95 | |||
96 | /** |
||
97 | * Returns the alias. |
||
98 | * |
||
99 | * @return string |
||
100 | */ |
||
101 | public function getAlias() |
||
105 | |||
106 | /** |
||
107 | * Sets the alias. |
||
108 | * |
||
109 | * @Important |
||
110 | * |
||
111 | * @param string $alias |
||
112 | */ |
||
113 | public function setAlias($alias) |
||
117 | |||
118 | private $joinType; |
||
119 | |||
120 | /** |
||
121 | * Returns the join type. |
||
122 | * |
||
123 | * @return string |
||
124 | */ |
||
125 | public function getJoinType() |
||
129 | |||
130 | /** |
||
131 | * Sets the join type (JOIN, LEFT JOIN, RIGHT JOIN, etc...). |
||
132 | * |
||
133 | * @Important |
||
134 | * |
||
135 | * @param string $joinType |
||
136 | */ |
||
137 | public function setJoinType($joinType) |
||
141 | |||
142 | private $refClause; |
||
143 | |||
144 | /** |
||
145 | * Returns the list of refClause statements. |
||
146 | * |
||
147 | * @return NodeInterface[]|NodeInterface |
||
148 | */ |
||
149 | public function getRefClause() |
||
153 | |||
154 | /** |
||
155 | * Sets the list of refClause statements. |
||
156 | * |
||
157 | * @Important |
||
158 | * |
||
159 | * @param NodeInterface[]|NodeInterface $refClause |
||
160 | */ |
||
161 | public function setRefClause($refClause) |
||
165 | |||
166 | /** |
||
167 | * Returns a Mouf instance descriptor describing this object. |
||
168 | * |
||
169 | * @param MoufManager $moufManager |
||
170 | * |
||
171 | * @return MoufInstanceDescriptor |
||
172 | */ |
||
173 | public function toInstanceDescriptor(MoufManager $moufManager) |
||
174 | { |
||
175 | $instanceDescriptor = $moufManager->createInstance(get_called_class()); |
||
176 | $instanceDescriptor->getProperty('database')->setValue($this->database); |
||
177 | $instanceDescriptor->getProperty('table')->setValue($this->table); |
||
178 | $instanceDescriptor->getProperty('alias')->setValue($this->alias); |
||
179 | $instanceDescriptor->getProperty('joinType')->setValue($this->joinType); |
||
180 | $instanceDescriptor->getProperty('refClause')->setValue(NodeFactory::nodeToInstanceDescriptor($this->refClause, $moufManager)); |
||
181 | |||
182 | return $instanceDescriptor; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Renders the object as a SQL string. |
||
187 | * |
||
188 | * @param Connection $dbConnection |
||
189 | * @param array $parameters |
||
190 | * @param number $indent |
||
191 | * @param int $conditionsMode |
||
192 | * |
||
193 | * @return string |
||
194 | */ |
||
195 | public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY) |
||
196 | { |
||
197 | $sql = ''; |
||
198 | if ($this->refClause || $this->joinType === 'CROSS JOIN') { |
||
199 | $sql .= "\n ".$this->joinType.' '; |
||
200 | } |
||
201 | if ($this->database) { |
||
202 | $sql .= NodeFactory::escapeDBItem($this->database, $dbConnection).'.'; |
||
203 | } |
||
204 | $sql .= NodeFactory::escapeDBItem($this->table, $dbConnection); |
||
205 | if ($this->alias) { |
||
206 | $sql .= ' AS '.NodeFactory::escapeDBItem($this->alias, $dbConnection); |
||
207 | } |
||
208 | View Code Duplication | if ($this->refClause) { |
|
|
|||
209 | $sql .= ' ON '; |
||
210 | $sql .= NodeFactory::toSql($this->refClause, $dbConnection, $parameters, ' ', true, $indent, $conditionsMode); |
||
211 | } |
||
212 | |||
213 | return $sql; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Walks the tree of nodes, calling the visitor passed in parameter. |
||
218 | * |
||
219 | * @param VisitorInterface $visitor |
||
220 | */ |
||
221 | View Code Duplication | public function walk(VisitorInterface $visitor) |
|
250 | } |
||
251 |
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.