Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.71% covered (warning)
85.71%
12 / 14
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Token
85.71% covered (warning)
85.71%
12 / 14
75.00% covered (warning)
75.00%
3 / 4
10.29
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fromList
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 len
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 transformList
81.82% covered (warning)
81.82%
9 / 11
0.00% covered (danger)
0.00%
0 / 1
6.22
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray\Finder\Input;
6
7use Celemas\Quma\Database;
8use Cosray\Exception\ParserException;
9
10readonly class Token
11{
12    public function __construct(
13        public TokenGroup $group,
14        public TokenType $type,
15        public int $position,
16        public string $lexeme,
17        private ?int $length = null,
18    ) {}
19
20    /** @param array<Token> $list */
21    public static function fromList(
22        TokenGroup $group,
23        TokenType $type,
24        int $position,
25        array $list,
26        int $length,
27        Database $db,
28    ): self {
29        return new self($group, $type, $position, self::transformList($list, $db), $length);
30    }
31
32    public function len(): int
33    {
34        return $this->length ?: strlen($this->lexeme);
35    }
36
37    /** @param array<Token> $list */
38    private static function transformList(array $list, Database $db): string
39    {
40        $result = [];
41        $type = null;
42
43        foreach ($list as $item) {
44            if ($type === null) {
45                $type = $item->type;
46            } else {
47                if ($type !== $item->type) {
48                    throw new ParserException('Invalid query: mixed list item types');
49                }
50            }
51
52            if ($type === TokenType::String || $type === TokenType::Number) {
53                $result[] = $db->quote($item->lexeme);
54            } else {
55                throw new ParserException('Invalid query: token type not supported in list');
56            }
57        }
58
59        return '(' . implode(', ', $result) . ')';
60    }
61}