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 |
||
25 | class JoinKeyword extends Component |
||
26 | { |
||
27 | |||
28 | /** |
||
29 | * Types of join. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | public static $JOINS = array( |
||
34 | 'FULL JOIN' => 'FULL', |
||
35 | 'INNER JOIN' => 'INNER', |
||
36 | 'JOIN' => 'JOIN', |
||
37 | 'LEFT JOIN' => 'LEFT', |
||
38 | 'LEFT OUTER JOIN' => 'LEFT', |
||
39 | 'RIGHT JOIN' => 'RIGHT', |
||
40 | 'RIGHT OUTER JOIN' => 'RIGHT', |
||
41 | 'STRAIGHT_JOIN' => 'STRAIGHT', |
||
42 | ); |
||
43 | |||
44 | /** |
||
45 | * Type of this join. |
||
46 | * |
||
47 | * @see static::$JOINS |
||
48 | * @var string |
||
49 | */ |
||
50 | public $type; |
||
51 | |||
52 | /** |
||
53 | * Join expression. |
||
54 | * |
||
55 | * @var Expression |
||
56 | */ |
||
57 | public $expr; |
||
58 | |||
59 | /** |
||
60 | * Join conditions. |
||
61 | * |
||
62 | * @var Condition[] |
||
63 | */ |
||
64 | public $on; |
||
65 | |||
66 | /** |
||
67 | * Columns in Using clause |
||
68 | * |
||
69 | * @var ArrayObj |
||
70 | */ |
||
71 | public $using; |
||
72 | |||
73 | /** |
||
74 | * @param Parser $parser The parser that serves as context. |
||
75 | * @param TokensList $list The list of tokens that are being parsed. |
||
76 | * @param array $options Parameters for parsing. |
||
77 | * |
||
78 | * @return JoinKeyword[] |
||
79 | */ |
||
80 | 7 | public static function parse(Parser $parser, TokensList $list, array $options = array()) |
|
81 | { |
||
82 | 7 | $ret = array(); |
|
83 | |||
84 | 7 | $expr = new JoinKeyword(); |
|
85 | |||
86 | /** |
||
87 | * The state of the parser. |
||
88 | * |
||
89 | * Below are the states of the parser. |
||
90 | * |
||
91 | * 0 -----------------------[ JOIN ]----------------------> 1 |
||
92 | * |
||
93 | * 1 -----------------------[ expr ]----------------------> 2 |
||
94 | * |
||
95 | * 2 ------------------------[ ON ]-----------------------> 3 |
||
96 | * 2 -----------------------[ USING ]---------------------> 4 |
||
97 | * |
||
98 | * 3 --------------------[ conditions ]-------------------> 0 |
||
99 | * |
||
100 | * 4 ----------------------[ columns ]--------------------> 0 |
||
101 | * |
||
102 | * @var int $state |
||
103 | */ |
||
104 | 7 | $state = 0; |
|
105 | |||
106 | // By design, the parser will parse first token after the keyword. |
||
107 | // In this case, the keyword must be analyzed too, in order to determine |
||
108 | // the type of this join. |
||
109 | 7 | if ($list->idx > 0) { |
|
110 | 4 | --$list->idx; |
|
111 | 4 | } |
|
112 | |||
113 | 7 | for (; $list->idx < $list->count; ++$list->idx) { |
|
114 | /** |
||
115 | * Token parsed at this moment. |
||
116 | * |
||
117 | * @var Token $token |
||
118 | */ |
||
119 | 7 | $token = $list->tokens[$list->idx]; |
|
120 | |||
121 | // End of statement. |
||
122 | 7 | if ($token->type === Token::TYPE_DELIMITER) { |
|
123 | 6 | break; |
|
124 | } |
||
125 | |||
126 | // Skipping whitespaces and comments. |
||
127 | 7 | if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) { |
|
128 | 7 | continue; |
|
129 | } |
||
130 | |||
131 | 7 | if ($state === 0) { |
|
132 | 7 | if (($token->type === Token::TYPE_KEYWORD) |
|
133 | 7 | && (!empty(static::$JOINS[$token->value])) |
|
134 | 7 | ) { |
|
135 | 7 | $expr->type = static::$JOINS[$token->value]; |
|
136 | 7 | $state = 1; |
|
137 | 7 | } else { |
|
138 | 1 | break; |
|
139 | } |
||
140 | 7 | } elseif ($state === 1) { |
|
141 | 7 | $expr->expr = Expression::parse($parser, $list, array('field' => 'table')); |
|
142 | 7 | $state = 2; |
|
143 | 7 | View Code Duplication | } elseif ($state === 2) { |
1 ignored issue
–
show
|
|||
144 | 6 | if ($token->type === Token::TYPE_KEYWORD) { |
|
145 | 6 | if ($token->value === 'ON') { |
|
146 | 5 | $state = 3; |
|
147 | 6 | } elseif ($token->value === 'USING') { |
|
148 | 1 | $state = 4; |
|
149 | 1 | } |
|
150 | 6 | } |
|
151 | 6 | } elseif ($state === 3) { |
|
152 | 5 | $expr->on = Condition::parse($parser, $list); |
|
153 | 5 | $ret[] = $expr; |
|
154 | 5 | $expr = new JoinKeyword(); |
|
155 | 5 | $state = 0; |
|
156 | 6 | } elseif ($state === 4) { |
|
157 | 1 | $expr->using = ArrayObj::parse($parser, $list); |
|
158 | 1 | $ret[] = $expr; |
|
159 | 1 | $expr = new JoinKeyword(); |
|
160 | 1 | $state = 0; |
|
161 | 1 | } |
|
162 | |||
163 | 7 | } |
|
164 | |||
165 | 7 | if (!empty($expr->type)) { |
|
166 | 1 | $ret[] = $expr; |
|
167 | 1 | } |
|
168 | |||
169 | 7 | --$list->idx; |
|
170 | 7 | return $ret; |
|
171 | } |
||
172 | |||
173 | /** |
||
174 | * @param JoinKeyword[] $component The component to be built. |
||
175 | * @param array $options Parameters for building. |
||
176 | * |
||
177 | * @return string |
||
178 | */ |
||
179 | 2 | public static function build($component, array $options = array()) |
|
190 | } |
||
191 |
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.