Skip to content

[TASK] Add native type declarations for Color #1204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions config/phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,6 @@ parameters:
count: 3
path: ../src/Value/Color.php

-
message: '#^Provide more specific return type "Sabberworm\\CSS\\Value\\Color" over abstract one$#'
identifier: typePerfect.narrowReturnObjectType
count: 1
path: ../src/Value/Color.php

-
message: '#^Loose comparison via "\!\=" is not allowed\.$#'
identifier: notEqual.notAllowed
Expand Down
25 changes: 12 additions & 13 deletions src/Value/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class Color extends CSSFunction
{
/**
* @param array<array-key, Value|string> $colorValues
* @param array<non-empty-string, Value|string> $colorValues
* @param int<0, max> $lineNumber
*/
public function __construct(array $colorValues, int $lineNumber = 0)
Expand All @@ -32,17 +32,16 @@ public function __construct(array $colorValues, int $lineNumber = 0)
*/
public static function parse(ParserState $parserState, bool $ignoreCase = false): CSSFunction
{
return
$parserState->comes('#')
? self::parseHexColor($parserState)
: self::parseColorFunction($parserState);
return $parserState->comes('#')
? self::parseHexColor($parserState)
: self::parseColorFunction($parserState);
}

/**
* @throws UnexpectedEOFException
* @throws UnexpectedTokenException
*/
private static function parseHexColor(ParserState $parserState): CSSFunction
private static function parseHexColor(ParserState $parserState): Color
{
$parserState->consume('#');
$hexValue = $parserState->parseIdentifier(false);
Expand Down Expand Up @@ -183,10 +182,9 @@ private static function parseColorFunction(ParserState $parserState): CSSFunctio
}
$parserState->consume(')');

return
$containsVar
? new CSSFunction($colorMode, \array_values($colorValues), ',', $parserState->currentLine())
: new Color($colorValues, $parserState->currentLine());
return $containsVar
? new CSSFunction($colorMode, \array_values($colorValues), ',', $parserState->currentLine())
: new Color($colorValues, $parserState->currentLine());
}

private static function mapRange(float $value, float $fromMin, float $fromMax, float $toMin, float $toMax): float
Expand All @@ -196,19 +194,20 @@ private static function mapRange(float $value, float $fromMin, float $fromMax, f
$multiplier = $toRange / $fromRange;
$newValue = $value - $fromMin;
$newValue *= $multiplier;

return $newValue + $toMin;
}

/**
* @return array<array-key, Value|string>
* @return array<non-empty-string, Value|string>
*/
public function getColor()
public function getColor(): array
{
return $this->components;
}

/**
* @param array<array-key, Value|string> $colorValues
* @param array<non-empty-string, Value|string> $colorValues
*/
public function setColor(array $colorValues): void
{
Expand Down