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 |
||
15 | class Urn implements UrnInterface |
||
16 | { |
||
17 | /** |
||
18 | * Namespace identifier regex pattern. |
||
19 | * |
||
20 | * @const string |
||
21 | */ |
||
22 | const NID_PATTERN = '/^[A-Za-z0-9-][A-Za-z0-9-]{0,31}$/'; |
||
23 | |||
24 | /** |
||
25 | * Namespace specific string regex pattern. |
||
26 | */ |
||
27 | const NSS_PATTERN = '/^[A-Za-z0-9()+,\-.:=@;$_!*\'%\/?#]+$/'; |
||
28 | |||
29 | /** |
||
30 | * The namespace identifier. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $namespaceIdentifier; |
||
35 | |||
36 | /** |
||
37 | * The namespace specific string. |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $namespaceSpecificString; |
||
42 | |||
43 | /** |
||
44 | * The protected constructor. |
||
45 | */ |
||
46 | 4 | protected function __construct() |
|
50 | |||
51 | /** |
||
52 | * Create URN object from array. |
||
53 | * |
||
54 | * @param array $array |
||
55 | * |
||
56 | * @return static |
||
57 | * @throws \InvalidArgumentException |
||
58 | */ |
||
59 | 4 | public static function fromArray(array $array) |
|
76 | |||
77 | /** |
||
78 | * Create URN object from string. |
||
79 | * |
||
80 | * @param string $string |
||
81 | * |
||
82 | * @return static |
||
83 | * @throws \InvalidArgumentException |
||
84 | */ |
||
85 | 6 | public static function fromString($string) |
|
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | 4 | public function getNamespaceIdentifier() |
|
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | 4 | public function getNamespaceSpecificString() |
|
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | 4 | View Code Duplication | public function withNamespaceIdentifier($nid) |
136 | |||
137 | /** |
||
138 | * {@inheritdoc} |
||
139 | */ |
||
140 | 4 | View Code Duplication | public function withNamespaceSpecificString($nss) |
151 | |||
152 | /** |
||
153 | * {@inheritdoc} |
||
154 | */ |
||
155 | 4 | public function __toString() |
|
159 | |||
160 | /** |
||
161 | * Convert URN into a string representation. |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | 4 | public function toString() |
|
173 | } |
||
174 |
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.