Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
TokenListTokenizer
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
4 / 4
10
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
6
 getParseErrors
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 clearParseErrors
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 consumeToken
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * @file
4 * @license https://opensource.org/licenses/Apache-2.0 Apache-2.0
5 */
6
7namespace Wikimedia\CSS\Parser;
8
9use InvalidArgumentException;
10use Wikimedia\CSS\Objects\Token;
11use Wikimedia\CSS\Objects\TokenList;
12use Wikimedia\CSS\Util;
13
14/**
15 * Tokenizer that just returns a predefined list of tokens
16 */
17class TokenListTokenizer implements Tokenizer {
18
19    /** @var Token[] */
20    protected $tokens;
21
22    /** @var Token */
23    protected $eof;
24
25    /**
26     * @param Token[]|TokenList $tokens Tokens to return
27     * @param Token|null $eof Token to copy as EOF
28     */
29    public function __construct( $tokens, ?Token $eof = null ) {
30        if ( $tokens instanceof TokenList ) {
31            $this->tokens = $tokens->toTokenArray();
32        } elseif ( is_array( $tokens ) ) {
33            Util::assertAllInstanceOf( $tokens, Token::class, '$tokens' );
34            $this->tokens = $tokens;
35        } else {
36            throw new InvalidArgumentException( '$tokens must be a TokenList or an array of tokens' );
37        }
38
39        if ( $eof && $eof->type() === Token::T_EOF ) {
40            $this->eof = $eof;
41        } else {
42            $data = [];
43            if ( $eof ) {
44                $data['position'] = $eof->getPosition();
45            }
46            $this->eof = new Token( Token::T_EOF, $data );
47        }
48    }
49
50    /** @inheritDoc */
51    public function getParseErrors() {
52        return [];
53    }
54
55    /** @inheritDoc */
56    public function clearParseErrors() {
57    }
58
59    /** @inheritDoc */
60    public function consumeToken() {
61        return array_shift( $this->tokens ) ?: $this->eof;
62    }
63
64}