Conditions | 17 |
Paths | 50 |
Total Lines | 46 |
Lines | 6 |
Ratio | 13.04 % |
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 |
||
7 | function getDefaultTemplate() |
||
8 | { |
||
9 | $modx = evolutionCMS(); |
||
10 | |||
11 | $default_template = ''; |
||
12 | switch ($modx->config['auto_template_logic']) { |
||
13 | case 'sibling': |
||
14 | if (!isset($_GET['pid']) || empty($_GET['pid'])) { |
||
15 | $site_start = $modx->config['site_start']; |
||
16 | $where = "sc.isfolder=0 AND sc.id!='{$site_start}'"; |
||
17 | $sibl = $modx->getDocumentChildren($_REQUEST['pid'], 1, 0, 'template', $where, 'menuindex', 'ASC', |
||
18 | 1); |
||
19 | View Code Duplication | if (isset($sibl[0]['template']) && $sibl[0]['template'] !== '') { |
|
20 | $default_template = $sibl[0]['template']; |
||
21 | } |
||
22 | } else { |
||
23 | $sibl = $modx->getDocumentChildren($_REQUEST['pid'], 1, 0, 'template', 'isfolder=0', 'menuindex', |
||
24 | 'ASC', 1); |
||
25 | if (isset($sibl[0]['template']) && $sibl[0]['template'] !== '') { |
||
26 | $default_template = $sibl[0]['template']; |
||
27 | } else { |
||
28 | $sibl = $modx->getDocumentChildren($_REQUEST['pid'], 0, 0, 'template', 'isfolder=0', |
||
29 | 'menuindex', 'ASC', 1); |
||
30 | View Code Duplication | if (isset($sibl[0]['template']) && $sibl[0]['template'] !== '') { |
|
31 | $default_template = $sibl[0]['template']; |
||
32 | } |
||
33 | } |
||
34 | } |
||
35 | if (isset($default_template)) { |
||
36 | break; |
||
37 | } // If $default_template could not be determined, fall back / through to "parent"-mode |
||
38 | case 'parent': |
||
39 | if (isset($_REQUEST['pid']) && !empty($_REQUEST['pid'])) { |
||
40 | $parent = $modx->getPageInfo($_REQUEST['pid'], 0, 'template'); |
||
41 | if (isset($parent['template'])) { |
||
42 | $default_template = $parent['template']; |
||
43 | } |
||
44 | } |
||
45 | break; |
||
46 | case 'system': |
||
47 | default: // default_template is already set |
||
48 | $default_template = $modx->config['default_template']; |
||
49 | } |
||
50 | |||
51 | return empty($default_template) ? $modx->config['default_template'] : $default_template; |
||
52 | } |
||
53 | } |
||
54 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: