Add Format enum and typed exception subclasses - #22
Merged
Merged
Conversation
Follow-up to the audit on issue #4. Two BC-relevant cleanups packaged together because deferring them past 2.0 would force users to widen their catch blocks and method signatures later. ## Format enum Same pattern as Driver / Position / FlipDirection. The 'convert' operation and the per-format quality map were the last places magic strings leaked into the public API. $variant->convert(Format::Webp); // typed $variant->convert($config['format'] ?? 'webp'); // string fallback ImageVariant::convert() and the registry's 'convert' factory both accept Format|string. Format::fromName() recognises the obvious aliases (jpg → Jpeg, tif → Tiff, pjpeg → Pjpg, heif → Heic, j2k / jp2k → Jp2) so config-driven setups with non-canonical names keep resolving. ## Typed exception subclasses All three extend ImageProcessingException so callers can settle on one base catch: - ImageCorruptedException — wraps intervention/image's decode failures during process(). Catch this for truncated uploads, wrong-extension files, unsupported codecs. - UnsupportedFormatException — replaces the raw InvalidArgumentException previously thrown when encodeImage() got an empty extension. - DriverUnavailableException — replaces the raw RuntimeException previously thrown by Driver::Auto when neither Imagick nor GD is installed. ## Doc / changelog - readme.md and docs/Available-Operations.md show Format::Webp on convert() - CHANGELOG 2.0 entry calls out Format alongside the other enums and documents the exception-hierarchy shifts in Breaking + Added
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two BC-relevant cleanups from #4 packaged together — both are small enough to share a PR and both close BC windows that would be expensive to retrofit in 2.x.
Format enum
Same pattern as Driver / Position / FlipDirection. The
convertoperation was the last place magic strings leaked into the public API.ImageVariant::convert()and the registry'sconvertfactory both acceptFormat|string.Format::fromName()recognises the obvious aliases (jpg→Jpeg,tif→Tiff,pjpeg→Pjpg,heif→Heic,j2k/jp2k→Jp2) so existing config-driven setups with non-canonical names keep resolving.Typed exception subclasses
Three new classes, all extending
ImageProcessingExceptionso callers can settle on a uniformcatch (ImageProcessingException $e):ImageCorruptedExceptionprocess()UnsupportedFormatExceptionInvalidArgumentExceptionthrown when the encoder has no file extensionDriverUnavailableExceptionRuntimeExceptionthrown byDriver::Autowith no Imagick / GD availableThe decode wrap is the meaningful one —
process()now catches anything intervention raises duringdecodePath()and rethrows asImageCorruptedExceptioncarrying the underlying exception asprevious. Apps catching truncated uploads / wrong-extension files / unsupported codecs no longer need to know intervention's exception class names.Why ship both now
Magic-string
convert(string)and raw exception types are both structural signatures — tightening them later means widening user code's catch blocks and method signatures. 2.0 is the only cheap moment to flip them; deferring would mean either dragging the magic strings into 2.x permanently or shipping another major.Verification
vendor/bin/phpunit— 82 tests, 227 assertions, all passingvendor/bin/phpstan analyze— no errorsvendor/bin/phpcs -s— no errorsCHANGELOG / docs
convert(Format::Webp)in the Quick Exampleconvert(Format|string $format)section refreshed with both call styles