CharAt::call()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 6
nop 1
dl 0
loc 20
ccs 12
cts 12
cp 1
crap 4
rs 9.8333
c 0
b 0
f 0
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\TokenInteger;
13
use nicoSWD\Rule\TokenStream\Token\TokenString;
14
15
final class CharAt extends CallableFunction
16
{
17 10
    public function call(?BaseToken ...$parameters): BaseToken
18
    {
19 10
        $tokenValue = $this->token->getValue();
0 ignored issues
show
Bug Best Practice introduced by
The property token does not exist on nicoSWD\Rule\Grammar\JavaScript\Methods\CharAt. Did you maybe forget to declare it?
Loading history...
20 10
        $offset = $this->parseParameter($parameters, numParam: 0);
21
22 10
        if (!$offset) {
23 2
            $offset = 0;
24 8
        } elseif (!$offset instanceof TokenInteger) {
25 2
            $offset = (int) $offset->getValue();
26
        } else {
27 6
            $offset = $offset->getValue();
28
        }
29
30 10
        if (!isset($tokenValue[$offset])) {
31 2
            $char = '';
32
        } else {
33 8
            $char = $tokenValue[$offset];
34
        }
35
36 10
        return new TokenString($char);
37
    }
38
}
39