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 |
||
45 | class Parameter implements NodeInterface |
||
46 | { |
||
47 | protected $name; |
||
48 | protected $discardedOnNull = true; |
||
49 | |||
50 | /** |
||
51 | * Returns the name. |
||
52 | * |
||
53 | * @return string |
||
54 | */ |
||
55 | public function getName() |
||
59 | |||
60 | /** |
||
61 | * Sets the name. |
||
62 | * If the name ends with !, the parameter will be considered not nullable (regarding magicparameter settings). |
||
63 | * |
||
64 | * @Important |
||
65 | * |
||
66 | * @param string $name |
||
67 | */ |
||
68 | public function setName($name) |
||
78 | |||
79 | /** |
||
80 | * @var string |
||
81 | */ |
||
82 | protected $autoPrepend; |
||
83 | |||
84 | /** |
||
85 | * @var string |
||
86 | */ |
||
87 | protected $autoAppend; |
||
88 | |||
89 | /** |
||
90 | * @return string |
||
91 | */ |
||
92 | public function getAutoPrepend() |
||
96 | |||
97 | /** |
||
98 | * Sets a string that will automatically be appended to the parameter, if the parameter is available. |
||
99 | * Very useful to automatically add "%" to a parameter used in a LIKE. |
||
100 | * |
||
101 | * @Important IfSet |
||
102 | * |
||
103 | * @param string $autoPrepend |
||
104 | */ |
||
105 | public function setAutoPrepend($autoPrepend) |
||
111 | |||
112 | /** |
||
113 | * @return string |
||
114 | */ |
||
115 | public function getAutoAppend() |
||
119 | |||
120 | /** |
||
121 | * Sets a string that will automatically be preprended to the parameter, if the parameter is available. |
||
122 | * Very useful to automatically add "%" to a parameter used in a LIKE. |
||
123 | * |
||
124 | * @Important IfSet |
||
125 | * |
||
126 | * @param string $autoAppend |
||
127 | */ |
||
128 | public function setAutoAppend($autoAppend) |
||
134 | |||
135 | /** |
||
136 | * Returns a Mouf instance descriptor describing this object. |
||
137 | * |
||
138 | * @param MoufManager $moufManager |
||
139 | * |
||
140 | * @return MoufInstanceDescriptor |
||
141 | */ |
||
142 | public function toInstanceDescriptor(MoufManager $moufManager) |
||
151 | |||
152 | /** |
||
153 | * Renders the object as a SQL string. |
||
154 | * |
||
155 | * @param Connection $dbConnection |
||
156 | * @param array $parameters |
||
157 | * @param number $indent |
||
158 | * @param int $conditionsMode |
||
159 | * |
||
160 | * @return string |
||
161 | */ |
||
162 | public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY) |
||
192 | |||
193 | /** |
||
194 | * Walks the tree of nodes, calling the visitor passed in parameter. |
||
195 | * |
||
196 | * @param VisitorInterface $visitor |
||
197 | * |
||
198 | * @return NodeInterface|null|string Can return null if nothing is to be done or a node that should replace this node, or NodeTraverser::REMOVE_NODE to remove the node |
||
199 | */ |
||
200 | View Code Duplication | public function walk(VisitorInterface $visitor) |
|
210 | |||
211 | /** |
||
212 | * Returns whether the parameter can be discarded if provided value is null. |
||
213 | * |
||
214 | * @return bool |
||
215 | */ |
||
216 | public function isDiscardedOnNull() |
||
220 | } |
||
221 |
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.