Completed
Push — master ( 94ba74...5b4930 )
by Filip
04:55
created

PrefixedTranslator   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 119
Duplicated Lines 10.08 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 7
dl 12
loc 119
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 6 16 4
C translate() 6 23 8
A unwrap() 0 4 1
A unregister() 0 6 1
A register() 0 6 1
A overrideTemplateTranslator() 0 4 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 the Kdyby (http://www.kdyby.org)
5
 *
6
 * Copyright (c) 2008 Filip Procházka ([email protected])
7
 *
8
 * For the full copyright and license information, please view the file license.txt that was distributed with this source code.
9
 */
10
11
namespace Kdyby\Translation;
12
13
use Kdyby;
14
use Latte;
15
use Nette\Utils\Strings;
16
17
18
19
/**
20
 * @author Filip Procházka <[email protected]>
21
 */
22
class PrefixedTranslator implements ITranslator
23
{
24
25
	use Kdyby\StrictObjects\Scream;
26
27
	/**
28
	 * @var \Kdyby\Translation\ITranslator|\Kdyby\Translation\Translator|\Kdyby\Translation\PrefixedTranslator
29
	 */
30
	private $translator;
31
32
	/**
33
	 * @var string
34
	 */
35
	private $prefix;
36
37
38
39
	/**
40
	 * @param string $prefix
41
	 * @param \Kdyby\Translation\ITranslator $translator
42
	 * @throws \Kdyby\Translation\InvalidArgumentException
43
	 */
44
	public function __construct($prefix, ITranslator $translator)
45
	{
46 View Code Duplication
		if (!$translator instanceof Translator && !$translator instanceof PrefixedTranslator) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
47
			throw new InvalidArgumentException(sprintf(
48
				'The given translator must be instance of Kdyby\Translation\Translator or Kdyby\Translation\PrefixedTranslator, bug %s was given',
49
				get_class($translator)
50
			));
51
		}
52
53
		if ($translator instanceof PrefixedTranslator) { // todo: this is just an experiment
54
			$translator = $translator->unwrap();
55
		}
56
57
		$this->translator = $translator;
58
		$this->prefix = rtrim($prefix, '.');
59
	}
60
61
62
63
	/**
64
	 * @param string|\Kdyby\Translation\Phrase $message
65
	 * @param int|array|NULL $count
66
	 * @param array|string|NULL $parameters
67
	 * @param string|NULL $domain
68
	 * @param string|NULL $locale
69
	 * @return string|\Nette\Utils\IHtmlString|\Latte\Runtime\IHtmlString
70
	 */
71
	public function translate($message, $count = NULL, $parameters = [], $domain = NULL, $locale = NULL)
72
	{
73
		$translationString = ($message instanceof Phrase ? $message->message : $message);
74
		$prefix = $this->prefix . '.';
75
76
		if (Strings::startsWith($message, '//')) {
77
			$prefix = NULL;
78
			$translationString = Strings::substring($translationString, 2);
79
		}
80
81
		if ($message instanceof Phrase) {
82
			return $this->translator->translate(new Phrase($prefix . $translationString, $message->count, $message->parameters, $message->domain, $message->locale));
83
		}
84
85 View Code Duplication
		if (is_array($count)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
86
			$locale = $domain !== NULL ? (string) $domain : NULL;
87
			$domain = $parameters !== NULL && !empty($parameters) ? (string) $parameters : NULL;
88
			$parameters = $count;
89
			$count = NULL;
90
		}
91
92
		return $this->translator->translate($prefix . $translationString, $count, (array) $parameters, $domain, $locale);
0 ignored issues
show
Unused Code introduced by
The call to ITranslator::translate() has too many arguments starting with (array) $parameters.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
93
	}
94
95
96
97
	/**
98
	 * @return \Kdyby\Translation\ITranslator
99
	 */
100
	public function unwrap()
101
	{
102
		return $this->translator;
103
	}
104
105
106
107
	/**
108
	 * @param Latte\Runtime\Template $template
109
	 * @return \Kdyby\Translation\ITranslator
110
	 */
111
	public function unregister(Latte\Runtime\Template $template)
112
	{
113
		$translator = $this->unwrap();
114
		self::overrideTemplateTranslator($template, $translator);
115
		return $translator;
116
	}
117
118
119
120
	/**
121
	 * @param Latte\Runtime\Template $template
122
	 * @param string $prefix
123
	 * @throws \Kdyby\Translation\InvalidArgumentException
124
	 * @return \Kdyby\Translation\ITranslator
125
	 */
126
	public static function register(Latte\Runtime\Template $template, $prefix)
127
	{
128
		$translator = new static($prefix, $template->global->translator);
129
		self::overrideTemplateTranslator($template, $translator);
130
		return $translator;
131
	}
132
133
134
135
	private static function overrideTemplateTranslator(Latte\Runtime\Template $template, ITranslator $translator)
136
	{
137
		$template->getEngine()->addFilter('translate', [new TemplateHelpers($translator), 'translate']);
138
	}
139
140
}
141