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 | * Columns in Using clause |
||
| 67 | * |
||
| 68 | * @var ArrayObj |
||
| 69 | */ |
||
| 70 | public $using; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param Parser $parser The parser that serves as context. |
||
| 74 | * @param TokensList $list The list of tokens that are being parsed. |
||
| 75 | * @param array $options Parameters for parsing. |
||
| 76 | * |
||
| 77 | * @return JoinKeyword[] |
||
| 78 | */ |
||
| 79 | 6 | public static function parse(Parser $parser, TokensList $list, array $options = array()) |
|
| 171 | |||
| 172 | /** |
||
| 173 | * @param JoinKeyword[] $component The component to be built. |
||
| 174 | * @param array $options Parameters for building. |
||
| 175 | * |
||
| 176 | * @return string |
||
| 177 | */ |
||
| 178 | 2 | public static function build($component, array $options = array()) |
|
| 189 | } |
||
| 190 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.