Completed
Push — master ( 11e182...8521e7 )
by Lukas
15:27
created

L10NString   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 57
rs 10
wmc 5
lcom 0
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __toString() 0 19 3
A jsonSerialize() 0 3 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Bart Visscher <[email protected]>
6
 * @author Bernhard Posselt <[email protected]>
7
 * @author Jakob Sack <[email protected]>
8
 * @author Joas Schilling <[email protected]>
9
 * @author Jörn Friedrich Dreyer <[email protected]>
10
 * @author Morris Jobke <[email protected]>
11
 * @author Thomas Müller <[email protected]>
12
 *
13
 * @license AGPL-3.0
14
 *
15
 * This code is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License, version 3,
17
 * as published by the Free Software Foundation.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License, version 3,
25
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
26
 *
27
 */
28
29
namespace OC\L10N;
30
31
class L10NString implements \JsonSerializable {
32
	/** @var \OC\L10N\L10N */
33
	protected $l10n;
34
35
	/** @var string */
36
	protected $text;
37
38
	/** @var array */
39
	protected $parameters;
40
41
	/** @var integer */
42
	protected $count;
43
44
	/**
45
	 * @param \OC\L10N\L10N $l10n
46
	 * @param string|string[] $text
47
	 * @param array $parameters
48
	 * @param int $count
49
	 */
50
	public function __construct(\OC\L10N\L10N $l10n, $text, $parameters, $count = 1) {
51
		$this->l10n = $l10n;
52
		$this->text = $text;
0 ignored issues
show
Documentation Bug introduced by
It seems like $text can also be of type array<integer,string>. However, the property $text is declared as type string. 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...
53
		$this->parameters = $parameters;
54
		$this->count = $count;
55
	}
56
57
	/**
58
	 * @return string
59
	 */
60
	public function __toString() {
61
		$translations = $this->l10n->getTranslations();
62
63
		$text = $this->text;
64
		if(array_key_exists($this->text, $translations)) {
65
			if(is_array($translations[$this->text])) {
66
				$fn = $this->l10n->getPluralFormFunction();
67
				$id = $fn($this->count);
68
				$text = $translations[$this->text][$id];
69
			}
70
			else{
71
				$text = $translations[$this->text];
72
			}
73
		}
74
75
		// Replace %n first (won't interfere with vsprintf)
76
		$text = str_replace('%n', $this->count, $text);
77
		return vsprintf($text, $this->parameters);
78
	}
79
80
81
	/**
82
	 * @return string
83
	 */
84
	public function jsonSerialize() {
85
		return $this->__toString();
86
	}
87
}
88