Passed
Push — master ( ff616c...b121cb )
by Bernardette
01:55
created

Plural::plural()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 2
1
<?php declare(strict_types=1);
2
3
namespace Rostenkowski\Translate;
4
5
6
use function array_key_exists;
0 ignored issues
show
Bug introduced by
The type array_key_exists 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...
7
8
class Plural implements PluralInterface
9
{
10
11
12
	/**
13
	 * default plural schema (english-compatible)
14
	 *
15
	 * @var string
16
	 */
17
	private $defaultSchema = 'nplurals=2; plural=(n != 1)';
18
19
	/**
20
	 * @var int
21
	 */
22
	private $evalCounter = 0;
23
24
	/**
25
	 * @var int
26
	 */
27
	private $evalCacheHitCounter = 0;
28
29
	/**
30
	 * @var array
31
	 */
32
	private $evalCache = [];
33
34
	/**
35
	 * locale-indexed map of irregular plural schemas
36
	 *
37
	 * @var string[]
38
	 */
39
	private $schemas = [
40
		// czech
41
		'cs_CZ' => 'nplurals=3; plural=(n==1) ? 0 : ((n>=2 && n<=4) ? 1 : 2)',
42
		// croatian
43
		'cr_CR' => 'nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : (n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2))',
44
		// french
45
		'fr_FR' => 'nplurals=2; plural=(n > 1)',
46
		// indonesian
47
		'id_ID' => 'nplurals=1; plural=0',
48
		// icelandic
49
		'is_IS' => 'nplurals=2; plural=(n%10!=1 || n%100==11)',
50
		// japanese
51
		'ja_JP' => 'nplurals=1; plural=0',
52
		// georgian
53
		'ka_GE' => 'nplurals=1; plural=0',
54
		// korean
55
		'ko_KR' => 'nplurals=1; plural=0',
56
		// lao
57
		'lo_LA' => 'nplurals=1; plural=0',
58
		// lithuanian
59
		'lt_LT' => 'nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : (n%10>=2 && (n%100<10 or n%100>=20) ? 1 : 2))',
60
		// macedonian
61
		'mk_MK' => 'nplurals=2; plural= n==1 || n%10==1 ? 0 : 1',
62
		// maltese
63
		'mt_MT' => 'nplurals=4; plural=(n==1 ? 0 : (n==0 || ( n%100>1 && n%100<11) ? 1 : ((n%100>10 && n%100<20 ) ? 2 : 3)))',
64
		// malay
65
		'ms_MY' => 'nplurals=1; plural=0',
66
		// burmese
67
		'my_MM' => 'nplurals=1; plural=0',
68
		// polish
69
		'pl_PL' => 'nplurals=3; plural=(n==1 ? 0 : (n%10>=2 && n%10<=4 && (n%100<10 || (n%100>=20) ? 1 : 2)))',
70
		// slovak
71
		'sk_SK' => 'nplurals=3; plural=(n==1) ? 0 : ((n>=2 && n<=4) ? 1 : 2)',
72
		// slovenian
73
		'sl_SL' => 'nplurals=4; plural=(n%100==1 ? 1 : ((n%100==2 ? 2 : n%100==3) || (n%100==4 ? 3 : 0)))',
74
		// romanian
75
		'ro_RO' => 'nplurals=3; plural=(n==1 ? 0 : ((n==0 || (n%100 > 0 && n%100 < 20)) ? 1 : 2));',
76
		// russian
77
		'ru_RU' => 'nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : (n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2))',
78
		// thai
79
		'th_TH' => 'nplurals=1; plural=0',
80
		// turkish
81
		'tr_TR' => 'nplurals=2; plural=(n>1)',
82
		// ukrainian
83
		'uk_UA' => 'nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : (n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2))',
84
		// uzbek
85
		'uz_UZ' => 'nplurals=2; plural=(n > 1)',
86
		// vietnamese
87
		'vi_VN' => 'nplurals=1; plural=0',
88
		// chinese
89
		'zh_CN' => 'nplurals=1; plural=0',
90
	];
91
92
93
	public function plural(string $locale, int $count): int
94
	{
95
		// cache eval results
96
		if (array_key_exists($cacheKey = "$locale.$count", $this->evalCache)) {
97
			$this->evalCacheHitCounter++;
98
99
			return $this->evalCache[$cacheKey];
100
		}
101
102
		// evaluate schema
103
		$schema = $this->defaultSchema;
104
		if (isset($this->schemas[$locale])) {
105
			$schema = $this->schemas[$locale];
106
		}
107
108
		// create php code from the schema
109
		$code = preg_replace('/([a-z]+)/', '$$1', "n=$count; " . $schema) . '; return (int) $plural;';
110
		$this->evalCounter++;
111
112
		return $this->evalCache[$cacheKey] = eval($code);
113
	}
114
115
}
116