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

MyIntervals / PHP-CSS-Parser / 13700765474

06 Mar 2025 02:18PM UTC coverage: 55.626% (-0.03%) from 55.655%
13700765474

push

github

web-flow
[CLEANUP] Avoid Hungarian notation for `sizeUnit` (#1095)

Part of #756

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

1053 of 1893 relevant lines covered (55.63%)

12.21 hits per line

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

81.16
/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
/**
13
 * A `Size` consists of a numeric `size` value and a unit.
14
 */
15
class Size extends PrimitiveValue
16
{
17
    /**
18
     * vh/vw/vm(ax)/vmin/rem are absolute insofar as they don’t scale to the immediate parent (only the viewport)
19
     *
20
     * @var array<int, string>
21
     */
22
    private const ABSOLUTE_SIZE_UNITS = [
23
        'px',
24
        'pt',
25
        'pc',
26
        'cm',
27
        'mm',
28
        'mozmm',
29
        'in',
30
        'vh',
31
        'dvh',
32
        'svh',
33
        'lvh',
34
        'vw',
35
        'vmin',
36
        'vmax',
37
        'rem',
38
    ];
39

40
    /**
41
     * @var array<int, string>
42
     */
43
    private const RELATIVE_SIZE_UNITS = ['%', 'em', 'ex', 'ch', 'fr'];
44

45
    /**
46
     * @var array<int, string>
47
     */
48
    private const NON_SIZE_UNITS = ['deg', 'grad', 'rad', 's', 'ms', 'turn', 'Hz', 'kHz'];
49

50
    /**
51
     * @var array<int, array<string, string>>|null
52
     */
53
    private static $SIZE_UNITS = null;
54

55
    /**
56
     * @var float
57
     */
58
    private $size;
59

60
    /**
61
     * @var string|null
62
     */
63
    private $unit;
64

65
    /**
66
     * @var bool
67
     */
68
    private $isColorComponent;
69

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

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

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

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

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

146
        return self::$SIZE_UNITS;
61✔
147
    }
148

149
    /**
150
     * @param string $unit
151
     */
152
    public function setUnit($unit): void
×
153
    {
154
        $this->unit = $unit;
×
155
    }
×
156

157
    /**
158
     * @return string|null
159
     */
160
    public function getUnit()
39✔
161
    {
162
        return $this->unit;
39✔
163
    }
164

165
    /**
166
     * @param float|int|string $size
167
     */
168
    public function setSize($size): void
2✔
169
    {
170
        $this->size = (float) $size;
2✔
171
    }
2✔
172

173
    /**
174
     * @return float
175
     */
176
    public function getSize()
9✔
177
    {
178
        return $this->size;
9✔
179
    }
180

181
    /**
182
     * @return bool
183
     */
184
    public function isColorComponent()
2✔
185
    {
186
        return $this->isColorComponent;
2✔
187
    }
188

189
    /**
190
     * Returns whether the number stored in this Size really represents a size (as in a length of something on screen).
191
     *
192
     * Returns `false` if the unit is an angle, a duration, a frequency, or the number is a component in a `Color`
193
     * object.
194
     */
195
    public function isSize(): bool
2✔
196
    {
197
        if (\in_array($this->unit, self::NON_SIZE_UNITS, true)) {
2✔
198
            return false;
1✔
199
        }
200
        return !$this->isColorComponent();
2✔
201
    }
202

203
    public function isRelative(): bool
2✔
204
    {
205
        if (\in_array($this->unit, self::RELATIVE_SIZE_UNITS, true)) {
2✔
206
            return true;
1✔
207
        }
208
        if ($this->unit === null && $this->size != 0) {
2✔
209
            return true;
2✔
210
        }
211
        return false;
2✔
212
    }
213

214
    /**
215
     * @deprecated in V8.8.0, will be removed in V9.0.0. Use `render` instead.
216
     */
217
    public function __toString(): string
×
218
    {
219
        return $this->render(new OutputFormat());
×
220
    }
221

222
    public function render(OutputFormat $outputFormat): string
28✔
223
    {
224
        $locale = \localeconv();
28✔
225
        $decimalPoint = \preg_quote($locale['decimal_point'], '/');
28✔
226
        $size = \preg_match('/[\\d\\.]+e[+-]?\\d+/i', (string) $this->size)
28✔
227
            ? \preg_replace("/$decimalPoint?0+$/", '', \sprintf('%f', $this->size)) : (string) $this->size;
28✔
228

229
        return \preg_replace(["/$decimalPoint/", '/^(-?)0\\./'], ['.', '$1.'], $size) . ($this->unit ?? '');
28✔
230
    }
231
}
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