• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MyIntervals / PHP-CSS-Parser / 20772587941

07 Jan 2026 06:19AM UTC coverage: 70.316% (+1.1%) from 69.191%
20772587941

Pull #1442

github

web-flow
Merge 44fbf83d1 into 2b61cd568
Pull Request #1442: [FEATURE] Convert legacy color notation to modern css 4 notation

4 of 9 new or added lines in 2 files covered. (44.44%)

65 existing lines in 7 files now uncovered.

1400 of 1991 relevant lines covered (70.32%)

32.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

47.89
/src/Value/Size.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Sabberworm\CSS\Value;
6

7
use Sabberworm\CSS\OutputFormat;
8
use Sabberworm\CSS\Parsing\ParserState;
9
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
10
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
11
use Sabberworm\CSS\ShortClassNameProvider;
12

13
use function Safe\preg_match;
14
use function Safe\preg_replace;
15

16
/**
17
 * A `Size` consists of a numeric `size` value and a unit.
18
 */
19
class Size extends PrimitiveValue
20
{
21
    use ShortClassNameProvider;
22

23
    /**
24
     * vh/vw/vm(ax)/vmin/rem are absolute insofar as they don’t scale to the immediate parent (only the viewport)
25
     */
26
    private const ABSOLUTE_SIZE_UNITS = [
27
        'px',
28
        'pt',
29
        'pc',
30
        'cm',
31
        'mm',
32
        'mozmm',
33
        'in',
34
        'vh',
35
        'dvh',
36
        'svh',
37
        'lvh',
38
        'vw',
39
        'vmin',
40
        'vmax',
41
        'rem',
42
    ];
43

44
    private const RELATIVE_SIZE_UNITS = ['%', 'em', 'ex', 'ch', 'fr'];
45

46
    private const NON_SIZE_UNITS = ['deg', 'grad', 'rad', 's', 'ms', 'turn', 'Hz', 'kHz'];
47

48
    /**
49
     * @var array<int<1, max>, array<lowercase-string, non-empty-string>>|null
50
     */
51
    private static $SIZE_UNITS = null;
52

53
    /**
54
     * @var float
55
     */
56
    private $size;
57

58
    /**
59
     * @var string|null
60
     */
61
    private $unit;
62

63
    /**
64
     * @var bool
65
     */
66
    private $isColorComponent;
67

68
    /**
69
     * @param float|int|string $size
70
     * @param int<1, max>|null $lineNumber
71
     */
72
    public function __construct($size, ?string $unit = null, bool $isColorComponent = false, ?int $lineNumber = null)
33✔
73
    {
74
        parent::__construct($lineNumber);
33✔
75
        $this->size = (float) $size;
33✔
76
        $this->unit = $unit;
33✔
77
        $this->isColorComponent = $isColorComponent;
33✔
78
    }
33✔
79

80
    /**
81
     * @throws UnexpectedEOFException
82
     * @throws UnexpectedTokenException
83
     *
84
     * @internal since V8.8.0
85
     */
86
    public static function parse(ParserState $parserState, bool $isColorComponent = false): Size
28✔
87
    {
88
        $size = '';
28✔
89
        if ($parserState->comes('-')) {
28✔
UNCOV
90
            $size .= $parserState->consume('-');
×
91
        }
92
        while (\is_numeric($parserState->peek()) || $parserState->comes('.') || $parserState->comes('e', true)) {
28✔
93
            if ($parserState->comes('.')) {
28✔
UNCOV
94
                $size .= $parserState->consume('.');
×
95
            } elseif ($parserState->comes('e', true)) {
28✔
96
                $lookahead = $parserState->peek(1, 1);
2✔
97
                if (\is_numeric($lookahead) || $lookahead === '+' || $lookahead === '-') {
2✔
UNCOV
98
                    $size .= $parserState->consume(2);
×
99
                } else {
100
                    break; // Reached the unit part of the number like "em" or "ex"
2✔
101
                }
102
            } else {
103
                $size .= $parserState->consume(1);
28✔
104
            }
105
        }
106

107
        $unit = null;
28✔
108
        $sizeUnits = self::getSizeUnits();
28✔
109
        foreach ($sizeUnits as $length => &$values) {
28✔
110
            $key = \strtolower($parserState->peek($length));
28✔
111
            if (\array_key_exists($key, $values)) {
28✔
112
                if (($unit = $values[$key]) !== null) {
28✔
113
                    $parserState->consume($length);
28✔
114
                    break;
28✔
115
                }
116
            }
117
        }
118
        return new Size((float) $size, $unit, $isColorComponent, $parserState->currentLine());
28✔
119
    }
120

121
    /**
122
     * @return array<int<1, max>, array<lowercase-string, non-empty-string>>
123
     */
124
    private static function getSizeUnits(): array
28✔
125
    {
126
        if (!\is_array(self::$SIZE_UNITS)) {
28✔
127
            self::$SIZE_UNITS = [];
×
128
            $sizeUnits = \array_merge(self::ABSOLUTE_SIZE_UNITS, self::RELATIVE_SIZE_UNITS, self::NON_SIZE_UNITS);
×
129
            foreach ($sizeUnits as $sizeUnit) {
×
UNCOV
130
                $tokenLength = \strlen($sizeUnit);
×
131
                if (!isset(self::$SIZE_UNITS[$tokenLength])) {
×
UNCOV
132
                    self::$SIZE_UNITS[$tokenLength] = [];
×
133
                }
134
                self::$SIZE_UNITS[$tokenLength][\strtolower($sizeUnit)] = $sizeUnit;
×
135
            }
136

UNCOV
137
            \krsort(self::$SIZE_UNITS, SORT_NUMERIC);
×
138
        }
139

140
        return self::$SIZE_UNITS;
28✔
141
    }
142

143
    public function setUnit(string $unit): void
×
144
    {
UNCOV
145
        $this->unit = $unit;
×
UNCOV
146
    }
×
147

148
    public function getUnit(): ?string
28✔
149
    {
150
        return $this->unit;
28✔
151
    }
152

153
    /**
154
     * @param float|int|string $size
155
     */
156
    public function setSize($size): void
×
157
    {
158
        $this->size = (float) $size;
×
UNCOV
159
    }
×
160

UNCOV
161
    public function getSize(): float
×
162
    {
163
        return $this->size;
×
164
    }
165

UNCOV
166
    public function isColorComponent(): bool
×
167
    {
UNCOV
168
        return $this->isColorComponent;
×
169
    }
170

171
    /**
172
     * Returns whether the number stored in this Size really represents a size (as in a length of something on screen).
173
     *
174
     * Returns `false` if the unit is an angle, a duration, a frequency, or the number is a component in a `Color`
175
     * object.
176
     */
177
    public function isSize(): bool
×
178
    {
179
        if (\in_array($this->unit, self::NON_SIZE_UNITS, true)) {
×
UNCOV
180
            return false;
×
181
        }
182
        return !$this->isColorComponent();
×
183
    }
184

185
    public function isRelative(): bool
×
186
    {
187
        if (\in_array($this->unit, self::RELATIVE_SIZE_UNITS, true)) {
×
188
            return true;
×
189
        }
190
        if ($this->unit === null && $this->size !== 0.0) {
×
UNCOV
191
            return true;
×
192
        }
UNCOV
193
        return false;
×
194
    }
195

196
    /**
197
     * @return non-empty-string
198
     */
199
    public function render(OutputFormat $outputFormat): string
×
200
    {
201
        $locale = \localeconv();
×
UNCOV
202
        $decimalPoint = \preg_quote($locale['decimal_point'], '/');
×
203
        $size = preg_match('/[\\d\\.]+e[+-]?\\d+/i', (string) $this->size) === 1
×
UNCOV
204
            ? preg_replace("/$decimalPoint?0+$/", '', \sprintf('%f', $this->size)) : (string) $this->size;
×
205

UNCOV
206
        return preg_replace(["/$decimalPoint/", '/^(-?)0\\./'], ['.', '$1.'], $size) . ($this->unit ?? '');
×
207
    }
208

209
    /**
210
     * @return array<string, bool|int|float|string|array<mixed>|null>
211
     *
212
     * @internal
213
     */
214
    public function getArrayRepresentation(): array
3✔
215
    {
216
        return [
217
            'class' => $this->getShortClassName(),
3✔
218
            // 'number' is the official W3C terminology (not 'size')
219
            'number' => $this->size,
3✔
220
            'unit' => $this->unit,
3✔
221
        ];
222
    }
223
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc