Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
CSSFunction
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
7 / 7
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 __clone
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 newFromName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toTokenArray
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 __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 function, eg. `rgb(255, 0, 0)`
16 */
17class CSSFunction extends ComponentValue {
18
19    /** @var string */
20    protected $name;
21
22    /** @var ComponentValueList */
23    protected $value;
24
25    /**
26     * @param Token $token Function token starting the rule
27     */
28    public function __construct( Token $token ) {
29        if ( $token->type() !== Token::T_FUNCTION ) {
30            throw new InvalidArgumentException(
31                "CSS function must begin with a function token, got {$token->type()}"
32            );
33        }
34
35        [ $this->line, $this->pos ] = $token->getPosition();
36        $this->name = $token->value();
37        $this->value = new ComponentValueList();
38    }
39
40    public function __clone() {
41        $this->value = clone $this->value;
42    }
43
44    /**
45     * Create a function by name
46     * @param string $name
47     * @return CSSFunction
48     */
49    public static function newFromName( $name ) {
50        return new static( new Token( Token::T_FUNCTION, $name ) );
51    }
52
53    /**
54     * Return the function's name
55     * @return string
56     */
57    public function getName() {
58        return $this->name;
59    }
60
61    /**
62     * Return the function's value
63     * @return ComponentValueList
64     */
65    public function getValue() {
66        return $this->value;
67    }
68
69    /**
70     * Return an array of Tokens that correspond to this object.
71     * @return Token[]
72     */
73    public function toTokenArray() {
74        $ret = [];
75
76        $ret[] = new Token(
77            Token::T_FUNCTION,
78            [ 'value' => $this->name, 'position' => [ $this->line, $this->pos ] ]
79        );
80        // Manually looping and appending turns out to be noticeably faster than array_merge.
81        foreach ( $this->value->toTokenArray() as $v ) {
82            $ret[] = $v;
83        }
84        $ret[] = new Token( Token::T_RIGHT_PAREN );
85
86        return $ret;
87    }
88
89    public function __toString() {
90        return Util::stringify( $this );
91    }
92}