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

MyIntervals / PHP-CSS-Parser / 13562750149

27 Feb 2025 09:09AM UTC coverage: 54.784%. Remained the same
13562750149

push

github

web-flow
[TASK] Deprecate `__toString()` (#1006)

Part of #998

1042 of 1902 relevant lines covered (54.78%)

12.12 hits per line

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

82.61
/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 $fSize;
59

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

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

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

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

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

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

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

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

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

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

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

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

180
    /**
181
     * @return bool
182
     */
183
    public function isColorComponent()
2✔
184
    {
185
        return $this->bIsColorComponent;
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->sUnit, 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->sUnit, self::RELATIVE_SIZE_UNITS, true)) {
2✔
205
            return true;
1✔
206
        }
207
        if ($this->sUnit === null && $this->fSize != 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
29✔
222
    {
223
        $l = \localeconv();
29✔
224
        $sPoint = \preg_quote($l['decimal_point'], '/');
29✔
225
        $sSize = \preg_match('/[\\d\\.]+e[+-]?\\d+/i', (string) $this->fSize)
29✔
226
            ? \preg_replace("/$sPoint?0+$/", '', \sprintf('%f', $this->fSize)) : (string) $this->fSize;
29✔
227
        return \preg_replace(["/$sPoint/", '/^(-?)0\\./'], ['.', '$1.'], $sSize)
29✔
228
            . ($this->sUnit ?? '');
29✔
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