| Conditions | 30 |
| Paths | > 20000 |
| Total Lines | 184 |
| Lines | 6 |
| Ratio | 3.26 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php namespace EvolutionCMS; |
||
| 256 | public function buildCache($modx) |
||
| 257 | { |
||
| 258 | $content = "<?php\n"; |
||
| 259 | |||
| 260 | // SETTINGS & DOCUMENT LISTINGS CACHE |
||
| 261 | |||
| 262 | // get settings |
||
| 263 | $rs = $modx->getDatabase()->select('*', $modx->getDatabase()->getFullTableName('system_settings')); |
||
| 264 | $config = array(); |
||
| 265 | $content .= '$c=&$this->config;'; |
||
| 266 | while (list($key, $value) = $modx->getDatabase()->getRow($rs, 'num')) { |
||
| 267 | $content .= '$c[\'' . $key . '\']="' . $this->escapeDoubleQuotes($value) . '";'; |
||
| 268 | $config[$key] = $value; |
||
| 269 | } |
||
| 270 | |||
| 271 | if ($config['enable_filter']) { |
||
| 272 | $where = "plugincode LIKE '%phx.parser.class.inc.php%OnParseDocument();%' AND disabled != 1"; |
||
| 273 | $count = $modx->getDatabase()->getRecordCount( |
||
| 274 | $modx->getDatabase()->select('id', $modx->getDatabase()->getFullTableName('site_plugins'), $where) |
||
| 275 | ); |
||
| 276 | if ($count) { |
||
| 277 | $content .= '$this->config[\'enable_filter\']=\'0\';'; |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | if ($config['aliaslistingfolder'] == 1) { |
||
| 282 | $f['id'] = 'c.id'; |
||
| 283 | $f['alias'] = "IF( c.alias='', c.id, c.alias)"; |
||
| 284 | $f['parent'] = 'c.parent'; |
||
| 285 | $f['isfolder'] = 'c.isfolder'; |
||
| 286 | $f['alias_visible'] = 'c.alias_visible'; |
||
| 287 | $from = array(); |
||
| 288 | $from[] = $modx->getDatabase()->getFullTableName('site_content') . ' c'; |
||
| 289 | $from[] = 'LEFT JOIN ' . $modx->getDatabase()->getFullTableName('site_content') . ' p ON p.id=c.parent'; |
||
| 290 | $where = 'c.deleted=0 AND (c.isfolder=1 OR p.alias_visible=0)'; |
||
| 291 | $rs = $modx->getDatabase()->select($f, $from, $where, 'c.parent, c.menuindex'); |
||
| 292 | } else { |
||
| 293 | $rs = $modx->getDatabase()->select( |
||
| 294 | "id, IF(alias='', id, alias) AS alias, parent, isfolder, alias_visible", |
||
| 295 | $modx->getDatabase()->getFullTableName('site_content'), |
||
| 296 | 'deleted=0', |
||
| 297 | 'parent, menuindex' |
||
| 298 | ); |
||
| 299 | } |
||
| 300 | |||
| 301 | $use_alias_path = ($config['friendly_urls'] && $config['use_alias_path']) ? 1 : 0; |
||
| 302 | $tmpPath = ''; |
||
| 303 | $content .= '$this->aliasListing=array();'; |
||
| 304 | $content .= '$a=&$this->aliasListing;'; |
||
| 305 | $content .= '$d=&$this->documentListing;'; |
||
| 306 | $content .= '$m=&$this->documentMap;'; |
||
| 307 | while ($doc = $modx->getDatabase()->getRow($rs)) { |
||
| 308 | $docid = $doc['id']; |
||
| 309 | if ($use_alias_path) { |
||
| 310 | $tmpPath = $this->getParents($doc['parent']); |
||
| 311 | $alias = (strlen($tmpPath) > 0 ? "$tmpPath/" : '') . $doc['alias']; |
||
| 312 | $key = $alias; |
||
| 313 | } else { |
||
| 314 | $key = $doc['alias']; |
||
| 315 | } |
||
| 316 | |||
| 317 | $doc['path'] = $tmpPath; |
||
| 318 | $content .= '$a[' . $docid . ']=array(\'id\'=>' . $docid . ',\'alias\'=>\'' . $doc['alias'] . '\',\'path\'=>\'' . $doc['path'] . '\',\'parent\'=>' . $doc['parent'] . ',\'isfolder\'=>' . $doc['isfolder'] . ',\'alias_visible\'=>' . $doc['alias_visible'] . ');'; |
||
| 319 | $content .= '$d[\'' . $key . '\']=' . $docid . ';'; |
||
| 320 | $content .= '$m[]=array(' . $doc['parent'] . '=>' . $docid . ');'; |
||
| 321 | } |
||
| 322 | |||
| 323 | // get content types |
||
| 324 | $rs = $modx->getDatabase()->select( |
||
| 325 | 'id, contentType', |
||
| 326 | $modx->getDatabase()->getFullTableName('site_content'), |
||
| 327 | "contentType!='text/html'" |
||
| 328 | ); |
||
| 329 | $content .= '$c=&$this->contentTypes;'; |
||
| 330 | while ($doc = $modx->getDatabase()->getRow($rs)) { |
||
| 331 | $content .= '$c[\'' . $doc['id'] . '\']=\'' . $doc['contentType'] . '\';'; |
||
| 332 | } |
||
| 333 | |||
| 334 | // WRITE Chunks to cache file |
||
| 335 | $rs = $modx->getDatabase()->select('*', $modx->getDatabase()->getFullTableName('site_htmlsnippets')); |
||
| 336 | $content .= '$c=&$this->chunkCache;'; |
||
| 337 | while ($doc = $modx->getDatabase()->getRow($rs)) { |
||
| 338 | if ($modx->config['minifyphp_incache']) { |
||
| 339 | $doc['snippet'] = $this->php_strip_whitespace($doc['snippet']); |
||
| 340 | } |
||
| 341 | $content .= '$c[\'' . $doc['name'] . '\']=\'' . ($doc['disabled'] ? '' : $this->escapeSingleQuotes($doc['snippet'])) . '\';'; |
||
| 342 | } |
||
| 343 | |||
| 344 | // WRITE snippets to cache file |
||
| 345 | $f = 'ss.*, sm.properties as sharedproperties'; |
||
| 346 | $from = $modx->getDatabase()->getFullTableName('site_snippets') . ' ss LEFT JOIN ' . |
||
| 347 | $modx->getDatabase()->getFullTableName('site_modules') . ' sm on sm.guid=ss.moduleguid'; |
||
| 348 | $rs = $modx->getDatabase()->select($f, $from); |
||
| 349 | $content .= '$s=&$this->snippetCache;'; |
||
| 350 | while ($row = $modx->getDatabase()->getRow($rs)) { |
||
| 351 | $key = $row['name']; |
||
| 352 | if ($row['disabled']) { |
||
| 353 | $content .= '$s[\'' . $key . '\']=\'return false;\';'; |
||
| 354 | } else { |
||
| 355 | $value = trim($row['snippet']); |
||
| 356 | if ($modx->config['minifyphp_incache']) { |
||
| 357 | $value = $this->php_strip_whitespace($value); |
||
| 358 | } |
||
| 359 | $content .= '$s[\'' . $key . '\']=\'' . $this->escapeSingleQuotes($value) . '\';'; |
||
| 360 | $properties = $modx->parseProperties($row['properties']); |
||
| 361 | $sharedproperties = $modx->parseProperties($row['sharedproperties']); |
||
| 362 | $properties = array_merge($sharedproperties, $properties); |
||
| 363 | View Code Duplication | if (0 < count($properties)) { |
|
| 364 | $content .= '$s[\'' . $key . 'Props\']=\'' . $this->escapeSingleQuotes(json_encode($properties)) . '\';'; |
||
| 365 | } |
||
| 366 | } |
||
| 367 | } |
||
| 368 | |||
| 369 | // WRITE plugins to cache file |
||
| 370 | $f = 'sp.*, sm.properties as sharedproperties'; |
||
| 371 | $from = array(); |
||
| 372 | $from[] = $modx->getDatabase()->getFullTableName('site_plugins') . ' sp'; |
||
| 373 | $from[] = 'LEFT JOIN ' . $modx->getDatabase()->getFullTableName('site_modules') . ' sm on sm.guid=sp.moduleguid'; |
||
| 374 | $rs = $modx->getDatabase()->select($f, $from, 'sp.disabled=0'); |
||
| 375 | $content .= '$p=&$this->pluginCache;'; |
||
| 376 | while ($row = $modx->getDatabase()->getRow($rs)) { |
||
| 377 | $key = $row['name']; |
||
| 378 | $value = trim($row['plugincode']); |
||
| 379 | if ($modx->config['minifyphp_incache']) { |
||
| 380 | $value = $this->php_strip_whitespace($value); |
||
| 381 | } |
||
| 382 | $content .= '$p[\'' . $key . '\']=\'' . $this->escapeSingleQuotes($value) . '\';'; |
||
| 383 | if ($row['properties'] != '' || $row['sharedproperties'] != '') { |
||
| 384 | $properties = $modx->parseProperties($row['properties']); |
||
| 385 | $sharedproperties = $modx->parseProperties($row['sharedproperties']); |
||
| 386 | $properties = array_merge($sharedproperties, $properties); |
||
| 387 | View Code Duplication | if (0 < count($properties)) { |
|
| 388 | $content .= '$p[\'' . $key . 'Props\']=\'' . $this->escapeSingleQuotes(json_encode($properties)) . '\';'; |
||
| 389 | } |
||
| 390 | } |
||
| 391 | } |
||
| 392 | |||
| 393 | // WRITE system event triggers |
||
| 394 | $f = 'sysevt.name as evtname, event.pluginid, plugin.name as pname'; |
||
| 395 | $from = array(); |
||
| 396 | $from[] = $modx->getDatabase()->getFullTableName('system_eventnames') . ' sysevt'; |
||
| 397 | $from[] = 'INNER JOIN ' . $modx->getDatabase()->getFullTableName('site_plugin_events') . ' event ON event.evtid=sysevt.id'; |
||
| 398 | $from[] = 'INNER JOIN ' . $modx->getDatabase()->getFullTableName('site_plugins') . ' plugin ON plugin.id=event.pluginid'; |
||
| 399 | $rs = $modx->getDatabase()->select($f, $from, 'plugin.disabled=0', 'sysevt.name, event.priority'); |
||
| 400 | $content .= '$e=&$this->pluginEvent;'; |
||
| 401 | $events = array(); |
||
| 402 | while ($row = $modx->getDatabase()->getRow($rs)) { |
||
| 403 | $evtname = $row['evtname']; |
||
| 404 | if (!isset($events[$evtname])) { |
||
| 405 | $events[$evtname] = array(); |
||
| 406 | } |
||
| 407 | $events[$evtname][] = $row['pname']; |
||
| 408 | } |
||
| 409 | foreach ($events as $evtname => $pluginnames) { |
||
| 410 | $events[$evtname] = $pluginnames; |
||
| 411 | $content .= '$e[\'' . $evtname . '\']=array(\'' . implode('\',\'', |
||
| 412 | $this->escapeSingleQuotes($pluginnames)) . '\');'; |
||
| 413 | } |
||
| 414 | |||
| 415 | $content .= "\n"; |
||
| 416 | |||
| 417 | // close and write the file |
||
| 418 | $filename = $this->cachePath . 'siteCache.idx.php'; |
||
| 419 | |||
| 420 | // invoke OnBeforeCacheUpdate event |
||
| 421 | if ($modx) { |
||
| 422 | $modx->invokeEvent('OnBeforeCacheUpdate'); |
||
| 423 | } |
||
| 424 | |||
| 425 | if (@file_put_contents($filename, $content) === false) { |
||
| 426 | exit("Cannot write main MODX cache file! Make sure the assets/cache directory is writable!"); |
||
| 427 | } |
||
| 428 | |||
| 429 | if (!is_file($this->cachePath . '/.htaccess')) { |
||
| 430 | file_put_contents($this->cachePath . '/.htaccess', "order deny,allow\ndeny from all\n"); |
||
| 431 | } |
||
| 432 | |||
| 433 | // invoke OnCacheUpdate event |
||
| 434 | if ($modx) { |
||
| 435 | $modx->invokeEvent('OnCacheUpdate'); |
||
| 436 | } |
||
| 437 | |||
| 438 | return true; |
||
| 439 | } |
||
| 440 | |||
| 516 |
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.