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 (#818)
by Sebastian
03:23
created

MediaPlayerConfigViewHelper::renderStatic()   B

Complexity

Conditions 6
Paths 20

Size

Total Lines 51
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 26
nc 20
nop 3
dl 0
loc 51
rs 8.8817
c 1
b 0
f 0

How to fix   Long Method   

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\ViewHelpers;
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
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
14
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
15
use TYPO3\CMS\Core\Localization\LocalizationFactory;
16
use TYPO3\CMS\Core\Utility\GeneralUtility;
17
use TYPO3\CMS\Core\Utility\PathUtility;
18
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
19
20
/**
21
 * This view helper serializes media player configuration to JSON and makes it
22
 * available as a variable to be passed to the player.
23
 */
24
class MediaPlayerConfigViewHelper extends AbstractViewHelper
25
{
26
    protected $escapeOutput = false;
27
28
    public function initializeArguments()
29
    {
30
        $this->registerArgument('id', 'string', 'ID of the generated player configuration', true);
31
        $this->registerArgument('settings', 'array', 'the settings array that is converted to JSON', true);
32
    }
33
34
    public static function renderStatic(
35
        array $arguments,
36
        \Closure $renderChildrenClosure,
37
        RenderingContextInterface $renderingContext
38
    ) {
39
        $id = $arguments['id'];
40
        $inputSettings = $arguments['settings'];
41
42
        // Whitelist keys to keep out stuff such as playerTranslations
43
        $allowedKeys = ['shareButtons', 'screenshotCaptions', 'constants', 'equalizer'];
44
        $result = array_intersect_key($inputSettings, array_flip($allowedKeys));
45
46
        // Add translations
47
        $translationBaseFiles = $inputSettings['playerTranslations']['baseFile'] ?? 'EXT:dlf/Resources/Private/Language/locallang_media.xlf';
48
        if (!is_array($translationBaseFiles)) {
49
            $translationBaseFiles = [$translationBaseFiles];
50
        }
51
        $result['lang'] = self::getTranslations($translationBaseFiles);
52
53
        // Resolve paths
54
        foreach ($result['shareButtons'] ?? [] as $key => $button) {
55
            // For Flexforms-configured button
56
            if (isset($button['singleButton'])) {
57
                $button = $button['singleButton'];
58
            }
59
60
            if ($button['type'] === 'image') {
61
                $filePath = GeneralUtility::getFileAbsFileName($button['src']);
62
                $webPath = PathUtility::getAbsoluteWebPath($filePath);
63
64
                $button['src'] = $webPath;
65
            }
66
67
            $result['shareButtons'][$key] = $button;
68
        }
69
70
        // Allow using (and overriding) non-numeric keys for shareButtons
71
        $result['shareButtons'] = array_values($result['shareButtons'] ?? []);
72
73
        // Equalizer configuration
74
        foreach ($result['equalizer']['presets'] ?? [] as $key => &$preset) {
0 ignored issues
show
Bug introduced by
The expression $result['equalizer']['presets'] ?? array() cannot be used as a reference.

Let?s assume that you have the following foreach statement:

foreach ($array as &$itemValue) { }

$itemValue is assigned by reference. This is possible because the expression (in the example $array) can be used as a reference target.

However, if we were to replace $array with something different like the result of a function call as in

foreach (getArray() as &$itemValue) { }

then assigning by reference is not possible anymore as there is no target that could be modified.

Available Fixes

1. Do not assign by reference
foreach (getArray() as $itemValue) { }
2. Assign to a local variable first
$array = getArray();
foreach ($array as &$itemValue) {}
3. Return a reference
function &getArray() { $array = array(); return $array; }

foreach (getArray() as &$itemValue) { }
Loading history...
75
            $result['equalizer']['presets'][$key]['key'] = $key;
76
        }
77
        $result['equalizer']['presets'] = array_values($result['equalizer']['presets'] ?? []);
78
79
        $idJson = json_encode($id);
80
        $resultJson = json_encode($result);
81
82
        return <<<CONFIG
83
<script>
84
    window[$idJson] = $resultJson;
85
</script>
86
CONFIG;
87
    }
88
89
    /**
90
     * Collect translation keys from the specified XLIFF file and translate them
91
     * using the current language.
92
     *
93
     * Keys of the result array:
94
     * - locale: Locale identifier of current site language
95
     * - twoLetterIsoCode: Two-letter ISO code of current site language
96
     * - phrases: Map from translation keys to their translations
97
     */
98
    private static function getTranslations(array $translationBaseFiles)
99
    {
100
        $language = $GLOBALS['TYPO3_REQUEST']->getAttribute('language');
101
        $languageKey = $language->getTypo3Language();
102
        $phrases = [];
103
104
        $localizationFactory = GeneralUtility::makeInstance(LocalizationFactory::class);
105
106
        // TODO: Wouldn't there be a TypoScript utility?
107
        ksort($translationBaseFiles);
108
        foreach ($translationBaseFiles as $filename) {
109
            // Grab available translation keys from the specified file
110
            $translationKeys = array_keys($localizationFactory->getParsedData($filename, 'default')['default']);
111
112
            // Translate each available key as per the current language.
113
            // - This falls back to default language if a key isn't translated
114
            // - Pass $languageKey to ensure that translation matches ISO code
115
            foreach ($translationKeys as $transKey) {
116
                $phrases[$transKey] = LocalizationUtility::translate(
117
                    "LLL:$filename:$transKey",
118
                    /* extensionName= */null,
119
                    /* arguments= */null,
120
                    $languageKey
121
                );
122
            }
123
        }
124
125
        return [
126
            'locale' => $language->getLocale(),
127
            'twoLetterIsoCode' => $language->getTwoLetterIsoCode(),
128
            'phrases' => $phrases,
129
        ];
130
    }
131
}
132