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 |
||
5 | class ExpressionToken |
||
6 | { |
||
7 | private $subTree; |
||
8 | private $expression; |
||
9 | private $key; |
||
10 | private $token; |
||
11 | private $tokenType; |
||
12 | private $trim; |
||
13 | private $upper; |
||
14 | |||
15 | public function __construct($key = '', $token = '') |
||
16 | { |
||
17 | $this->subTree = false; |
||
18 | $this->expression = ''; |
||
19 | $this->key = $key; |
||
20 | $this->token = $token; |
||
21 | $this->tokenType = false; |
||
22 | $this->trim = trim($token); |
||
23 | $this->upper = strtoupper($this->trim); |
||
24 | } |
||
25 | |||
26 | # TODO: we could replace it with a constructor new ExpressionToken(this, "*") |
||
27 | public function addToken($string) |
||
31 | |||
32 | public function isEnclosedWithinParenthesis() |
||
36 | |||
37 | public function setSubTree($tree) |
||
41 | |||
42 | public function getSubTree() |
||
46 | |||
47 | public function getUpper($idx = false) |
||
51 | |||
52 | public function getTrim($idx = false) |
||
56 | |||
57 | public function getToken($idx = false) |
||
61 | |||
62 | public function setTokenType($type) |
||
66 | |||
67 | View Code Duplication | public function endsWith($needle) |
|
|
|||
68 | { |
||
69 | $length = strlen($needle); |
||
70 | if ($length == 0) { |
||
71 | return true; |
||
72 | } |
||
73 | |||
74 | $start = $length * -1; |
||
75 | |||
76 | return (substr($this->token, $start) === $needle); |
||
77 | } |
||
78 | |||
79 | public function isWhitespaceToken() |
||
83 | |||
84 | public function isCommaToken() |
||
88 | |||
89 | public function isVariableToken() |
||
93 | |||
94 | public function isSubQueryToken() |
||
98 | |||
99 | public function isExpression() |
||
103 | |||
104 | public function isBracketExpression() |
||
108 | |||
109 | public function isOperator() |
||
113 | |||
114 | public function isInList() |
||
118 | |||
119 | public function isFunction() |
||
123 | |||
124 | public function isUnspecified() |
||
128 | |||
129 | public function isAggregateFunction() |
||
133 | |||
134 | public function isColumnReference() |
||
138 | |||
139 | public function isConstant() |
||
143 | |||
144 | public function isSign() |
||
148 | |||
149 | public function isSubQuery() |
||
153 | |||
154 | public function toArray() |
||
158 | } |
||
159 |
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.