| Total Complexity | 98 |
| Total Lines | 728 |
| 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 |
||
| 33 | class View implements RenderableInterface |
||
| 34 | { |
||
| 35 | use FilePathCollectorTrait; |
||
| 36 | use FileExtensionCollectorTrait; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * View Config |
||
| 40 | * |
||
| 41 | * @var \O2System\Kernel\DataStructures\Config |
||
| 42 | */ |
||
| 43 | protected $config; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * View HTML Document |
||
| 47 | * |
||
| 48 | * @var Html\Document |
||
| 49 | */ |
||
| 50 | protected $document; |
||
| 51 | |||
| 52 | // ------------------------------------------------------------------------ |
||
| 53 | |||
| 54 | /** |
||
| 55 | * View::__construct |
||
| 56 | * |
||
| 57 | * @return View |
||
| 58 | */ |
||
| 59 | public function __construct() |
||
| 60 | { |
||
| 61 | $this->setFileDirName('views'); |
||
| 62 | $this->addFilePath(PATH_RESOURCES); |
||
| 63 | |||
| 64 | output()->addFilePath(PATH_RESOURCES); |
||
| 65 | |||
| 66 | $this->config = config()->loadFile('view', true); |
||
| 67 | |||
| 68 | $this->setFileExtensions( |
||
| 69 | [ |
||
| 70 | '.php', |
||
| 71 | '.phtml', |
||
| 72 | ] |
||
| 73 | ); |
||
| 74 | |||
| 75 | if ($this->config->offsetExists('extensions')) { |
||
| 76 | $this->setFileExtensions($this->config[ 'extensions' ]); |
||
| 77 | } |
||
| 78 | |||
| 79 | $this->document = new Html\Document(); |
||
| 80 | $this->document->formatOutput = (bool)$this->config->beautify; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * View::__get |
||
| 85 | * |
||
| 86 | * @param string $property |
||
| 87 | * |
||
| 88 | * @return bool Returns FALSE when property is not set. |
||
| 89 | */ |
||
| 90 | public function &__get($property) |
||
| 91 | { |
||
| 92 | $get[ $property ] = false; |
||
| 93 | |||
| 94 | if (property_exists($this, $property)) { |
||
| 95 | return $this->{$property}; |
||
| 96 | } |
||
| 97 | |||
| 98 | return $get[ $property ]; |
||
| 99 | } |
||
| 100 | |||
| 101 | // ------------------------------------------------------------------------ |
||
| 102 | |||
| 103 | /** |
||
| 104 | * View::parse |
||
| 105 | * |
||
| 106 | * @param string $string |
||
| 107 | * @param array $vars |
||
| 108 | * |
||
| 109 | * @return bool|string Returns FALSE if failed. |
||
| 110 | */ |
||
| 111 | public function parse($string, array $vars = []) |
||
| 112 | { |
||
| 113 | parser()->loadString($string); |
||
| 114 | |||
| 115 | return parser()->parse($vars); |
||
| 116 | } |
||
| 117 | |||
| 118 | // ------------------------------------------------------------------------ |
||
| 119 | |||
| 120 | /** |
||
| 121 | * View::with |
||
| 122 | * |
||
| 123 | * @param mixed $vars |
||
| 124 | * @param mixed $value |
||
| 125 | * |
||
| 126 | * @return static |
||
| 127 | */ |
||
| 128 | public function with($vars, $value = null) |
||
| 137 | } |
||
| 138 | |||
| 139 | // ------------------------------------------------------------------------ |
||
| 140 | |||
| 141 | /** |
||
| 142 | * View::modal |
||
| 143 | * |
||
| 144 | * @param string $filename |
||
| 145 | * @param array $vars |
||
| 146 | */ |
||
| 147 | public function modal($filename, array $vars = []) |
||
| 148 | { |
||
| 149 | if (presenter()->theme->hasLayout('modal')) { |
||
| 150 | if (presenter()->theme->hasLayout('modal')) { |
||
| 151 | presenter()->theme->setLayout('modal'); |
||
| 152 | echo $this->load($filename, $vars, true); |
||
| 153 | exit(EXIT_SUCCESS); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | if ($content = $this->load($filename, $vars, true)) { |
||
| 158 | echo $content; |
||
| 159 | exit(EXIT_SUCCESS); |
||
| 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 | if(is_ajax()) { |
||
| 190 | parser()->loadFile($filePath); |
||
| 191 | $content = parser()->parse(presenter()->getArrayCopy()); |
||
| 192 | |||
| 193 | presenter()->partials->addPartial('content', $content); |
||
| 194 | } else { |
||
| 195 | presenter()->partials->addPartial('content', $filePath); |
||
| 196 | } |
||
| 197 | } else { |
||
| 198 | presenter()->partials->addPartial(pathinfo($filePath, PATHINFO_FILENAME), $filePath); |
||
| 199 | } |
||
| 200 | } else { |
||
| 201 | parser()->loadFile($filePath); |
||
| 202 | |||
| 203 | return parser()->parse(presenter()->getArrayCopy()); |
||
| 204 | } |
||
| 205 | } else { |
||
| 206 | $vars = presenter()->getArrayCopy(); |
||
| 207 | extract($vars); |
||
| 208 | |||
| 209 | $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
||
| 210 | |||
| 211 | $error = new ErrorException( |
||
| 212 | 'E_VIEW_NOT_FOUND', |
||
| 213 | 0, |
||
| 214 | @$backtrace[ 0 ][ 'file' ], |
||
| 215 | @$backtrace[ 0 ][ 'line' ], |
||
| 216 | [trim($filename)] |
||
| 217 | ); |
||
| 218 | |||
| 219 | unset($backtrace); |
||
| 220 | |||
| 221 | ob_start(); |
||
| 222 | include output()->getFilePath('error'); |
||
| 223 | $content = ob_get_contents(); |
||
| 224 | ob_end_clean(); |
||
| 225 | |||
| 226 | if ($return === false) { |
||
| 227 | if (presenter()->partials->hasPartial('content') === false) { |
||
| 228 | presenter()->addPartial('content', $content); |
||
| 229 | } else { |
||
| 230 | presenter()->addPartial(pathinfo($filePath, PATHINFO_FILENAME), $content); |
||
| 231 | } |
||
| 232 | } else { |
||
| 233 | return $content; |
||
| 234 | } |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | // ------------------------------------------------------------------------ |
||
| 239 | |||
| 240 | /** |
||
| 241 | * View::page |
||
| 242 | * |
||
| 243 | * @param string $filename |
||
| 244 | * @param array $vars |
||
| 245 | * @param bool $return |
||
| 246 | * |
||
| 247 | * @return bool|string Returns FALSE if failed. |
||
| 248 | */ |
||
| 249 | public function page($filename, array $vars = [], $return = false) |
||
| 250 | { |
||
| 251 | if ( ! is_file($filename)) { |
||
| 252 | $pageDirectories = modules()->getResourcesDirs('pages'); |
||
| 253 | foreach ($pageDirectories as $pageDirectory) { |
||
| 254 | if (is_file($pageFilePath = $pageDirectory . $filename . '.phtml')) { |
||
| 255 | $filename = $pageFilePath; |
||
| 256 | break; |
||
| 257 | } |
||
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | if (count($vars)) { |
||
| 262 | presenter()->merge($vars); |
||
| 263 | } |
||
| 264 | |||
| 265 | presenter()->merge(presenter()->page->getVars()); |
||
| 266 | |||
| 267 | if ($return === false) { |
||
| 268 | if (presenter()->partials->hasPartial('content') === false) { |
||
| 269 | presenter()->partials->addPartial('content', $filename); |
||
| 270 | } else { |
||
| 271 | presenter()->partials->addPartial(pathinfo($filename, PATHINFO_FILENAME), $filename); |
||
| 272 | } |
||
| 273 | } elseif (parser()->loadFile($filename)) { |
||
| 274 | return parser()->parse(presenter()->getArrayCopy()); |
||
| 275 | } |
||
| 276 | } |
||
| 277 | |||
| 278 | // ------------------------------------------------------------------------ |
||
| 279 | |||
| 280 | /** |
||
| 281 | * View::getFilePath |
||
| 282 | * |
||
| 283 | * @param string $filename |
||
| 284 | * |
||
| 285 | * @return bool|string |
||
| 286 | */ |
||
| 287 | public function getFilePath($filename) |
||
| 288 | { |
||
| 289 | $filename = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $filename); |
||
| 290 | |||
| 291 | if (is_file($filename)) { |
||
| 292 | return realpath($filename); |
||
| 293 | } else { |
||
| 294 | $viewsDirectories = array_merge([ |
||
| 295 | PATH_KERNEL . 'Views' . DIRECTORY_SEPARATOR, |
||
| 296 | PATH_FRAMEWORK . 'Views' . DIRECTORY_SEPARATOR, |
||
| 297 | ], $this->filePaths); |
||
| 298 | |||
| 299 | $viewsDirectories = array_unique($viewsDirectories); |
||
| 300 | $viewsDirectories = array_reverse($viewsDirectories); |
||
| 301 | |||
| 302 | $controllerSubDir = services('controller')->getParameter() . DIRECTORY_SEPARATOR; |
||
| 303 | |||
| 304 | foreach ($viewsDirectories as $viewsDirectory) { |
||
| 305 | $filename = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $filename); |
||
| 306 | |||
| 307 | // Find specific view file for mobile version |
||
| 308 | if (services('userAgent')->isMobile()) { |
||
| 309 | // Find without controller parameter as sub directory |
||
| 310 | if (is_file($filePath = $viewsDirectory . $filename . '.mobile.phtml')) { |
||
| 311 | return realpath($filePath); |
||
| 312 | break; |
||
| 313 | } |
||
| 314 | |||
| 315 | // Find without controller parameter as sub directory |
||
| 316 | if (is_file($filePath = $viewsDirectory . $controllerSubDir . $filename . '.mobile.phtml')) { |
||
| 317 | return realpath($filePath); |
||
| 318 | break; |
||
| 319 | } |
||
| 320 | } |
||
| 321 | |||
| 322 | // Find without controller parameter as sub directory |
||
| 323 | if (is_file($filePath = $viewsDirectory . $filename . '.phtml')) { |
||
| 324 | return realpath($filePath); |
||
| 325 | break; |
||
| 326 | } |
||
| 327 | |||
| 328 | // Find without controller parameter as sub directory |
||
| 329 | if (is_file($filePath = $viewsDirectory . $controllerSubDir . $filename . '.phtml')) { |
||
| 330 | print_line('found: ' . $viewsDirectory . $controllerSubDir . $filename . '.phtml'); |
||
| 331 | return realpath($filePath); |
||
| 332 | break; |
||
| 333 | } |
||
| 334 | } |
||
| 335 | } |
||
| 336 | |||
| 337 | return false; |
||
| 338 | } |
||
| 339 | |||
| 340 | // ------------------------------------------------------------------------ |
||
| 341 | |||
| 342 | /** |
||
| 343 | * View::render |
||
| 344 | * |
||
| 345 | * @param array $options |
||
| 346 | * |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | public function render(array $options = []) |
||
| 761 | } |
||
| 762 | } |
||
| 763 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.