FallbackTranslator::getCatalogue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Happyr\AutoFallbackTranslationBundle\Translator;
4
5
use Happyr\AutoFallbackTranslationBundle\Service\TranslatorService;
6
use Symfony\Component\Translation\TranslatorBagInterface;
7
use Symfony\Component\Translation\TranslatorInterface;
8
9
/**
10
 * @author Tobias Nyholm <[email protected]>
11
 */
12
class FallbackTranslator implements TranslatorInterface, TranslatorBagInterface
13
{
14
    /**
15
     * @var TranslatorInterface|TranslatorBagInterface
16
     */
17
    private $symfonyTranslator;
18
19
    /**
20
     * @var TranslatorService
21
     */
22
    private $translatorService;
23
24
    /**
25
     * @var string
26
     */
27
    private $defaultLocale;
28
29
    /**
30
     * @param string              $defaultLocale
31
     * @param TranslatorInterface $symfonyTranslator
32
     * @param TranslatorService   $translatorService
33
     */
34
    public function __construct($defaultLocale, TranslatorInterface $symfonyTranslator, TranslatorService $translatorService)
35
    {
36
        $this->symfonyTranslator = $symfonyTranslator;
37
        $this->translatorService = $translatorService;
38
        $this->defaultLocale = $defaultLocale;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 View Code Duplication
    public function trans($id, array $parameters = array(), $domain = null, $locale = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46
        $id = (string) $id;
47
        if (!$domain) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $domain of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
48
            $domain = 'messages';
49
        }
50
51
        $catalogue = $this->getCatalogue($locale);
52
        if ($catalogue->defines($id, $domain)) {
53
            return $this->symfonyTranslator->trans($id, $parameters, $domain, $locale);
0 ignored issues
show
Bug introduced by
The method trans does only exist in Symfony\Component\Translation\TranslatorInterface, but not in Symfony\Component\Transl...\TranslatorBagInterface.

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
        $locale = $catalogue->getLocale();
57
        if ($locale === $this->defaultLocale) {
58
            // we cant do anything...
59
            return $id;
60
        }
61
62
        $orgString = $this->symfonyTranslator->trans($id, $parameters, $domain, $this->defaultLocale);
63
64
        return $this->translateWithSubstitutedParameters($orgString, $locale, $parameters);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 View Code Duplication
    public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        $id = (string) $id;
73
        if (!$domain) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $domain of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
74
            $domain = 'messages';
75
        }
76
77
        $catalogue = $this->getCatalogue($locale);
78
        if ($catalogue->defines($id, $domain)) {
79
            return $this->symfonyTranslator->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\Component\Transl...\TranslatorBagInterface.

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...
80
        }
81
82
        $locale = $catalogue->getLocale();
83
        if ($locale === $this->defaultLocale) {
84
            // we cant do anything...
85
            return $id;
86
        }
87
88
        $orgString = $this->symfonyTranslator->transChoice($id, $number, $parameters, $domain, $this->defaultLocale);
89
90
        return $this->translateWithSubstitutedParameters($orgString, $locale, $parameters);
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function setLocale($locale)
97
    {
98
        $this->symfonyTranslator->setLocale($locale);
0 ignored issues
show
Bug introduced by
The method setLocale does only exist in Symfony\Component\Translation\TranslatorInterface, but not in Symfony\Component\Transl...\TranslatorBagInterface.

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...
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function getLocale()
105
    {
106
        return $this->symfonyTranslator->getLocale();
0 ignored issues
show
Bug introduced by
The method getLocale does only exist in Symfony\Component\Translation\TranslatorInterface, but not in Symfony\Component\Transl...\TranslatorBagInterface.

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...
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function getCatalogue($locale = null)
113
    {
114
        return $this->symfonyTranslator->getCatalogue($locale);
0 ignored issues
show
Bug introduced by
The method getCatalogue does only exist in Symfony\Component\Transl...\TranslatorBagInterface, 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...
115
    }
116
117
    /**
118
     * Passes through all unknown calls onto the translator object.
119
     */
120
    public function __call($method, $args)
121
    {
122
        return call_user_func_array(array($this->symfonyTranslator, $method), $args);
123
    }
124
125
    /**
126
     * @param string $orgString  This is the string in the default locale. It has the values of $parameters in the string already.
127
     * @param string $locale     you wan to translate to.
128
     * @param array  $parameters
129
     *
130
     * @return string
131
     */
132
    private function translateWithSubstitutedParameters($orgString, $locale, array $parameters)
133
    {
134
        // Replace parameters
135
        $replacements = [];
136
        foreach ($parameters as $placeholder => $nonTranslatableValue) {
137
            $replacements[(string) $nonTranslatableValue] = uniqid();
138
        }
139
140
        $replacedString = str_replace(array_keys($replacements), array_values($replacements), $orgString);
141
        $translatedString = $this->getTranslatorService()->translate($replacedString, $this->defaultLocale, $locale);
142
143
        return str_replace(array_values($replacements), array_keys($replacements), $translatedString);
144
    }
145
146
    /**
147
     * @return TranslatorService
148
     */
149
    protected function getTranslatorService()
150
    {
151
        return $this->translatorService;
152
    }
153
}
154