Completed
Push — master ( 705fb1...b4c0e6 )
by Tobias
06:39
created

EditInPlaceTranslator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 87
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getCatalogue() 0 4 1
A trans() 0 19 3
A transChoice() 0 12 2
A setLocale() 0 4 1
A getLocale() 0 4 1
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
    public function __construct(TranslatorInterface $translator, ActivatorInterface $activator, RequestStack $requestStack)
42
    {
43
        $this->translator = $translator;
44
        $this->activator = $activator;
45
        $this->requestStack = $requestStack;
46
    }
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
    public function trans($id, array $parameters = [], $domain = null, $locale = null)
60
    {
61
        if (!$this->activator->checkRequest($this->requestStack->getMasterRequest())) {
62
            return $this->translator->trans($id, $parameters, $domain, $locale);
63
        }
64
65
        if (null === $domain) {
66
            $domain = 'messages';
67
        }
68
69
        $original = $this->translator->trans($id, $parameters, $domain, $locale);
70
71
        // todo add data-value="" or data-type with real content, add parameters, domain...
72
        return sprintf('<x-trans data-key="%s|%s">%s</x-trans>',
73
            $domain,
74
            $id,
75
            $original
76
        );
77
    }
78
79
    /**
80
     * @see Translator::trans
81
     */
82
    public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null)
83
    {
84
        if (!$this->activator->checkRequest($this->requestStack->getMasterRequest())) {
85
            return $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
86
        }
87
88
        $parameters = array_merge([
89
            '%count%' => $number,
90
        ], $parameters);
91
92
        return $this->trans($id, $parameters, $domain, $locale);
93
    }
94
95
    /**
96
     * @see Translator::trans
97
     */
98
    public function setLocale($locale)
99
    {
100
        $this->translator->setLocale($locale);
101
    }
102
103
    /**
104
     * @see Translator::trans
105
     */
106
    public function getLocale()
107
    {
108
        return $this->translator->getLocale();
109
    }
110
}
111