Translator   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 138
Duplicated Lines 14.49 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 20
loc 138
c 0
b 0
f 0
wmc 12
lcom 1
cbo 2
rs 10
ccs 21
cts 21
cp 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A translationService() 0 4 1
A currentLanguage() 0 4 1
A domain() 0 4 1
A translate() 7 7 3
A translatePlural() 13 13 3
A addTranslation() 0 14 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * This file is part of slick/i18n package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\I18n;
11
12
use Zend\I18n\Translator\Translator as ZendTranslator;
13
14
/**
15
 * Translator
16
 *
17
 * @package Slick\I18n
18
 */
19
class Translator implements TranslatorInterface
20
{
21
    /**
22
     * @var Translation[]
23
     */
24
    private $translations = [];
25
26
    /**
27
     * @var TranslatorInterface|ZendTranslator
28
     */
29
    private $service;
30
31
    /**
32
     * @var string
33
     */
34
    private $domain;
35
36
    /**
37
     * @var Language
38
     */
39
    private $currentLanguage;
40
41
    /**
42
     * @var array
43
     */
44
    private $types = [
45
        Translation::TYPE_GETTEXT   => '.mo',
46
        Translation::TYPE_PHP_ARRAY => '.php'
47
    ];
48
49
    /**
50
     * Translator constructor.
51
     * @param Translation $translation
52
     * @param ZendTranslator|null $service
53
     */
54
    public function __construct(Translation $translation, ZendTranslator $service = null)
55
    {
56
        $this->service = ( null == $service) ? new ZendTranslator() : $service;
57
58
        $this->currentLanguage = $translation->language();
59
        $this->domain = $translation->domain();
60
61
        $this->addTranslation($translation);
62
    }
63
64
    /**
65
     * The translation service
66
     *
67
     * @return ZendTranslator
68
     */
69
    public function translationService()
70
    {
71
        return $this->service;
72
    }
73
74
    /**
75
     * The current default language
76
     *
77
     * @return Language
78
     */
79
    public function currentLanguage()
80
    {
81
        return $this->currentLanguage;
82
    }
83
84
    /**
85
     * Current default domain
86
     *
87
     * @return string
88
     */
89
    public function domain()
90
    {
91
        return $this->domain;
92
    }
93
94
    /**
95
     * Translate a message.
96
     *
97
     * @param  string $message
98
     * @param  string $textDomain
99
     * @param  string $locale
100
     * @return string
101
     */
102 2 View Code Duplication
    public function translate($message, $textDomain = 'default', $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...
103
    {
104 2
        $textDomain = $textDomain !== 'default' ? $textDomain : $this->domain();
105 2
        $locale = $locale ?: (string) $this->currentLanguage();
106
107
        return $this->translationService()->translate($message, $textDomain, $locale);
108
    }
109
110
    /**
111
     * Translate a plural message.
112
     *
113
     * @param  string $singular
114
     * @param  string $plural
115
     * @param  int $number
116
     * @param  string $textDomain
117
     * @param  string|null $locale
118
     * @return string
119
     */
120 View Code Duplication
    public function translatePlural(
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...
121
        $singular,
122
        $plural,
123
        $number,
124 6
        $textDomain = 'default',
125
        $locale = null
126 6
    )
127 2
    {
128 2
        $textDomain = $textDomain !== 'default' ? $textDomain : $this->domain();
129 6
        $locale = $locale ?: (string) $this->currentLanguage();
130
131
        return $this->translationService()->translatePlural($singular, $plural, $number, $textDomain, $locale);
132
    }
133
134
    /**
135
     * Adds a translation to the translator
136
     *
137
     * @param Translation $translation
138
     *
139
     * @return self|Translator
140 4
     */
141
    public function addTranslation(Translation $translation)
142 4
    {
143 4
        $key = "{$translation->language()}::{$translation->domain()}";
144 4
        $name = "{$translation->domain()}{$this->types[$translation->type()]}";
145
        $this->translationService()
146 4
            ->addTranslationFilePattern(
147 2
                $translation->type(),
148 2
                $translation->path(),
149
                "%s/{$name}",
150 2
                $translation->domain()
151 2
            );
152 2
        $this->translations[$key] = $translation;
153 2
        return $this;
154
    }
155 2
156
}
157