Passed
Pull Request — main (#3432)
by Rafael
49:10 queued 14:01
created

TranslateViewHelper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 85.19%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 20
dl 0
loc 84
ccs 23
cts 27
cp 0.8519
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A replaceTranslationPrefixesWithAtWithStringMarker() 0 6 2
A render() 0 9 2
A compile() 0 12 1
A translateAndReplaceMarkers() 0 13 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace ApacheSolrForTypo3\Solr\ViewHelpers;
19
20
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
21
use TYPO3\CMS\Fluid\ViewHelpers\TranslateViewHelper as CoreTranslateViewHelper;
22
use TYPO3Fluid\Fluid\Core\Compiler\TemplateCompiler;
23
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\ViewHelperNode;
24
25
/**
26
 * Class TranslateViewHelper
27
 *
28
 * @author Frans Saris <[email protected]>
29
 * @author Timo Hund <[email protected]>
30
 */
31
class TranslateViewHelper extends CoreTranslateViewHelper
32
{
33
    /**
34
     * @var bool
35
     */
36
    protected $escapeChildren = true;
37
38
    /**
39
     * @return string|null
40
     *
41
     * @noinspection PhpMissingReturnTypeInspection
42
     */
43 29
    public function render()
44
    {
45 29
        $result = parent::render();
46 29
        $result = self::replaceTranslationPrefixesWithAtWithStringMarker($result);
47 29
        if (is_array($this->arguments['arguments'])) {
48 25
            $result = vsprintf($result, $this->arguments['arguments']);
49
        }
50
51 29
        return $result;
52
    }
53
54
    /**
55
     * Wrapper call to static LocalizationUtility
56
     *
57
     * @param string $id Translation Key compatible to TYPO3 Flow
58
     * @param string $extensionName UpperCamelCased extension key (for example BlogExample)
59
     * @param array|null $arguments Arguments to be replaced in the resulting string
60
     * @param string|null $languageKey Language key to use for this translation
61
     * @param string[]|null $alternativeLanguageKeys Alternative language keys if no translation does exist
62
     *
63
     * @return string|null
64
     */
65 3
    public static function translateAndReplaceMarkers(
66
        string $id,
67
        string $extensionName = 'solr',
68
        ?array $arguments = null,
69
        ?string $languageKey = null,
70
        ?array $alternativeLanguageKeys = null
71
    ): ?string {
72 3
        $result = LocalizationUtility::translate($id, $extensionName, $arguments, $languageKey, $alternativeLanguageKeys);
73 3
        $result = self::replaceTranslationPrefixesWithAtWithStringMarker($result);
74 3
        if (is_array($arguments)) {
75 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

75
            $result = vsprintf(/** @scrutinizer ignore-type */ $result, $arguments);
Loading history...
76
        }
77 3
        return $result;
78
    }
79
80
    /**
81
     * @param string $argumentsName
82
     * @param string $closureName
83
     * @param string $initializationPhpCode
84
     * @param ViewHelperNode $node
85
     * @param TemplateCompiler $compiler
86
     * @return string
87
     *
88
     * @noinspection PhpMissingReturnTypeInspection
89
     */
90 30
    public function compile(
91
        $argumentsName,
92
        $closureName,
93
        &$initializationPhpCode,
94
        ViewHelperNode $node,
95
        TemplateCompiler $compiler
96
    ) {
97 30
        return sprintf(
98 30
            '\\%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()',
99 30
            static::class,
100 30
            $argumentsName,
101 30
            $closureName
102 30
        );
103
    }
104
105
    /**
106
     * @param $result
107
     * @return mixed
108
     */
109 29
    protected static function replaceTranslationPrefixesWithAtWithStringMarker($result)
110
    {
111 29
        if (strpos((string)$result, '@') !== false) {
112 25
            $result = preg_replace('~\"?@[a-zA-Z]*\"?~', '%s', $result);
113
        }
114 29
        return $result;
115
    }
116
}
117