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 |
||
14 | final class Pair implements \JsonSerializable |
||
15 | { |
||
16 | /** |
||
17 | * @var mixed The pair's key |
||
18 | */ |
||
19 | public $key; |
||
20 | |||
21 | /** |
||
22 | * @var mixed The pair's value |
||
23 | */ |
||
24 | public $value; |
||
25 | |||
26 | /** |
||
27 | * Creates a new instance. |
||
28 | * |
||
29 | * @param mixed $key |
||
30 | * @param mixed $value |
||
31 | */ |
||
32 | public function __construct($key = null, $value = null) |
||
37 | |||
38 | /** |
||
39 | * |
||
40 | * @param mixed $name |
||
41 | * |
||
42 | * @return mixed|null |
||
|
|||
43 | */ |
||
44 | public function __isset($name) |
||
51 | |||
52 | /** |
||
53 | * This allows unset($pair->key) to not completely remove the property, |
||
54 | * but be set to null instead. |
||
55 | * |
||
56 | * @param mixed $name |
||
57 | * |
||
58 | * @return mixed|null |
||
59 | */ |
||
60 | View Code Duplication | public function __unset($name) |
|
68 | |||
69 | /** |
||
70 | * @param mixed $name |
||
71 | * |
||
72 | * @return mixed|null |
||
73 | */ |
||
74 | public function &__get($name) |
||
81 | |||
82 | /** |
||
83 | * @param mixed $name |
||
84 | * @param mixed $value |
||
85 | * |
||
86 | * @return mixed|null |
||
87 | */ |
||
88 | View Code Duplication | public function __set($name, $value) |
|
96 | |||
97 | /** |
||
98 | * Returns a copy of the Pair |
||
99 | */ |
||
100 | public function copy(): self |
||
104 | |||
105 | /** |
||
106 | * Returns a representation to be used for var_dump and print_r. |
||
107 | * |
||
108 | * @return array |
||
109 | */ |
||
110 | public function __debugInfo() |
||
114 | |||
115 | /** |
||
116 | * @return array |
||
117 | */ |
||
118 | public function toArray(): array |
||
125 | |||
126 | /** |
||
127 | * @inheritDoc |
||
128 | */ |
||
129 | public function jsonSerialize() |
||
133 | |||
134 | /** |
||
135 | * Returns a string representation of the pair. |
||
136 | */ |
||
137 | public function __toString() |
||
141 | } |
||
142 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.