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 | View Code Duplication | class HeaderKey |
|
8 | { |
||
9 | use EnsureIsStringTrait; |
||
10 | use EnsureIsNotEmptyTrait; |
||
11 | |||
12 | const KEY_IS_NOT_A_STRING = 1; |
||
13 | const KEY_IS_EMPTY = 2; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | private $key; |
||
19 | |||
20 | /** |
||
21 | * @param string $key |
||
22 | */ |
||
23 | public function __construct($key) |
||
29 | |||
30 | /** |
||
31 | * @return string |
||
32 | */ |
||
33 | public function asString() |
||
37 | |||
38 | /** |
||
39 | * @param mixed $key |
||
40 | */ |
||
41 | private function ensureKey($key) |
||
46 | } |
||
47 |