EndsWith::call()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 17
ccs 10
cts 10
cp 1
crap 3
rs 9.9332
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\Parser\Exception\ParserException;
12
use nicoSWD\Rule\TokenStream\Token\BaseToken;
13
use nicoSWD\Rule\TokenStream\Token\TokenBool;
14
use nicoSWD\Rule\TokenStream\Token\TokenString;
15
16
final class EndsWith extends CallableFunction
17
{
18 8
    public function call(?BaseToken ...$parameters): BaseToken
19
    {
20 8
        if (!$this->token instanceof TokenString) {
0 ignored issues
show
Bug Best Practice introduced by
The property token does not exist on nicoSWD\Rule\Grammar\JavaScript\Methods\EndsWith. Did you maybe forget to declare it?
Loading history...
21 2
            throw new ParserException('Call to undefined method "endsWith" on non-string');
22
        }
23
24 6
        $needle = $this->parseParameter($parameters, numParam: 0);
25 6
        $haystack = $this->token->getValue();
26
27 6
        if (!$needle) {
28 2
            $result = false;
29
        } else {
30 4
            $needle = $needle->getValue();
31 4
            $result = str_ends_with($haystack, $needle);
32
        }
33
34 6
        return TokenBool::fromBool($result);
35
    }
36
}
37