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 declare(strict_types=1); |
||
25 | class ObjectMap implements ObjectMapInterface |
||
26 | { |
||
27 | const WEAK_KEY = 1 << 0; |
||
28 | const WEAK_VALUE = 1 << 1; |
||
29 | const WEAK_KEY_VALUE = self::WEAK_KEY | self::WEAK_VALUE; |
||
30 | |||
31 | protected $behavior = 0; |
||
32 | |||
33 | /** |
||
34 | * @var Bucket[] |
||
35 | */ |
||
36 | protected $keys = []; |
||
37 | |||
38 | /** |
||
39 | * @param int $behavior |
||
40 | */ |
||
41 | 1 | public function __construct(int $behavior = 0) |
|
45 | |||
46 | /** |
||
47 | * {@inheritdoc} |
||
48 | */ |
||
49 | 1 | public function put($key, $value) |
|
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | 1 | public function get($key) |
|
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | public function has($key): bool |
||
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | public function remove($key) |
||
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | */ |
||
107 | public function count() |
||
111 | |||
112 | /** |
||
113 | * {@inheritdoc} |
||
114 | */ |
||
115 | public function clear() |
||
119 | |||
120 | /** |
||
121 | * @param $value |
||
122 | * |
||
123 | * @return string |
||
124 | */ |
||
125 | 1 | protected function getHash($value) |
|
129 | |||
130 | /** |
||
131 | * @param string $hash |
||
132 | * |
||
133 | * @return void |
||
134 | */ |
||
135 | protected function doRemove(string $hash) |
||
139 | |||
140 | /** |
||
141 | * @param object $key |
||
142 | * @param object $value |
||
143 | * @param string $hash |
||
144 | * |
||
145 | * @return Bucket |
||
146 | */ |
||
147 | 1 | protected function createBucket($key, $value, string $hash) |
|
163 | } |
||
164 | |||
171 |
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.