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 |
||
| 7 | class DbSqlLiteral extends DbSqlAware { |
||
| 8 | |||
| 9 | const SQL_LITERAL_ALIAS_NONE = null; |
||
| 10 | const SQL_LITERAL_ALIAS_AUTO = ''; |
||
| 11 | |||
| 12 | public $literal = ''; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * DbSqlLiteral constructor. |
||
| 16 | * |
||
| 17 | * @param db_mysql|null $db |
||
| 18 | * @param string $literal |
||
| 19 | */ |
||
| 20 | 1 | public function __construct($db = null, $literal = '') { |
|
| 24 | // |
||
|
|
|||
| 25 | // public static function __callStatic($name, $arguments) { |
||
| 26 | // // method_exists(get_called_class(), $name) |
||
| 27 | // return call_user_func_array(array(static::build(), '_' .$name), $arguments); |
||
| 28 | // } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Renders single argument function |
||
| 32 | * - supports autobuild alias |
||
| 33 | * - supports direct alias |
||
| 34 | * - supports dotted field names like 'table.field' |
||
| 35 | * - supports all fields with '*' and 'table.*' |
||
| 36 | * |
||
| 37 | * @param string $functionName |
||
| 38 | * @param string $field |
||
| 39 | * - '*' - all fields |
||
| 40 | * - string - quoted as field name |
||
| 41 | * @param null|string $alias |
||
| 42 | * - DbSqlLiteral::SQL_LITERAL_ALIAS_NONE: no alias |
||
| 43 | * - DbSqlLiteral::SQL_LITERAL_ALIAS_AUTO: alias from $field |
||
| 44 | * - other value: using supplied alias |
||
| 45 | * |
||
| 46 | * @return $this |
||
| 47 | */ |
||
| 48 | 12 | View Code Duplication | public function buildSingleArgument($functionName, $field = '*', $alias = self::SQL_LITERAL_ALIAS_NONE) { |
| 61 | |||
| 62 | /** |
||
| 63 | * @param string $field |
||
| 64 | * @param null|string $alias |
||
| 65 | * |
||
| 66 | * @return static |
||
| 67 | * |
||
| 68 | * @see buildSingleArgument |
||
| 69 | */ |
||
| 70 | 12 | public function max($field = '*', $alias = self::SQL_LITERAL_ALIAS_NONE) { |
|
| 73 | |||
| 74 | /** |
||
| 75 | * @param string $field |
||
| 76 | * @param null|string $alias |
||
| 77 | * |
||
| 78 | * @return $this |
||
| 79 | * |
||
| 80 | * @see buildSingleArgument |
||
| 81 | */ |
||
| 82 | public function count($field = '*', $alias = self::SQL_LITERAL_ALIAS_NONE) { |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param string $field |
||
| 88 | * @param null|string $alias |
||
| 89 | * |
||
| 90 | * @return $this |
||
| 91 | * |
||
| 92 | * @see buildSingleArgument |
||
| 93 | */ |
||
| 94 | public function sum($field = '*', $alias = self::SQL_LITERAL_ALIAS_NONE) { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param string $field |
||
| 100 | * @param null|string $alias |
||
| 101 | * |
||
| 102 | * @return $this |
||
| 103 | * |
||
| 104 | * @see buildSingleArgument |
||
| 105 | */ |
||
| 106 | View Code Duplication | public function isNull($field = '*', $alias = self::SQL_LITERAL_ALIAS_NONE) { |
|
| 121 | |||
| 122 | |||
| 123 | /** |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | 12 | public function __toString() { |
|
| 129 | |||
| 130 | } |
||
| 131 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.