EditInPlaceTranslator::getCatalogue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Translation\Bundle\Translator;
13
14
use Symfony\Component\HttpFoundation\RequestStack;
15
use Symfony\Component\Translation\MessageCatalogueInterface;
16
use Symfony\Component\Translation\TranslatorBagInterface;
17
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Translation\TranslatorInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Symfony\Contracts\Translation\LocaleAwareInterface;
19
use Symfony\Contracts\Translation\TranslatorInterface as NewTranslatorInterface;
20
use Translation\Bundle\EditInPlace\ActivatorInterface;
21
22
/**
23
 * Custom Translator for HTML rendering only (output `<x-trans>` tags).
24
 *
25
 * @author Damien Alexandre <[email protected]>
26
 */
27
final class EditInPlaceTranslator implements TranslatorInterface
28
{
29
    /**
30
     * @var LegacyTranslatorInterface|NewTranslatorInterface
31
     */
32
    private $translator;
33
34
    /**
35
     * @var ActivatorInterface
36
     */
37
    private $activator;
38
39
    /**
40
     * @var RequestStack
41
     */
42
    private $requestStack;
43
44
    /**
45
     * $translator param can't be type hinted as we have to deal with both LegacyTranslatorInterface & NewTranslatorInterface.
46
     * Once we won't support sf ^3.4 anymore, we will be able to type hint $translator with NewTranslatorInterface.
47
     *
48
     * @param LegacyTranslatorInterface|NewTranslatorInterface $translator
49
     */
50 4
    public function __construct($translator, ActivatorInterface $activator, RequestStack $requestStack)
51
    {
52 4
        if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof LocaleAwareInterface) {
53 1
            throw new \InvalidArgumentException('The given translator must implements LocaleAwareInterface.');
54
        }
55 3
        if (!$translator instanceof TranslatorBagInterface) {
56
            throw new \InvalidArgumentException('The given translator must implements TranslatorBagInterface.');
57
        }
58
59 3
        $this->translator = $translator;
60 3
        $this->activator = $activator;
61 3
        $this->requestStack = $requestStack;
62 3
    }
63
64
    /**
65
     * @see Translator::getCatalogue
66
     */
67
    public function getCatalogue($locale = null): MessageCatalogueInterface
68
    {
69
        return $this->translator->getCatalogue($locale);
0 ignored issues
show
Bug introduced by
The method getCatalogue() does not exist on Symfony\Contracts\Translation\TranslatorInterface. It seems like you code against a sub-type of Symfony\Contracts\Translation\TranslatorInterface such as Symfony\Component\Translation\Translator or Symfony\Component\Transl...DataCollectorTranslator or Symfony\Component\Translation\LoggingTranslator. ( Ignorable by Annotation )

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

69
        return $this->translator->/** @scrutinizer ignore-call */ getCatalogue($locale);
Loading history...
70
    }
71
72
    /**
73
     * @see Translator::trans
74
     */
75 3
    public function trans($id, array $parameters = [], $domain = null, $locale = null): ?string
76
    {
77 3
        $original = $this->translator->trans($id, $parameters, $domain, $locale);
78 3
        if (!$this->activator->checkRequest($this->requestStack->getMasterRequest())) {
79 1
            return $original;
80
        }
81
82 2
        $plain = $this->translator->trans($id, [], $domain, $locale);
83
84 2
        if (null === $domain) {
85 2
            $domain = 'messages';
86
        }
87 2
        if (null === $locale) {
88 2
            $locale = $this->translator->getLocale();
0 ignored issues
show
Bug introduced by
The method getLocale() does not exist on Symfony\Contracts\Translation\TranslatorInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Symfony\Component\Transl...oLocalizationTranslator. Are you sure you never get one of those? ( Ignorable by Annotation )

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

88
            /** @scrutinizer ignore-call */ 
89
            $locale = $this->translator->getLocale();
Loading history...
89
        }
90
91
        // Render all data in the translation tag required to allow in-line translation
92 2
        return \sprintf('<x-trans data-key="%s|%s" data-value="%s" data-plain="%s" data-domain="%s" data-locale="%s">%s</x-trans>',
93 2
            $domain,
94 2
            $id,
95 2
            \htmlspecialchars($original),
96 2
            \htmlspecialchars($plain),
97 2
            $domain,
98 2
            $locale,
99 2
            $original
100
        );
101
    }
102
103
    /**
104
     * @see Translator::trans
105
     */
106
    public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null): ?string
107
    {
108
        if (!$this->activator->checkRequest($this->requestStack->getMasterRequest())) {
109
            return $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
0 ignored issues
show
Bug introduced by
The method transChoice() does not exist on Symfony\Contracts\Translation\TranslatorInterface. Did you maybe mean trans()? ( Ignorable by Annotation )

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

109
            return $this->translator->/** @scrutinizer ignore-call */ transChoice($id, $number, $parameters, $domain, $locale);

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...
110
        }
111
112
        $parameters = \array_merge([
113
            '%count%' => $number,
114
        ], $parameters);
115
116
        return $this->trans($id, $parameters, $domain, $locale);
117
    }
118
119
    /**
120
     * @see Translator::trans
121
     */
122
    public function setLocale($locale): void
123
    {
124
        $this->translator->setLocale($locale);
0 ignored issues
show
Bug introduced by
The method setLocale() does not exist on Symfony\Contracts\Translation\TranslatorInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Symfony\Component\Transl...oLocalizationTranslator. Are you sure you never get one of those? ( Ignorable by Annotation )

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

124
        $this->translator->/** @scrutinizer ignore-call */ 
125
                           setLocale($locale);
Loading history...
125
    }
126
127
    /**
128
     * @see Translator::trans
129
     */
130
    public function getLocale(): string
131
    {
132
        return $this->translator->getLocale();
133
    }
134
}
135