Complex classes like Template 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 Template, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
4 | class Template extends Base { |
||
5 | protected $table = 'templates'; |
||
6 | /** |
||
7 | * Get filepath for template name based on current PAGE and ACTION |
||
8 | */ |
||
9 | public function getFullpath($name) { |
||
17 | |||
18 | /** |
||
19 | * Get all available themes |
||
20 | * Read theme folders from TEMPLATE_DIR |
||
21 | * |
||
22 | * @return array - list of available themes |
||
23 | */ |
||
24 | public function getThemes() { |
||
33 | |||
34 | /** |
||
35 | * Get all available designs |
||
36 | * Read css files from css/design folder |
||
37 | * |
||
38 | * @return array - list of available designs |
||
39 | */ |
||
40 | public function getDesigns() { |
||
50 | |||
51 | /** |
||
52 | * Cached getActiveTemplates method |
||
53 | * |
||
54 | * @see getActiveTemplates |
||
55 | */ |
||
56 | private static $active_templates; |
||
57 | public function cachedGetActiveTemplates() { |
||
63 | /** |
||
64 | * Return the all active templates as hash, |
||
65 | * where key is template and value is modified_at |
||
66 | * |
||
67 | * @return array - list of active templates |
||
68 | */ |
||
69 | public function getActiveTemplates() { |
||
85 | |||
86 | /** |
||
87 | * Return the content of specific template file |
||
88 | * |
||
89 | * @param $file - file of template related to TEMPLATE_DIR |
||
90 | * @return string - content of the template file |
||
91 | */ |
||
92 | public function getTemplateContent($file) { |
||
97 | |||
98 | /** |
||
99 | * Get all possible templates of specific theme |
||
100 | * |
||
101 | * @param $theme - name of the theme |
||
102 | * @return array - list of available templates of theme |
||
103 | */ |
||
104 | public function getTemplateFiles($theme) { |
||
118 | |||
119 | /** |
||
120 | * Get tree of all possible templates, where key is filename |
||
121 | * and value is whether array of subfiles if filename is directory |
||
122 | * or true, if filename is file |
||
123 | * |
||
124 | * @param $themes - optional, themes array |
||
125 | * @return array - tree of all templates |
||
126 | */ |
||
127 | public function getTemplatesTree($themes = null) { |
||
139 | |||
140 | private function _getTemplatesTreeRecursive($path) { |
||
160 | |||
161 | /** |
||
162 | * Return specific template from database |
||
163 | * |
||
164 | * @param $template - name (filepath) of the template |
||
165 | * @return array - result from database |
||
166 | */ |
||
167 | public function getEntry($template, $columns = "*") { |
||
178 | |||
179 | /** |
||
180 | * Return last modified time of specific template from database |
||
181 | * |
||
182 | * @param $template - name (filepath) of the template |
||
183 | * @return timestamp - last modified time of template |
||
184 | */ |
||
185 | public function getEntryMTime($template) { |
||
194 | |||
195 | /** |
||
196 | * Update template in database |
||
197 | * |
||
198 | * @param $template - name (filepath) of the template |
||
199 | * @param $content - content of the template |
||
200 | * @param $active - active flag for the template |
||
201 | **/ |
||
202 | public function updateEntry($template, $content, $active=0) { |
||
212 | } |
||
213 | |||
217 |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.