Add Size::dimensions() for destructuring (fixes #1480)#1485
Conversation
|
Thank you for the contribution and for taking the time to work on this. Unfortunately, I can't merge the PR in its current form because the interface change introduce a breaking change. I'd also prefer the method to be named That said, there is also a larger issue with My actual goal is that one shouldn't have to call a method, but be able to destructure the object directly. Thanks again. |
|
Thanks for the detailed feedback — that helps a lot. Reading your three points, the architectural root seems to be that Would you accept a refactor where I'm happy to open a new PR with that scope if you'd consider merging it. Otherwise I'll close this out and wait for a future major release. |
|
I think decoupling Size from Polygon would be the best way to solve this. I have already tried that. However, I invested rather little time and noticed that it is more complex than can be done just like that. I believe one issue actually is this area: https://github.com/Intervention/image/blob/develop/src/Drivers/Gd/Modifiers/RotateModifier.php#L88-L94 $cutout = (new Size(
imagesx($frame->native()),
imagesy($frame->native()),
$container->pivot()
))->alignHorizontally(Alignment::CENTER)
->alignVertically(Alignment::CENTER)
->rotate($this->rotationAngle());A rectangular area is created from Size, but then methods are used that belong to Polygon. The blocker for simple decoupling is that size musts implement I think it makes sense to clean this up in the process. Maybe something like this would work: $cutout = Polygon::fromSize(new Size(
imagesx($frame->native()),
imagesy($frame->native()),
$container->pivot()
))->alignHorizontally(Alignment::CENTER)
->alignVertically(Alignment::CENTER)
->rotate($this->rotationAngle());This way the decoupled Size must not implement rotate() can be consumed by I'm not sure atm whether that's the only part that's problematic. I also noticed that Size is linked with Intervention\Image\Geometry\Rectangle. Unfortunately this whole arrangement is rather a design flaw and not ideal. I have a feeling this can not be cleaned up without breaking changes. |
|
Appreciate the detailed pointer — the
I'll put together a draft PR with the decoupled I'll open the new PR (rather than reopen this one) so the conversation stays focused. No timeline promises, but I'll keep it scoped. |
|
I had some time and sketched something out: #1487 |
|
Beautiful — you built exactly what we were converging on, including the keyed-destructuring ( |
Summary
Closes #1480.
Adds
Size::dimensions(): array{0: int, 1: int}so consumers can destructuresize into a
[width, height]int pair:Why not change
[$w, $h] = $image->size()directly?The literal syntax in the original issue would require changing
ArrayAccess::offsetGetonSize. That offset is currently the wayPolygon(Size's parent) exposes the four cornerPointobjects, andSize::setWidth()/setHeight()rely on it internally(
\$this[1]->setX(...), etc.). Repurposing those offsets to return intswould be a breaking change for every caller and for the class's own setters.
dimensions()is the non-breaking alternative: same ergonomics fordestructuring, zero risk for existing code.
Tests
Added to
tests/Unit/SizeTest.php:testDimensionsReturnsWidthAndHeightAsIntstestDimensionsEnablesArrayDestructuringtestDimensionsReflectsSetWidthAndSetHeighttestArrayAccessStillReturnsCornerPoints(explicit BC guard)vendor/bin/phpunit tests/Unit/SizeTest.phppasses locally (67 tests, 152 assertions).Notes
SizeInterfaceupdated to include the new method.PolygonorArrayAccesssemantics.