DLFixedPrepare::buildMenu()   F
last analyzed

Complexity

Conditions 12
Paths 432

Size

Total Lines 44
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 44
rs 3.5888
cc 12
nc 432
nop 4

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 (!class_exists("DLFixedPrepare", false)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after NOT operator; 0 found
Loading history...
4
    /**
5
     * Class DLFixedPrepare
6
     */
7
    class DLFixedPrepare
8
    {
9
        /**
10
         * @param array $data
11
         * @param DocumentParser $modx
12
         * @param $_DL
13
         * @param prepare_DL_Extender $_eDL
14
         * @return array
15
         */
16
        public static function buildMenu(array $data, DocumentParser $modx, $_DL, prepare_DL_Extender $_eDL)
17
        {
18
            $params = $_DL->getCFGDef('params', array());
19
            if ($_DL->getCfgDef('currentDepth', 1) < $_DL->getCFGDef('maxDepth', 5)) {
20
                $params['currentDepth'] = $_DL->getCfgDef('currentDepth', 1) + 1;
21
                $params['parents'] = $data['id'];
22
                $params['idType'] = 'parents';
23
                $params['documents'] = '';
24
                $data['dl.submenu'] = ($data['isfolder']) ? $modx->runSnippet('DLBuildMenu', $params) : '';
0 ignored issues
show
Bug introduced by
The method runSnippet() does not exist on DocumentParser. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
                $data['dl.submenu'] = ($data['isfolder']) ? $modx->/** @scrutinizer ignore-call */ runSnippet('DLBuildMenu', $params) : '';

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
25
            } else {
26
                $data['dl.submenu'] = '';
27
            }
28
            $data['dl.currentDepth'] = $_DL->getCfgDef('currentDepth', 1);
29
30
            if (($parentIDs = $_eDL->getStore('parentIDs')) === null) {
31
                $parentIDs = array_values($modx->getParentIds($modx->documentObject['id']));
0 ignored issues
show
Bug introduced by
The property documentObject does not seem to exist on DocumentParser.
Loading history...
Bug introduced by
The method getParentIds() does not exist on DocumentParser. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
                $parentIDs = array_values($modx->/** @scrutinizer ignore-call */ getParentIds($modx->documentObject['id']));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
32
                $_eDL->setStore('parentIDs', $parentIDs);
33
            }
34
            $isActive = ((is_array($parentIDs) && in_array($data['id'],
35
                        $parentIDs)) || $data['id'] == $modx->documentObject['id']);
36
            $activeClass = $_DL->getCfgDef('activeClass', 'active');
37
            if ($isActive) {
38
                $data['dl.class'] .= ' ' . $activeClass;
39
            }
40
41
            if (strpos($data['dl.class'], 'current') !== false && strpos($data['dl.class'],
42
                    ' ' . $activeClass) === false
43
            ) {
44
                $data['dl.class'] = str_replace('current', 'current ' . $activeClass, $data['dl.class']);
45
            }
46
47
            $tpl = empty($data['dl.submenu']) ? 'noChildrenRowTPL' : 'mainRowTpl';
48
49
            $_DL->renderTPL = $_DL->getCfgDef($tpl);
50
            if (strpos($data['dl.class'], 'current') !== false) {
51
                $_DL->renderTPL = $_DL->getCfgDef('TplCurrent', $_DL->renderTPL);
52
                $_DL->renderTPL = $_DL->getCfgDef('TplCurrent' . $data['dl.currentDepth'], $_DL->renderTPL);
53
                if (empty($data['dl.submenu'])) {
54
                    $_DL->renderTPL = $_DL->getCfgDef('TplCurrentNoChildren' . $data['dl.currentDepth'],
55
                        $_DL->renderTPL);
56
                }
57
            }
58
59
            return $data;
60
        }
61
62
        /**
63
         * @param array $data
64
         * @param DocumentParser $modx
65
         * @param DocLister $_DocLister
66
         * @param prepare_DL_Extender $_extDocLister
67
         * @return array
68
         */
69
        public static function firstChar(
0 ignored issues
show
Coding Style introduced by
Function's nesting level (4) exceeds 3; consider refactoring the function
Loading history...
70
            array $data,
71
            DocumentParser $modx,
72
            DocLister $_DocLister,
73
            prepare_DL_Extender $_extDocLister
74
        ) {
75
            $char = mb_substr($data['pagetitle'], 0, 1, 'UTF-8');
76
            $oldChar = $_extDocLister->getStore('char');
77
            if ($oldChar !== $char) {
78
                $sanitarInIDs = $_DocLister->sanitarIn($_DocLister->getIDs());
79
                $where = sqlHelper::trimLogicalOp($_DocLister->getCFGDef('addWhereList', ''));
80
                $where = sqlHelper::trimLogicalOp(($where ? $where . ' AND ' : '') . $_DocLister->filtersWhere());
81
                $where = sqlHelper::trimLogicalOp(($where ? $where . ' AND ' : '') . "SUBSTRING(c.pagetitle,1,1) = '" . $modx->db->escape($char) . "'");
82
83
                if ($_DocLister->getCFGDef('idType', 'parents') == 'parents') {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
84
85
                    if ($where != '') {
86
                        $where .= " AND ";
87
                    }
88
                    $where = "WHERE {$where} c.parent IN (" . $sanitarInIDs . ")";
89
                    if (!$_DocLister->getCFGDef('showNoPublish', 0)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after NOT operator; 0 found
Loading history...
90
                        $where .= " AND c.deleted=0 AND c.published=1";
91
                    }
92
                } else {
93
                    if ($sanitarInIDs != "''") {
94
                        $where .= ($where ? " AND " : "") . "c.id IN ({$sanitarInIDs}) AND";
95
                    }
96
                    $where = sqlHelper::trimLogicalOp($where);
97
                    if ($_DocLister->getCFGDef('showNoPublish', 0)) {
98
                        if ($where != '') {
99
                            $where = "WHERE {$where}";
100
                        }
101
                    } else {
102
                        if ($where != '') {
103
                            $where = "WHERE {$where} AND ";
104
                        } else {
105
                            $where = "WHERE {$where} ";
106
                        }
107
                        $where .= "c.deleted=0 AND c.published=1";
108
                    }
109
                }
110
                $q = $_DocLister->dbQuery("SELECT count(c.id) as total FROM " . $_DocLister->getTable('site_content',
111
                        'c') . " " . $where);
112
                $total = $modx->db->getValue($q);
113
                $data['OnNewChar'] = $_DocLister->parseChunk($_DocLister->getCFGDef('tplOnNewChar'),
114
                    compact("char", "total"));
115
                $_extDocLister->setStore('char', $char);
116
117
                if ($oldChar !== null) {
118
                    $data['CharSeparator'] = $_DocLister->parseChunk($_DocLister->getCFGDef('tplCharSeparator'),
119
                        compact("char", "total"));
120
                }
121
            }
122
123
            return $data;
124
        }
125
    }
126
}
127