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.
Passed
Pull Request — master (#803)
by Beatrycze
04:28
created

TableOfContentsController::filterAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
/**
3
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
4
 *
5
 * This file is part of the Kitodo and TYPO3 projects.
6
 *
7
 * @license GNU General Public License version 3 or later.
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
namespace Kitodo\Dlf\Controller;
13
14
use Kitodo\Dlf\Common\Helper;
15
use TYPO3\CMS\Core\Utility\MathUtility;
16
use TYPO3\CMS\Core\Utility\GeneralUtility;
17
18
/**
19
 * Controller class for plugin 'Table Of Contents'.
20
 *
21
 * @author Sebastian Meyer <[email protected]>
22
 * @package TYPO3
23
 * @subpackage dlf
24
 * @access public
25
 */
26
class TableOfContentsController extends AbstractController
27
{
28
    /**
29
     * This holds the active entries according to the currently selected page
30
     *
31
     * @var array
32
     * @access protected
33
     */
34
    protected $activeEntries = [];
35
36
    /**
37
     * @var array $this->filterParams: The current filter parameter
38
     * @access protected
39
     */
40
    protected $filterParams;
41
42
    /**
43
     * Filter Action
44
     *
45
     * @return void
46
     */
47
    public function filterAction()
48
    {
49
        // if filter was triggered, get filter parameters from POST variables
50
        $this->filterParams = $this->getParametersSafely('filterParameter');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getParametersSafely('filterParameter') can also be of type string. However, the property $filterParams is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
51
52
        // output is done by main action
53
        $this->forward('main', null, null, ['filterParameter' => $this->filterParams]);
54
    }
55
56
    /**
57
     * The main method of the plugin
58
     *
59
     * @return void
60
     */
61
    public function mainAction()
62
    {
63
        // Load current document.
64
        $this->loadDocument($this->requestData);
65
        if (
66
            $this->document === null
67
            || $this->document->getDoc() === null
68
        ) {
69
            // Quit without doing anything if required variables are not set.
70
            return;
71
        } else {
72
            if (!empty($this->requestData['logicalPage'])) {
73
                $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
74
                // The logical page parameter should not appear again
75
                unset($this->requestData['logicalPage']);
76
            }
77
            if ($this->document->getDoc()->tableOfContents[0]['type'] == 'collection') {
78
                $this->view->assign('type', 'collection');
79
                $this->view->assign('toc', $this->makeMenuFor3DObjects());
80
            } else {
81
                $this->view->assign('type', 'other');
82
                $this->view->assign('toc', $this->makeMenuArray());
83
            }
84
        }
85
    }
86
87
    /**
88
     * This builds a menu array for HMENU
89
     *
90
     * @access protected
91
     * @return array HMENU array
92
     */
93
    protected function makeMenuArray()
94
    {
95
        // Set default values for page if not set.
96
        // $this->requestData['page'] may be integer or string (physical structure @ID)
97
        if (
98
            (int) $this->requestData['page'] > 0
99
            || empty($this->requestData['page'])
100
        ) {
101
            $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
102
        } else {
103
            $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
104
        }
105
        $this->requestData['double'] = MathUtility::forceIntegerInRange($this->requestData['double'], 0, 1, 0);
106
        $menuArray = [];
107
        // Does the document have physical elements or is it an external file?
108
        if (
109
            !empty($this->document->getDoc()->physicalStructure)
110
            || !MathUtility::canBeInterpretedAsInteger($this->requestData['id'])
111
        ) {
112
            // Get all logical units the current page or track is a part of.
113
            if (
114
                !empty($this->requestData['page'])
115
                && !empty($this->document->getDoc()->physicalStructure)
116
            ) {
117
                $this->activeEntries = array_merge((array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[0]],
118
                    (array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[$this->requestData['page']]]);
119
                if (
120
                    !empty($this->requestData['double'])
121
                    && $this->requestData['page'] < $this->document->getDoc()->numPages
122
                ) {
123
                    $this->activeEntries = array_merge($this->activeEntries,
124
                        (array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[$this->requestData['page'] + 1]]);
125
                }
126
            }
127
            // Go through table of contents and create all menu entries.
128
            foreach ($this->document->getDoc()->tableOfContents as $entry) {
129
                $menuArray[] = $this->getMenuEntry($entry, true);
130
            }
131
        } else {
132
            // Go through table of contents and create top-level menu entries.
133
            foreach ($this->document->getDoc()->tableOfContents as $entry) {
134
                $menuArray[] = $this->getMenuEntry($entry, false);
135
            }
136
            // Build table of contents from database.
137
            $result = $this->documentRepository->getTableOfContentsFromDb($this->document->getUid(), $this->document->getPid(), $this->settings);
138
139
            $allResults = $result->fetchAll();
0 ignored issues
show
Bug introduced by
The method fetchAll() does not exist on TYPO3\CMS\Extbase\Persistence\QueryResultInterface. ( Ignorable by Annotation )

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

139
            /** @scrutinizer ignore-call */ 
140
            $allResults = $result->fetchAll();

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...
140
141
            if (count($allResults) > 0) {
142
                $menuArray[0]['ITEM_STATE'] = 'CURIFSUB';
143
                $menuArray[0]['_SUB_MENU'] = [];
144
                foreach ($allResults as $resArray) {
145
                    $entry = [
146
                        'label' => !empty($resArray['mets_label']) ? $resArray['mets_label'] : $resArray['title'],
147
                        'type' => $resArray['type'],
148
                        'volume' => $resArray['volume'],
149
                        'orderlabel' => $resArray['mets_orderlabel'],
150
                        'pagination' => '',
151
                        'targetUid' => $resArray['uid']
152
                    ];
153
                    $menuArray[0]['_SUB_MENU'][] = $this->getMenuEntry($entry, false);
154
                }
155
            }
156
        }
157
        return $menuArray;
158
    }
159
160
    /**
161
     * This builds a menu for list of 3D objects
162
     *
163
     * @access protected
164
     *
165
     * @param string $content: The PlugIn content
166
     * @param array $conf: The PlugIn configuration
167
     *
168
     * @return array HMENU array
169
     */
170
    protected function makeMenuFor3DObjects()
171
    {
172
        // Set default values for page if not set.
173
        // $this->requestData['page'] may be integer or string (physical structure @ID)
174
        if (
175
            (int) $this->requestData['page'] > 0
176
            || empty($this->requestData['page'])
177
        ) {
178
            $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
179
        } else {
180
            $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
181
        }
182
        $this->requestData['double'] = MathUtility::forceIntegerInRange($this->requestData['double'], 0, 1, 0);
183
        $menuArray = [];
184
        // Is the document an external file?
185
        if (!MathUtility::canBeInterpretedAsInteger($this->requestData['id'])) {
186
            // Go through table of contents and create all menu entries.
187
            foreach ($this->document->getDoc()->tableOfContents as $entry) {
188
                $menuEntry = $this->getMenuEntryWithImage($entry, true);
189
                if (!empty($menuEntry)) {
190
                    $menuArray[] = $menuEntry;
191
                }
192
            }
193
        }
194
        return $menuArray;
195
    }
196
197
    /**
198
     * This builds an array for one menu entry
199
     *
200
     * @access protected
201
     *
202
     * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure
203
     * @param bool $recursive : Whether to include the child entries
204
     *
205
     * @return array HMENU array for menu entry
206
     */
207
    protected function getMenuEntry(array $entry, $recursive = false)
208
    {
209
        $entryArray = [];
210
        // Set "title", "volume", "type" and "pagination" from $entry array.
211
        $entryArray['title'] = !empty($entry['label']) ? $entry['label'] : $entry['orderlabel'];
212
        $entryArray['volume'] = $entry['volume'];
213
        $entryArray['orderlabel'] = $entry['orderlabel'];
214
        $entryArray['type'] = Helper::translate($entry['type'], 'tx_dlf_structures', $this->settings['storagePid']);
215
        $entryArray['pagination'] = htmlspecialchars($entry['pagination']);
216
        $entryArray['_OVERRIDE_HREF'] = '';
217
        $entryArray['doNotLinkIt'] = 1;
218
        $entryArray['ITEM_STATE'] = 'NO';
219
        // Build menu links based on the $entry['points'] array.
220
        if (
221
            !empty($entry['points'])
222
            && MathUtility::canBeInterpretedAsInteger($entry['points'])
223
        ) {
224
            $entryArray['page'] = $entry['points'];
225
226
            $entryArray['doNotLinkIt'] = 0;
227
            if ($this->settings['basketButton']) {
228
                $entryArray['basketButton'] = [
229
                    'logId' => $entry['id'],
230
                    'startpage' => $entry['points']
231
                ];
232
            }
233
        } elseif (
234
            !empty($entry['points'])
235
            && is_string($entry['points'])
236
        ) {
237
            $entryArray['id'] = $entry['points'];
238
            $entryArray['page'] = 1;
239
            $entryArray['doNotLinkIt'] = 0;
240
            if ($this->settings['basketButton']) {
241
                $entryArray['basketButton'] = [
242
                    'logId' => $entry['id'],
243
                    'startpage' => $entry['points']
244
                ];
245
            }
246
        } elseif (!empty($entry['targetUid'])) {
247
            $entryArray['id'] = $entry['targetUid'];
248
            $entryArray['page'] = 1;
249
            $entryArray['doNotLinkIt'] = 0;
250
            if ($this->settings['basketButton']) {
251
                $entryArray['basketButton'] = [
252
                    'logId' => $entry['id'],
253
                    'startpage' => $entry['targetUid']
254
                ];
255
            }
256
        }
257
        // Set "ITEM_STATE" to "CUR" if this entry points to current page.
258
        if (in_array($entry['id'], $this->activeEntries)) {
259
            $entryArray['ITEM_STATE'] = 'CUR';
260
        }
261
        // Build sub-menu if available and called recursively.
262
        if (
263
            $recursive === true
264
            && !empty($entry['children'])
265
        ) {
266
            // Build sub-menu only if one of the following conditions apply:
267
            // 1. Current menu node is in rootline
268
            // 2. Current menu node points to another file
269
            // 3. Current menu node has no corresponding images
270
            if (
271
                $entryArray['ITEM_STATE'] == 'CUR'
272
                || is_string($entry['points'])
273
                || empty($this->document->getDoc()->smLinks['l2p'][$entry['id']])
274
            ) {
275
                $entryArray['_SUB_MENU'] = [];
276
                foreach ($entry['children'] as $child) {
277
                    // Set "ITEM_STATE" to "ACT" if this entry points to current page and has sub-entries pointing to the same page.
278
                    if (in_array($child['id'], $this->activeEntries)) {
279
                        $entryArray['ITEM_STATE'] = 'ACT';
280
                    }
281
                    $entryArray['_SUB_MENU'][] = $this->getMenuEntry($child, true);
282
                }
283
            }
284
            // Append "IFSUB" to "ITEM_STATE" if this entry has sub-entries.
285
            $entryArray['ITEM_STATE'] = ($entryArray['ITEM_STATE'] == 'NO' ? 'IFSUB' : $entryArray['ITEM_STATE'] . 'IFSUB');
286
        }
287
        return $entryArray;
288
    }
289
290
    protected function getMenuEntryWithImage(array $entry, $recursive = false)
291
    {
292
        $entryArray = [];
293
294
        // don't filter if the entry type is collection or search params are empty
295
        if ($entry['type'] != 'collection' && is_array($this->searchParams) && !empty($this->searchParams)) {
0 ignored issues
show
Bug Best Practice introduced by
The property searchParams does not exist on Kitodo\Dlf\Controller\TableOfContentsController. Did you maybe forget to declare it?
Loading history...
296
            // currently only title filtering is defined
297
            // TODO: add more logic here after another fields are defined
298
            if (!str_contains($entry['label'], $this->searchParams[0])) {
299
                return $entryArray;
300
            }
301
        }
302
303
        // Set "title", "volume", "type" and "pagination" from $entry array.
304
        $entryArray['title'] = !empty($entry['label']) ? $entry['label'] : $entry['orderlabel'];
305
        $entryArray['orderlabel'] = $entry['orderlabel'];
306
        $entryArray['type'] = Helper::translate($entry['type'], 'tx_dlf_structures', $this->settings['storagePid']);
307
        $entryArray['pagination'] = htmlspecialchars($entry['pagination']);
308
        $entryArray['doNotLinkIt'] = 1;
309
        $entryArray['ITEM_STATE'] = 'HEADER';
310
311
        if ($entry['children'] == NULL) {
312
            $entryArray['description'] = $entry['description'];
313
            var_dump($entryArray['description']);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($entryArray['description']) looks like debug code. Are you sure you do not want to remove it?
Loading history...
314
            $id = str_replace("LOG", "PHYS", $entry['id']);
315
            $entryArray['image'] = $this->document->getDoc()->getFileLocation($this->document->getDoc()->physicalStructureInfo[$id]['files']['THUMBS']);
316
            $entryArray['doNotLinkIt'] = 0;
317
            // index.php?tx_dlf%5Bid%5D=http%3A%2F%2Flink_to_METS_file.xml
318
            $entryArray['urlId'] = GeneralUtility::_GET('id');
319
            $entryArray['urlXml'] = urlencode($entry['points']);
320
            $entryArray['ITEM_STATE'] = 'ITEM';
321
        }
322
323
        // Build sub-menu if available and called recursively.
324
        if (
325
            $recursive == true
326
            && !empty($entry['children'])
327
        ) {
328
            // Build sub-menu only if one of the following conditions apply:
329
            // 1. Current menu node points to another file
330
            // 2. Current menu node has no corresponding images
331
            if (
332
                is_string($entry['points'])
333
                || empty($this->document->getDoc()->smLinks['l2p'][$entry['id']])
334
            ) {
335
                $entryArray['_SUB_MENU'] = [];
336
                foreach ($entry['children'] as $child) {
337
                    $entryArray['_SUB_MENU'][] = $this->getMenuEntryWithImage($child);
338
                }
339
            }
340
        }
341
        return $entryArray;
342
    }
343
}
344