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 Anneal |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var array |
||
| 11 | */ |
||
| 12 | private $domain; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var Cost |
||
| 16 | */ |
||
| 17 | private $cost; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var int |
||
| 21 | */ |
||
| 22 | private $temperature; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var float |
||
| 26 | */ |
||
| 27 | private $cool; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var int |
||
| 31 | */ |
||
| 32 | private $step; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @param array $domain |
||
| 36 | * @param Cost $cost |
||
| 37 | * @param int $temperature |
||
| 38 | * @param float $cool |
||
| 39 | * @param int $step |
||
| 40 | */ |
||
| 41 | public function __construct(array $domain, Cost $cost, int $temperature = 1000, float $cool = 0.99, int $step=1) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @return array |
||
| 52 | */ |
||
| 53 | public function run() |
||
| 86 | } |
||
| 87 |
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.