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 |
||
17 | class TokenType { |
||
18 | |||
19 | /** |
||
20 | * Available operands |
||
21 | * @var array |
||
22 | */ |
||
23 | private $operands = array( |
||
24 | // IDENTIFIER |
||
25 | T_VARIABLE |
||
26 | ,T_VAR |
||
27 | ,T_LNUMBER |
||
28 | ,T_DNUMBER |
||
29 | ,T_ARRAY |
||
30 | ,T_CONST |
||
31 | ,T_STRING |
||
32 | ,T_NUM_STRING |
||
33 | |||
34 | // TYPENAME |
||
35 | , T_INT_CAST |
||
36 | , T_ARRAY_CAST |
||
37 | , T_BOOL_CAST |
||
38 | , T_DOUBLE_CAST |
||
39 | , T_OBJECT_CAST |
||
40 | , T_STRING_CAST |
||
41 | , T_UNSET_CAST |
||
42 | |||
43 | ); |
||
44 | |||
45 | /** |
||
46 | * Available operators |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | private $operators = array( |
||
51 | // OPERATOR |
||
52 | T_IS_EQUAL |
||
53 | , T_AND_EQUAL |
||
54 | , T_CONCAT_EQUAL |
||
55 | , T_DIV_EQUAL |
||
56 | , T_MINUS_EQUAL |
||
57 | , T_MOD_EQUAL |
||
58 | , T_MUL_EQUAL |
||
59 | , T_OR_EQUAL |
||
60 | , T_PLUS_EQUAL |
||
61 | , T_SL_EQUAL |
||
62 | , T_SR_EQUAL |
||
63 | , T_XOR_EQUAL |
||
64 | , T_IS_GREATER_OR_EQUAL |
||
65 | , T_IS_SMALLER_OR_EQUAL |
||
66 | , T_IS_NOT_EQUAL |
||
67 | , T_IS_IDENTICAL |
||
68 | , T_BOOLEAN_AND |
||
69 | , T_BOOLEAN_AND |
||
70 | , T_INC |
||
71 | , T_OBJECT_OPERATOR |
||
72 | , T_DOUBLE_COLON |
||
73 | , T_PAAMAYIM_NEKUDOTAYIM |
||
74 | |||
75 | // SCSPEC |
||
76 | , T_STATIC |
||
77 | , T_ABSTRACT |
||
78 | |||
79 | // TYPE QUAL |
||
80 | , T_FINAL |
||
81 | |||
82 | // RESERVED |
||
83 | , T_CONST |
||
84 | , T_BREAK |
||
85 | , T_CASE |
||
86 | , T_CONTINUE |
||
87 | , T_DEFAULT |
||
88 | , T_DO |
||
89 | , T_IF |
||
90 | , T_ELSE |
||
91 | , T_ELSEIF |
||
92 | , T_FOR |
||
93 | , T_FOREACH |
||
94 | , T_GOTO |
||
95 | , T_NEW |
||
96 | , T_RETURN |
||
97 | , T_SWITCH |
||
98 | , T_WHILE |
||
99 | ); |
||
100 | |||
101 | /** |
||
102 | * Operators encapsuled int T_STRING |
||
103 | * |
||
104 | * @var array |
||
105 | */ |
||
106 | private $operatorsStrings = array( |
||
107 | ';', '*', '/', '%', '-','+' |
||
108 | , '!', '!=', '%', '%=', '&', '&&', '||', '&=', '(', ')' |
||
109 | /*, '{', '}'*/, '[', ']', '*', '*=', '+', '++', '+=', ',' |
||
110 | , '-', '--', '-=->', '.', '...', '/', '/=', ':', '::' |
||
111 | , '<', '<<', '<<=', '<=', '=', '==', '>', '>=', '>>' |
||
112 | , '>>=', '?', '^^=', '|', '|=', '~', ';', '=&', '“' |
||
113 | , '“', '‘', '‘', '#', '##' |
||
114 | ); |
||
115 | |||
116 | /** |
||
117 | * To bypass |
||
118 | * |
||
119 | * @var array |
||
120 | */ |
||
121 | private $byPass = array( |
||
122 | '{', '}' // in PHP, these case are counted with T_IF, '(', ... |
||
123 | , ')' , '(' // we count only the first ( |
||
124 | , ',' |
||
125 | ); |
||
126 | |||
127 | /** |
||
128 | * Constructor |
||
129 | */ |
||
130 | public function __construct() { |
||
148 | |||
149 | /** |
||
150 | * Check if the token is operand |
||
151 | * |
||
152 | * @param Token $token |
||
153 | * @return boolean |
||
154 | */ |
||
155 | View Code Duplication | public function isOperand(Token $token) |
|
163 | |||
164 | /** |
||
165 | * Check if the token is operator |
||
166 | * |
||
167 | * @param Token $token |
||
168 | * @return boolean |
||
169 | */ |
||
170 | View Code Duplication | public function isOperator(Token $token) |
|
181 | } |
||
182 |
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.