Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( c45d66...93ac39 )
by Sebastian
18s queued 13s
created

TableOfContents::getMenuEntry()   F

Complexity

Conditions 20
Paths 140

Size

Total Lines 62
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 62
rs 3.8333
c 0
b 0
f 0
cc 20
nc 140
nop 2

How to fix   Long Method    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
namespace Kitodo\Dlf\Plugin;
3
4
/**
5
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
6
 *
7
 * This file is part of the Kitodo and TYPO3 projects.
8
 *
9
 * @license GNU General Public License version 3 or later.
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 */
13
14
use Kitodo\Dlf\Common\Helper;
15
16
/**
17
 * Plugin 'Table Of Contents' for the 'dlf' extension
18
 *
19
 * @author Sebastian Meyer <[email protected]>
20
 * @package TYPO3
21
 * @subpackage dlf
22
 * @access public
23
 */
24
class TableOfContents extends \Kitodo\Dlf\Common\AbstractPlugin {
25
    public $scriptRelPath = 'Classes/Plugin/TableOfContents.php';
26
27
    /**
28
     * This holds the active entries according to the currently selected page
29
     *
30
     * @var array
31
     * @access protected
32
     */
33
    protected $activeEntries = [];
34
35
    /**
36
     * This builds an array for one menu entry
37
     *
38
     * @access protected
39
     *
40
     * @param array $entry: The entry's array from \Kitodo\Dlf\Common\Document->getLogicalStructure
41
     * @param boolean $recursive: Whether to include the child entries
42
     *
43
     * @return array HMENU array for menu entry
44
     */
45
    protected function getMenuEntry(array $entry, $recursive = FALSE) {
46
        $entryArray = [];
47
        // Set "title", "volume", "type" and "pagination" from $entry array.
48
        $entryArray['title'] = !empty($entry['label']) ? $entry['label'] : $entry['orderlabel'];
49
        $entryArray['volume'] = $entry['volume'];
50
        $entryArray['orderlabel'] = $entry['orderlabel'];
51
        $entryArray['type'] = Helper::translate($entry['type'], 'tx_dlf_structures', $this->conf['pages']);
52
        $entryArray['pagination'] = htmlspecialchars($entry['pagination']);
53
        $entryArray['_OVERRIDE_HREF'] = '';
54
        $entryArray['doNotLinkIt'] = 1;
55
        $entryArray['ITEM_STATE'] = 'NO';
56
        // Build menu links based on the $entry['points'] array.
57
        if (!empty($entry['points'])
58
            && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($entry['points'])) {
59
            $entryArray['_OVERRIDE_HREF'] = $this->pi_linkTP_keepPIvars_url(['page' => $entry['points']], TRUE, FALSE, $this->conf['targetPid']);
60
            $entryArray['doNotLinkIt'] = 0;
61
            if ($this->conf['basketButton']) {
62
                $entryArray['basketButtonHref'] = '<a href="'.$this->pi_linkTP_keepPIvars_url(['addToBasket' => 'toc', 'logId' => $entry['id'], 'startpage' => $entry['points']], TRUE, FALSE, $this->conf['targetBasket']).'">'.$this->pi_getLL('basketButton', '', TRUE).'</a>';
63
            }
64
        } elseif (!empty($entry['points'])
65
            && is_string($entry['points'])) {
66
            $entryArray['_OVERRIDE_HREF'] = $this->pi_linkTP_keepPIvars_url(['id' => $entry['points'], 'page' => 1], TRUE, FALSE, $this->conf['targetPid']);
67
            $entryArray['doNotLinkIt'] = 0;
68
            if ($this->conf['basketButton']) {
69
                $entryArray['basketButtonHref'] = '<a href="'.$this->pi_linkTP_keepPIvars_url(['addToBasket' => 'toc', 'logId' => $entry['id'], 'startpage' => $entry['points']], TRUE, FALSE, $this->conf['targetBasket']).'">'.$this->pi_getLL('basketButton', '', TRUE).'</a>';
70
            }
71
        } elseif (!empty($entry['targetUid'])) {
72
            $entryArray['_OVERRIDE_HREF'] = $this->pi_linkTP_keepPIvars_url(['id' => $entry['targetUid'], 'page' => 1], TRUE, FALSE, $this->conf['targetPid']);
73
            $entryArray['doNotLinkIt'] = 0;
74
            if ($this->conf['basketButton']) {
75
                $entryArray['basketButtonHref'] = '<a href="'.$this->pi_linkTP_keepPIvars_url(['addToBasket' => 'toc', 'logId' => $entry['id'], 'startpage' => $entry['targetUid']], TRUE, FALSE, $this->conf['targetBasket']).'">'.$this->pi_getLL('basketButton', '', TRUE).'</a>';
76
            }
77
        }
78
        // Set "ITEM_STATE" to "CUR" if this entry points to current page.
79
        if (in_array($entry['id'], $this->activeEntries)) {
80
            $entryArray['ITEM_STATE'] = 'CUR';
81
        }
82
        // Build sub-menu if available and called recursively.
83
        if ($recursive == TRUE
84
            && !empty($entry['children'])) {
85
            // Build sub-menu only if one of the following conditions apply:
86
            // 1. "expAll" is set for menu
87
            // 2. Current menu node is in rootline
88
            // 3. Current menu node points to another file
89
            // 4. Current menu node has no corresponding images
90
            if (!empty($this->conf['menuConf.']['expAll'])
91
                || $entryArray['ITEM_STATE'] == 'CUR'
92
                || is_string($entry['points'])
93
                || empty($this->doc->smLinks['l2p'][$entry['id']])) {
0 ignored issues
show
Bug Best Practice introduced by
The property $smLinks is declared protected in Kitodo\Dlf\Common\Document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
94
                $entryArray['_SUB_MENU'] = [];
95
                foreach ($entry['children'] as $child) {
96
                    // Set "ITEM_STATE" to "ACT" if this entry points to current page and has sub-entries pointing to the same page.
97
                    if (in_array($child['id'], $this->activeEntries)) {
98
                        $entryArray['ITEM_STATE'] = 'ACT';
99
                    }
100
                    $entryArray['_SUB_MENU'][] = $this->getMenuEntry($child, TRUE);
101
                }
102
            }
103
            // Append "IFSUB" to "ITEM_STATE" if this entry has sub-entries.
104
            $entryArray['ITEM_STATE'] = ($entryArray['ITEM_STATE'] == 'NO' ? 'IFSUB' : $entryArray['ITEM_STATE'].'IFSUB');
105
        }
106
        return $entryArray;
107
    }
108
109
    /**
110
     * The main method of the PlugIn
111
     *
112
     * @access public
113
     *
114
     * @param string $content: The PlugIn content
115
     * @param array $conf: The PlugIn configuration
116
     *
117
     * @return string The content that is displayed on the website
118
     */
119
    public function main($content, $conf) {
120
        $this->init($conf);
121
        // Check for typoscript configuration to prevent fatal error.
122
        if (empty($this->conf['menuConf.'])) {
123
            Helper::devLog('Incomplete plugin configuration', DEVLOG_SEVERITY_WARNING);
124
            return $content;
125
        }
126
        // Load template file.
127
        $this->getTemplate();
128
        $TSconfig = [];
129
        $TSconfig['special'] = 'userfunction';
130
        $TSconfig['special.']['userFunc'] = \Kitodo\Dlf\Plugin\TableOfContents::class.'->makeMenuArray';
131
        $TSconfig = Helper::mergeRecursiveWithOverrule($this->conf['menuConf.'], $TSconfig);
132
        $markerArray['###TOCMENU###'] = $this->cObj->cObjGetSingle('HMENU', $TSconfig);
1 ignored issue
show
Comprehensibility Best Practice introduced by
$markerArray was never initialized. Although not strictly required by PHP, it is generally a good practice to add $markerArray = array(); before regardless.
Loading history...
133
        $content .= $this->cObj->substituteMarkerArray($this->template, $markerArray);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Frontend\Conte...substituteMarkerArray() has been deprecated: since TYPO3 v8, will be removed in TYPO3 v9, please use the MarkerBasedTemplateService instead. ( Ignorable by Annotation )

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

133
        $content .= /** @scrutinizer ignore-deprecated */ $this->cObj->substituteMarkerArray($this->template, $markerArray);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
134
        return $this->pi_wrapInBaseClass($content);
135
    }
136
137
    /**
138
     * This builds a menu array for HMENU
139
     *
140
     * @access public
141
     *
142
     * @param string $content: The PlugIn content
143
     * @param array $conf: The PlugIn configuration
144
     *
145
     * @return array HMENU array
146
     */
147
    public function makeMenuArray($content, $conf) {
1 ignored issue
show
Unused Code introduced by
The parameter $content is not used and could be removed. ( Ignorable by Annotation )

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

147
    public function makeMenuArray(/** @scrutinizer ignore-unused */ $content, $conf) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
148
        $this->init($conf);
149
        // Load current document.
150
        $this->loadDocument();
151
        if ($this->doc === NULL) {
152
            // Quit without doing anything if required variables are not set.
153
            return [];
154
        } else {
155
            if (!empty($this->piVars['logicalPage'])) {
156
                $this->piVars['page'] = $this->doc->getPhysicalPage($this->piVars['logicalPage']);
157
                // The logical page parameter should not appear again
158
                unset($this->piVars['logicalPage']);
159
            }
160
            // Set default values for page if not set.
161
            // $this->piVars['page'] may be integer or string (physical structure @ID)
162
            if ((int) $this->piVars['page'] > 0
163
                || empty($this->piVars['page'])) {
164
                $this->piVars['page'] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange((int) $this->piVars['page'], 1, $this->doc->numPages, 1);
0 ignored issues
show
Bug Best Practice introduced by
The property $numPages is declared protected in Kitodo\Dlf\Common\Document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
165
            } else {
166
                $this->piVars['page'] = array_search($this->piVars['page'], $this->doc->physicalStructure);
0 ignored issues
show
Bug Best Practice introduced by
The property $physicalStructure is declared protected in Kitodo\Dlf\Common\Document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
167
            }
168
            $this->piVars['double'] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($this->piVars['double'], 0, 1, 0);
169
        }
170
        $menuArray = [];
171
        // Does the document have physical elements or is it an external file?
172
        if ($this->doc->physicalStructure
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->doc->physicalStructure of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
173
            || !\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($this->doc->uid)) {
0 ignored issues
show
Bug Best Practice introduced by
The property $uid is declared protected in Kitodo\Dlf\Common\Document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
174
            // Get all logical units the current page or track is a part of.
175
            if (!empty($this->piVars['page'])
176
                && $this->doc->physicalStructure) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->doc->physicalStructure of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
177
                $this->activeEntries = array_merge((array) $this->doc->smLinks['p2l'][$this->doc->physicalStructure[0]], (array) $this->doc->smLinks['p2l'][$this->doc->physicalStructure[$this->piVars['page']]]);
0 ignored issues
show
Bug Best Practice introduced by
The property $smLinks is declared protected in Kitodo\Dlf\Common\Document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
178
                if (!empty($this->piVars['double'])
179
                    && $this->piVars['page'] < $this->doc->numPages) {
180
                    $this->activeEntries = array_merge($this->activeEntries, (array) $this->doc->smLinks['p2l'][$this->doc->physicalStructure[$this->piVars['page'] + 1]]);
181
                }
182
            }
183
            // Go through table of contents and create all menu entries.
184
            foreach ($this->doc->tableOfContents as $entry) {
0 ignored issues
show
Bug Best Practice introduced by
The property $tableOfContents is declared protected in Kitodo\Dlf\Common\Document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
185
                $menuArray[] = $this->getMenuEntry($entry, TRUE);
186
            }
187
        } else {
188
            // Go through table of contents and create top-level menu entries.
189
            foreach ($this->doc->tableOfContents as $entry) {
190
                $menuArray[] = $this->getMenuEntry($entry, FALSE);
191
            }
192
            // Get all child documents from database.
193
            $whereClause = 'tx_dlf_documents.partof='.intval($this->doc->uid).' AND tx_dlf_documents.structure=tx_dlf_structures.uid AND tx_dlf_structures.pid='.$this->doc->pid.Helper::whereClause('tx_dlf_documents').Helper::whereClause('tx_dlf_structures');
0 ignored issues
show
Bug Best Practice introduced by
The property $pid is declared protected in Kitodo\Dlf\Common\Document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
194
            if ($this->conf['excludeOther']) {
195
                $whereClause .= ' AND tx_dlf_documents.pid='.intval($this->conf['pages']);
196
            }
197
            // Build table of contents from database.
198
            $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
199
                'tx_dlf_documents.uid AS uid,tx_dlf_documents.title AS title,tx_dlf_documents.volume AS volume,tx_dlf_structures.index_name AS type',
200
                'tx_dlf_documents,tx_dlf_structures',
201
                $whereClause,
202
                '',
203
                'tx_dlf_documents.volume_sorting'
204
            );
205
            if ($GLOBALS['TYPO3_DB']->sql_num_rows($result)) {
206
                $menuArray[0]['ITEM_STATE'] = 'CURIFSUB';
207
                $menuArray[0]['_SUB_MENU'] = [];
208
                while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
209
                    $entry = [
210
                        'label' => $resArray['title'],
211
                        'type' => $resArray['type'],
212
                        'volume' => $resArray['volume'],
213
                        'pagination' => '',
214
                        'targetUid' => $resArray['uid']
215
                    ];
216
                    $menuArray[0]['_SUB_MENU'][] = $this->getMenuEntry($entry, FALSE);
217
                }
218
            }
219
        }
220
        return $menuArray;
221
    }
222
}
223