Skip to content

[TASK] Add native type declarations in Size #1162

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 13, 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ Please also have a look at our
- Make all non-private properties `@internal` (#886)
- Use more native type declarations and strict mode
(#641, #772, #774, #778, #804, #841, #873, #875, #891, #922, #923, #933, #958,
#964, #967, #1000, #1044, #1134, #1136, #1137, #1139, #1140, #1141, #1145)
#964, #967, #1000, #1044, #1134, #1136, #1137, #1139, #1140, #1141, #1145,
#1162)
- Add visibility to all class/interface constants (#469)

### Deprecated
Expand Down
40 changes: 12 additions & 28 deletions src/Value/Size.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Size extends PrimitiveValue
/**
* vh/vw/vm(ax)/vmin/rem are absolute insofar as they don’t scale to the immediate parent (only the viewport)
*
* @var array<int, string>
* @var list<non-empty-string>
*/
private const ABSOLUTE_SIZE_UNITS = [
'px',
Expand All @@ -38,17 +38,17 @@ class Size extends PrimitiveValue
];

/**
* @var array<int, string>
* @var list<non-empty-string>
*/
private const RELATIVE_SIZE_UNITS = ['%', 'em', 'ex', 'ch', 'fr'];

/**
* @var array<int, string>
* @var list<non-empty-string>
*/
private const NON_SIZE_UNITS = ['deg', 'grad', 'rad', 's', 'ms', 'turn', 'Hz', 'kHz'];

/**
* @var array<int, array<string, string>>|null
* @var array<int<1, max>, array<lowercase-string, non-empty-string>>|null
*/
private static $SIZE_UNITS = null;

Expand All @@ -69,11 +69,9 @@ class Size extends PrimitiveValue

/**
* @param float|int|string $size
* @param string|null $unit
* @param bool $isColorComponent
* @param int<0, max> $lineNumber
*/
public function __construct($size, $unit = null, $isColorComponent = false, int $lineNumber = 0)
public function __construct($size, ?string $unit = null, bool $isColorComponent = false, int $lineNumber = 0)
{
parent::__construct($lineNumber);
$this->size = (float) $size;
Expand All @@ -82,14 +80,12 @@ public function __construct($size, $unit = null, $isColorComponent = false, int
}

/**
* @param bool $isColorComponent
*
* @throws UnexpectedEOFException
* @throws UnexpectedTokenException
*
* @internal since V8.8.0
*/
public static function parse(ParserState $parserState, $isColorComponent = false): Size
public static function parse(ParserState $parserState, bool $isColorComponent = false): Size
{
$size = '';
if ($parserState->comes('-')) {
Expand Down Expand Up @@ -125,9 +121,9 @@ public static function parse(ParserState $parserState, $isColorComponent = false
}

/**
* @return array<int, array<string, string>>
* @return array<int<1, max>, array<lowercase-string, non-empty-string>>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice and precise.

*/
private static function getSizeUnits()
private static function getSizeUnits(): array
{
if (!\is_array(self::$SIZE_UNITS)) {
self::$SIZE_UNITS = [];
Expand All @@ -146,18 +142,12 @@ private static function getSizeUnits()
return self::$SIZE_UNITS;
}

/**
* @param string $unit
*/
public function setUnit($unit): void
public function setUnit(string $unit): void
{
$this->unit = $unit;
}

/**
* @return string|null
*/
public function getUnit()
public function getUnit(): ?string
{
return $this->unit;
}
Expand All @@ -170,18 +160,12 @@ public function setSize($size): void
$this->size = (float) $size;
}

/**
* @return float
*/
public function getSize()
public function getSize(): float
{
return $this->size;
}

/**
* @return bool
*/
public function isColorComponent()
public function isColorComponent(): bool
{
return $this->isColorComponent;
}
Expand Down