Total Complexity | 50 |
Total Lines | 258 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like AbstractPosition 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 AbstractPosition, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | abstract class AbstractPosition |
||
26 | { |
||
27 | /** |
||
28 | * AbstractPosition::__get |
||
29 | * |
||
30 | * @param string $property |
||
31 | * |
||
32 | * @return \O2System\Framework\Http\Presenter\Assets\Collections\Css|\O2System\Framework\Http\Presenter\Assets\Collections\Font|\O2System\Framework\Http\Presenter\Assets\Collections\Javascript|null |
||
33 | */ |
||
34 | public function __get($property) |
||
35 | { |
||
36 | return isset($this->{$property}) ? $this->{$property} : null; |
||
37 | } |
||
38 | |||
39 | // ------------------------------------------------------------------------ |
||
40 | |||
41 | /** |
||
42 | * AbstractPosition::getUrl |
||
43 | * |
||
44 | * @param string $realPath |
||
45 | * |
||
46 | * @return string |
||
47 | */ |
||
48 | public function getUrl($realPath) |
||
49 | { |
||
50 | if (strpos($realPath, 'http') !== false) { |
||
51 | return $realPath; |
||
52 | } |
||
53 | |||
54 | return (new Uri()) |
||
55 | ->withQuery(null) |
||
56 | ->withSegments( |
||
57 | new Uri\Segments( |
||
58 | str_replace( |
||
59 | [PATH_PUBLIC, DIRECTORY_SEPARATOR], |
||
60 | ['', '/'], |
||
61 | $realPath |
||
62 | ) |
||
63 | ) |
||
64 | ) |
||
65 | ->__toString(); |
||
66 | } |
||
67 | |||
68 | // ------------------------------------------------------------------------ |
||
69 | |||
70 | /** |
||
71 | * AbstractPosition::loadCollections |
||
72 | * |
||
73 | * @param array $collections |
||
74 | */ |
||
75 | public function loadCollections(array $collections) |
||
76 | { |
||
77 | foreach ($collections as $subDir => $files) { |
||
78 | if (is_array($files) and count($files)) { |
||
79 | // normalize the subDirectory with a trailing separator |
||
80 | $subDir = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $subDir); |
||
81 | $subDir = rtrim($subDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
||
82 | |||
83 | foreach ($files as $file) { |
||
84 | if (strpos($file, 'http') !== false) { |
||
85 | $this->loadUrl($file, $subDir); |
||
86 | } else { |
||
87 | $this->loadFile($file, $subDir); |
||
88 | } |
||
89 | } |
||
90 | } elseif (is_string($files)) { |
||
91 | $this->loadFile($files); |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 | |||
96 | // ------------------------------------------------------------------------ |
||
97 | |||
98 | /** |
||
99 | * AbstractPosition::loadUrl |
||
100 | * |
||
101 | * @param string $url |
||
102 | * @param string|null $subDir |
||
103 | * |
||
104 | * @return bool |
||
105 | */ |
||
106 | public function loadUrl($url, $subDir = null) |
||
135 | } |
||
136 | |||
137 | // ------------------------------------------------------------------------ |
||
138 | |||
139 | /** |
||
140 | * AbstractPosition::loadFile |
||
141 | * |
||
142 | * @param string $filePath |
||
143 | * @param string $subDir |
||
144 | * |
||
145 | * @return bool |
||
146 | */ |
||
147 | public function loadFile($filePath, $subDir = null) |
||
148 | { |
||
149 | $directories = loader()->getPublicDirs(true); |
||
150 | $filePath = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $filePath); |
||
151 | |||
152 | // set filepaths |
||
153 | foreach ($directories as $directory) { |
||
154 | $extension = pathinfo($directory . $filePath, PATHINFO_EXTENSION); |
||
155 | |||
156 | if(empty($extension) and empty($subDir)) { |
||
157 | $extensions = ['.css', '.js']; |
||
158 | } elseif(empty($extension) and isset($subDir)) { |
||
159 | switch ($subDir) { |
||
160 | default: |
||
161 | case 'css/': |
||
162 | $property = 'css'; |
||
163 | $extensions = ['.css']; |
||
164 | break; |
||
165 | case 'font/': |
||
166 | case 'fonts/': |
||
167 | $property = 'font'; |
||
168 | $extensions = ['.css']; |
||
169 | break; |
||
170 | case 'js/': |
||
171 | case 'javascript/': |
||
172 | case 'javascripts/': |
||
173 | $property = 'javascript'; |
||
174 | $extensions = ['.js']; |
||
175 | break; |
||
176 | } |
||
177 | } else { |
||
178 | // remove filename extension |
||
179 | $filePath = str_replace('.' . $extension, '', $filePath); |
||
180 | switch ($extension) { |
||
181 | default: |
||
182 | case 'css': |
||
183 | $property = 'css'; |
||
184 | $subDir = 'css' . DIRECTORY_SEPARATOR; |
||
185 | $extensions = ['.css']; |
||
186 | break; |
||
187 | case 'font': |
||
188 | $property = 'font'; |
||
189 | $subDir = 'fonts' . DIRECTORY_SEPARATOR; |
||
190 | $extensions = ['.css']; |
||
191 | break; |
||
192 | case 'js': |
||
193 | case 'javascript': |
||
194 | $property = 'javascript'; |
||
195 | $subDir = 'js' . DIRECTORY_SEPARATOR; |
||
196 | $extensions = ['.js']; |
||
197 | break; |
||
198 | } |
||
199 | } |
||
200 | |||
201 | foreach($extensions as $extension) { |
||
202 | // without subdirectory |
||
203 | if (input()->env('DEBUG_STAGE') === 'PRODUCTION') { |
||
204 | $filePaths[] = $directory . $filePath . '.min' . $extension; // minify version support |
||
205 | } |
||
206 | |||
207 | $filePaths[] = $directory . $filePath . $extension; |
||
208 | |||
209 | // with subdirectory |
||
210 | if (isset($subDir)) { |
||
211 | if (input()->env('DEBUG_STAGE') === 'PRODUCTION') { |
||
212 | $filePaths[] = $directory . $subDir . $filePath . '.min' . $extension; // minify version support |
||
213 | } |
||
214 | |||
215 | $filePaths[] = $directory . $subDir . $filePath . $extension; |
||
216 | } |
||
217 | } |
||
218 | } |
||
219 | |||
220 | foreach ($filePaths as $filePath) { |
||
221 | if (is_file($filePath)) { |
||
222 | if(empty($property)) { |
||
223 | $extension = pathinfo($filePath, PATHINFO_EXTENSION); |
||
224 | switch ($extension) { |
||
225 | case 'font': |
||
226 | $extension = 'font'; |
||
227 | break; |
||
228 | case 'js': |
||
229 | $extension = 'javascript'; |
||
230 | break; |
||
231 | default: |
||
232 | case 'css': |
||
233 | $extension = 'css'; |
||
234 | break; |
||
235 | } |
||
236 | } |
||
237 | |||
238 | if (property_exists($this, $property)) { |
||
239 | if ( ! call_user_func_array([$this->{$property}, 'has'], [$filePath])) { |
||
240 | $this->{$property}->append($filePath); |
||
241 | |||
242 | return true; |
||
243 | break; |
||
244 | } |
||
245 | } |
||
246 | } |
||
247 | } |
||
248 | |||
249 | return false; |
||
250 | } |
||
251 | |||
252 | // ------------------------------------------------------------------------ |
||
253 | |||
254 | /** |
||
255 | * AbstractPosition::getVersion |
||
256 | * |
||
257 | * @param string $code |
||
258 | * |
||
259 | * @return string |
||
260 | */ |
||
261 | public function getVersion($codeSerialize) |
||
273 | } |
||
274 | |||
275 | // ------------------------------------------------------------------------ |
||
276 | |||
277 | /** |
||
278 | * AbstractPosition::__toString |
||
279 | * |
||
280 | * @return string |
||
281 | */ |
||
282 | abstract public function __toString(); |
||
283 | } |