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 |
||
5 | class TypeAdapter |
||
6 | { |
||
7 | /** |
||
8 | * @var Rule[] |
||
9 | */ |
||
10 | private $rules = []; |
||
11 | |||
12 | /** |
||
13 | * @param Rule |
||
14 | * |
||
15 | * @return $this |
||
16 | */ |
||
17 | public function register(Rule $rule) |
||
22 | |||
23 | /** |
||
24 | * @param string $class |
||
25 | * |
||
26 | * @return bool |
||
27 | */ |
||
28 | public function isClassRegistered(string $class): bool |
||
32 | |||
33 | /** |
||
34 | * @param mixed $value Array |
||
35 | * |
||
36 | * @return mixed Instance of registered class |
||
37 | */ |
||
38 | public function toObject($value) |
||
52 | |||
53 | /** |
||
54 | * @param mixed $value Instance of registered class |
||
55 | * |
||
56 | * @return array |
||
57 | */ |
||
58 | public function toArray($value) |
||
73 | |||
74 | /** |
||
75 | * @return bool |
||
76 | */ |
||
77 | public function isEmpty(): bool |
||
81 | |||
82 | /** |
||
83 | * @param Rule $expected Transform rule |
||
84 | * @param array $value Raw unknown value |
||
85 | * |
||
86 | * @return bool |
||
87 | */ |
||
88 | private function isMatch(Rule $expected, array $value): bool |
||
92 | |||
93 | /** |
||
94 | * @param string $class |
||
95 | * @param array $data |
||
96 | * |
||
97 | * @return object |
||
98 | */ |
||
99 | private function createInstance(string $class, array $data) |
||
115 | |||
116 | /** |
||
117 | * @param string $class |
||
118 | * @param object $object |
||
119 | * |
||
120 | * @return array |
||
121 | */ |
||
122 | private function createMap(string $class, $object) |
||
136 | } |
||
137 |