Completed
Push — master ( 2dc57f...5b6128 )
by Thomas
36:44 queued 22:51
created

L10NString::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * @author Bart Visscher <[email protected]>
4
 * @author Bernhard Posselt <[email protected]>
5
 * @author Jakob Sack <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 * @author Jörn Friedrich Dreyer <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Thomas Müller <[email protected]>
10
 *
11
 * @copyright Copyright (c) 2017, ownCloud GmbH
12
 * @license AGPL-3.0
13
 *
14
 * This code is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License, version 3,
16
 * as published by the Free Software Foundation.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License, version 3,
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
25
 *
26
 */
27
28
namespace OC\L10N;
29
use JsonSerializable;
30
31
class L10NString implements JsonSerializable {
32
	/** @var 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 L10N $l10n
46
	 * @param string|string[] $text
47
	 * @param array $parameters
48
	 * @param int $count
49
	 */
50
	public function __construct($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
	public function __toString() {
58
		$translations = $this->l10n->getTranslations();
59
60
		$text = $this->text;
61
		if (array_key_exists($this->text, $translations)) {
62
			if (is_array($translations[$this->text])) {
63
				$id = $this->l10n->computePlural($this->count);
64
				$text = $translations[$this->text][$id];
65
			} else {
66
				$text = $translations[$this->text];
67
			}
68
		}
69
70
		// Replace %n first (won't interfere with vsprintf)
71
		$text = str_replace('%n', $this->count, $text);
72
		return vsprintf($text, $this->parameters);
73
	}
74
75
76
	public function jsonSerialize() {
77
		return $this->__toString();
78
	}
79
}
80