Skip to content

Commit 98c8bb8

Browse files
authored
[CLEANUP] Avoid Hungarian notation in Color class (#802)
Also rename some variables to be more descriptive and/or avoid abbreviations. Co-authored-by: Jake Hotson <jake.github@qzdesign.co.uk>
1 parent 9c70d85 commit 98c8bb8

File tree

1 file changed

+83
-83
lines changed

1 file changed

+83
-83
lines changed

src/Value/Color.php

Lines changed: 83 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -16,86 +16,86 @@
1616
class Color extends CSSFunction
1717
{
1818
/**
19-
* @param array<int, Value|string> $aColor
20-
* @param int $iLineNo
19+
* @param array<int, Value|string> $colorValues
20+
* @param int $lineNumber
2121
*/
22-
public function __construct(array $aColor, $iLineNo = 0)
22+
public function __construct(array $colorValues, $lineNumber = 0)
2323
{
24-
parent::__construct(\implode('', \array_keys($aColor)), $aColor, ',', $iLineNo);
24+
parent::__construct(\implode('', \array_keys($colorValues)), $colorValues, ',', $lineNumber);
2525
}
2626

2727
/**
2828
* @throws UnexpectedEOFException
2929
* @throws UnexpectedTokenException
3030
*/
31-
public static function parse(ParserState $oParserState, bool $bIgnoreCase = false): CSSFunction
31+
public static function parse(ParserState $parserState, bool $ignoreCase = false): CSSFunction
3232
{
3333
return
34-
$oParserState->comes('#')
35-
? self::parseHexColor($oParserState)
36-
: self::parseColorFunction($oParserState);
34+
$parserState->comes('#')
35+
? self::parseHexColor($parserState)
36+
: self::parseColorFunction($parserState);
3737
}
3838

3939
/**
4040
* @throws UnexpectedEOFException
4141
* @throws UnexpectedTokenException
4242
*/
43-
private static function parseHexColor(ParserState $oParserState): CSSFunction
43+
private static function parseHexColor(ParserState $parserState): CSSFunction
4444
{
45-
$oParserState->consume('#');
46-
$sValue = $oParserState->parseIdentifier(false);
47-
if ($oParserState->strlen($sValue) === 3) {
48-
$sValue = $sValue[0] . $sValue[0] . $sValue[1] . $sValue[1] . $sValue[2] . $sValue[2];
49-
} elseif ($oParserState->strlen($sValue) === 4) {
50-
$sValue = $sValue[0] . $sValue[0] . $sValue[1] . $sValue[1] . $sValue[2] . $sValue[2] . $sValue[3]
51-
. $sValue[3];
45+
$parserState->consume('#');
46+
$hexValue = $parserState->parseIdentifier(false);
47+
if ($parserState->strlen($hexValue) === 3) {
48+
$hexValue = $hexValue[0] . $hexValue[0] . $hexValue[1] . $hexValue[1] . $hexValue[2] . $hexValue[2];
49+
} elseif ($parserState->strlen($hexValue) === 4) {
50+
$hexValue = $hexValue[0] . $hexValue[0] . $hexValue[1] . $hexValue[1] . $hexValue[2] . $hexValue[2]
51+
. $hexValue[3] . $hexValue[3];
5252
}
5353

54-
if ($oParserState->strlen($sValue) === 8) {
55-
$aColor = [
56-
'r' => new Size(\intval($sValue[0] . $sValue[1], 16), null, true, $oParserState->currentLine()),
57-
'g' => new Size(\intval($sValue[2] . $sValue[3], 16), null, true, $oParserState->currentLine()),
58-
'b' => new Size(\intval($sValue[4] . $sValue[5], 16), null, true, $oParserState->currentLine()),
54+
if ($parserState->strlen($hexValue) === 8) {
55+
$colorValues = [
56+
'r' => new Size(\intval($hexValue[0] . $hexValue[1], 16), null, true, $parserState->currentLine()),
57+
'g' => new Size(\intval($hexValue[2] . $hexValue[3], 16), null, true, $parserState->currentLine()),
58+
'b' => new Size(\intval($hexValue[4] . $hexValue[5], 16), null, true, $parserState->currentLine()),
5959
'a' => new Size(
60-
\round(self::mapRange(\intval($sValue[6] . $sValue[7], 16), 0, 255, 0, 1), 2),
60+
\round(self::mapRange(\intval($hexValue[6] . $hexValue[7], 16), 0, 255, 0, 1), 2),
6161
null,
6262
true,
63-
$oParserState->currentLine()
63+
$parserState->currentLine()
6464
),
6565
];
66-
} elseif ($oParserState->strlen($sValue) === 6) {
67-
$aColor = [
68-
'r' => new Size(\intval($sValue[0] . $sValue[1], 16), null, true, $oParserState->currentLine()),
69-
'g' => new Size(\intval($sValue[2] . $sValue[3], 16), null, true, $oParserState->currentLine()),
70-
'b' => new Size(\intval($sValue[4] . $sValue[5], 16), null, true, $oParserState->currentLine()),
66+
} elseif ($parserState->strlen($hexValue) === 6) {
67+
$colorValues = [
68+
'r' => new Size(\intval($hexValue[0] . $hexValue[1], 16), null, true, $parserState->currentLine()),
69+
'g' => new Size(\intval($hexValue[2] . $hexValue[3], 16), null, true, $parserState->currentLine()),
70+
'b' => new Size(\intval($hexValue[4] . $hexValue[5], 16), null, true, $parserState->currentLine()),
7171
];
7272
} else {
7373
throw new UnexpectedTokenException(
7474
'Invalid hex color value',
75-
$sValue,
75+
$hexValue,
7676
'custom',
77-
$oParserState->currentLine()
77+
$parserState->currentLine()
7878
);
7979
}
8080

81-
return new Color($aColor, $oParserState->currentLine());
81+
return new Color($colorValues, $parserState->currentLine());
8282
}
8383

8484
/**
8585
* @throws UnexpectedEOFException
8686
* @throws UnexpectedTokenException
8787
*/
88-
private static function parseColorFunction(ParserState $oParserState): CSSFunction
88+
private static function parseColorFunction(ParserState $parserState): CSSFunction
8989
{
90-
$aColor = [];
90+
$colorValues = [];
9191

92-
$sColorMode = $oParserState->parseIdentifier(true);
93-
$oParserState->consumeWhiteSpace();
94-
$oParserState->consume('(');
92+
$colorMode = $parserState->parseIdentifier(true);
93+
$parserState->consumeWhiteSpace();
94+
$parserState->consume('(');
9595

9696
// CSS Color Module Level 4 says that `rgb` and `rgba` are now aliases; likewise `hsl` and `hsla`.
9797
// So, attempt to parse with the `a`, and allow for it not being there.
98-
switch ($sColorMode) {
98+
switch ($colorMode) {
9999
case 'rgb':
100100
$colorModeForParsing = 'rgba';
101101
$mayHaveOptionalAlpha = true;
@@ -107,89 +107,89 @@ private static function parseColorFunction(ParserState $oParserState): CSSFuncti
107107
case 'rgba':
108108
// This is handled identically to the following case.
109109
case 'hsla':
110-
$colorModeForParsing = $sColorMode;
110+
$colorModeForParsing = $colorMode;
111111
$mayHaveOptionalAlpha = true;
112112
break;
113113
default:
114-
$colorModeForParsing = $sColorMode;
114+
$colorModeForParsing = $colorMode;
115115
$mayHaveOptionalAlpha = false;
116116
}
117117

118-
$bContainsVar = false;
118+
$containsVar = false;
119119
$isLegacySyntax = false;
120-
$iLength = $oParserState->strlen($colorModeForParsing);
121-
for ($i = 0; $i < $iLength; ++$i) {
122-
$oParserState->consumeWhiteSpace();
123-
if ($oParserState->comes('var')) {
124-
$aColor[$colorModeForParsing[$i]] = CSSFunction::parseIdentifierOrFunction($oParserState);
125-
$bContainsVar = true;
120+
$expectedArgumentCount = $parserState->strlen($colorModeForParsing);
121+
for ($argumentIndex = 0; $argumentIndex < $expectedArgumentCount; ++$argumentIndex) {
122+
$parserState->consumeWhiteSpace();
123+
if ($parserState->comes('var')) {
124+
$colorValues[$colorModeForParsing[$argumentIndex]] = CSSFunction::parseIdentifierOrFunction($parserState);
125+
$containsVar = true;
126126
} else {
127-
$aColor[$colorModeForParsing[$i]] = Size::parse($oParserState, true);
127+
$colorValues[$colorModeForParsing[$argumentIndex]] = Size::parse($parserState, true);
128128
}
129129

130130
// This must be done first, to consume comments as well, so that the `comes` test will work.
131-
$oParserState->consumeWhiteSpace();
131+
$parserState->consumeWhiteSpace();
132132

133133
// With a `var` argument, the function can have fewer arguments.
134134
// And as of CSS Color Module Level 4, the alpha argument is optional.
135135
$canCloseNow =
136-
$bContainsVar ||
137-
($mayHaveOptionalAlpha && $i >= $iLength - 2);
138-
if ($canCloseNow && $oParserState->comes(')')) {
136+
$containsVar ||
137+
($mayHaveOptionalAlpha && $argumentIndex >= $expectedArgumentCount - 2);
138+
if ($canCloseNow && $parserState->comes(')')) {
139139
break;
140140
}
141141

142142
// "Legacy" syntax is comma-delimited.
143143
// "Modern" syntax is space-delimited, with `/` as alpha delimiter.
144144
// They cannot be mixed.
145-
if ($i === 0) {
145+
if ($argumentIndex === 0) {
146146
// An immediate closing parenthesis is not valid.
147-
if ($oParserState->comes(')')) {
147+
if ($parserState->comes(')')) {
148148
throw new UnexpectedTokenException(
149149
'Color function with no arguments',
150150
'',
151151
'custom',
152-
$oParserState->currentLine()
152+
$parserState->currentLine()
153153
);
154154
}
155-
$isLegacySyntax = $oParserState->comes(',');
155+
$isLegacySyntax = $parserState->comes(',');
156156
}
157157

158-
if ($isLegacySyntax && $i < ($iLength - 1)) {
159-
$oParserState->consume(',');
158+
if ($isLegacySyntax && $argumentIndex < ($expectedArgumentCount - 1)) {
159+
$parserState->consume(',');
160160
}
161161

162162
// In the "modern" syntax, the alpha value must be delimited with `/`.
163163
if (!$isLegacySyntax) {
164-
if ($bContainsVar) {
164+
if ($containsVar) {
165165
// If the `var` substitution encompasses more than one argument,
166166
// the alpha deliminator may come at any time.
167-
if ($oParserState->comes('/')) {
168-
$oParserState->consume('/');
167+
if ($parserState->comes('/')) {
168+
$parserState->consume('/');
169169
}
170-
} elseif (($colorModeForParsing[$i + 1] ?? '') === 'a') {
170+
} elseif (($colorModeForParsing[$argumentIndex + 1] ?? '') === 'a') {
171171
// Alpha value is the next expected argument.
172172
// Since a closing parenthesis was not found, a `/` separator is now required.
173-
$oParserState->consume('/');
173+
$parserState->consume('/');
174174
}
175175
}
176176
}
177-
$oParserState->consume(')');
177+
$parserState->consume(')');
178178

179179
return
180-
$bContainsVar
181-
? new CSSFunction($sColorMode, \array_values($aColor), ',', $oParserState->currentLine())
182-
: new Color($aColor, $oParserState->currentLine());
180+
$containsVar
181+
? new CSSFunction($colorMode, \array_values($colorValues), ',', $parserState->currentLine())
182+
: new Color($colorValues, $parserState->currentLine());
183183
}
184184

185-
private static function mapRange(float $fVal, float $fFromMin, float $fFromMax, float $fToMin, float $fToMax): float
185+
private static function mapRange(float $value, float $fromMin, float $fromMax, float $toMin, float $toMax): float
186186
{
187-
$fFromRange = $fFromMax - $fFromMin;
188-
$fToRange = $fToMax - $fToMin;
189-
$fMultiplier = $fToRange / $fFromRange;
190-
$fNewVal = $fVal - $fFromMin;
191-
$fNewVal *= $fMultiplier;
192-
return $fNewVal + $fToMin;
187+
$fromRange = $fromMax - $fromMin;
188+
$toRange = $toMax - $toMin;
189+
$multiplier = $toRange / $fromRange;
190+
$newValue = $value - $fromMin;
191+
$newValue *= $multiplier;
192+
return $newValue + $toMin;
193193
}
194194

195195
/**
@@ -201,12 +201,12 @@ public function getColor()
201201
}
202202

203203
/**
204-
* @param array<int, Value|string> $aColor
204+
* @param array<int, Value|string> $colorValues
205205
*/
206-
public function setColor(array $aColor): void
206+
public function setColor(array $colorValues): void
207207
{
208-
$this->setName(\implode('', \array_keys($aColor)));
209-
$this->aComponents = $aColor;
208+
$this->setName(\implode('', \array_keys($colorValues)));
209+
$this->aComponents = $colorValues;
210210
}
211211

212212
/**
@@ -222,19 +222,19 @@ public function __toString(): string
222222
return $this->render(new OutputFormat());
223223
}
224224

225-
public function render(OutputFormat $oOutputFormat): string
225+
public function render(OutputFormat $outputFormat): string
226226
{
227227
// Shorthand RGB color values
228-
if ($oOutputFormat->getRGBHashNotation() && \implode('', \array_keys($this->aComponents)) === 'rgb') {
229-
$sResult = \sprintf(
228+
if ($outputFormat->getRGBHashNotation() && \implode('', \array_keys($this->aComponents)) === 'rgb') {
229+
$result = \sprintf(
230230
'%02x%02x%02x',
231231
$this->aComponents['r']->getSize(),
232232
$this->aComponents['g']->getSize(),
233233
$this->aComponents['b']->getSize()
234234
);
235-
return '#' . (($sResult[0] == $sResult[1]) && ($sResult[2] == $sResult[3]) && ($sResult[4] == $sResult[5])
236-
? "$sResult[0]$sResult[2]$sResult[4]" : $sResult);
235+
return '#' . (($result[0] == $result[1]) && ($result[2] == $result[3]) && ($result[4] == $result[5])
236+
? "$result[0]$result[2]$result[4]" : $result);
237237
}
238-
return parent::render($oOutputFormat);
238+
return parent::render($outputFormat);
239239
}
240240
}

0 commit comments

Comments
 (0)