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 |
||
10 | class StructMetadata extends PropertyMetadata |
||
11 | { |
||
12 | /** |
||
13 | * Structure properties |
||
14 | * |
||
15 | * @var array |
||
16 | */ |
||
17 | private $properties = []; |
||
18 | |||
19 | /** |
||
20 | * Class constructor |
||
21 | * |
||
22 | * @param string $name OPTIONAL Property name |
||
23 | * @param string $class OPTIONAL Class name for property object |
||
24 | * @param array $config OPTIONAL Configuration options for property object |
||
25 | * @param array $properties OPTIONAL Structure properties |
||
26 | * @throws \InvalidArgumentException |
||
27 | */ |
||
28 | 199 | public function __construct($name = null, $class = null, $config = null, $properties = null) |
|
35 | |||
36 | /** |
||
37 | * Check if structure property with given name is available |
||
38 | * |
||
39 | * @param string $name Property name |
||
40 | * @return boolean |
||
41 | */ |
||
42 | 2 | public function hasProperty($name) |
|
46 | |||
47 | /** |
||
48 | * Get metadata for structure property with given name |
||
49 | * |
||
50 | * @param string $name Property name |
||
51 | * @throws Exception |
||
52 | * @return MetadataInterface |
||
53 | */ |
||
54 | 40 | public function getProperty($name) |
|
61 | |||
62 | /** |
||
63 | * Add given metadata as structure property |
||
64 | * |
||
65 | * @param MetadataInterface $metadata Property metadata |
||
66 | * @return $this |
||
67 | */ |
||
68 | 185 | public function addProperty(MetadataInterface $metadata) |
|
74 | |||
75 | /** |
||
76 | * Remove structure property with given name |
||
77 | * |
||
78 | * @param string $name Property name |
||
79 | * @return $this |
||
80 | */ |
||
81 | 39 | public function removeProperty($name) |
|
87 | |||
88 | /** |
||
89 | * Clear all registered structure properties |
||
90 | * |
||
91 | * @return $this |
||
92 | */ |
||
93 | 20 | public function clearProperties() |
|
99 | |||
100 | /** |
||
101 | * Defined by Serializable interface |
||
102 | * |
||
103 | * @return string |
||
104 | */ |
||
105 | 17 | View Code Duplication | public function serialize() |
114 | |||
115 | /** |
||
116 | * Get metadata for all registered structure properties |
||
117 | * |
||
118 | * @return array |
||
119 | */ |
||
120 | 186 | public function getProperties() |
|
124 | |||
125 | /** |
||
126 | * Set structure properties metadata |
||
127 | * |
||
128 | * @param array $properties |
||
129 | * @return $this |
||
130 | */ |
||
131 | 19 | public function setProperties(array $properties) |
|
139 | |||
140 | /** |
||
141 | * Defined by Serializable interface |
||
142 | * |
||
143 | * @param string $serialized |
||
144 | * @return void |
||
145 | * @throws \InvalidArgumentException |
||
146 | */ |
||
147 | 18 | public function unserialize($serialized) |
|
166 | |||
167 | /** |
||
168 | * {@inheritdoc} |
||
169 | */ |
||
170 | 14 | public function toArray() |
|
185 | |||
186 | /** |
||
187 | * {@inheritdoc} |
||
188 | */ |
||
189 | 94 | public function getHash() |
|
205 | } |
||
206 |
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.