Completed
Push — master ( 8bad7f...f84ff2 )
by Olivier
02:14 queued 11s
created

EditInPlaceTranslator::getLocale()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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;
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);
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 only exist in Symfony\Component\Translation\TranslatorInterface, but not in Symfony\Contracts\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...
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 only exist in Symfony\Component\Translation\TranslatorInterface, but not in Symfony\Contracts\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...
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 only exist in Symfony\Component\Translation\TranslatorInterface, but not in Symfony\Contracts\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...
125
    }
126
127
    /**
128
     * @see Translator::trans
129
     */
130
    public function getLocale(): string
131
    {
132
        return $this->translator->getLocale();
0 ignored issues
show
Bug introduced by
The method getLocale does only exist in Symfony\Component\Translation\TranslatorInterface, but not in Symfony\Contracts\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...
133
    }
134
}
135