Total Complexity | 84 |
Total Lines | 599 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like View 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 View, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class View implements RenderableInterface |
||
33 | { |
||
34 | use FilePathCollectorTrait; |
||
35 | use FileExtensionCollectorTrait; |
||
36 | |||
37 | /** |
||
38 | * View Config |
||
39 | * |
||
40 | * @var \O2System\Kernel\DataStructures\Config |
||
41 | */ |
||
42 | protected $config; |
||
43 | |||
44 | /** |
||
45 | * View HTML Document |
||
46 | * |
||
47 | * @var Html\Document |
||
48 | */ |
||
49 | protected $document; |
||
50 | |||
51 | // ------------------------------------------------------------------------ |
||
52 | |||
53 | /** |
||
54 | * View::__construct |
||
55 | * |
||
56 | * @return View |
||
57 | */ |
||
58 | public function __construct() |
||
59 | { |
||
60 | $this->setFileDirName('Views'); |
||
61 | $this->addFilePath(PATH_APP); |
||
62 | |||
63 | output()->addFilePath(PATH_APP); |
||
64 | |||
65 | $this->config = config()->loadFile('view', true); |
||
66 | |||
67 | $this->setFileExtensions( |
||
68 | [ |
||
69 | '.php', |
||
70 | '.phtml', |
||
71 | ] |
||
72 | ); |
||
73 | |||
74 | if ($this->config->offsetExists('extensions')) { |
||
75 | $this->setFileExtensions($this->config[ 'extensions' ]); |
||
76 | } |
||
77 | |||
78 | $this->document = new Html\Document(); |
||
79 | $this->document->formatOutput = (bool)$this->config->beautify; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * View::__get |
||
84 | * |
||
85 | * @param string $property |
||
86 | * |
||
87 | * @return bool Returns FALSE when property is not set. |
||
88 | */ |
||
89 | public function &__get($property) |
||
90 | { |
||
91 | $get[ $property ] = false; |
||
92 | |||
93 | if (property_exists($this, $property)) { |
||
94 | return $this->{$property}; |
||
95 | } |
||
96 | |||
97 | return $get[ $property ]; |
||
98 | } |
||
99 | |||
100 | // ------------------------------------------------------------------------ |
||
101 | |||
102 | /** |
||
103 | * View::parse |
||
104 | * |
||
105 | * @param string $string |
||
106 | * @param array $vars |
||
107 | * |
||
108 | * @return bool|string Returns FALSE if failed. |
||
109 | */ |
||
110 | public function parse($string, array $vars = []) |
||
111 | { |
||
112 | parser()->loadString($string); |
||
113 | |||
114 | return parser()->parse($vars); |
||
115 | } |
||
116 | |||
117 | // ------------------------------------------------------------------------ |
||
118 | |||
119 | /** |
||
120 | * View::with |
||
121 | * |
||
122 | * @param mixed $vars |
||
123 | * @param mixed $value |
||
124 | * |
||
125 | * @return static |
||
126 | */ |
||
127 | public function with($vars, $value = null) |
||
136 | } |
||
137 | |||
138 | // ------------------------------------------------------------------------ |
||
139 | |||
140 | /** |
||
141 | * View::modal |
||
142 | * |
||
143 | * @param string $filename |
||
144 | * @param array $vars |
||
145 | */ |
||
146 | public function modal($filename, array $vars = []) |
||
160 | } |
||
161 | } |
||
162 | |||
163 | // ------------------------------------------------------------------------ |
||
164 | |||
165 | /** |
||
166 | * View::load |
||
167 | * |
||
168 | * @param string $filename |
||
169 | * @param array $vars |
||
170 | * @param bool $return |
||
171 | * |
||
172 | * @return false|string |
||
173 | */ |
||
174 | public function load($filename, array $vars = [], $return = false) |
||
175 | { |
||
176 | if ($filename instanceof \SplFileInfo) { |
||
177 | return $this->page($filename->getRealPath(), array_merge($vars, $filename->getVars())); |
||
178 | } |
||
179 | |||
180 | if (strpos($filename, 'Pages') !== false) { |
||
181 | return $this->page($filename, $vars, $return); |
||
182 | } |
||
183 | |||
184 | presenter()->merge($vars); |
||
185 | |||
186 | if (false !== ($filePath = $this->getFilePath($filename))) { |
||
187 | if ($return === false) { |
||
188 | if (presenter()->partials->hasPartial('content') === false) { |
||
189 | presenter()->partials->addPartial('content', $filePath); |
||
190 | } else { |
||
191 | presenter()->partials->addPartial(pathinfo($filePath, PATHINFO_FILENAME), $filePath); |
||
192 | } |
||
193 | } else { |
||
194 | parser()->loadFile($filePath); |
||
195 | |||
196 | return parser()->parse(presenter()->getArrayCopy()); |
||
197 | } |
||
198 | } else { |
||
199 | $vars = presenter()->getArrayCopy(); |
||
200 | extract($vars); |
||
201 | |||
202 | $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
||
203 | |||
204 | $error = new ErrorException( |
||
205 | 'E_VIEW_NOT_FOUND', |
||
206 | 0, |
||
207 | @$backtrace[ 0 ][ 'file' ], |
||
208 | @$backtrace[ 0 ][ 'line' ], |
||
209 | [trim($filename)] |
||
210 | ); |
||
211 | |||
212 | unset($backtrace); |
||
213 | |||
214 | ob_start(); |
||
215 | include output()->getFilePath('error'); |
||
216 | $content = ob_get_contents(); |
||
217 | ob_end_clean(); |
||
218 | |||
219 | if ($return === false) { |
||
220 | if (presenter()->partials->hasPartial('content') === false) { |
||
221 | presenter()->addPartial('content', $content); |
||
222 | } else { |
||
223 | presenter()->addPartial(pathinfo($filePath, PATHINFO_FILENAME), $content); |
||
224 | } |
||
225 | } else { |
||
226 | return $content; |
||
227 | } |
||
228 | } |
||
229 | } |
||
230 | |||
231 | // ------------------------------------------------------------------------ |
||
232 | |||
233 | /** |
||
234 | * View::page |
||
235 | * |
||
236 | * @param string $filename |
||
237 | * @param array $vars |
||
238 | * @param bool $return |
||
239 | * |
||
240 | * @return bool|string Returns FALSE if failed. |
||
241 | */ |
||
242 | public function page($filename, array $vars = [], $return = false) |
||
243 | { |
||
244 | if ( ! is_file($filename)) { |
||
245 | $pageDirectories = modules()->getDirs('Pages'); |
||
246 | foreach ($pageDirectories as $pageDirectory) { |
||
247 | if (is_file($pageFilePath = $pageDirectory . $filename . '.phtml')) { |
||
248 | $filename = $pageFilePath; |
||
249 | break; |
||
250 | } |
||
251 | } |
||
252 | } |
||
253 | |||
254 | if (count($vars)) { |
||
255 | presenter()->merge($vars); |
||
256 | } |
||
257 | |||
258 | presenter()->merge(presenter()->page->getVars()); |
||
259 | |||
260 | if ($return === false) { |
||
261 | if (presenter()->partials->hasPartial('content') === false) { |
||
262 | presenter()->partials->addPartial('content', $filename); |
||
263 | } else { |
||
264 | presenter()->partials->addPartial(pathinfo($filename, PATHINFO_FILENAME), $filename); |
||
265 | } |
||
266 | } elseif (parser()->loadFile($filename)) { |
||
267 | return parser()->parse(presenter()->getArrayCopy()); |
||
268 | } |
||
269 | } |
||
270 | |||
271 | // ------------------------------------------------------------------------ |
||
272 | |||
273 | /** |
||
274 | * View::getFilePath |
||
275 | * |
||
276 | * @param string $filename |
||
277 | * |
||
278 | * @return bool|string |
||
279 | */ |
||
280 | public function getFilePath($filename) |
||
281 | { |
||
282 | $filename = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $filename); |
||
283 | |||
284 | if (is_file($filename)) { |
||
285 | return realpath($filename); |
||
286 | } else { |
||
287 | $viewsFileExtensions = $this->fileExtensions; |
||
288 | $viewsDirectories = modules()->getDirs('Views'); |
||
289 | $viewsDirectories = array_merge($viewsDirectories, $this->filePaths); |
||
290 | $viewsDirectories = array_unique($viewsDirectories); |
||
291 | |||
292 | $deviceDirectory = null; |
||
293 | if (services('userAgent')->isMobile()) { |
||
294 | $deviceDirectory = 'mobile'; |
||
295 | } |
||
296 | |||
297 | if (presenter()->theme) { |
||
298 | $moduleReplacementPath = presenter()->theme->getPathName() |
||
299 | . DIRECTORY_SEPARATOR |
||
300 | . 'views' |
||
301 | . DIRECTORY_SEPARATOR |
||
302 | . strtolower( |
||
303 | str_replace(PATH_APP, '', modules()->current()->getRealpath()) |
||
304 | ); |
||
305 | |||
306 | if (is_dir($moduleReplacementPath)) { |
||
307 | array_unshift($viewsDirectories, $moduleReplacementPath); |
||
308 | |||
309 | // Add Theme File Extensions |
||
310 | if (presenter()->theme->getPresets()->offsetExists('extension')) { |
||
311 | array_unshift($viewsFileExtensions, |
||
312 | presenter()->theme->getPresets()->offsetGet('extension')); |
||
313 | } elseif (presenter()->theme->getPresets()->offsetExists('extensions')) { |
||
314 | $viewsFileExtensions = array_merge( |
||
315 | presenter()->theme->getPresets()->offsetGet('extensions'), |
||
316 | $viewsFileExtensions |
||
317 | ); |
||
318 | } |
||
319 | |||
320 | // Add Theme Parser Engine |
||
321 | if (presenter()->theme->getPresets()->offsetExists('driver')) { |
||
322 | $parserDriverClassName = '\O2System\Parser\Drivers\\' . camelcase( |
||
323 | presenter()->theme->getPresets()->offsetGet('driver') |
||
324 | ); |
||
325 | |||
326 | if (class_exists($parserDriverClassName)) { |
||
327 | parser()->addDriver( |
||
328 | new $parserDriverClassName(), |
||
329 | presenter()->theme->getPresets()->offsetGet('driver') |
||
330 | ); |
||
331 | } |
||
332 | } |
||
333 | } |
||
334 | } |
||
335 | |||
336 | foreach ($viewsDirectories as $viewsDirectory) { |
||
337 | foreach ($viewsFileExtensions as $fileExtension) { |
||
338 | $filename = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $filename); |
||
339 | |||
340 | if (is_file($filePath = $viewsDirectory . $filename . $fileExtension)) { |
||
341 | return realpath($filePath); |
||
342 | break; |
||
343 | } |
||
344 | } |
||
345 | } |
||
346 | } |
||
347 | |||
348 | return false; |
||
349 | } |
||
350 | |||
351 | // ------------------------------------------------------------------------ |
||
352 | |||
353 | /** |
||
354 | * View::render |
||
355 | * |
||
356 | * @param array $options |
||
357 | * |
||
358 | * @return string |
||
359 | */ |
||
360 | public function render(array $options = []) |
||
631 | } |
||
632 | } |
||
633 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.