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

MyIntervals / PHP-CSS-Parser / 21410963225

27 Jan 2026 07:24PM UTC coverage: 70.34% (-1.0%) from 71.315%
21410963225

Pull #1484

github

web-flow
Merge 2320afc9e into 96410045c
Pull Request #1484: Remove `thecodingmachine/safe` dependency (2)

25 of 70 new or added lines in 8 files covered. (35.71%)

5 existing lines in 3 files now uncovered.

1449 of 2060 relevant lines covered (70.34%)

30.37 hits per line

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

42.5
/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
/**
14
 * A `Size` consists of a numeric `size` value and a unit.
15
 */
16
class Size extends PrimitiveValue
17
{
18
    use ShortClassNameProvider;
19

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

41
    private const RELATIVE_SIZE_UNITS = ['%', 'em', 'ex', 'ch', 'fr'];
42

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

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

50
    /**
51
     * @var float
52
     */
53
    private $size;
54

55
    /**
56
     * @var string|null
57
     */
58
    private $unit;
59

60
    /**
61
     * @var bool
62
     */
63
    private $isColorComponent;
64

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

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

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

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

134
            \krsort(self::$SIZE_UNITS, SORT_NUMERIC);
×
135
        }
136

137
        return self::$SIZE_UNITS;
28✔
138
    }
139

140
    public function setUnit(string $unit): void
×
141
    {
142
        $this->unit = $unit;
×
143
    }
×
144

145
    public function getUnit(): ?string
28✔
146
    {
147
        return $this->unit;
28✔
148
    }
149

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

158
    public function getSize(): float
×
159
    {
160
        return $this->size;
×
161
    }
162

163
    public function isColorComponent(): bool
×
164
    {
165
        return $this->isColorComponent;
×
166
    }
167

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

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

193
    /**
194
     * @return non-empty-string
195
     */
196
    public function render(OutputFormat $outputFormat): string
×
197
    {
198
        $locale = \localeconv();
×
199
        $decimalPoint = \preg_quote($locale['decimal_point'], '/');
×
200
        /** @phpstan-ignore theCodingMachineSafe.function */
NEW
201
        $matchResult = \preg_match('/[\\d\\.]+e[+-]?\\d+/i', (string) $this->size);
×
NEW
202
        if ($matchResult === false) {
×
NEW
203
            throw new \RuntimeException('Unexpected error');
×
204
        }
NEW
205
        if ($matchResult === 1) {
×
206
            /** @phpstan-ignore theCodingMachineSafe.function */
NEW
207
            $size = \preg_replace("/$decimalPoint?0+$/", '', \sprintf('%f', $this->size));
×
NEW
208
            if ($size === null) {
×
NEW
209
                throw new \RuntimeException('Unexpected error');
×
210
            }
211
        } else {
NEW
212
            $size = (string) $this->size;
×
213
        }
214

215
        /** @phpstan-ignore theCodingMachineSafe.function */
NEW
216
        $result = \preg_replace(["/$decimalPoint/", '/^(-?)0\\./'], ['.', '$1.'], $size);
×
NEW
217
        if ($result === null) {
×
NEW
218
            throw new \RuntimeException('Unexpected error');
×
219
        }
NEW
220
        return $result . ($this->unit ?? '');
×
221
    }
222

223
    /**
224
     * @return array<string, bool|int|float|string|array<mixed>|null>
225
     *
226
     * @internal
227
     */
228
    public function getArrayRepresentation(): array
3✔
229
    {
230
        return [
231
            'class' => $this->getShortClassName(),
3✔
232
            // 'number' is the official W3C terminology (not 'size')
233
            'number' => $this->size,
3✔
234
            'unit' => $this->unit,
3✔
235
        ];
236
    }
237
}
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