Split   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 26
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A call() 0 24 5
1
<?php declare(strict_types=1);
2
3
/**
4
 * @license     http://opensource.org/licenses/mit-license.php MIT
5
 * @link        https://github.com/nicoSWD
6
 * @author      Nicolas Oelgart <[email protected]>
7
 */
8
namespace nicoSWD\Rule\Grammar\JavaScript\Methods;
9
10
use nicoSWD\Rule\Grammar\CallableFunction;
11
use nicoSWD\Rule\TokenStream\Token\BaseToken;
12
use nicoSWD\Rule\TokenStream\Token\TokenArray;
13
use nicoSWD\Rule\TokenStream\Token\TokenRegex;
14
15
final class Split extends CallableFunction
16
{
17 22
    public function call(?BaseToken ...$parameters): BaseToken
18
    {
19 22
        $separator = $this->parseParameter($parameters, numParam: 0);
20
21 22
        if (!$separator || !is_string($separator->getValue())) {
22 4
            $newValue = [$this->token->getValue()];
0 ignored issues
show
Bug Best Practice introduced by
The property token does not exist on nicoSWD\Rule\Grammar\JavaScript\Methods\Split. Did you maybe forget to declare it?
Loading history...
23
        } else {
24 18
            $params = [$separator->getValue(), $this->token->getValue()];
25 18
            $limit = $this->parseParameter($parameters, numParam: 1);
26
27 18
            if ($limit !== null) {
28 4
                $params[] = (int) $limit->getValue();
29
            }
30
31 18
            if ($separator instanceof TokenRegex) {
32 4
                $func = 'preg_split';
33
            } else {
34 14
                $func = 'explode';
35
            }
36
37 18
            $newValue = $func(...$params);
38
        }
39
40 22
        return new TokenArray($newValue);
41
    }
42
}
43