Completed
Push — master ( 76acb2...4e56bc )
by Nico
02:11
created

Split   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 34
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B call() 0 26 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\Rule\Grammar\JavaScript\Methods;
11
12
use nicoSWD\Rule\Grammar\CallableFunction;
13
use nicoSWD\Rule\TokenStream\Token\BaseToken;
14
use nicoSWD\Rule\TokenStream\Token\TokenArray;
15
use nicoSWD\Rule\TokenStream\Token\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