Skip to content

Commit 6393660

Browse files
authored
[TASK] Add native type declarations for Color (#1204)
Also make some types more specific. Also improve code formatting a bit. Part of #811
1 parent 43b8afa commit 6393660

File tree

2 files changed

+12
-19
lines changed

2 files changed

+12
-19
lines changed

config/phpstan-baseline.neon

-6
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,6 @@ parameters:
9090
count: 3
9191
path: ../src/Value/Color.php
9292

93-
-
94-
message: '#^Provide more specific return type "Sabberworm\\CSS\\Value\\Color" over abstract one$#'
95-
identifier: typePerfect.narrowReturnObjectType
96-
count: 1
97-
path: ../src/Value/Color.php
98-
9993
-
10094
message: '#^Loose comparison via "\!\=" is not allowed\.$#'
10195
identifier: notEqual.notAllowed

src/Value/Color.php

+12-13
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
class Color extends CSSFunction
1717
{
1818
/**
19-
* @param array<array-key, Value|string> $colorValues
19+
* @param array<non-empty-string, Value|string> $colorValues
2020
* @param int<0, max> $lineNumber
2121
*/
2222
public function __construct(array $colorValues, int $lineNumber = 0)
@@ -32,17 +32,16 @@ public function __construct(array $colorValues, int $lineNumber = 0)
3232
*/
3333
public static function parse(ParserState $parserState, bool $ignoreCase = false): CSSFunction
3434
{
35-
return
36-
$parserState->comes('#')
37-
? self::parseHexColor($parserState)
38-
: self::parseColorFunction($parserState);
35+
return $parserState->comes('#')
36+
? self::parseHexColor($parserState)
37+
: self::parseColorFunction($parserState);
3938
}
4039

4140
/**
4241
* @throws UnexpectedEOFException
4342
* @throws UnexpectedTokenException
4443
*/
45-
private static function parseHexColor(ParserState $parserState): CSSFunction
44+
private static function parseHexColor(ParserState $parserState): Color
4645
{
4746
$parserState->consume('#');
4847
$hexValue = $parserState->parseIdentifier(false);
@@ -183,10 +182,9 @@ private static function parseColorFunction(ParserState $parserState): CSSFunctio
183182
}
184183
$parserState->consume(')');
185184

186-
return
187-
$containsVar
188-
? new CSSFunction($colorMode, \array_values($colorValues), ',', $parserState->currentLine())
189-
: new Color($colorValues, $parserState->currentLine());
185+
return $containsVar
186+
? new CSSFunction($colorMode, \array_values($colorValues), ',', $parserState->currentLine())
187+
: new Color($colorValues, $parserState->currentLine());
190188
}
191189

192190
private static function mapRange(float $value, float $fromMin, float $fromMax, float $toMin, float $toMax): float
@@ -196,19 +194,20 @@ private static function mapRange(float $value, float $fromMin, float $fromMax, f
196194
$multiplier = $toRange / $fromRange;
197195
$newValue = $value - $fromMin;
198196
$newValue *= $multiplier;
197+
199198
return $newValue + $toMin;
200199
}
201200

202201
/**
203-
* @return array<array-key, Value|string>
202+
* @return array<non-empty-string, Value|string>
204203
*/
205-
public function getColor()
204+
public function getColor(): array
206205
{
207206
return $this->components;
208207
}
209208

210209
/**
211-
* @param array<array-key, Value|string> $colorValues
210+
* @param array<non-empty-string, Value|string> $colorValues
212211
*/
213212
public function setColor(array $colorValues): void
214213
{

0 commit comments

Comments
 (0)