Passed
Push — task/2976_TYPO3.11_compatibili... ( d7dfd0...4be1cc )
by Rafael
03:40
created

TranslateViewHelper::translateAndReplaceMarkers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 5
crap 2
1
<?php
2
namespace ApacheSolrForTypo3\Solr\ViewHelpers;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
18
use TYPO3\CMS\Fluid\ViewHelpers\TranslateViewHelper as CoreTranslateViewHelper;
19
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\ViewHelperNode;
20
use TYPO3Fluid\Fluid\Core\Compiler\TemplateCompiler;
21
22
/**
23
 * Class TranslateViewHelper
24
 *
25
 * @author Frans Saris <[email protected]>
26
 * @author Timo Hund <[email protected]>
27
 */
28
class TranslateViewHelper extends CoreTranslateViewHelper
29
{
30
    /**
31
     * @var bool
32
     */
33
    protected $escapeChildren = true;
34
35
    /**
36
     * @return string|null
37
     */
38 28
    public function render()
39
    {
40 28
        $result = parent::render();
41 28
        $result = self::replaceTranslationPrefixesWithAtWithStringMarker($result);
42 28
        if (is_array($this->arguments['arguments'])) {
43 25
            $result = vsprintf($result, $this->arguments['arguments']);
44
        }
45
46 28
        return $result;
47
    }
48
49
    /**
50
     * Wrapper call to static LocalizationUtility
51
     *
52
     * @param string $id Translation Key compatible to TYPO3 Flow
53
     * @param string $extensionName UpperCamelCased extension key (for example BlogExample)
54
     * @param array $arguments Arguments to be replaced in the resulting string
55
     * @param string $languageKey Language key to use for this translation
56
     * @param string[] $alternativeLanguageKeys Alternative language keys if no translation does exist
57
     *
58
     * @return string|null
59
     */
60 3
    public static function translateAndReplaceMarkers($id, $extensionName = 'solr', $arguments = null, $languageKey = null, $alternativeLanguageKeys = null)
61
    {
62 3
        $result = LocalizationUtility::translate($id, $extensionName, $arguments, $languageKey, $alternativeLanguageKeys);
63 3
        $result = self::replaceTranslationPrefixesWithAtWithStringMarker($result);
64 3
        if (is_array($arguments)) {
65 3
            $result = vsprintf($result, $arguments);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type null; however, parameter $format of vsprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

65
            $result = vsprintf(/** @scrutinizer ignore-type */ $result, $arguments);
Loading history...
66
        }
67 3
        return $result;
68
    }
69
70
    /**
71
     * @param string $argumentsName
72
     * @param string $closureName
73
     * @param string $initializationPhpCode
74
     * @param ViewHelperNode $node
75
     * @param TemplateCompiler $compiler
76
     * @return string
77
     */
78 29
    public function compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler)
79
    {
80 29
        return sprintf(
81 29
            '\\%1$s::translateAndReplaceMarkers(%2$s[\'key\'] ?? %2$s[\'id\'], %2$s[\'extensionName\'] ?? $renderingContext->getControllerContext()->getRequest()->getControllerExtensionName(), %2$s[\'arguments\'], %2$s[\'languageKey\'], %2$s[\'alternativeLanguageKeys\']) ?? %2$s[\'default\'] ?? %3$s()',
82 29
            static::class,
83
            $argumentsName,
84
            $closureName
85
        );
86
    }
87
88
    /**
89
     * @param $result
90
     * @return mixed
91
     */
92 28
    protected static function replaceTranslationPrefixesWithAtWithStringMarker($result)
93
    {
94 28
        if (strpos($result, '@') !== false) {
95 25
            $result = preg_replace('~\"?@[a-zA-Z]*\"?~', '%s', $result);
96
        }
97 28
        return $result;
98
    }
99
}
100