Complex classes like Component 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Component, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Component extends Blog |
||
11 | { |
||
12 | 22 | public function page() |
|
13 | { |
||
14 | 22 | $page = Page::html(); |
|
15 | 22 | $listings = $this->config('blog', 'listings'); |
|
16 | 22 | if ($id = $this->file($page->url['path'])) { |
|
17 | 8 | $params = array('id'=>$id, 'path'=>$page->url['path']); |
|
18 | 8 | $method = 'blog'; |
|
19 | 22 | } elseif ($route = $page->routes(array( |
|
20 | 14 | $listings, |
|
21 | 14 | $listings.'/[archives:path]/[i:year]?/[i:month]?/[i:day]?', |
|
22 | 14 | $listings.'/[authors|tags:path]/[:name]?', |
|
23 | 14 | $listings.'/[feed:path].xml', |
|
24 | 14 | '[**:category]', |
|
25 | 14 | ))) { |
|
26 | 14 | $params = $route['params']; |
|
27 | 14 | $method = (isset($params['path'])) ? $params['path'] : 'listings'; |
|
28 | 14 | if ($method == 'listings' && $search = $page->request->query->get('search')) { |
|
29 | 2 | $params['search'] = $search; |
|
30 | 2 | } |
|
31 | 14 | } |
|
32 | 22 | if (!isset($method) || (isset($params['category']) && !is_dir($this->folder.'content/'.$params['category']))) { |
|
33 | $template = false; |
||
34 | 22 | } elseif ($template = $this->$method($params)) { |
|
|
|||
35 | 22 | $template = array_combine(array('file', 'vars'), $template); |
|
36 | 22 | $template['default'] = $page->dir($page->dirname(__CLASS__), 'theme'); |
|
37 | 22 | if ($template['file'] == 'blog-feed.tpl') { |
|
38 | 1 | $type = 'xml'; |
|
39 | 1 | $xml = $this->theme->fetchSmarty($template); |
|
40 | 1 | if (preg_match('/<\/(?P<type>(rss|feed))>\s*$/', $xml, $matches)) { |
|
41 | $type = ($matches['type'] == 'rss') ? 'rss' : 'atom'; |
||
42 | } |
||
43 | 1 | $template = $page->send(Asset::dispatch($type, $xml)); |
|
44 | 1 | } |
|
45 | 22 | } |
|
46 | |||
47 | 22 | return $template; |
|
48 | } |
||
49 | |||
50 | 8 | private function blog($params, array $vars = array()) |
|
51 | { |
||
52 | 8 | $page = Page::html(); |
|
53 | 8 | extract($params); // 'id' and 'path' |
|
54 | 8 | $page->enforce($path); |
|
55 | 8 | $info = $this->info($id); |
|
56 | 8 | $this->theme->globalVars('blog', array('page' => ($info['page'] ? 'page' : 'post'))); |
|
57 | 8 | $vars['post'] = $info; |
|
58 | 8 | if ($search = $page->request->query->get('search')) { |
|
59 | $sitemap = new Sitemap(); |
||
60 | if ($docid = $sitemap->db->value('SELECT docid FROM sitemap WHERE path = ?', $path)) { |
||
61 | $words = $sitemap->words($search, $docid); |
||
62 | if (!empty($words)) { |
||
63 | $vars['search'] = $words; |
||
64 | } |
||
65 | } |
||
66 | unset($sitemap); |
||
67 | } |
||
68 | 8 | $vars['breadcrumbs'] = $this->breadcrumbs(); |
|
69 | 8 | foreach ($vars['post']['categories'] as $category) { |
|
70 | 4 | $vars['breadcrumbs'][$category['name']] = $category['url']; |
|
71 | 8 | } |
|
72 | 8 | $vars['breadcrumbs'][$vars['post']['title']] = $vars['post']['url']; |
|
73 | |||
74 | 8 | return array('blog-post.tpl', $vars); |
|
75 | } |
||
76 | |||
77 | 5 | private function listings($params, array $vars = array()) |
|
116 | |||
117 | 4 | private function archives($params, array $vars = array()) |
|
162 | |||
163 | 2 | private function authors($params, array $vars = array()) |
|
164 | { |
||
165 | 2 | $page = Page::html(); |
|
166 | 2 | extract($params); // 'path' and 'name'? |
|
167 | 2 | $this->theme->globalVars('blog', array('page' => 'authors')); |
|
168 | 2 | $vars['breadcrumbs'] = $this->breadcrumbs(array('Authors' => $path)); |
|
169 | 2 | if (!isset($name)) { // just authors, no posts |
|
170 | 1 | $page->enforce($page->url('blog/listings', $path)); |
|
171 | 2 | $vars['authors'] = $this->query('authors'); |
|
172 | |||
173 | 1 | return array('blog-authors.tpl', $vars); |
|
174 | } |
||
175 | 1 | $vars['author'] = $this->query('authors', $name); |
|
176 | 1 | if (empty($vars['author'])) { |
|
177 | 1 | $page->eject($page->url('blog/listings', $path)); |
|
178 | |||
179 | 1 | return false; |
|
180 | } |
||
181 | 1 | $vars['breadcrumbs'][$vars['author']['name']] = $page->url('blog/listings', $path, $vars['author']['path']); |
|
182 | 1 | $vars['listings']['count'] = $vars['author']['count']; |
|
183 | 1 | $vars['listings']['authors'] = $name; |
|
184 | |||
185 | 1 | return array('blog-listings.tpl', $vars); |
|
186 | } |
||
187 | |||
188 | 2 | private function tags($params, array $vars = array()) |
|
189 | { |
||
190 | 2 | $page = Page::html(); |
|
191 | 2 | extract($params); // 'path' and 'name'? |
|
192 | 2 | $this->theme->globalVars('blog', array('page' => 'tags')); |
|
193 | 2 | $vars['breadcrumbs'] = $this->breadcrumbs(array('Tags' => $path)); |
|
194 | 2 | if (!isset($name)) { // search all tags and get a frequency count |
|
195 | 1 | $page->enforce($page->url('blog/listings', $path)); |
|
196 | 1 | $vars['tags'] = $this->query('tags'); |
|
197 | |||
198 | 1 | return array('blog-tags.tpl', $vars); |
|
199 | } |
||
200 | 1 | $vars['tag'] = $this->query('tags', $name); |
|
201 | 1 | if (empty($vars['tag'])) { |
|
202 | 1 | $page->eject($page->url('blog/listings', $path)); |
|
203 | |||
204 | 1 | return false; |
|
205 | } |
||
206 | 1 | $vars['breadcrumbs'][$vars['tag']['name']] = $page->url('blog/listings', $path, $vars['tag']['path']); |
|
207 | 1 | $vars['listings']['count'] = $vars['tag']['count']; |
|
208 | 1 | $vars['listings']['tags'] = $vars['tag']['path']; |
|
209 | |||
210 | 1 | return array('blog-listings.tpl', $vars); |
|
211 | } |
||
212 | |||
213 | 1 | private function feed($params, array $vars = array()) |
|
220 | |||
221 | 21 | private function breadcrumbs(array $links = array()) |
|
233 | |||
234 | 3 | private function range($Y, $m = null, $d = null) |
|
249 | } |
||
250 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: