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

MyIntervals / PHP-CSS-Parser / 11881080229

17 Nov 2024 06:08PM UTC coverage: 38.583%. Remained the same
11881080229

push

github

web-flow
[TASK] Add another native return type declaration (#774)

As suggested by Rector.

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

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

NEW
105
    private static function mapRange(float $fVal, float $fFromMin, float $fFromMax, float $fToMin, float $fToMax): float
×
106
    {
107
        $fFromRange = $fFromMax - $fFromMin;
×
108
        $fToRange = $fToMax - $fToMin;
×
109
        $fMultiplier = $fToRange / $fFromRange;
×
110
        $fNewVal = $fVal - $fFromMin;
×
111
        $fNewVal *= $fMultiplier;
×
112
        return $fNewVal + $fToMin;
×
113
    }
114

115
    /**
116
     * @return array<int, Value|string>
117
     */
118
    public function getColor()
×
119
    {
120
        return $this->aComponents;
×
121
    }
122

123
    /**
124
     * @param array<int, Value|string> $aColor
125
     */
126
    public function setColor(array $aColor): void
×
127
    {
128
        $this->setName(\implode('', \array_keys($aColor)));
×
129
        $this->aComponents = $aColor;
×
130
    }
×
131

132
    /**
133
     * @return string
134
     */
135
    public function getColorDescription()
×
136
    {
137
        return $this->getName();
×
138
    }
139

140
    public function __toString(): string
×
141
    {
142
        return $this->render(new OutputFormat());
×
143
    }
144

145
    public function render(OutputFormat $oOutputFormat): string
×
146
    {
147
        // Shorthand RGB color values
148
        if ($oOutputFormat->getRGBHashNotation() && \implode('', \array_keys($this->aComponents)) === 'rgb') {
×
149
            $sResult = \sprintf(
×
150
                '%02x%02x%02x',
×
151
                $this->aComponents['r']->getSize(),
×
152
                $this->aComponents['g']->getSize(),
×
153
                $this->aComponents['b']->getSize()
×
154
            );
155
            return '#' . (($sResult[0] == $sResult[1]) && ($sResult[2] == $sResult[3]) && ($sResult[4] == $sResult[5])
×
156
                    ? "$sResult[0]$sResult[2]$sResult[4]" : $sResult);
×
157
        }
158
        return parent::render($oOutputFormat);
×
159
    }
160
}
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