Completed
Push — master ( ffca13...6343e7 )
by Tobias
38:45 queued 28:47
created

EditInPlaceTranslator::trans()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 17
cts 17
cp 1
rs 9.488
c 0
b 0
f 0
cc 4
nc 5
nop 4
crap 4
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\TranslatorInterface;
16
use Symfony\Component\Translation\TranslatorBagInterface;
17
use Translation\Bundle\EditInPlace\ActivatorInterface;
18
19
/**
20
 * Custom Translator for HTML rendering only (output `<x-trans>` tags).
21
 *
22
 * @author Damien Alexandre <[email protected]>
23
 */
24
final class EditInPlaceTranslator implements TranslatorInterface, TranslatorBagInterface
25
{
26
    /**
27
     * @var TranslatorInterface|\Symfony\Component\Translation\Translator
28
     */
29
    private $translator;
30
31
    /**
32
     * @var ActivatorInterface
33
     */
34
    private $activator;
35
36
    /**
37
     * @var RequestStack
38
     */
39
    private $requestStack;
40
41 3
    public function __construct(TranslatorInterface $translator, ActivatorInterface $activator, RequestStack $requestStack)
42
    {
43 3
        $this->translator = $translator;
44 3
        $this->activator = $activator;
45 3
        $this->requestStack = $requestStack;
46 3
    }
47
48
    /**
49
     * @see Translator::getCatalogue
50
     */
51
    public function getCatalogue($locale = null)
52
    {
53
        return $this->translator->getCatalogue($locale);
0 ignored issues
show
Bug introduced by
The method getCatalogue does only exist in Symfony\Component\Translation\Translator, but not in Symfony\Component\Translation\TranslatorInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
54
    }
55
56
    /**
57
     * @see Translator::trans
58
     */
59 3
    public function trans($id, array $parameters = [], $domain = null, $locale = null)
60
    {
61 3
        $original = $this->translator->trans($id, $parameters, $domain, $locale);
62 3
        if (!$this->activator->checkRequest($this->requestStack->getMasterRequest())) {
63 1
            return $original;
64
        }
65
66 2
        $plain = $this->translator->trans($id, [], $domain, $locale);
67
68 2
        if (null === $domain) {
69 2
            $domain = 'messages';
70
        }
71 2
        if (null === $locale) {
72 2
            $locale = $this->translator->getLocale();
73
        }
74
75
        // Render all data in the translation tag required to allow in-line translation
76 2
        return sprintf('<x-trans data-key="%s|%s" data-value="%s" data-plain="%s" data-domain="%s" data-locale="%s">%s</x-trans>',
77 2
            $domain,
78 2
            $id,
79 2
            htmlspecialchars($original),
80 2
            htmlspecialchars($plain),
81 2
            $domain,
82 2
            $locale,
83 2
            $original
84
        );
85
    }
86
87
    /**
88
     * @see Translator::trans
89
     */
90
    public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null)
91
    {
92
        if (!$this->activator->checkRequest($this->requestStack->getMasterRequest())) {
93
            return $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
94
        }
95
96
        $parameters = array_merge([
97
            '%count%' => $number,
98
        ], $parameters);
99
100
        return $this->trans($id, $parameters, $domain, $locale);
101
    }
102
103
    /**
104
     * @see Translator::trans
105
     */
106
    public function setLocale($locale)
107
    {
108
        $this->translator->setLocale($locale);
109
    }
110
111
    /**
112
     * @see Translator::trans
113
     */
114
    public function getLocale()
115
    {
116
        return $this->translator->getLocale();
117
    }
118
}
119