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 |
||
13 | class LinksObject implements ObjectInterface { |
||
|
|||
14 | use AtMemberManager; |
||
15 | |||
16 | /** @var array with string|LinkObject */ |
||
17 | protected $links = []; |
||
18 | |||
19 | /** |
||
20 | * human api |
||
21 | */ |
||
22 | |||
23 | /** |
||
24 | * @param array $links key-value with values being href strings |
||
25 | * @return LinksObject |
||
26 | */ |
||
27 | public static function fromArray(array $links) { |
||
36 | |||
37 | /** |
||
38 | * @param object $links |
||
39 | * @return LinksObject |
||
40 | */ |
||
41 | public static function fromObject($links) { |
||
46 | |||
47 | /** |
||
48 | * @param string $key |
||
49 | * @param string $href |
||
50 | * @param array $meta optional, if given a LinkObject is added, otherwise a link string is added |
||
51 | */ |
||
52 | public function add($key, $href, array $meta=[]) { |
||
60 | |||
61 | /** |
||
62 | * appends a link to an array of links under a specific key |
||
63 | * |
||
64 | * @see LinksArray for use cases |
||
65 | * |
||
66 | * @param string $key |
||
67 | * @param string $href |
||
68 | * @param array $meta optional, if given a LinkObject is added, otherwise a link string is added |
||
69 | * |
||
70 | * @throws DuplicateException if another link is already using that $key but is not an array |
||
71 | */ |
||
72 | View Code Duplication | public function append($key, $href, array $meta=[]) { |
|
84 | |||
85 | /** |
||
86 | * spec api |
||
87 | */ |
||
88 | |||
89 | /** |
||
90 | * @param string $key |
||
91 | * @param string $href |
||
92 | * |
||
93 | * @throws DuplicateException if another link is already using that $key |
||
94 | */ |
||
95 | View Code Duplication | public function addLinkString($key, $href) { |
|
104 | |||
105 | /** |
||
106 | * @param string $key |
||
107 | * @param LinkObject $linkObject |
||
108 | * |
||
109 | * @throws DuplicateException if another link is already using that $key |
||
110 | */ |
||
111 | View Code Duplication | public function addLinkObject($key, LinkObject $linkObject) { |
|
120 | |||
121 | /** |
||
122 | * @param string $key |
||
123 | * @param LinksArray $linksArray |
||
124 | * |
||
125 | * @throws DuplicateException if another link is already using that $key |
||
126 | */ |
||
127 | View Code Duplication | public function addLinksArray($key, LinksArray $linksArray) { |
|
136 | |||
137 | /** |
||
138 | * @param string $key |
||
139 | * @param LinkObject $linkObject |
||
140 | * |
||
141 | * @throws DuplicateException if another link is already using that $key but is not an array |
||
142 | */ |
||
143 | View Code Duplication | public function appendLinkObject($key, LinkObject $linkObject) { |
|
155 | |||
156 | /** |
||
157 | * ObjectInterface |
||
158 | */ |
||
159 | |||
160 | /** |
||
161 | * @inheritDoc |
||
162 | */ |
||
163 | public function isEmpty() { |
||
166 | |||
167 | /** |
||
168 | * @inheritDoc |
||
169 | */ |
||
170 | public function toArray() { |
||
190 | } |
||
191 |