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

Join::call()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 17
cts 17
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 18
nc 5
nop 1
crap 4
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\TokenStream\Token\BaseToken;
13
use nicoSWD\Rule\TokenStream\Token\TokenArray;
14
use nicoSWD\Rule\TokenStream\Token\TokenString;
15
use nicoSWD\Rule\TokenStream\TokenCollection;
16
use nicoSWD\Rule\Parser\Exception\ParserException;
17
use nicoSWD\Rule\Grammar\CallableFunction;
18
19
final class Join extends CallableFunction
20
{
21
    /**
22
     * @param BaseToken $glue
23
     * @return BaseToken
24
     * @throws ParserException
25
     */
26 18
    public function call($glue = null): BaseToken
27
    {
28 18
        if (!$this->token instanceof TokenArray) {
29 2
            throw new ParserException(sprintf(
30 2
                '%s.join is not a function at position %d on line %d',
31 2
                $this->token->getValue(),
32 2
                $this->token->getPosition(),
33 2
                $this->token->getLine()
34
            ));
35
        }
36
37 16
        if ($glue) {
38 14
            $glue = $glue->getValue();
39
        } else {
40 2
            $glue = ',';
41
        }
42
43 16
        $array = $this->token->getValue();
44
45 16
        if ($array instanceof TokenCollection) {
46 14
            $array = $array->toArray();
47
        }
48
49 16
        return new TokenString(
50 16
            implode($glue, $array),
51 16
            $this->token->getOffset(),
52 16
            $this->token->getStack()
53
        );
54
    }
55
}
56