Complex classes like Theme 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 Theme, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class Theme |
||
35 | { |
||
36 | public $theme = ''; |
||
37 | public $theme_info = []; |
||
|
|||
38 | |||
39 | /** |
||
40 | * Constructor, or what ;). |
||
41 | * |
||
42 | * @param string $theme Name of the Theme. |
||
43 | * |
||
44 | * @return \Koch\View\Helper\Theme |
||
45 | */ |
||
46 | public function __construct($theme) |
||
54 | |||
55 | /** |
||
56 | * @param string $theme |
||
57 | */ |
||
58 | public function setThemeName($theme) |
||
66 | |||
67 | /** |
||
68 | * Getter for the "theme_info.xml" file of the currently activated theme. |
||
69 | * |
||
70 | * @return string Filepath to "theme_info.xml" of the currently activated theme. |
||
71 | */ |
||
72 | public function getCurrentThemeInfoFile() |
||
85 | |||
86 | /** |
||
87 | * Looks for the requested theme in the frontend and backend theme folder |
||
88 | * and returns the theme path. |
||
89 | * |
||
90 | * @param string $theme Theme name. |
||
91 | * |
||
92 | * @return string Path to theme. |
||
93 | */ |
||
94 | public function getPath($theme = null) |
||
95 | { |
||
96 | if ($theme === null) { |
||
97 | $theme = $this->getName(); |
||
98 | } |
||
99 | |||
100 | if (is_dir(APPLICATION_PATH . 'Themes') === false) { |
||
101 | throw new \RuntimeException('The application themes folder was not found.'); |
||
102 | } |
||
103 | |||
104 | $frontend = APPLICATION_PATH . 'Themes/frontend/' . $theme . DIRECTORY_SEPARATOR; |
||
105 | if (is_dir($frontend)) { |
||
106 | return $frontend; |
||
107 | } |
||
108 | |||
109 | $backend = APPLICATION_PATH . 'Themes/backend/' . $theme . DIRECTORY_SEPARATOR; |
||
110 | if (is_dir($backend)) { |
||
111 | return $backend; |
||
112 | } |
||
113 | |||
114 | return false; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Looks for the requested theme in the frontend and backend theme folder |
||
119 | * and returns the web path of the theme. |
||
120 | * |
||
121 | * @param string $theme Theme name. |
||
122 | * |
||
123 | * @return string Webpath of theme (for usage in templates). |
||
124 | */ |
||
125 | public function getWebPath($theme = null) |
||
126 | { |
||
127 | if ($theme === null) { |
||
128 | $theme = $this->getName(); |
||
129 | } |
||
130 | |||
131 | // check absolute, return www |
||
132 | if (is_dir(APPLICATION_PATH . 'Themes/frontend/' . $theme)) { |
||
133 | return WWW_ROOT_THEMES_FRONTEND . $theme . '/'; |
||
134 | } |
||
135 | |||
136 | // check absolute, return www |
||
137 | if (is_dir(APPLICATION_PATH . 'Themes/backend/' . $theme)) { |
||
138 | return WWW_ROOT_THEMES_BACKEND . $theme . '/'; |
||
139 | } |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Returns "theme_info.xml" for the requested theme. |
||
144 | * |
||
145 | * @param string $theme Theme name. |
||
146 | * |
||
147 | * @return string File path to "theme_info.xml" file. |
||
148 | * |
||
149 | * @throws \Koch\Exception\Exception |
||
150 | */ |
||
151 | public function getThemeInfoFile($theme) |
||
161 | |||
162 | /** |
||
163 | * Returns Theme Infos as array. |
||
164 | * |
||
165 | * @param string $theme Name of the Theme. |
||
166 | * |
||
167 | * @return array Theme_Info.xml as Array. |
||
168 | */ |
||
169 | public function getInfoArray($theme = null) |
||
170 | { |
||
171 | $file = $this->getThemeInfoFile($theme); |
||
172 | |||
173 | $array = \Koch\Config\Adapter\XML::read($file); |
||
174 | |||
175 | // when setting array as object property remove the inner theme array |
||
176 | $this->theme_info = $array['theme']; |
||
177 | |||
178 | return $this->theme_info; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * -------------------------------------------------------------------------------------------- |
||
183 | * GETTERS |
||
184 | * --------------------------------------------------------------------------------------------. |
||
185 | */ |
||
186 | |||
187 | /** |
||
188 | * Gets shortname or folder name. |
||
189 | * |
||
190 | * @return string short name / folder name. |
||
191 | */ |
||
192 | public function getName() |
||
196 | |||
197 | public function getFullName() |
||
201 | |||
202 | public function getAuthor() |
||
206 | |||
207 | public function getVersion() |
||
211 | |||
212 | public function getRequiredVersion() |
||
216 | |||
217 | public function getDate() |
||
221 | |||
222 | public function getLayout() |
||
226 | |||
227 | public function getCss() |
||
231 | |||
232 | public function getLayoutFile() |
||
250 | |||
251 | public function getRenderEngine() |
||
255 | |||
256 | public function isBackendTheme() |
||
260 | |||
261 | public function isFrontendTheme() |
||
265 | |||
266 | public function getArray() |
||
270 | |||
271 | public static function getThemeDirectories() |
||
272 | { |
||
273 | return array_merge( |
||
278 | |||
279 | /** |
||
280 | * Iterates over a theme dir (backend / frontend) and fetches some data. |
||
281 | * |
||
282 | * @param string $dir APPLICATION_FRONTEND_THEMES_PATH, APPLICATION_BACKEND_THEMES_PATH |
||
283 | * @param string $type 'frontend' or 'backend' |
||
284 | * @param bool $only_index_name |
||
285 | * |
||
286 | * @return string |
||
287 | */ |
||
288 | protected static function iterateDir($dir, $type, $only_index_name = true) |
||
334 | } |
||
335 |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.