Completed
Push — master ( 2a4290...c23846 )
by Nico
01:31
created

Split::call()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 14
cts 14
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 16
nc 5
nop 2
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @license     http://opensource.org/licenses/mit-license.php MIT
7
 * @link        https://github.com/nicoSWD
8
 * @author      Nicolas Oelgart <[email protected]>
9
 */
10
namespace nicoSWD\Rules\Grammar\JavaScript\Methods;
11
12
use nicoSWD\Rules\Core\CallableFunction;
13
use nicoSWD\Rules\Tokens\BaseToken;
14
use nicoSWD\Rules\Tokens\TokenArray;
15
use nicoSWD\Rules\Tokens\TokenRegex;
16
17
final class Split extends CallableFunction
18
{
19
    /**
20
     * @param BaseToken $separator
21
     * @param BaseToken $limit
22
     * @return BaseToken
23
     */
24 22
    public function call($separator = null, $limit = null): BaseToken
25
    {
26 22
        if (!$separator || !is_string($separator->getValue())) {
27 4
            $newValue = [$this->token->getValue()];
28
        } else {
29 18
            $params = [$separator->getValue(), $this->token->getValue()];
30
31 18
            if ($limit) {
32 4
                $params[] = (int) $limit->getValue();
33
            }
34
35 18
            if ($separator instanceof TokenRegex) {
36 4
                $func = 'preg_split';
37
            } else {
38 14
                $func = 'explode';
39
            }
40
41 18
            $newValue = $func(...$params);
42
        }
43
44 22
        return new TokenArray(
45 22
            $newValue,
46 22
            $this->token->getOffset(),
47 22
            $this->token->getStack()
48
        );
49
    }
50
}
51