GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 0f873c...627a89 )
by Olivier
01:53
created

Plurals::extract_samples()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ICanBoogie\CLDR;
13
14
use ICanBoogie\CLDR\Plurals\Rule;
15
use ICanBoogie\CLDR\Plurals\Samples;
16
17
/**
18
 * Representation of plurals
19
 */
20
class Plurals extends \ArrayObject
21
{
22
	const COUNT_ZERO = 'zero';
23
	const COUNT_ONE = 'one';
24
	const COUNT_TWO = 'two';
25
	const COUNT_FEW = 'few';
26
	const COUNT_MANY = 'many';
27
	const COUNT_OTHER = 'other';
28
29
	const RULE_COUNT_PREFIX = 'pluralRule-count-';
30
31
	/**
32
	 * @var Rule[][]
33
	 */
34
	private $rules = [];
35
36
	/**
37
	 * @var Samples[][]
38
	 */
39
	private $samples = [];
40
41
	/**
42
	 * @param number $number
43
	 * @param string $locale
44
	 *
45
	 * @return string One of `COUNT_*`.
46
	 */
47
	public function rule_for($number, $locale)
48
	{
49
		foreach ($this->rule_instances_for($locale) as $count => $rule)
50
		{
51
			if ($rule->validate($number))
52
			{
53
				return $count;
54
			}
55
		}
56
57
		return self::COUNT_OTHER; // @codeCoverageIgnore
58
	}
59
60
	/**
61
	 * @param string $locale
62
	 *
63
	 * @return string[]
64
	 */
65
	public function rules_for($locale)
66
	{
67
		return array_keys($this->rule_instances_for($locale));
68
	}
69
70
	/**
71
	 * @param string $locale
72
	 *
73
	 * @return Samples[]
74
	 */
75
	public function samples_for($locale)
76
	{
77
		$samples = &$this->samples[$locale];
78
79
		return $samples ?: $samples = $this->create_samples_for($locale);
80
	}
81
82
	/**
83
	 * @param string $locale
84
	 *
85
	 * @return Rule[]
86
	 */
87
	private function rule_instances_for($locale)
88
	{
89
		$rules = &$this->rules[$locale];
90
91
		return $rules ?: $rules = $this->create_rules_for($locale);
92
	}
93
94
	/**
95
	 * @param string $locale
96
	 *
97
	 * @return Rule[]
98
	 */
99 View Code Duplication
	private function create_rules_for($locale)
1 ignored issue
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...
100
	{
101
		$rules = [];
102
		$prefix_length = strlen(self::RULE_COUNT_PREFIX);
103
104
		foreach ($this[$locale] as $count => $rule_string)
105
		{
106
			$count = substr($count, $prefix_length);
107
			$rules[$count] = Rule::from($this->extract_rule($rule_string));
108
		}
109
110
		return $rules;
111
	}
112
113
	/**
114
	 * @param string $rule_string
115
	 *
116
	 * @return string
117
	 */
118
	private function extract_rule($rule_string)
119
	{
120
		$rule = explode('@', $rule_string, 2);
121
		$rule = array_shift($rule);
122
		$rule = trim($rule);
123
124
		return $rule;
125
	}
126
127
	/**
128
	 * @param string $locale
129
	 *
130
	 * @return Samples[]
131
	 */
132 View Code Duplication
	private function create_samples_for($locale)
1 ignored issue
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...
133
	{
134
		$samples = [];
135
		$prefix_length = strlen(self::RULE_COUNT_PREFIX);
136
137
		foreach ($this[$locale] as $count => $rule_string)
138
		{
139
			$count = substr($count, $prefix_length);
140
			$samples[$count] = Samples::from($this->extract_samples($rule_string));
141
		}
142
143
		return $samples;
144
	}
145
146
	/**
147
	 * @param string $rule_string
148
	 *
149
	 * @return string
150
	 */
151
	private function extract_samples($rule_string)
152
	{
153
		return substr($rule_string, strpos($rule_string, '@'));
154
	}
155
}
156