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 RenameOperation extends Component |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * The old table name. |
||
| 25 | * |
||
| 26 | * @var Expression |
||
| 27 | */ |
||
| 28 | public $old; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The new table name. |
||
| 32 | * |
||
| 33 | * @var Expression |
||
| 34 | */ |
||
| 35 | public $new; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Constructor. |
||
| 39 | * |
||
| 40 | * @param Expression $old old expression |
||
|
|
|||
| 41 | * @param Expression $new new expression containing new name |
||
| 42 | */ |
||
| 43 | 8 | public function __construct($old = null, $new = null) |
|
| 48 | |||
| 49 | /** |
||
| 50 | * @param Parser $parser the parser that serves as context |
||
| 51 | * @param TokensList $list the list of tokens that are being parsed |
||
| 52 | * @param array $options parameters for parsing |
||
| 53 | * |
||
| 54 | * @return RenameOperation[] |
||
| 55 | */ |
||
| 56 | 8 | public static function parse(Parser $parser, TokensList $list, array $options = array()) |
|
| 57 | { |
||
| 58 | 8 | $ret = array(); |
|
| 59 | |||
| 60 | 8 | $expr = new self(); |
|
| 61 | |||
| 62 | /** |
||
| 63 | * The state of the parser. |
||
| 64 | * |
||
| 65 | * Below are the states of the parser. |
||
| 66 | * |
||
| 67 | * 0 ---------------------[ old name ]--------------------> 1 |
||
| 68 | * |
||
| 69 | * 1 ------------------------[ TO ]-----------------------> 2 |
||
| 70 | * |
||
| 71 | * 2 ---------------------[ new name ]--------------------> 3 |
||
| 72 | * |
||
| 73 | * 3 ------------------------[ , ]------------------------> 0 |
||
| 74 | * 3 -----------------------[ else ]----------------------> (END) |
||
| 75 | * |
||
| 76 | * @var int |
||
| 77 | */ |
||
| 78 | 8 | $state = 0; |
|
| 79 | |||
| 80 | 8 | for (; $list->idx < $list->count; ++$list->idx) { |
|
| 81 | /** |
||
| 82 | * Token parsed at this moment. |
||
| 83 | * |
||
| 84 | * @var Token |
||
| 85 | */ |
||
| 86 | 8 | $token = $list->tokens[$list->idx]; |
|
| 87 | |||
| 88 | // End of statement. |
||
| 89 | 8 | if ($token->type === Token::TYPE_DELIMITER) { |
|
| 90 | 7 | break; |
|
| 91 | } |
||
| 92 | |||
| 93 | // Skipping whitespaces and comments. |
||
| 94 | 8 | if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) { |
|
| 95 | 7 | continue; |
|
| 96 | } |
||
| 97 | |||
| 98 | 8 | if ($state === 0) { |
|
| 99 | 8 | $expr->old = Expression::parse( |
|
| 100 | 8 | $parser, |
|
| 101 | 8 | $list, |
|
| 102 | array( |
||
| 103 | 8 | 'breakOnAlias' => true, |
|
| 104 | 'parseField' => 'table' |
||
| 105 | ) |
||
| 106 | ); |
||
| 107 | 8 | if (empty($expr->old)) { |
|
| 108 | 1 | $parser->error( |
|
| 109 | 1 | 'The old name of the table was expected.', |
|
| 110 | $token |
||
| 111 | ); |
||
| 112 | } |
||
| 113 | 8 | $state = 1; |
|
| 114 | 7 | } elseif ($state === 1) { |
|
| 115 | 7 | if ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'TO') { |
|
| 116 | 7 | $state = 2; |
|
| 117 | } else { |
||
| 118 | $parser->error( |
||
| 119 | 'Keyword "TO" was expected.', |
||
| 120 | $token |
||
| 121 | ); |
||
| 122 | 7 | break; |
|
| 123 | } |
||
| 124 | 7 | } elseif ($state === 2) { |
|
| 125 | 7 | $expr->new = Expression::parse( |
|
| 126 | 7 | $parser, |
|
| 127 | 7 | $list, |
|
| 128 | array( |
||
| 129 | 7 | 'breakOnAlias' => true, |
|
| 130 | 'parseField' => 'table' |
||
| 131 | ) |
||
| 132 | ); |
||
| 133 | 7 | if (empty($expr->new)) { |
|
| 134 | 1 | $parser->error( |
|
| 135 | 1 | 'The new name of the table was expected.', |
|
| 136 | $token |
||
| 137 | ); |
||
| 138 | } |
||
| 139 | 7 | $state = 3; |
|
| 140 | 5 | } elseif ($state === 3) { |
|
| 141 | 5 | if (($token->type === Token::TYPE_OPERATOR) && ($token->value === ',')) { |
|
| 142 | 4 | $ret[] = $expr; |
|
| 143 | 4 | $expr = new self(); |
|
| 144 | 4 | $state = 0; |
|
| 145 | } else { |
||
| 146 | 1 | break; |
|
| 147 | } |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | 8 | if ($state !== 3) { |
|
| 152 | 1 | $parser->error( |
|
| 153 | 1 | 'A rename operation was expected.', |
|
| 154 | 1 | $list->tokens[$list->idx - 1] |
|
| 155 | ); |
||
| 156 | } |
||
| 157 | |||
| 158 | // Last iteration was not saved. |
||
| 159 | 8 | if (! empty($expr->old)) { |
|
| 160 | 7 | $ret[] = $expr; |
|
| 161 | } |
||
| 162 | |||
| 163 | 8 | --$list->idx; |
|
| 164 | |||
| 165 | 8 | return $ret; |
|
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param RenameOperation $component the component to be built |
||
| 170 | * @param array $options parameters for building |
||
| 171 | * |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | 2 | View Code Duplication | public static function build($component, array $options = array()) |
| 182 | } |
||
| 183 |
This check looks for
@paramannotations 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.