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 |
||
21 | class GroupKeyword extends Component |
||
22 | { |
||
23 | /** |
||
24 | * The expression that is used for grouping. |
||
25 | * |
||
26 | * @var Expression |
||
27 | */ |
||
28 | public $expr; |
||
29 | |||
30 | /** |
||
31 | * Constructor. |
||
32 | * |
||
33 | * @param Expression $expr the expression that we are sorting by |
||
|
|||
34 | */ |
||
35 | 4 | public function __construct($expr = null) |
|
39 | |||
40 | /** |
||
41 | * @param Parser $parser the parser that serves as context |
||
42 | * @param TokensList $list the list of tokens that are being parsed |
||
43 | * @param array $options parameters for parsing |
||
44 | * |
||
45 | * @return GroupKeyword[] |
||
46 | */ |
||
47 | 3 | View Code Duplication | public static function parse(Parser $parser, TokensList $list, array $options = array()) |
48 | { |
||
49 | 3 | $ret = array(); |
|
50 | |||
51 | 3 | $expr = new self(); |
|
52 | |||
53 | /** |
||
54 | * The state of the parser. |
||
55 | * |
||
56 | * Below are the states of the parser. |
||
57 | * |
||
58 | * 0 --------------------[ expression ]-------------------> 1 |
||
59 | * |
||
60 | * 1 ------------------------[ , ]------------------------> 0 |
||
61 | * 1 -------------------[ ASC / DESC ]--------------------> 1 |
||
62 | * |
||
63 | * @var int |
||
64 | */ |
||
65 | 3 | $state = 0; |
|
66 | |||
67 | 3 | for (; $list->idx < $list->count; ++$list->idx) { |
|
68 | /** |
||
69 | * Token parsed at this moment. |
||
70 | * |
||
71 | * @var Token |
||
72 | */ |
||
73 | 3 | $token = $list->tokens[$list->idx]; |
|
74 | |||
75 | // End of statement. |
||
76 | 3 | if ($token->type === Token::TYPE_DELIMITER) { |
|
77 | 2 | break; |
|
78 | } |
||
79 | |||
80 | // Skipping whitespaces and comments. |
||
81 | 3 | if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) { |
|
82 | 3 | continue; |
|
83 | } |
||
84 | |||
85 | 3 | if ($state === 0) { |
|
86 | 3 | $expr->expr = Expression::parse($parser, $list); |
|
87 | 3 | $state = 1; |
|
88 | 1 | } elseif ($state === 1) { |
|
89 | 1 | if (($token->type === Token::TYPE_KEYWORD) |
|
90 | 1 | && (($token->keyword === 'ASC') || ($token->keyword === 'DESC')) |
|
91 | ) { |
||
92 | $expr->type = $token->keyword; |
||
93 | 1 | } elseif (($token->type === Token::TYPE_OPERATOR) |
|
94 | && ($token->value === ',') |
||
95 | ) { |
||
96 | if (!empty($expr->expr)) { |
||
97 | $ret[] = $expr; |
||
98 | } |
||
99 | $expr = new self(); |
||
100 | $state = 0; |
||
101 | } else { |
||
102 | 1 | break; |
|
103 | } |
||
104 | } |
||
105 | } |
||
106 | |||
107 | // Last iteration was not processed. |
||
108 | 3 | if (!empty($expr->expr)) { |
|
109 | 3 | $ret[] = $expr; |
|
110 | } |
||
111 | |||
112 | 3 | --$list->idx; |
|
113 | |||
114 | 3 | return $ret; |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * @param GroupKeyword|GroupKeyword[] $component the component to be built |
||
119 | * @param array $options parameters for building |
||
120 | * |
||
121 | * @return string |
||
122 | */ |
||
123 | 3 | public static function build($component, array $options = array()) |
|
132 | } |
||
133 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.