TranslationExtension::getFilters()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
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;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Translation\TranslatorInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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) {
45
            throw new \InvalidArgumentException('Cannot deal with given translator.');
46
        }
47
48 3
        $this->translator = $translator;
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 not exist on Symfony\Contracts\Translation\TranslatorInterface. It seems like you code against a sub-type of Symfony\Contracts\Translation\TranslatorInterface such as Symfony\Component\Translation\Translator or Symfony\Component\Transl...DataCollectorTranslator or Symfony\Component\Translation\LoggingTranslator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
        if (false === $this->translator->/** @scrutinizer ignore-call */ getCatalogue($locale)->defines($message, $domain)) {
Loading history...
81
            return $this->translator->transChoice($defaultMessage, $count, \array_merge(['%count%' => $count], $arguments), $domain, $locale);
0 ignored issues
show
Bug introduced by
The method transChoice() does not exist on Symfony\Component\Transl...\TranslatorBagInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Symfony\Component\Translation\Translator or Translation\Bundle\Translator\TranslatorInterface or Symfony\Bundle\Framework...\Translation\Translator. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
            return $this->translator->/** @scrutinizer ignore-call */ transChoice($defaultMessage, $count, \array_merge(['%count%' => $count], $arguments), $domain, $locale);
Loading history...
Bug introduced by
The method transChoice() does not exist on Symfony\Contracts\Translation\TranslatorInterface. Did you maybe mean trans()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
            return $this->translator->/** @scrutinizer ignore-call */ transChoice($defaultMessage, $count, \array_merge(['%count%' => $count], $arguments), $domain, $locale);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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