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 |
||
| 12 | final class MySQLConnector implements Connector |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var null|MySQLi |
||
| 16 | */ |
||
| 17 | private $connection = null; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var bool |
||
| 21 | */ |
||
| 22 | private $ready = true; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var float |
||
| 26 | */ |
||
| 27 | private $poll = 0.000001; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @inheritdoc |
||
| 31 | * |
||
| 32 | * @param array $config |
||
| 33 | * |
||
| 34 | * @throws InvalidArgumentException |
||
| 35 | */ |
||
| 36 | public function connect(array $config) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @inheritdoc |
||
| 74 | * |
||
| 75 | * @param string $query |
||
| 76 | * |
||
| 77 | * @return PromiseInterface |
||
| 78 | */ |
||
| 79 | public function query($query) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @inheritdoc |
||
| 130 | * |
||
| 131 | * @param mixed $value |
||
| 132 | * |
||
| 133 | * @return mixed |
||
| 134 | */ |
||
| 135 | public function escape($value) |
||
| 139 | } |
||
| 140 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.