Completed
Push — develop ( 5cb106...80f130 )
by Dmytro
13:36
created

mutate_content.php ➔ getDefaultTemplate()   C

Complexity

Conditions 17
Paths 50

Size

Total Lines 46

Duplication

Lines 6
Ratio 13.04 %

Importance

Changes 0
Metric Value
cc 17
nc 50
nop 0
dl 6
loc 46
rs 5.2166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
if(!function_exists('getDefaultTemplate')) {
4
    /**
5
     * @return string
6
     */
7
    function getDefaultTemplate()
0 ignored issues
show
Coding Style introduced by
getDefaultTemplate uses the super-global variable $_GET which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
getDefaultTemplate uses the super-global variable $_REQUEST which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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