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

MyIntervals / PHP-CSS-Parser / 17759736899

16 Sep 2025 08:29AM UTC coverage: 59.658%. Remained the same
17759736899

push

github

web-flow
[BUGFIX] Use safe preg functions in `Size` (#1380)

Also use typesafe comparisons in the affected line.

Part of #1168

0 of 3 new or added lines in 1 file covered. (0.0%)

1118 of 1874 relevant lines covered (59.66%)

25.34 hits per line

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

44.78
/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

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

15
/**
16
 * A `Size` consists of a numeric `size` value and a unit.
17
 */
18
class Size extends PrimitiveValue
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
     * @var list<non-empty-string>
24
     */
25
    private const ABSOLUTE_SIZE_UNITS = [
26
        'px',
27
        'pt',
28
        'pc',
29
        'cm',
30
        'mm',
31
        'mozmm',
32
        'in',
33
        'vh',
34
        'dvh',
35
        'svh',
36
        'lvh',
37
        'vw',
38
        'vmin',
39
        'vmax',
40
        'rem',
41
    ];
42

43
    /**
44
     * @var list<non-empty-string>
45
     */
46
    private const RELATIVE_SIZE_UNITS = ['%', 'em', 'ex', 'ch', 'fr'];
47

48
    /**
49
     * @var list<non-empty-string>
50
     */
51
    private const NON_SIZE_UNITS = ['deg', 'grad', 'rad', 's', 'ms', 'turn', 'Hz', 'kHz'];
52

53
    /**
54
     * @var array<int<1, max>, array<lowercase-string, non-empty-string>>|null
55
     */
56
    private static $SIZE_UNITS = null;
57

58
    /**
59
     * @var float
60
     */
61
    private $size;
62

63
    /**
64
     * @var string|null
65
     */
66
    private $unit;
67

68
    /**
69
     * @var bool
70
     */
71
    private $isColorComponent;
72

73
    /**
74
     * @param float|int|string $size
75
     * @param int<1, max>|null $lineNumber
76
     */
77
    public function __construct($size, ?string $unit = null, bool $isColorComponent = false, ?int $lineNumber = null)
30✔
78
    {
79
        parent::__construct($lineNumber);
30✔
80
        $this->size = (float) $size;
30✔
81
        $this->unit = $unit;
30✔
82
        $this->isColorComponent = $isColorComponent;
30✔
83
    }
30✔
84

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

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

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

142
            \krsort(self::$SIZE_UNITS, SORT_NUMERIC);
×
143
        }
144

145
        return self::$SIZE_UNITS;
28✔
146
    }
147

148
    public function setUnit(string $unit): void
×
149
    {
150
        $this->unit = $unit;
×
151
    }
×
152

153
    public function getUnit(): ?string
28✔
154
    {
155
        return $this->unit;
28✔
156
    }
157

158
    /**
159
     * @param float|int|string $size
160
     */
161
    public function setSize($size): void
×
162
    {
163
        $this->size = (float) $size;
×
164
    }
×
165

166
    public function getSize(): float
×
167
    {
168
        return $this->size;
×
169
    }
170

171
    public function isColorComponent(): bool
×
172
    {
173
        return $this->isColorComponent;
×
174
    }
175

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

190
    public function isRelative(): bool
×
191
    {
192
        if (\in_array($this->unit, self::RELATIVE_SIZE_UNITS, true)) {
×
193
            return true;
×
194
        }
195
        if ($this->unit === null && $this->size !== 0.0) {
×
196
            return true;
×
197
        }
198
        return false;
×
199
    }
200

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

NEW
211
        return preg_replace(["/$decimalPoint/", '/^(-?)0\\./'], ['.', '$1.'], $size) . ($this->unit ?? '');
×
212
    }
213
}
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

© 2025 Coveralls, Inc