Total Complexity | 67 |
Total Lines | 370 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Complex classes like Header often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Header, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Header { |
||
12 | public $nav_highlights; |
||
13 | |||
14 | public $data; |
||
15 | |||
16 | private $keywords_title; |
||
17 | |||
18 | public function __construct() { |
||
19 | $this->data = []; |
||
20 | |||
21 | $this->get_page_url(); |
||
22 | $this->get_page_title(); |
||
23 | $this->get_page_keywords(); |
||
24 | $this->get_header_links(); |
||
25 | $this->get_next_prev_links(); |
||
26 | $this->get_rss_link(); |
||
27 | $this->get_menu_highlights(); |
||
28 | $this->get_top_and_bottom_links(); |
||
29 | $this->add_robots_metadata(); |
||
30 | } |
||
31 | |||
32 | private function add_robots_metadata() { |
||
33 | global $DATA, $this_page; |
||
34 | |||
35 | # Robots header |
||
36 | if (DEVSITE) { |
||
37 | $this->data['robots'] = 'noindex,nofollow'; |
||
38 | } elseif ($robots = $DATA->page_metadata($this_page, 'robots')) { |
||
39 | $this->data['robots'] = $robots; |
||
40 | } |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Work out what the page url |
||
45 | */ |
||
46 | private function get_page_url() { |
||
47 | $protocol = 'https://'; |
||
48 | if (DEVSITE) { |
||
49 | $protocol = 'http://'; |
||
50 | } |
||
51 | $url = $protocol . DOMAIN; |
||
52 | if (array_key_exists('REQUEST_URI', $_SERVER)) { |
||
53 | $url = $url . $_SERVER['REQUEST_URI']; |
||
54 | } |
||
55 | $this->data['page_url'] = $url; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Work out what the page title and the keywords for it should be |
||
60 | */ |
||
61 | private function get_page_title() { |
||
62 | global $DATA, $this_page; |
||
63 | |||
64 | $this->data['page_title'] = ''; |
||
65 | $sitetitle = $DATA->page_metadata($this_page, "sitetitle"); |
||
66 | $og_title = ''; |
||
67 | $this->keywords_title = ''; |
||
68 | |||
69 | if ($this_page == 'overview') { |
||
70 | $this->data['page_title'] = $sitetitle . ': ' . $DATA->page_metadata($this_page, "title"); |
||
71 | |||
72 | } else { |
||
73 | |||
74 | if ($page_title = $DATA->page_metadata($this_page, "title")) { |
||
75 | $this->data['page_title'] = $page_title; |
||
76 | } |
||
77 | // We'll put this in the meta keywords tag. |
||
78 | $this->keywords_title = $this->data['page_title']; |
||
79 | |||
80 | $parent_page = $DATA->page_metadata($this_page, 'parent'); |
||
81 | if ($parent_title = $DATA->page_metadata($parent_page, 'title')) { |
||
82 | if ($this->data['page_title']) { |
||
83 | $this->data['page_title'] .= ': '; |
||
84 | } |
||
85 | $this->data['page_title'] .= $parent_title; |
||
86 | } |
||
87 | |||
88 | if ($this->data['page_title'] == '') { |
||
89 | $this->data['page_title'] = $sitetitle; |
||
90 | } else { |
||
91 | $og_title = $this->data['page_title']; |
||
92 | $this->data['page_title'] .= ' - ' . $sitetitle; |
||
93 | } |
||
94 | } |
||
95 | |||
96 | # for overriding the OpenGraph image |
||
97 | $this->data['og_image'] = ''; |
||
98 | |||
99 | // Pages can specify a custom OpenGraph title if they want, otherwise |
||
100 | // we use the page title without the trailing " - $sitetitle". |
||
101 | $this->data['og_title'] = $DATA->page_metadata($this_page, "og_title") ?: $og_title; |
||
102 | |||
103 | } |
||
104 | |||
105 | private function get_page_keywords() { |
||
120 | } |
||
121 | } |
||
122 | |||
123 | private function get_header_links() { |
||
124 | global $this_page; |
||
125 | |||
126 | $this->data['header_links'] = []; |
||
127 | if ($this_page != 'overview') { |
||
128 | |||
129 | $URL = new \MySociety\TheyWorkForYou\Url('overview'); |
||
130 | |||
131 | $this->data['header_links'][] = [ |
||
132 | 'rel' => 'start', |
||
133 | 'title' => 'Home', |
||
134 | 'href' => $URL->generate(), |
||
135 | ]; |
||
136 | |||
137 | } |
||
138 | } |
||
139 | |||
140 | private function generate_next_prev_link($nextprev, $linktype) { |
||
141 | $link = null; |
||
142 | if (isset($nextprev[$linktype]) && isset($nextprev[$linktype]['url'])) { |
||
143 | |||
144 | if (isset($nextprev[$linktype]['body'])) { |
||
145 | $linktitle = _htmlentities(trim_characters($nextprev[$linktype]['body'], 0, 40)); |
||
146 | if (isset($nextprev[$linktype]['speaker']) && |
||
147 | count($nextprev[$linktype]['speaker']) > 0) { |
||
148 | $linktitle = $nextprev[$linktype]['speaker']['name'] . ': ' . $linktitle; |
||
149 | } |
||
150 | |||
151 | } elseif (isset($nextprev[$linktype]['hdate'])) { |
||
152 | $linktitle = format_date($nextprev[$linktype]['hdate'], SHORTDATEFORMAT); |
||
153 | } |
||
154 | |||
155 | $link = [ |
||
156 | 'rel' => $linktype, |
||
157 | 'title' => $linktitle, |
||
|
|||
158 | 'href' => $nextprev[$linktype]['url'], |
||
159 | ]; |
||
160 | } |
||
161 | |||
162 | return $link; |
||
163 | } |
||
164 | |||
165 | private function get_next_prev_links() { |
||
166 | global $DATA, $this_page; |
||
167 | |||
168 | $nextprev = $DATA->page_metadata($this_page, "nextprev"); |
||
169 | |||
170 | if ($nextprev) { |
||
171 | // Four different kinds of back/forth links we might build. |
||
172 | $links = ["first", "prev", "up", "next", "last"]; |
||
173 | |||
174 | foreach ($links as $type) { |
||
175 | if ($link = $this->generate_next_prev_link($nextprev, $type)) { |
||
176 | |||
177 | $this->data['header_links'][] = $link; |
||
178 | } |
||
179 | } |
||
180 | } |
||
181 | } |
||
182 | |||
183 | private function get_rss_link() { |
||
184 | global $DATA, $this_page; |
||
185 | |||
186 | if ($DATA->page_metadata($this_page, 'rss')) { |
||
187 | // If this page has an RSS feed set. |
||
188 | $this->data['page_rss_url'] = 'https://' . DOMAIN . WEBPATH . $DATA->page_metadata($this_page, 'rss'); |
||
189 | } |
||
190 | } |
||
191 | |||
192 | // We work out which of the items in the top and bottom menus |
||
193 | // are highlighted |
||
194 | private function get_menu_highlights() { |
||
195 | global $DATA, $this_page; |
||
196 | |||
197 | $parent = $DATA->page_metadata($this_page, 'parent'); |
||
198 | |||
199 | if (!$parent) { |
||
200 | |||
201 | $top_highlight = $this_page; |
||
202 | $bottom_highlight = ''; |
||
203 | |||
204 | $selected_top_link = $DATA->page_metadata('hansard', 'menu'); |
||
205 | $url = new \MySociety\TheyWorkForYou\Url('hansard'); |
||
206 | $selected_top_link['link'] = $url->generate(); |
||
207 | |||
208 | } else { |
||
209 | |||
210 | $parents = [$parent]; |
||
211 | $p = $parent; |
||
212 | while ($p) { |
||
213 | $p = $DATA->page_metadata($p, 'parent'); |
||
214 | if ($p) { |
||
215 | $parents[] = $p; |
||
216 | } |
||
217 | } |
||
218 | |||
219 | $top_highlight = array_pop($parents); |
||
220 | if (!$parents) { |
||
221 | // No grandparent - this page's parent is in the top menu. |
||
222 | // We're on one of the pages linked to by the bottom menu. |
||
223 | // So highlight it and its parent. |
||
224 | $bottom_highlight = $this_page; |
||
225 | } else { |
||
226 | // This page is not in either menu. So highlight its parent |
||
227 | // (in the bottom menu) and its grandparent (in the top). |
||
228 | $bottom_highlight = array_pop($parents); |
||
229 | } |
||
230 | |||
231 | $selected_top_link = $DATA->page_metadata($top_highlight, 'menu'); |
||
232 | if (!$selected_top_link) { |
||
233 | # Just in case something's gone wrong |
||
234 | $selected_top_link = $DATA->page_metadata('hansard', 'menu'); |
||
235 | } |
||
236 | $url = new \MySociety\TheyWorkForYou\Url($top_highlight); |
||
237 | $selected_top_link['link'] = $url->generate(); |
||
238 | |||
239 | } |
||
240 | |||
241 | if ($top_highlight == 'hansard') { |
||
242 | $section = 'uk'; |
||
243 | $selected_top_link['text'] = 'UK'; |
||
244 | } elseif ($top_highlight == 'ni_home') { |
||
245 | $section = 'ni'; |
||
246 | $selected_top_link['text'] = 'Northern Ireland'; |
||
247 | } elseif ($top_highlight == 'sp_home') { |
||
248 | $section = 'scotland'; |
||
249 | $selected_top_link['text'] = 'Scotland'; |
||
250 | } elseif ($top_highlight == 'wales_home') { |
||
251 | $section = 'wales'; |
||
252 | $selected_top_link['text'] = gettext('Wales'); |
||
253 | } elseif ($top_highlight == 'london_home') { |
||
254 | $section = 'london'; |
||
255 | $selected_top_link['text'] = 'London Assembly'; |
||
256 | } else { |
||
257 | $section = ''; |
||
258 | } |
||
259 | |||
260 | // if we're searching within a parliament, put this in the top bar |
||
261 | if ($this_page == "search") { |
||
262 | if ($section = get_http_var('section')) { |
||
263 | if ($section == 'scotland') { |
||
264 | $selected_top_link['text'] = 'Scotland'; |
||
265 | } elseif ($section == 'ni') { |
||
266 | $selected_top_link['text'] = 'Northern Ireland'; |
||
267 | } elseif ($section == 'wales') { |
||
268 | $selected_top_link['text'] = gettext('Wales'); |
||
269 | } elseif ($section == 'london') { |
||
270 | $selected_top_link['text'] = 'London Assembly'; |
||
271 | } else { |
||
272 | $selected_top_link['text'] = 'UK'; |
||
273 | } |
||
274 | } |
||
275 | } |
||
276 | |||
277 | // for the alerts page, put the most recent membership's house |
||
278 | // in the top bar |
||
279 | if ($this_page == "alert") { |
||
280 | if ($pid = get_http_var('pid')) { |
||
281 | $person = new \MySociety\TheyWorkForYou\Member(['person_id' => $pid]); |
||
282 | $membership = $person->getMostRecentMembership(); |
||
283 | $parliament = $membership['house']; |
||
284 | if ($parliament == 'ni') { |
||
285 | $selected_top_link['text'] = 'Northern Ireland'; |
||
286 | } elseif ($parliament == HOUSE_TYPE_SCOTLAND) { |
||
287 | $selected_top_link['text'] = 'Scotland'; |
||
288 | } elseif ($parliament == HOUSE_TYPE_WALES) { |
||
289 | $selected_top_link['text'] = gettext('Wales'); |
||
290 | } elseif ($parliament == HOUSE_TYPE_LONDON_ASSEMBLY) { |
||
291 | $selected_top_link['text'] = 'London Assembly'; |
||
292 | } |
||
293 | } |
||
294 | } |
||
295 | |||
296 | $this->nav_highlights = [ |
||
297 | 'top' => $top_highlight, |
||
298 | 'bottom' => $bottom_highlight, |
||
299 | 'top_selected' => $selected_top_link, |
||
300 | 'section' => $section, |
||
301 | ]; |
||
302 | } |
||
303 | |||
304 | private function get_top_and_bottom_links() { |
||
381 | } |
||
382 | |||
383 | } |
||
384 |