| Total Complexity | 105 |
| Total Lines | 526 |
| Duplicated Lines | 0 % |
| Changes | 11 | ||
| Bugs | 4 | Features | 0 |
Complex classes like DLTemplate 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 DLTemplate, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class DLTemplate |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Объект DocumentParser - основной класс MODX |
||
| 13 | * @var DocumentParser $modx |
||
| 14 | * @access protected |
||
| 15 | */ |
||
| 16 | protected $modx = null; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var DLTemplate cached reference to singleton instance |
||
| 20 | */ |
||
| 21 | protected static $instance; |
||
| 22 | |||
| 23 | protected $templatePath = 'assets/templates/'; |
||
| 24 | |||
| 25 | protected $templateExtension = 'html'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var null|Twig_Environment twig object |
||
| 29 | */ |
||
| 30 | protected $twig = null; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var null|Jenssegers\Blade\Blade blade object |
||
| 34 | */ |
||
| 35 | protected $blade = null; |
||
| 36 | |||
| 37 | protected $twigEnabled = false; |
||
| 38 | |||
| 39 | protected $bladeEnabled = false; |
||
| 40 | |||
| 41 | protected $templateData = array(); |
||
| 42 | |||
| 43 | public $phx = null; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * gets the instance via lazy initialization (created on first usage) |
||
| 47 | * |
||
| 48 | * @return self |
||
| 49 | */ |
||
| 50 | public static function getInstance(DocumentParser $modx) |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * is not allowed to call from outside: private! |
||
| 62 | * |
||
| 63 | */ |
||
| 64 | private function __construct(DocumentParser $modx) |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * prevent the instance from being cloned |
||
| 71 | * |
||
| 72 | * @return void |
||
| 73 | */ |
||
| 74 | private function __clone() |
||
| 75 | { |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * prevent from being unserialized |
||
| 80 | * |
||
| 81 | * @return void |
||
| 82 | */ |
||
| 83 | private function __wakeup() |
||
| 84 | { |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Задает относительный путь к папке с шаблонами |
||
| 89 | * |
||
| 90 | * @param $path |
||
| 91 | * @param bool $supRoot |
||
| 92 | * @return $this |
||
| 93 | */ |
||
| 94 | public function setTemplatePath($path, $supRoot = false) |
||
| 95 | { |
||
| 96 | $path = trim($path); |
||
| 97 | if ($supRoot === false) { |
||
| 98 | $path = preg_replace(array( |
||
| 99 | '/\.*[\/|\\\]/i', |
||
| 100 | '/[\/|\\\]+/i' |
||
| 101 | ), array('/', '/'), $path); |
||
| 102 | } |
||
| 103 | |||
| 104 | if (!empty($path)) { |
||
| 105 | $this->templatePath = $path; |
||
| 106 | } |
||
| 107 | |||
| 108 | return $this; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Задает расширение файла с шаблоном |
||
| 113 | * |
||
| 114 | * @param $ext |
||
| 115 | * @return $this |
||
| 116 | */ |
||
| 117 | public function setTemplateExtension($ext) |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Additional data for external templates |
||
| 133 | * |
||
| 134 | * @param array $data |
||
| 135 | * @return $this |
||
| 136 | */ |
||
| 137 | public function setTemplateData($data = array()) |
||
| 138 | { |
||
| 139 | if (is_array($data)) { |
||
| 140 | $this->templateData = $data; |
||
| 141 | } |
||
| 142 | |||
| 143 | return $this; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param array $data |
||
| 148 | * @return array |
||
| 149 | */ |
||
| 150 | public function getTemplateData($data = array()) |
||
| 151 | { |
||
| 152 | $plh = $this->templateData; |
||
| 153 | $plh['data'] = $data; |
||
| 154 | $plh['modx'] = $this->modx; |
||
| 155 | |||
| 156 | return $plh; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Сохранение данных в массив плейсхолдеров |
||
| 161 | * |
||
| 162 | * @param mixed $data данные |
||
| 163 | * @param int $set устанавливать ли глобальнй плейсхолдер MODX |
||
| 164 | * @param string $key ключ локального плейсхолдера |
||
| 165 | * @param string $prefix префикс для ключей массива |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | public function toPlaceholders($data, $set = 0, $key = 'contentPlaceholder', $prefix = '') |
||
| 169 | { |
||
| 170 | $out = ''; |
||
| 171 | if ($set != 0) { |
||
| 172 | $this->modx->toPlaceholder($key, $data, $prefix); |
||
| 173 | } else { |
||
| 174 | $out = $data; |
||
| 175 | } |
||
| 176 | |||
| 177 | return $out; |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * refactor $modx->getChunk(); |
||
| 182 | * |
||
| 183 | * @param string $name Template: chunk name || @CODE: template || @FILE: file with template |
||
| 184 | * @return string html template with placeholders without data |
||
| 185 | */ |
||
| 186 | public function getChunk($name) |
||
| 187 | { |
||
| 188 | $tpl = ''; |
||
| 189 | $this->twigEnabled = (0 === strpos($name, '@T_')); |
||
| 190 | $this->bladeEnabled = (0 === strpos($name, '@B_')); |
||
| 191 | if ($name != '' && !isset($this->modx->chunkCache[$name])) { |
||
| 192 | $mode = (preg_match( |
||
| 193 | '/^((@[A-Z_]+)[:]{0,1})(.*)/Asu', |
||
| 194 | trim($name), |
||
| 195 | $tmp |
||
| 196 | ) && isset($tmp[2], $tmp[3])) ? $tmp[2] : false; |
||
| 197 | $subTmp = (isset($tmp[3])) ? trim($tmp[3]) : null; |
||
| 198 | if ($this->twigEnabled) { |
||
| 199 | $mode = '@' . substr($mode, 3); |
||
| 200 | } elseif ($this->bladeEnabled) { |
||
| 201 | $mode = '@' . substr($mode, 3); |
||
| 202 | } |
||
| 203 | switch ($mode) { |
||
| 204 | case '@FILE': |
||
| 205 | if ($subTmp != '') { |
||
| 206 | $real = realpath(MODX_BASE_PATH . $this->templatePath); |
||
| 207 | $path = realpath(MODX_BASE_PATH . $this->templatePath . preg_replace(array( |
||
| 208 | '/\.*[\/|\\\]/i', |
||
| 209 | '/[\/|\\\]+/i' |
||
| 210 | ), array('/', '/'), $subTmp) . '.' . $this->templateExtension); |
||
| 211 | if (basename($path, '.' . $this->templateExtension) !== '' && |
||
| 212 | 0 === strpos($path, $real) && |
||
| 213 | file_exists($path) |
||
| 214 | ) { |
||
| 215 | $tpl = file_get_contents($path); |
||
| 216 | } |
||
| 217 | } |
||
| 218 | break; |
||
| 219 | case '@CHUNK': |
||
| 220 | if ($subTmp != '') { |
||
| 221 | $tpl = $this->modx->getChunk($subTmp); |
||
| 222 | } |
||
| 223 | break; |
||
| 224 | case '@INLINE': |
||
| 225 | case '@TPL': |
||
| 226 | case '@CODE': |
||
| 227 | $tpl = $tmp[3]; //without trim |
||
| 228 | break; |
||
| 229 | case '@DOCUMENT': |
||
| 230 | case '@DOC': |
||
| 231 | switch (true) { |
||
| 232 | case ((int)$subTmp > 0): |
||
| 233 | $tpl = $this->modx->getPageInfo((int)$subTmp, 0, "content"); |
||
| 234 | $tpl = isset($tpl['content']) ? $tpl['content'] : ''; |
||
| 235 | break; |
||
| 236 | case ((int)$subTmp == 0): |
||
| 237 | $tpl = $this->modx->documentObject['content']; |
||
| 238 | break; |
||
| 239 | } |
||
| 240 | break; |
||
| 241 | case '@PLH': |
||
| 242 | case '@PLACEHOLDER': |
||
| 243 | if ($subTmp != '') { |
||
| 244 | $tpl = $this->modx->getPlaceholder($subTmp); |
||
| 245 | } |
||
| 246 | break; |
||
| 247 | case '@CFG': |
||
| 248 | case '@CONFIG': |
||
| 249 | case '@OPTIONS': |
||
| 250 | if ($subTmp != '') { |
||
| 251 | $tpl = $this->modx->getConfig($subTmp); |
||
| 252 | } |
||
| 253 | break; |
||
| 254 | case '@SNIPPET': |
||
| 255 | if ($subTmp != '') { |
||
| 256 | $tpl = $this->modx->runSnippet($subTmp, $this->modx->event->params); |
||
| 257 | } |
||
| 258 | break; |
||
| 259 | case '@RENDERPAGE': |
||
| 260 | $tpl = $this->renderDoc($subTmp, false); |
||
| 261 | break; |
||
| 262 | case '@LOADPAGE': |
||
| 263 | $tpl = $this->renderDoc($subTmp, true); |
||
| 264 | break; |
||
| 265 | case '@TEMPLATE': |
||
| 266 | $tpl = $this->getTemplate($subTmp); |
||
| 267 | break; |
||
| 268 | default: |
||
| 269 | $tpl = $this->modx->getChunk($name); |
||
| 270 | } |
||
| 271 | $this->modx->chunkCache[$name] = $tpl; |
||
| 272 | } else { |
||
| 273 | if ($name != '') { |
||
| 274 | $tpl = $this->modx->getChunk($name); |
||
| 275 | } |
||
| 276 | } |
||
| 277 | |||
| 278 | return $tpl; |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Рендер документа с подстановкой плейсхолдеров и выполнением сниппетов |
||
| 283 | * |
||
| 284 | * @param int $id ID документа |
||
| 285 | * @param bool $events Во время рендера документа стоит ли вызывать события OnLoadWebDocument и OnLoadDocumentObject (внутри метода getDocumentObject). |
||
| 286 | * @param mixed $tpl Шаблон с которым необходимо отрендерить документ. Возможные значения: |
||
| 287 | * null - Использовать шаблон который назначен документу |
||
| 288 | * int(0-n) - Получить шаблон из базы данных с указанным ID и применить его к документу |
||
| 289 | * string - Применить шаблон указанный в строке к документу |
||
| 290 | * @return string |
||
| 291 | * |
||
| 292 | * Событие OnLoadWebDocument дополнительно передает параметры: |
||
| 293 | * - с источиком от куда произошел вызов события |
||
| 294 | * - оригинальный экземпляр класса DocumentParser |
||
| 295 | */ |
||
| 296 | public function renderDoc($id, $events = false, $tpl = null) |
||
| 297 | { |
||
| 298 | $id = (int)$id; |
||
| 299 | if ($id <= 0) { |
||
| 300 | return ''; |
||
| 301 | } |
||
| 302 | |||
| 303 | $m = clone $this->modx; //Чтобы была возможность вызывать события |
||
| 304 | $m->documentIdentifier = $id; |
||
| 305 | $m->documentObject = $m->getDocumentObject('id', (int)$id, $events ? 'prepareResponse' : null); |
||
| 306 | if ($m->documentObject['type'] == "reference") { |
||
| 307 | if (is_numeric($m->documentObject['content']) && $m->documentObject['content'] > 0) { |
||
| 308 | $m->documentObject['content'] = $this->renderDoc($m->documentObject['content'], $events); |
||
| 309 | } |
||
| 310 | } |
||
| 311 | switch (true) { |
||
| 312 | case is_integer($tpl): |
||
| 313 | $tpl = $this->getTemplate($tpl); |
||
| 314 | break; |
||
| 315 | case is_string($tpl): |
||
| 316 | break; |
||
| 317 | case is_null($tpl): |
||
| 318 | default: |
||
| 319 | $tpl = $this->getTemplate($m->documentObject['template']); |
||
| 320 | } |
||
| 321 | $m->documentContent = $tpl; |
||
| 322 | if ($events) { |
||
| 323 | $m->invokeEvent("OnLoadWebDocument", array( |
||
| 324 | 'source' => 'DLTemplate', |
||
| 325 | 'mainModx' => $this->modx, |
||
| 326 | )); |
||
| 327 | } |
||
| 328 | |||
| 329 | return $this->parseDocumentSource($m->documentContent, $m); |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Получить содержимое шаблона с определенным номером |
||
| 334 | * @param int $id Номер шаблона |
||
| 335 | * @return string HTML код шаблона |
||
| 336 | */ |
||
| 337 | public function getTemplate($id) |
||
| 338 | { |
||
| 339 | $tpl = null; |
||
| 340 | $id = (int)$id; |
||
| 341 | if ($id > 0) { |
||
| 342 | $tpl = $this->modx->getDatabase()->getValue("SELECT `content` FROM {$this->modx->getDatabase()->getFullTableName("site_templates")} WHERE `id` = {$id}"); |
||
| 343 | } |
||
| 344 | if (is_null($tpl)) { |
||
| 345 | $tpl = '[*content*]'; |
||
| 346 | } |
||
| 347 | |||
| 348 | return $tpl; |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * refactor $modx->parseChunk(); |
||
| 353 | * |
||
| 354 | * @param string $name Template: chunk name || @CODE: template || @FILE: file with template |
||
| 355 | * @param array $data paceholder |
||
| 356 | * @param bool $parseDocumentSource render html template via DocumentParser::parseDocumentSource() |
||
| 357 | * @return string html template with data without placeholders |
||
| 358 | */ |
||
| 359 | public function parseChunk($name, $data = array(), $parseDocumentSource = false, $disablePHx = false) |
||
| 360 | { |
||
| 361 | $out = $this->getChunk($name); |
||
| 362 | switch (true) { |
||
| 363 | case $this->twigEnabled && $out !== '' && ($twig = $this->getTwig($name, $out)): |
||
| 364 | $out = $twig->render(md5($name), $this->getTemplateData($data)); |
||
| 365 | break; |
||
| 366 | case $this->bladeEnabled && $out !== '' && ($blade = $this->getBlade($name, $out)): |
||
| 367 | $out = $blade->with($this->getTemplateData($data))->render(); |
||
| 368 | break; |
||
| 369 | case is_array($data) && ($out != ''): |
||
| 370 | if (preg_match("/\[\+[A-Z0-9\.\_\-]+\+\]/is", $out)) { |
||
| 371 | $item = $this->renameKeyArr($data, '[', ']', '+'); |
||
| 372 | $out = str_replace(array_keys($item), array_values($item), $out); |
||
| 373 | } |
||
| 374 | if (!$disablePHx && preg_match("/:([^:=]+)(?:=`(.*?)`(?=:[^:=]+|$))?/is", $out)) { |
||
| 375 | if (is_null($this->phx) || !($this->phx instanceof DLphx)) { |
||
| 376 | $this->phx = $this->createPHx(0, 1000); |
||
| 377 | } |
||
| 378 | $this->phx->placeholders = array(); |
||
| 379 | $this->setPHxPlaceholders($data); |
||
| 380 | $out = $this->phx->Parse($out); |
||
| 381 | $out = $this->cleanPHx($out); |
||
| 382 | } |
||
| 383 | break; |
||
| 384 | } |
||
| 385 | if ($parseDocumentSource) { |
||
| 386 | $out = $this->parseDocumentSource($out); |
||
| 387 | } |
||
| 388 | |||
| 389 | return $out; |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * |
||
| 394 | * @param string|array $value |
||
| 395 | * @param string $key |
||
| 396 | * @param string $path |
||
| 397 | */ |
||
| 398 | public function setPHxPlaceholders($value = '', $key = '', $path = '') |
||
| 399 | { |
||
| 400 | $keypath = !empty($path) ? $path . "." . $key : $key; |
||
| 401 | $this->phx->curPass = 0; |
||
| 402 | if (is_array($value)) { |
||
| 403 | foreach ($value as $subkey => $subval) { |
||
| 404 | $this->setPHxPlaceholders($subval, $subkey, $keypath); |
||
| 405 | } |
||
| 406 | } else { |
||
| 407 | $this->phx->setPHxVariable($keypath, $value); |
||
| 408 | } |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Return clone of twig |
||
| 413 | * |
||
| 414 | * @param string $name |
||
| 415 | * @param string $tpl |
||
| 416 | * @return null |
||
| 417 | */ |
||
| 418 | protected function getTwig($name, $tpl) |
||
| 419 | { |
||
| 420 | if (is_null($this->twig) && isset($this->modx->twig)) { |
||
| 421 | $twig = clone $this->modx->twig; |
||
| 422 | $this->twig = $twig; |
||
| 423 | } else { |
||
| 424 | $twig = $this->twig; |
||
| 425 | } |
||
| 426 | if ($twig && class_exists('Twig_Loader_Array')) { |
||
| 427 | $twig->getLoader()->addLoader( |
||
| 428 | new Twig_Loader_Array(array( |
||
| 429 | md5($name) => $tpl |
||
| 430 | )) |
||
| 431 | ); |
||
| 432 | } |
||
| 433 | |||
| 434 | return $twig; |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Return clone of blade |
||
| 439 | * |
||
| 440 | * @param string $name |
||
| 441 | * @param string $tpl |
||
| 442 | * @return null |
||
| 443 | */ |
||
| 444 | protected function getBlade($name, $tpl) |
||
| 445 | { |
||
| 446 | if ($this->blade === null && isset($this->modx->blade)) { |
||
| 447 | $blade = clone $this->modx->blade; |
||
| 448 | $this->blade = $blade; |
||
| 449 | } else { |
||
| 450 | $blade = $this->blade; |
||
| 451 | } |
||
| 452 | |||
| 453 | $cache = md5($name). '-'. sha1($tpl); |
||
| 454 | $path = $blade->viewPaths . '/~cache/' . $cache . '.blade.php'; |
||
| 455 | if (! file_exists($path)) { |
||
| 456 | file_put_contents($path, $tpl); |
||
| 457 | } |
||
| 458 | |||
| 459 | return $blade->make('~cache/' . $cache); |
||
| 460 | } |
||
| 461 | |||
| 462 | /** |
||
| 463 | * |
||
| 464 | * @param string $string |
||
| 465 | * @return string |
||
| 466 | */ |
||
| 467 | public function cleanPHx($string) |
||
| 468 | { |
||
| 469 | preg_match_all('~\[(\+|\*|\()([^:\+\[\]]+)([^\[\]]*?)(\1|\))\]~s', $string, $matches); |
||
| 470 | if ($matches[0]) { |
||
| 471 | $string = str_replace($matches[0], '', $string); |
||
| 472 | } |
||
| 473 | |||
| 474 | return $string; |
||
| 475 | } |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @param int $debug |
||
| 479 | * @param int $maxpass |
||
| 480 | * @return DLphx |
||
| 481 | */ |
||
| 482 | public function createPHx($debug = 0, $maxpass = 50) |
||
| 483 | { |
||
| 484 | if (!class_exists('DLphx')) { |
||
| 485 | include_once(__DIR__ . '/DLphx.class.php'); |
||
| 486 | } |
||
| 487 | |||
| 488 | return new DLphx($this->modx, $debug, $maxpass); |
||
| 489 | } |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Переменовывание элементов массива |
||
| 493 | * |
||
| 494 | * @param array $data массив с данными |
||
| 495 | * @param string $prefix префикс ключей |
||
| 496 | * @param string $suffix суффикс ключей |
||
| 497 | * @param string $sep разделитель суффиксов, префиксов и ключей массива |
||
| 498 | * @return array массив с переименованными ключами |
||
| 499 | */ |
||
| 500 | public function renameKeyArr($data, $prefix = '', $suffix = '', $sep = '.') |
||
| 501 | { |
||
| 502 | return APIhelpers::renameKeyArr($data, $prefix, $suffix, $sep); |
||
| 503 | } |
||
| 504 | |||
| 505 | /** |
||
| 506 | * @param $out |
||
| 507 | * @param DocumentParser|null $modx |
||
| 508 | * @return mixed|string |
||
| 509 | */ |
||
| 510 | public function parseDocumentSource($out, $modx = null) |
||
| 535 | } |
||
| 536 | } |
||
| 537 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths