Skip to content
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ Please also have a look at our

### Changed

- `Size::isSize()` is renamed to `isLength()` (#1645)

### Deprecated

- `Size::isSize()` is deprecated; use `isLength()` instead; it is a direct
replacement (#1645)

### Removed

### Fixed
Expand Down
16 changes: 12 additions & 4 deletions src/Value/Size.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,19 +216,27 @@ public function isColorComponent(): bool
}

/**
* Returns whether the number stored in this Size really represents a size (as in a length of something on screen).
*
* Returns `false` if the unit is an angle, a duration, a frequency, a resolution, or the number is a component in
* @return bool
* `true` if the stored number represents a length, e.g. of something on screen;
* `false` if the unit is an angle, a duration, a frequency, a resolution, or the number is a component in
* a `Color` object.
*/
public function isSize(): bool
public function isLength(): bool
{
if (\in_array($this->unit, self::NON_SIZE_UNITS, true)) {
return false;
}
return !$this->isColorComponent();
}

/**
* @deprecated in version 9.6, will be removed in v10.0. Use `isLength()` instead, which is a direct replacement.
*/
public function isSize(): bool
{
return $this->isLength();
}

public function isRelative(): bool
{
if (\in_array($this->unit, self::RELATIVE_SIZE_UNITS, true)) {
Expand Down
4 changes: 2 additions & 2 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ public function slashedValues(): void
$document->render()
);
foreach ($document->getAllValues(null) as $value) {
if ($value instanceof Size && $value->isSize() && !$value->isRelative()) {
if ($value instanceof Size && $value->isLength() && !$value->isRelative()) {
$value->setSize($value->getSize() * 3);
}
}
Expand Down Expand Up @@ -412,7 +412,7 @@ public function functionSyntax(): void
self::assertSame($expected, $document->render());

foreach ($document->getAllValues(null, null, true) as $value) {
if ($value instanceof Size && $value->isSize()) {
if ($value instanceof Size && $value->isLength()) {
$value->setSize($value->getSize() * 3);
}
}
Expand Down
76 changes: 61 additions & 15 deletions tests/Unit/Value/SizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ public function isValue(): void
}

/**
* @return array<string, array{0: non-empty-string}>
* @return array<non-empty-string, array{0: non-empty-string}>
*/
public static function provideUnit(): array
public static function provideLengthUnit(): array
{
$units = [
return self::mapUnitListForDataProvider([
'px',
'pt',
'pc',
Expand Down Expand Up @@ -95,6 +95,15 @@ public static function provideUnit(): array
'cqb',
'cqmin',
'cqmax',
]);
}

/**
* @return array<non-empty-string, array{0: non-empty-string}>
*/
public static function provideNonLengthUnit(): array
{
return self::mapUnitListForDataProvider([
'deg',
'grad',
'rad',
Expand All @@ -107,25 +116,16 @@ public static function provideUnit(): array
'dpcm',
'dppx',
'x',
];

return \array_combine(
$units,
\array_map(
static function (string $unit): array {
return [$unit];
},
$units
)
);
]);
}

/**
* @test
*
* @param non-empty-string $unit
*
* @dataProvider provideUnit
* @dataProvider provideLengthUnit
* @dataProvider provideNonLengthUnit
*/
public function parsesUnit(string $unit): void
{
Expand All @@ -134,6 +134,34 @@ public function parsesUnit(string $unit): void
self::assertSame($unit, $parsedSize->getUnit());
}

/**
* @test
*
* @param non-empty-string $unit
*
* @dataProvider provideLengthUnit
*/
public function isLengthReturnsTrueForLengthUnit(string $unit): void
{
$parsedSize = Size::parse(new ParserState('1' . $unit, Settings::create()));

self::assertTrue($parsedSize->isLength());
}

/**
* @test
*
* @param non-empty-string $unit
*
* @dataProvider provideNonLengthUnit
*/
public function isLengthReturnsFalseForNonLengthUnit(string $unit): void
{
$parsedSize = Size::parse(new ParserState('1' . $unit, Settings::create()));

self::assertFalse($parsedSize->isLength());
}

/**
* @test
*/
Expand Down Expand Up @@ -169,4 +197,22 @@ public function getArrayRepresentationIncludesUnit(): void

self::assertSame('px', $result['unit']);
}

/**
* @param array<non-empty-string> $units
*
* @return array<non-empty-string, array{0: non-empty-string}>
*/
private static function mapUnitListForDataProvider(array $units): array
{
return \array_combine(
$units,
\array_map(
static function (string $unit): array {
return [$unit];
},
$units
)
);
}
}