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

MyIntervals / PHP-CSS-Parser / 13700018541

06 Mar 2025 01:39PM UTC coverage: 55.655%. Remained the same
13700018541

push

github

web-flow
[CLEANUP] Avoid Hungarian notation for `tokenLength` (#1089)

Part of #756

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

1053 of 1892 relevant lines covered (55.66%)

12.21 hits per line

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

82.35
/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 = [];
×
134
            foreach (\array_merge(self::ABSOLUTE_SIZE_UNITS, self::RELATIVE_SIZE_UNITS, self::NON_SIZE_UNITS) as $val) {
×
NEW
135
                $tokenLength = \strlen($val);
×
NEW
136
                if (!isset(self::$SIZE_UNITS[$tokenLength])) {
×
NEW
137
                    self::$SIZE_UNITS[$tokenLength] = [];
×
138
                }
NEW
139
                self::$SIZE_UNITS[$tokenLength][\strtolower($val)] = $val;
×
140
            }
141

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

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

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

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

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

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

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

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

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

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

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

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