Passed
Pull Request — master (#22)
by Nico
18:37 queued 03:42
created

TokenFactory::buildTokenCollection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
ccs 4
cts 4
cp 1
crap 2
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\TokenStream\Token;
9
10
use nicoSWD\Rule\Parser\Exception\ParserException;
0 ignored issues
show
Bug introduced by
The type nicoSWD\Rule\Parser\Exception\ParserException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use nicoSWD\Rule\TokenStream\TokenCollection;
12
13
class TokenFactory
14
{
15
    /** @throws ParserException */
16
    public function createFromPHPType(mixed $value): BaseToken
0 ignored issues
show
Bug introduced by
The type nicoSWD\Rule\TokenStream\Token\BaseToken was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
    {
18
        return match (gettype($value)) {
19
            'string' => new TokenString($value),
20
            'integer' => new TokenInteger($value),
21
            'boolean' => TokenBool::fromBool($value),
22
            'NULL' => new TokenNull($value),
23
            'double' => new TokenFloat($value),
24
            'object' => new TokenObject($value),
25
            'array' => $this->buildTokenCollection($value),
26
            default => throw ParserException::unsupportedType(gettype($value)),
27
        };
28
    }
29
30
    public function createFromToken(Token $token): string
31
    {
32
        return match ($token) {
33
            Token::AND => TokenAnd::class,
34
            Token::OR => TokenOr::class,
35
            Token::NOT_EQUAL_STRICT => TokenNotEqualStrict::class,
36
            Token::NOT_EQUAL => TokenNotEqual::class,
37
            Token::EQUAL_STRICT => TokenEqualStrict::class,
38
            Token::EQUAL => TokenEqual::class,
39
            Token::IN => TokenIn::class,
40
            Token::NOT_IN => TokenNotIn::class,
41
            Token::BOOL_TRUE => TokenBoolTrue::class,
42
            Token::BOOL_FALSE => TokenBoolFalse::class,
43
            Token::NULL => TokenNull::class,
44
            Token::METHOD => TokenMethod::class,
45
            Token::FUNCTION => TokenFunction::class,
46
            Token::VARIABLE => TokenVariable::class,
47
            Token::FLOAT => TokenFloat::class,
48
            Token::INTEGER => TokenInteger::class,
49 148
            Token::ENCAPSED_STRING => TokenEncapsedString::class,
50
            Token::SMALLER_EQUAL => TokenSmallerEqual::class,
51 148
            Token::GREATER_EQUAL => TokenGreaterEqual::class,
52 148
            Token::SMALLER => TokenSmaller::class,
53 90
            Token::GREATER => TokenGreater::class,
54 94
            Token::OPENING_PARENTHESIS => TokenOpeningParenthesis::class,
55 36
            Token::CLOSING_PARENTHESIS => TokenClosingParenthesis::class,
56 68
            Token::OPENING_ARRAY => TokenOpeningArray::class,
57 8
            Token::CLOSING_ARRAY => TokenClosingArray::class,
58 66
            Token::COMMA => TokenComma::class,
59 4
            Token::REGEX => TokenRegex::class,
60 64
            Token::COMMENT => TokenComment::class,
61 6
            Token::NEWLINE => TokenNewline::class,
62 60
            Token::SPACE => TokenSpace::class,
63 16
            Token::UNKNOWN => TokenUnknown::class,
64 46
        };
65 44
    }
66
67 2
    /** @throws ParserException */
68 2
    private function buildTokenCollection(array $items): TokenArray
69 2
    {
70
        $tokenCollection = new TokenCollection();
71
72
        foreach ($items as $item) {
73
            $tokenCollection->attach($this->createFromPHPType($item));
74 290
        }
75
76 290
        return new TokenArray($tokenCollection);
77 2
    }
78
}
79