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 NotEqualRule extends NotRule |
||
8 | { |
||
9 | use Trait_RuleWithField; |
||
10 | |||
11 | /** @var string operator */ |
||
12 | const operator = '!='; |
||
13 | |||
14 | /** @var mixed value */ |
||
15 | protected $value; |
||
16 | |||
17 | /** |
||
18 | * @param string $field The field to apply the rule on. |
||
19 | * @param mixed $value The value the field can equal to. |
||
20 | */ |
||
21 | 40 | public function __construct( $field, $value, array $options=[] ) |
|
29 | |||
30 | /** |
||
31 | */ |
||
32 | 23 | public function isNormalizationAllowed(array $contextual_options=[]) |
|
41 | |||
42 | /** |
||
43 | */ |
||
44 | 40 | public function getValue() |
|
48 | |||
49 | /** |
||
50 | */ |
||
51 | 12 | public function getValues() |
|
55 | |||
56 | /** |
||
57 | * @param array $options + show_instance=false Display the operator of the rule or its instance id |
||
58 | * |
||
59 | * @return array |
||
60 | */ |
||
61 | 40 | View Code Duplication | public function toArray(array $options=[]) |
89 | |||
90 | /** |
||
91 | */ |
||
92 | 2 | public function toString(array $options=[]) |
|
105 | |||
106 | /** |
||
107 | * By default, every atomic rule can have a solution by itself |
||
108 | * |
||
109 | * @return bool |
||
110 | */ |
||
111 | 13 | public function hasSolution(array $simplification_options=[]) |
|
115 | |||
116 | /**/ |
||
117 | } |
||
118 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.