Conditions | 30 |
Paths | > 20000 |
Total Lines | 173 |
Code Lines | 119 |
Lines | 6 |
Ratio | 3.47 % |
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; |
||
247 | public function buildCache($modx) |
||
248 | { |
||
249 | $content = "<?php\n"; |
||
250 | |||
251 | // SETTINGS & DOCUMENT LISTINGS CACHE |
||
252 | |||
253 | // get settings |
||
254 | $rs = $modx->db->select('*', '[+prefix+]system_settings'); |
||
255 | $config = array(); |
||
256 | $content .= '$c=&$this->config;'; |
||
257 | while (list($key, $value) = $modx->db->getRow($rs, 'num')) { |
||
258 | $content .= '$c[\'' . $key . '\']="' . $this->escapeDoubleQuotes($value) . '";'; |
||
259 | $config[$key] = $value; |
||
260 | } |
||
261 | |||
262 | if ($config['enable_filter']) { |
||
263 | $where = "plugincode LIKE '%phx.parser.class.inc.php%OnParseDocument();%' AND disabled != 1"; |
||
264 | $count = $modx->db->getRecordCount($modx->db->select('id', '[+prefix+]site_plugins', $where)); |
||
265 | if ($count) { |
||
266 | $content .= '$this->config[\'enable_filter\']=\'0\';'; |
||
267 | } |
||
268 | } |
||
269 | |||
270 | if ($config['aliaslistingfolder'] == 1) { |
||
271 | $f['id'] = 'c.id'; |
||
272 | $f['alias'] = "IF( c.alias='', c.id, c.alias)"; |
||
273 | $f['parent'] = 'c.parent'; |
||
274 | $f['isfolder'] = 'c.isfolder'; |
||
275 | $f['alias_visible'] = 'c.alias_visible'; |
||
276 | $from = array(); |
||
277 | $from[] = '[+prefix+]site_content c'; |
||
278 | $from[] = 'LEFT JOIN [+prefix+]site_content p ON p.id=c.parent'; |
||
279 | $where = 'c.deleted=0 AND (c.isfolder=1 OR p.alias_visible=0)'; |
||
280 | $rs = $modx->db->select($f, $from, $where, 'c.parent, c.menuindex'); |
||
281 | } else { |
||
282 | $f = "id, IF(alias='', id, alias) AS alias, parent, isfolder, alias_visible"; |
||
283 | $rs = $modx->db->select($f, '[+prefix+]site_content', 'deleted=0', 'parent, menuindex'); |
||
284 | } |
||
285 | |||
286 | $use_alias_path = ($config['friendly_urls'] && $config['use_alias_path']) ? 1 : 0; |
||
287 | $tmpPath = ''; |
||
288 | $content .= '$this->aliasListing=array();'; |
||
289 | $content .= '$a=&$this->aliasListing;'; |
||
290 | $content .= '$d=&$this->documentListing;'; |
||
291 | $content .= '$m=&$this->documentMap;'; |
||
292 | while ($doc = $modx->db->getRow($rs)) { |
||
293 | $docid = $doc['id']; |
||
294 | if ($use_alias_path) { |
||
295 | $tmpPath = $this->getParents($doc['parent']); |
||
296 | $alias = (strlen($tmpPath) > 0 ? "$tmpPath/" : '') . $doc['alias']; |
||
297 | $key = $alias; |
||
298 | } else { |
||
299 | $key = $doc['alias']; |
||
300 | } |
||
301 | |||
302 | $doc['path'] = $tmpPath; |
||
303 | $content .= '$a[' . $docid . ']=array(\'id\'=>' . $docid . ',\'alias\'=>\'' . $doc['alias'] . '\',\'path\'=>\'' . $doc['path'] . '\',\'parent\'=>' . $doc['parent'] . ',\'isfolder\'=>' . $doc['isfolder'] . ',\'alias_visible\'=>' . $doc['alias_visible'] . ');'; |
||
304 | $content .= '$d[\'' . $key . '\']=' . $docid . ';'; |
||
305 | $content .= '$m[]=array(' . $doc['parent'] . '=>' . $docid . ');'; |
||
306 | } |
||
307 | |||
308 | // get content types |
||
309 | $rs = $modx->db->select('id, contentType', '[+prefix+]site_content', "contentType!='text/html'"); |
||
310 | $content .= '$c=&$this->contentTypes;'; |
||
311 | while ($doc = $modx->db->getRow($rs)) { |
||
312 | $content .= '$c[\'' . $doc['id'] . '\']=\'' . $doc['contentType'] . '\';'; |
||
313 | } |
||
314 | |||
315 | // WRITE Chunks to cache file |
||
316 | $rs = $modx->db->select('*', '[+prefix+]site_htmlsnippets'); |
||
317 | $content .= '$c=&$this->chunkCache;'; |
||
318 | while ($doc = $modx->db->getRow($rs)) { |
||
319 | if ($modx->config['minifyphp_incache']) { |
||
320 | $doc['snippet'] = $this->php_strip_whitespace($doc['snippet']); |
||
321 | } |
||
322 | $content .= '$c[\'' . $doc['name'] . '\']=\'' . ($doc['disabled'] ? '' : $this->escapeSingleQuotes($doc['snippet'])) . '\';'; |
||
323 | } |
||
324 | |||
325 | // WRITE snippets to cache file |
||
326 | $f = 'ss.*, sm.properties as sharedproperties'; |
||
327 | $from = '[+prefix+]site_snippets ss LEFT JOIN [+prefix+]site_modules sm on sm.guid=ss.moduleguid'; |
||
328 | $rs = $modx->db->select($f, $from); |
||
329 | $content .= '$s=&$this->snippetCache;'; |
||
330 | while ($row = $modx->db->getRow($rs)) { |
||
331 | $key = $row['name']; |
||
332 | if ($row['disabled']) { |
||
333 | $content .= '$s[\'' . $key . '\']=\'return false;\';'; |
||
334 | } else { |
||
335 | $value = trim($row['snippet']); |
||
336 | if ($modx->config['minifyphp_incache']) { |
||
337 | $value = $this->php_strip_whitespace($value); |
||
338 | } |
||
339 | $content .= '$s[\'' . $key . '\']=\'' . $this->escapeSingleQuotes($value) . '\';'; |
||
340 | $properties = $modx->parseProperties($row['properties']); |
||
341 | $sharedproperties = $modx->parseProperties($row['sharedproperties']); |
||
342 | $properties = array_merge($sharedproperties, $properties); |
||
343 | View Code Duplication | if (0 < count($properties)) { |
|
344 | $content .= '$s[\'' . $key . 'Props\']=\'' . $this->escapeSingleQuotes(json_encode($properties)) . '\';'; |
||
345 | } |
||
346 | } |
||
347 | } |
||
348 | |||
349 | // WRITE plugins to cache file |
||
350 | $f = 'sp.*, sm.properties as sharedproperties'; |
||
351 | $from = array(); |
||
352 | $from[] = '[+prefix+]site_plugins sp'; |
||
353 | $from[] = 'LEFT JOIN [+prefix+]site_modules sm on sm.guid=sp.moduleguid'; |
||
354 | $rs = $modx->db->select($f, $from, 'sp.disabled=0'); |
||
355 | $content .= '$p=&$this->pluginCache;'; |
||
356 | while ($row = $modx->db->getRow($rs)) { |
||
357 | $key = $row['name']; |
||
358 | $value = trim($row['plugincode']); |
||
359 | if ($modx->config['minifyphp_incache']) { |
||
360 | $value = $this->php_strip_whitespace($value); |
||
361 | } |
||
362 | $content .= '$p[\'' . $key . '\']=\'' . $this->escapeSingleQuotes($value) . '\';'; |
||
363 | if ($row['properties'] != '' || $row['sharedproperties'] != '') { |
||
364 | $properties = $modx->parseProperties($row['properties']); |
||
365 | $sharedproperties = $modx->parseProperties($row['sharedproperties']); |
||
366 | $properties = array_merge($sharedproperties, $properties); |
||
367 | View Code Duplication | if (0 < count($properties)) { |
|
368 | $content .= '$p[\'' . $key . 'Props\']=\'' . $this->escapeSingleQuotes(json_encode($properties)) . '\';'; |
||
369 | } |
||
370 | } |
||
371 | } |
||
372 | |||
373 | // WRITE system event triggers |
||
374 | $f = 'sysevt.name as evtname, event.pluginid, plugin.name as pname'; |
||
375 | $from = array(); |
||
376 | $from[] = '[+prefix+]system_eventnames sysevt'; |
||
377 | $from[] = 'INNER JOIN [+prefix+]site_plugin_events event ON event.evtid=sysevt.id'; |
||
378 | $from[] = 'INNER JOIN [+prefix+]site_plugins plugin ON plugin.id=event.pluginid'; |
||
379 | $rs = $modx->db->select($f, $from, 'plugin.disabled=0', 'sysevt.name, event.priority'); |
||
380 | $content .= '$e=&$this->pluginEvent;'; |
||
381 | $events = array(); |
||
382 | while ($row = $modx->db->getRow($rs)) { |
||
383 | $evtname = $row['evtname']; |
||
384 | if (!isset($events[$evtname])) { |
||
385 | $events[$evtname] = array(); |
||
386 | } |
||
387 | $events[$evtname][] = $row['pname']; |
||
388 | } |
||
389 | foreach ($events as $evtname => $pluginnames) { |
||
390 | $events[$evtname] = $pluginnames; |
||
391 | $content .= '$e[\'' . $evtname . '\']=array(\'' . implode('\',\'', |
||
392 | $this->escapeSingleQuotes($pluginnames)) . '\');'; |
||
393 | } |
||
394 | |||
395 | $content .= "\n"; |
||
396 | |||
397 | // close and write the file |
||
398 | $filename = $this->cachePath . 'siteCache.idx.php'; |
||
399 | |||
400 | // invoke OnBeforeCacheUpdate event |
||
401 | if ($modx) { |
||
402 | $modx->invokeEvent('OnBeforeCacheUpdate'); |
||
403 | } |
||
404 | |||
405 | if (@file_put_contents($filename, $content) === false) { |
||
406 | exit("Cannot write main MODX cache file! Make sure the assets/cache directory is writable!"); |
||
407 | } |
||
408 | |||
409 | if (!is_file($this->cachePath . '/.htaccess')) { |
||
410 | file_put_contents($this->cachePath . '/.htaccess', "order deny,allow\ndeny from all\n"); |
||
411 | } |
||
412 | |||
413 | // invoke OnCacheUpdate event |
||
414 | if ($modx) { |
||
415 | $modx->invokeEvent('OnCacheUpdate'); |
||
416 | } |
||
417 | |||
418 | return true; |
||
419 | } |
||
420 | |||
496 |
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
.