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 ( 5d16d0...819586 )
by Carlos
03:00
created

Pinyin::splitWords()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 6
nc 2
nop 2
crap 3
1
<?php
2
3
/*
4
 * This file is part of the overtrue/pinyin.
5
 *
6
 * (c) 2016 overtrue <[email protected]>
7
 */
8
9
namespace Overtrue\Pinyin;
10
11
use InvalidArgumentException;
12
13 1
define('PINYIN_NONE', 'none');
14 1
define('PINYIN_ASCII', 'ascii');
15 1
define('PINYIN_UNICODE', 'unicode');
16
17
/**
18
 * Chinese to pinyin translator.
19
 *
20
 * @author    overtrue <[email protected]>
21
 * @copyright 2015 overtrue <[email protected]>
22
 *
23
 * @link      https://github.com/overtrue/pinyin
24
 * @link      http://overtrue.me
25
 */
26
class Pinyin
27
{
28
    const NONE = 'none';
29
    const ASCII = 'ascii';
30
    const UNICODE = 'unicode';
31
32
    /**
33
     * Dict loader.
34
     *
35
     * @var \Overtrue\Pinyin\DictLoaderInterface
36
     */
37
    protected $loader;
38
39
    /**
40
     * Punctuations map.
41
     *
42
     * @var array
43
     */
44
    protected $punctuations = array(
45
        ',' => ',',
46
        '。' => '.',
47
        '!' => '!',
48
        '?' => '?',
49
        ':' => ':',
50
        '“' => '"',
51
        '”' => '"',
52
        '‘' => "'",
53
        '’' => "'",
54
    );
55
56
    /**
57
     * Constructor.
58
     *
59
     * @param \Overtrue\Pinyin\DictLoaderInterface $loader
60
     */
61 10
    public function __construct(DictLoaderInterface $loader = null)
62
    {
63 10
        $this->loader = $loader;
64 10
    }
65
66
    /**
67
     * Convert string to pinyin.
68
     *
69
     * @param string $string
70
     * @param string $option
71
     *
72
     * @return array
73
     */
74 4
    public function convert($string, $option = self::NONE)
75
    {
76 4
        $pinyin = $this->romanize($string);
77
78 4
        return $this->splitWords($pinyin, $option);
79
    }
80
81
    /**
82
     * Convert string (person name) to pinyin.
83
     *
84
     * @param string $stringName
85
     * @param string $option
86
     *
87
     * @return array
88
     */
89 1
    public function name($stringName, $option = self::NONE)
90
    {
91 1
        $pinyin = $this->romanize($stringName, true);
92
93 1
        return $this->splitWords($pinyin, $option);
94
    }
95
96
    /**
97
     * Return a pinyin permlink from string.
98
     *
99
     * @param string $string
100
     * @param string $delimiter
101
     *
102
     * @return string
103
     */
104 1
    public function permlink($string, $delimiter = '-')
105
    {
106 1
        if (!in_array($delimiter, array('_', '-', '.', ''), true)) {
107 1
            throw new InvalidArgumentException("Delimiter must be one of: '_', '-', '', '.'.");
108
        }
109
110 1
        return implode($delimiter, $this->convert($string, false));
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
111
    }
112
113
    /**
114
     * Return first letters.
115
     *
116
     * @param string $string
117
     * @param string $delimiter
118
     *
119
     * @return string
120
     */
121 1
    public function abbr($string, $delimiter = '')
122
    {
123
        return implode($delimiter, array_map(function ($pinyin) {
124 1
            return $pinyin[0];
125 1
        }, $this->convert($string, false)));
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
126
    }
127
128
    /**
129
     * Chinese to pinyin sentense.
130
     *
131
     * @param string $sentence
132
     * @param string $option
0 ignored issues
show
Bug introduced by
There is no parameter named $option. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
133
     *
134
     * @return string
135
     */
136 5
    public function sentence($sentence, $withTone = false)
137
    {
138 5
        $marks = array_keys($this->punctuations);
139 5
        $punctuationsRegex = preg_quote(implode(array_merge($marks, $this->punctuations)), '/');
140 5
        $regex = '/[^üāēīōūǖáéíóúǘǎěǐǒǔǚàèìòùǜa-z0-9'.$punctuationsRegex.'\s_]+/iu';
141
142 5
        $pinyin = preg_replace($regex, '', $this->romanize($sentence));
143
144 5
        $punctuations = array_merge($this->punctuations, array("\t" => ' ', '  ' => ' '));
145 5
        $pinyin = trim(str_replace(array_keys($punctuations), $punctuations, $pinyin));
146
147 5
        return $withTone ? $pinyin : $this->format($pinyin, false);
148
    }
149
150
    /**
151
     * Loader setter.
152
     *
153
     * @param \Overtrue\Pinyin\DictLoaderInterface $loader
154
     *
155
     * @return $this
156
     */
157 1
    public function setLoader(DictLoaderInterface $loader)
158
    {
159 1
        $this->loader = $loader;
160
161 1
        return $this;
162
    }
163
164
    /**
165
     * Return dict loader,.
166
     *
167
     * @return \Overtrue\Pinyin\DictLoaderInterface
168
     */
169 10
    public function getLoader()
170
    {
171 10
        return $this->loader ?: new FileDictLoader(__DIR__.'/../data/');
172
    }
173
174
    /**
175
     * Preprocess.
176
     *
177
     * @param string $string
178
     *
179
     * @return string
180
     */
181 10
    protected function prepare($string)
182
    {
183
        $string = preg_replace_callback('~[a-z0-9_-]+~i', function ($matches) {
184 3
            return "\t".$matches[0];
185 10
        }, $string);
186
187 10
        return preg_replace("~[^\p{Han}\p{P}\p{Z}\p{M}\p{N}\p{L}\t]~u", '', $string);
188
    }
189
190
    /**
191
     * Convert Chinese to pinyin.
192
     *
193
     * @param string $string
194
     * @param bool   $isName
195
     *
196
     * @return string
197
     */
198 10
    protected function romanize($string, $isName = false)
199
    {
200 10
        $string = $this->prepare($string);
201
202 10
        $dictLoader = $this->getLoader();
203
204 10
        if ($isName) {
205 1
            $string = $this->convertSurname($string, $dictLoader);
206 1
        }
207
208
        $dictLoader->map(function ($dictionary) use (&$string) {
209 10
            $string = strtr($string, $dictionary);
210 10
        });
211
212 10
        return $string;
213
    }
214
215
    /**
216
     * Convert Chinese Surname to pinyin.
217
     *
218
     * @param string                               $string
219
     * @param \Overtrue\Pinyin\DictLoaderInterface $dictLoader
220
     *
221
     * @return string
222
     */
223
    protected function convertSurname($string, $dictLoader) {
224 1
        $dictLoader->mapSurname(function ($dictionary) use (&$string) {
225 1
            foreach ($dictionary as $surname => $pinyin) {
226 1
                if (strpos($string, $surname) === 0) {
227 1
                    $string = $pinyin . mb_substr($string, mb_strlen($surname, 'UTF-8'), null, 'UTF-8');
228 1
                    break;
229
                }
230 1
            }
231 1
        });
232
233 1
        return $string;
234
    }
235
236
    /**
237
     * Split pinyin string to words.
238
     *
239
     * @param string $pinyin
240
     * @param string $option
241
     *
242
     * @return array
243
     */
244 5
    public function splitWords($pinyin, $option)
245
    {
246 5
        $split = array_filter(preg_split('/[^üāēīōūǖáéíóúǘǎěǐǒǔǚàèìòùǜa-z]+/iu', $pinyin));
247
248 5
        if ($option !== self::UNICODE) {
249 4
            foreach ($split as $index => $pinyin) {
250 4
                $split[$index] = $this->format($pinyin, $option === self::ASCII);
251 4
            }
252 4
        }
253
254 5
        return array_values($split);
255
    }
256
257
    /**
258
     * Format.
259
     *
260
     * @param string $pinyin
261
     * @param bool   $tone
262
     *
263
     * @return string
264
     */
265 8
    protected function format($pinyin, $tone = false)
266
    {
267
        $replacements = array(
268 8
            'üē' => array('ue', 1), 'üé' => array('ue', 2), 'üě' => array('ue', 3), 'üè' => array('ue', 4),
269 8
            'ā' => array('a', 1), 'ē' => array('e', 1), 'ī' => array('i', 1), 'ō' => array('o', 1), 'ū' => array('u', 1), 'ǖ' => array('v', 1),
270 8
            'á' => array('a', 2), 'é' => array('e', 2), 'í' => array('i', 2), 'ó' => array('o', 2), 'ú' => array('u', 2), 'ǘ' => array('v', 2),
271 8
            'ǎ' => array('a', 3), 'ě' => array('e', 3), 'ǐ' => array('i', 3), 'ǒ' => array('o', 3), 'ǔ' => array('u', 3), 'ǚ' => array('v', 3),
272 8
            'à' => array('a', 4), 'è' => array('e', 4), 'ì' => array('i', 4), 'ò' => array('o', 4), 'ù' => array('u', 4), 'ǜ' => array('v', 4),
273 8
        );
274
275 8
        foreach ($replacements as $unicde => $replacements) {
276 8
            if (false !== strpos($pinyin, $unicde)) {
277 7
                $pinyin = str_replace($unicde, $replacements[0], $pinyin).($tone ? $replacements[1] : '');
278 7
            }
279 8
        }
280
281 8
        return $pinyin;
282
    }
283
}
284