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 |
||
14 | class SearchResultUrlParser |
||
15 | { |
||
16 | /** |
||
17 | * @var Http |
||
18 | */ |
||
19 | protected $url; |
||
20 | |||
21 | /** |
||
22 | * @var int |
||
23 | */ |
||
24 | protected $nbPages; |
||
25 | |||
26 | /** |
||
27 | * @param string $url |
||
28 | * @param int $nbPages |
||
29 | */ |
||
30 | 44 | public function __construct($url, $nbPages = 1) |
|
36 | |||
37 | /** |
||
38 | * @return Http |
||
39 | */ |
||
40 | 36 | public function current() |
|
41 | { |
||
42 | // set the default page to 1 unless it is set |
||
43 | 36 | if (!$this->url->query->hasKey('o')) { |
|
44 | 24 | $newUrl = new MergeQuery('o=1'); |
|
45 | 24 | $this->url = $newUrl($this->url); |
|
46 | } |
||
47 | |||
48 | // remove th (thumb image) |
||
49 | 36 | $newUrl = new RemoveQueryKeys(['th']); |
|
50 | 36 | $this->url = $newUrl($this->url); |
|
51 | |||
52 | 36 | return $this->url; |
|
53 | } |
||
54 | |||
55 | /** |
||
56 | * Return the next page URL or null if non. |
||
57 | * |
||
58 | * @return UriInterface |
||
59 | */ |
||
60 | 10 | View Code Duplication | public function next() |
68 | |||
69 | /** |
||
70 | * Return the previous page URL or null if none |
||
71 | * |
||
72 | * @return UriInterface |
||
73 | */ |
||
74 | 10 | View Code Duplication | public function previous() |
75 | { |
||
76 | 10 | if ((int) $this->current()->query->getValue('o') === 1) { |
|
77 | 4 | return null; |
|
78 | } |
||
79 | |||
80 | 8 | return $this->getIndexUrl(-1); |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param int $index |
||
85 | * |
||
86 | * @return UriInterface |
||
87 | */ |
||
88 | 10 | public function getIndexUrl($index) |
|
89 | { |
||
90 | 10 | $oParam = (int) $this->current()->query->getValue('o') + $index; |
|
91 | 10 | $newQuery = new MergeQuery('o='.$oParam); |
|
92 | |||
93 | 10 | return $newQuery($this->url); |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * Return a meta array containing the nav links and the page |
||
98 | * |
||
99 | * @return array |
||
100 | */ |
||
101 | 6 | public function getNav() |
|
102 | { |
||
103 | return [ |
||
104 | 6 | 'page' => (int) $this->current()->query->getValue('o'), |
|
105 | 'links' => [ |
||
106 | 6 | 'current' => (string) $this->current(), |
|
107 | 6 | 'previous' => (string) $this->previous(), |
|
108 | 6 | 'next' => (string) $this->next(), |
|
109 | ] |
||
110 | ]; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Return the category |
||
115 | * |
||
116 | * @return string |
||
117 | */ |
||
118 | 10 | public function getCategory() |
|
128 | |||
129 | /** |
||
130 | * Return the search area |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | 8 | public function getSearchArea() |
|
146 | |||
147 | /** |
||
148 | * Return one or more location (separated by a comma) or null if none |
||
149 | * |
||
150 | * @return null |
||
151 | */ |
||
152 | 10 | public function getLocation() |
|
156 | |||
157 | /** |
||
158 | * Return the type of the ads |
||
159 | * |
||
160 | * @return string all|part|pro |
||
161 | */ |
||
162 | 8 | public function getType() |
|
163 | { |
||
164 | 8 | switch ($this->current()->query->getValue('f')) { |
|
165 | 8 | case 'p': |
|
166 | 2 | return 'part'; |
|
167 | 8 | case 'c': |
|
168 | 2 | return 'pro'; |
|
169 | } |
||
170 | |||
171 | 8 | return 'all'; |
|
172 | } |
||
173 | |||
174 | /** |
||
175 | * Return the sorting type |
||
176 | * |
||
177 | * @return string price|date |
||
178 | */ |
||
179 | 8 | public function getSortType() |
|
187 | } |
||
188 |