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 |
||
6 | class Nested |
||
7 | { |
||
8 | /** |
||
9 | * @var ArrayImitator |
||
10 | */ |
||
11 | protected $arrayContainer; |
||
12 | |||
13 | /** |
||
14 | * Nested constructor. |
||
15 | * |
||
16 | * @param ArrayImitator $arrayObject |
||
17 | */ |
||
18 | 4 | public function __construct($arrayObject) |
|
22 | |||
23 | /** |
||
24 | * Check value for this keys exist. |
||
25 | * |
||
26 | * @param $keyString |
||
27 | * @param string $separator |
||
28 | * @return bool |
||
29 | */ |
||
30 | 4 | public function has($keyString, $separator = '.') |
|
42 | |||
43 | /** |
||
44 | * Return value from nested array or default value. |
||
45 | * |
||
46 | * @param $keyString |
||
47 | * @param string $separator |
||
48 | * @return array|mixed |
||
49 | */ |
||
50 | 4 | public function get($keyString, $separator = '.') |
|
62 | |||
63 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.