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

CharAt   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 32
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B call() 0 25 4
1
<?php
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
declare(strict_types=1);
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\TokenInteger;
15
use nicoSWD\Rules\Tokens\TokenString;
16
17
final class CharAt extends CallableFunction
18
{
19
    /**
20
     * @param BaseToken $offset
21
     * @return BaseToken
22
     */
23 10
    public function call($offset = null): BaseToken
24
    {
25 10
        $tokenValue = $this->token->getValue();
26
27 10
        if (!$offset) {
28 2
            $offset = 0;
29
        }
30 8
        elseif (!$offset instanceof TokenInteger) {
31 2
            $offset = (int) $offset->getValue();
32
        } else {
33 6
            $offset = $offset->getValue();
34
        }
35
36 10
        if (!isset($tokenValue[$offset])) {
37 2
            $char = '';
38
        } else {
39 8
            $char = $tokenValue[$offset];
40
        }
41
42 10
        return new TokenString(
43 10
            $char,
44 10
            $this->token->getOffset(),
45 10
            $this->token->getStack()
46
        );
47
    }
48
}
49