Completed
Push — master ( ceccbd...e1228d )
by Tobias
08:59
created

TranslationExtension::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 5
cts 6
cp 0.8333
rs 9.8666
c 0
b 0
f 0
cc 3
nc 2
nop 2
crap 3.0416
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\Twig;
13
14
use Symfony\Component\Translation\TranslatorBagInterface;
15
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
16
use Symfony\Contracts\Translation\TranslatorInterface;
17
use Translation\Bundle\Twig\Visitor\DefaultApplyingNodeVisitor;
18
use Translation\Bundle\Twig\Visitor\NormalizingNodeVisitor;
19
use Translation\Bundle\Twig\Visitor\RemovingNodeVisitor;
20
use Twig\Extension\AbstractExtension;
21
use Twig\TwigFilter;
22
23
/**
24
 * @author Johannes M. Schmitt <[email protected]>
25
 * @author Tobias Nyholm <[email protected]>
26
 */
27
final class TranslationExtension extends AbstractExtension
28
{
29
    /**
30
     * @var TranslatorInterface|TranslatorBagInterface
31
     */
32
    private $translator;
33
34
    /**
35
     * @var bool
36
     */
37
    private $debug;
38
39 3
    public function __construct($translator, bool $debug = false)
40
    {
41
        // The TranslatorInterface has been deprecated in favor of Symfony\Contracts\Translation\TranslatorInterface in sf4.2.
42
        // Use this class to type hint event & remove the following condition once sf ^4.2 become the minimum supported version.
43
        // @see https://github.com/symfony/symfony/blob/master/UPGRADE-4.2.md#translation
44 3
        if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
0 ignored issues
show
Bug introduced by
The class Symfony\Component\Translation\TranslatorInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
45
            throw new \InvalidArgumentException('Cannot deal with given translator.');
46
        }
47
48 3
        $this->translator = $translator;
0 ignored issues
show
Documentation Bug introduced by
It seems like $translator can also be of type object<Symfony\Component...on\TranslatorInterface>. However, the property $translator is declared as type object<Symfony\Contracts...TranslatorBagInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
49 3
        $this->debug = $debug;
50 3
    }
51
52 3
    public function getFilters(): array
53
    {
54
        return [
55 3
            new TwigFilter('desc', [$this, 'desc']),
56 3
            new TwigFilter('meaning', [$this, 'meaning']),
57
        ];
58
    }
59
60 3
    public function getNodeVisitors(): array
61
    {
62
        $visitors = [
63 3
            new NormalizingNodeVisitor(),
64 3
            new RemovingNodeVisitor(),
65
        ];
66
67 3
        if ($this->debug) {
68 1
            $visitors[] = new DefaultApplyingNodeVisitor();
69
        }
70
71 3
        return $visitors;
72
    }
73
74
    public function transchoiceWithDefault(string $message, string $defaultMessage, int $count, array $arguments = [], ?string $domain = null, ?string $locale = null): string
75
    {
76
        if (null === $domain) {
77
            $domain = 'messages';
78
        }
79
80
        if (false === $this->translator->getCatalogue($locale)->defines($message, $domain)) {
0 ignored issues
show
Bug introduced by
The method getCatalogue does only exist in Symfony\Component\Transl...\TranslatorBagInterface, 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...
81
            return $this->translator->transChoice($defaultMessage, $count, \array_merge(['%count%' => $count], $arguments), $domain, $locale);
82
        }
83
84
        return $this->translator->transChoice($message, $count, \array_merge(['%count%' => $count], $arguments), $domain, $locale);
85
    }
86
87
    /**
88
     * @param mixed $v
89
     *
90
     * @return mixed
91
     */
92
    public function desc($v)
93
    {
94
        return $v;
95
    }
96
97
    /**
98
     * @param mixed $v
99
     *
100
     * @return mixed
101
     */
102
    public function meaning($v)
103
    {
104
        return $v;
105
    }
106
107
    public function getName(): string
108
    {
109
        return 'php-translation';
110
    }
111
}
112