Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
QualifiedRule
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
9 / 9
13
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 __clone
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getPrelude
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getBlock
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setBlock
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 toTokenOrCVArray
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 toTokenArray
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toComponentValueArray
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2declare( strict_types = 1 );
3
4/**
5 * @file
6 * @license https://opensource.org/licenses/Apache-2.0 Apache-2.0
7 */
8
9namespace Wikimedia\CSS\Objects;
10
11use InvalidArgumentException;
12use Wikimedia\CSS\Util;
13
14/**
15 * Represent a CSS qualified rule, eg. `div { color: red }`
16 */
17class QualifiedRule extends Rule {
18
19    /** @var ComponentValueList */
20    protected $prelude;
21
22    /** @var SimpleBlock */
23    protected $block;
24
25    /** @inheritDoc */
26    public function __construct( ?Token $token = null ) {
27        parent::__construct( $token ?: new Token( Token::T_EOF ) );
28        $this->prelude = new ComponentValueList();
29        $this->block = SimpleBlock::newFromDelimiter( Token::T_LEFT_BRACE );
30    }
31
32    public function __clone() {
33        $this->prelude = clone $this->prelude;
34        $this->block = clone $this->block;
35    }
36
37    /**
38     * Return the rule's prelude
39     * @return ComponentValueList
40     */
41    public function getPrelude() {
42        return $this->prelude;
43    }
44
45    /**
46     * Return the rule's block
47     * @return SimpleBlock
48     */
49    public function getBlock() {
50        return $this->block;
51    }
52
53    /**
54     * Set the block
55     * @param SimpleBlock|null $block
56     */
57    public function setBlock( ?SimpleBlock $block = null ) {
58        if ( $block->getStartTokenType() !== Token::T_LEFT_BRACE ) {
59            throw new InvalidArgumentException( 'Qualified rule block must be delimited by {}' );
60        }
61        $this->block = $block;
62    }
63
64    /**
65     * @param string $function Function to call, toTokenArray() or toComponentValueArray()
66     * @return Token[]|ComponentValue[]
67     */
68    private function toTokenOrCVArray( $function ) {
69        $ret = [];
70
71        // Manually looping and appending turns out to be noticeably faster than array_merge.
72        foreach ( $this->prelude->$function() as $v ) {
73            $ret[] = $v;
74        }
75        foreach ( $this->block->$function() as $v ) {
76            $ret[] = $v;
77        }
78        return $ret;
79    }
80
81    /** @inheritDoc */
82    public function toTokenArray() {
83        return $this->toTokenOrCVArray( __FUNCTION__ );
84    }
85
86    /** @inheritDoc */
87    public function toComponentValueArray() {
88        return $this->toTokenOrCVArray( __FUNCTION__ );
89    }
90
91    public function __toString() {
92        return Util::stringify( $this );
93    }
94}