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

MyIntervals / PHP-CSS-Parser / 11767738200

10 Nov 2024 07:04PM UTC coverage: 38.583% (-0.04%) from 38.622%
11767738200

Pull #772

github

web-flow
Merge 43e100d73 into b56f7c697
Pull Request #772: [TASK] Clean up the code with Rector

9 of 18 new or added lines in 9 files covered. (50.0%)

1 existing line in 1 file now uncovered.

779 of 2019 relevant lines covered (38.58%)

5.33 hits per line

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

61.04
/src/Value/Color.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
 * `Color's can be input in the form #rrggbb, #rgb or schema(val1, val2, …) but are always stored as an array of
14
 * ('s' => val1, 'c' => val2, 'h' => val3, …) and output in the second form.
15
 */
16
class Color extends CSSFunction
17
{
18
    /**
19
     * @param array<int, Value|string> $aColor
20
     * @param int $iLineNo
21
     */
22
    public function __construct(array $aColor, $iLineNo = 0)
×
23
    {
24
        parent::__construct(\implode('', \array_keys($aColor)), $aColor, ',', $iLineNo);
×
25
    }
×
26

27
    /**
28
     * @throws UnexpectedEOFException
29
     * @throws UnexpectedTokenException
30
     */
31
    public static function parse(ParserState $oParserState, bool $bIgnoreCase = false): CSSFunction
18✔
32
    {
33
        $aColor = [];
18✔
34
        if ($oParserState->comes('#')) {
18✔
35
            $oParserState->consume('#');
14✔
36
            $sValue = $oParserState->parseIdentifier(false);
14✔
37
            if ($oParserState->strlen($sValue) === 3) {
14✔
38
                $sValue = $sValue[0] . $sValue[0] . $sValue[1] . $sValue[1] . $sValue[2] . $sValue[2];
7✔
39
            } elseif ($oParserState->strlen($sValue) === 4) {
9✔
40
                $sValue = $sValue[0] . $sValue[0] . $sValue[1] . $sValue[1] . $sValue[2] . $sValue[2] . $sValue[3]
2✔
41
                    . $sValue[3];
2✔
42
            }
43

44
            if ($oParserState->strlen($sValue) === 8) {
14✔
45
                $aColor = [
46
                    'r' => new Size(\intval($sValue[0] . $sValue[1], 16), null, true, $oParserState->currentLine()),
2✔
47
                    'g' => new Size(\intval($sValue[2] . $sValue[3], 16), null, true, $oParserState->currentLine()),
2✔
48
                    'b' => new Size(\intval($sValue[4] . $sValue[5], 16), null, true, $oParserState->currentLine()),
2✔
49
                    'a' => new Size(
2✔
50
                        \round(self::mapRange(\intval($sValue[6] . $sValue[7], 16), 0, 255, 0, 1), 2),
2✔
51
                        null,
2✔
52
                        true,
2✔
53
                        $oParserState->currentLine()
2✔
54
                    ),
55
                ];
56
            } elseif ($oParserState->strlen($sValue) === 6) {
13✔
57
                $aColor = [
58
                    'r' => new Size(\intval($sValue[0] . $sValue[1], 16), null, true, $oParserState->currentLine()),
11✔
59
                    'g' => new Size(\intval($sValue[2] . $sValue[3], 16), null, true, $oParserState->currentLine()),
11✔
60
                    'b' => new Size(\intval($sValue[4] . $sValue[5], 16), null, true, $oParserState->currentLine()),
11✔
61
                ];
62
            } else {
63
                throw new UnexpectedTokenException(
4✔
64
                    'Invalid hex color value',
4✔
65
                    $sValue,
66
                    'custom',
4✔
67
                    $oParserState->currentLine()
14✔
68
                );
69
            }
70
        } else {
71
            $sColorMode = $oParserState->parseIdentifier(true);
8✔
72
            $oParserState->consumeWhiteSpace();
8✔
73
            $oParserState->consume('(');
8✔
74

75
            $bContainsVar = false;
8✔
76
            $iLength = $oParserState->strlen($sColorMode);
8✔
77
            for ($i = 0; $i < $iLength; ++$i) {
8✔
78
                $oParserState->consumeWhiteSpace();
8✔
79
                if ($oParserState->comes('var')) {
8✔
80
                    $aColor[$sColorMode[$i]] = CSSFunction::parseIdentifierOrFunction($oParserState);
2✔
81
                    $bContainsVar = true;
2✔
82
                } else {
83
                    $aColor[$sColorMode[$i]] = Size::parse($oParserState, true);
8✔
84
                }
85

86
                if ($bContainsVar && $oParserState->comes(')')) {
8✔
87
                    // With a var argument the function can have fewer arguments
88
                    break;
2✔
89
                }
90

91
                $oParserState->consumeWhiteSpace();
8✔
92
                if ($i < ($iLength - 1)) {
8✔
93
                    $oParserState->consume(',');
8✔
94
                }
95
            }
96
            $oParserState->consume(')');
8✔
97

98
            if ($bContainsVar) {
8✔
99
                return new CSSFunction($sColorMode, \array_values($aColor), ',', $oParserState->currentLine());
2✔
100
            }
101
        }
102
        return new Color($aColor, $oParserState->currentLine());
16✔
103
    }
104

105
    /**
106
     * @param float $fVal
107
     * @param float $fFromMin
108
     * @param float $fFromMax
109
     * @param float $fToMin
110
     * @param float $fToMax
111
     *
112
     * @return float
113
     */
NEW
114
    private static function mapRange(float $fVal, float $fFromMin, float $fFromMax, float $fToMin, float $fToMax)
×
115
    {
116
        $fFromRange = $fFromMax - $fFromMin;
×
117
        $fToRange = $fToMax - $fToMin;
×
118
        $fMultiplier = $fToRange / $fFromRange;
×
119
        $fNewVal = $fVal - $fFromMin;
×
120
        $fNewVal *= $fMultiplier;
×
121
        return $fNewVal + $fToMin;
×
122
    }
123

124
    /**
125
     * @return array<int, Value|string>
126
     */
127
    public function getColor()
×
128
    {
129
        return $this->aComponents;
×
130
    }
131

132
    /**
133
     * @param array<int, Value|string> $aColor
134
     */
135
    public function setColor(array $aColor): void
×
136
    {
137
        $this->setName(\implode('', \array_keys($aColor)));
×
138
        $this->aComponents = $aColor;
×
139
    }
×
140

141
    /**
142
     * @return string
143
     */
144
    public function getColorDescription()
×
145
    {
146
        return $this->getName();
×
147
    }
148

149
    public function __toString(): string
×
150
    {
151
        return $this->render(new OutputFormat());
×
152
    }
153

NEW
154
    public function render(OutputFormat $oOutputFormat): string
×
155
    {
156
        // Shorthand RGB color values
157
        if ($oOutputFormat->getRGBHashNotation() && \implode('', \array_keys($this->aComponents)) === 'rgb') {
×
158
            $sResult = \sprintf(
×
159
                '%02x%02x%02x',
×
160
                $this->aComponents['r']->getSize(),
×
161
                $this->aComponents['g']->getSize(),
×
162
                $this->aComponents['b']->getSize()
×
163
            );
164
            return '#' . (($sResult[0] == $sResult[1]) && ($sResult[2] == $sResult[3]) && ($sResult[4] == $sResult[5])
×
165
                    ? "$sResult[0]$sResult[2]$sResult[4]" : $sResult);
×
166
        }
167
        return parent::render($oOutputFormat);
×
168
    }
169
}
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