Completed
Push — master ( 00951d...4d50ae )
by Tobias
09:16
created

translateWithSubstitutedParameters()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 9
cts 10
cp 0.9
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 4
nop 3
crap 3.009
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\Translation\TranslatorBagInterface;
15
use Symfony\Component\Translation\TranslatorInterface;
16
use Translation\Translator\Translator;
17
18
/**
19
 * This is a bridge between Symfony's translator service and Translation\Translator\Translator.
20
 *
21
 * @author Tobias Nyholm <[email protected]>
22
 */
23
class FallbackTranslator implements TranslatorInterface, TranslatorBagInterface
24
{
25
    /**
26
     * @var TranslatorInterface|TranslatorBagInterface
27
     */
28
    private $symfonyTranslator;
29
30
    /**
31
     * @var Translator
32
     */
33
    private $externalTranslator;
34
35
    /**
36
     * @var string
37
     */
38
    private $defaultLocale;
39
40
    /**
41
     * @param string              $defaultLocale
42
     * @param TranslatorInterface $symfonyTranslator
43
     * @param Translator          $translatorService
0 ignored issues
show
Bug introduced by
There is no parameter named $translatorService. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
44
     */
45 1
    public function __construct($defaultLocale, TranslatorInterface $symfonyTranslator, Translator $externalTranslator)
46
    {
47 1
        $this->symfonyTranslator = $symfonyTranslator;
48 1
        $this->externalTranslator = $externalTranslator;
49 1
        $this->defaultLocale = $defaultLocale;
50 1
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 View Code Duplication
    public function trans($id, array $parameters = [], $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...
56
    {
57
        $id = (string) $id;
58
        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...
59
            $domain = 'messages';
60
        }
61
62
        $catalogue = $this->getCatalogue($locale);
63
        if ($catalogue->defines($id, $domain)) {
64
            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...
65
        }
66
67
        $locale = $catalogue->getLocale();
68
        if ($locale === $this->defaultLocale) {
69
            // we cant do anything...
70
            return $id;
71
        }
72
73
        $orgString = $this->symfonyTranslator->trans($id, $parameters, $domain, $this->defaultLocale);
74
75
        return $this->translateWithSubstitutedParameters($orgString, $locale, $parameters);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 View Code Duplication
    public function transChoice($id, $number, array $parameters = [], $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...
82
    {
83
        $id = (string) $id;
84
        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...
85
            $domain = 'messages';
86
        }
87
88
        $catalogue = $this->getCatalogue($locale);
89
        if ($catalogue->defines($id, $domain)) {
90
            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...
91
        }
92
93
        $locale = $catalogue->getLocale();
94
        if ($locale === $this->defaultLocale) {
95
            // we cant do anything...
96
            return $id;
97
        }
98
99
        $orgString = $this->symfonyTranslator->transChoice($id, $number, $parameters, $domain, $this->defaultLocale);
100
101
        return $this->translateWithSubstitutedParameters($orgString, $locale, $parameters);
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function setLocale($locale)
108
    {
109
        $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...
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function getLocale()
116
    {
117
        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...
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function getCatalogue($locale = null)
124
    {
125
        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...
126
    }
127
128
    /**
129
     * Passes through all unknown calls onto the translator object.
130
     */
131
    public function __call($method, $args)
132
    {
133
        return call_user_func_array([$this->symfonyTranslator, $method], $args);
134
    }
135
136
    /**
137
     * @param string $orgString  This is the string in the default locale. It has the values of $parameters in the string already.
138
     * @param string $locale     you wan to translate to.
139
     * @param array  $parameters
140
     *
141
     * @return string
142
     */
143 1
    private function translateWithSubstitutedParameters($orgString, $locale, array $parameters)
144
    {
145
        // Replace parameters
146 1
        $replacements = [];
147 1
        foreach ($parameters as $placeholder => $nonTranslatableValue) {
148 1
            $replacements[(string) $nonTranslatableValue] = uniqid();
149 1
        }
150
151 1
        $replacedString = str_replace(array_keys($replacements), array_values($replacements), $orgString);
152 1
        $translatedString = $this->externalTranslator->translate($replacedString, $this->defaultLocale, $locale);
153
154 1
        if ($translatedString === null) {
155
            // Could not be translated
156
            return $orgString;
157
        }
158
159 1
        return str_replace(array_values($replacements), array_keys($replacements), $translatedString);
160
    }
161
}
162