Skip to content

Add Format enum and typed exception subclasses - #22

Merged
dereuromark merged 1 commit into
masterfrom
feature/exceptions-and-format-enum
Apr 29, 2026
Merged

dereuromark merged 1 commit into
masterfrom
feature/exceptions-and-format-enum

Conversation

@dereuromark

Copy link
Copy Markdown
Contributor

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 convert operation was the last place magic strings leaked into the public API.

use PhpCollective\Infrastructure\Storage\Processor\Image\Format;

$variant->convert(Format::Webp);                  // typed (preferred)
$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 (jpgJpeg, tifTiff, pjpegPjpg, heifHeic, j2k / jp2kJp2) so existing config-driven setups with non-canonical names keep resolving.

Typed exception subclasses

Three new classes, all extending ImageProcessingException so callers can settle on a uniform catch (ImageProcessingException $e):

Class Replaces
ImageCorruptedException Raw intervention/image decode exception out of process()
UnsupportedFormatException Raw InvalidArgumentException thrown when the encoder has no file extension
DriverUnavailableException Raw RuntimeException thrown by Driver::Auto with no Imagick / GD available

The decode wrap is the meaningful one — process() now catches anything intervention raises during decodePath() and rethrows as ImageCorruptedException carrying the underlying exception as previous. 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 passing
  • vendor/bin/phpstan analyze — no errors
  • vendor/bin/phpcs -s — no errors
  • Branch on top of latest master, no conflicts
  • Diff: 4 new files (Format enum + 3 exceptions), 4 new tests, 8 touched files; +350 / -23

CHANGELOG / docs

  • CHANGELOG 2.0 entry: Format added to the enum list, exception subclasses listed under Added, exception-hierarchy shifts called out under Breaking
  • readme.md: convert(Format::Webp) in the Quick Example
  • docs/Available-Operations.md: convert(Format|string $format) section refreshed with both call styles

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
@dereuromark dereuromark added the enhancement New feature or request label Apr 29, 2026
@dereuromark
dereuromark merged commit 50c0284 into master Apr 29, 2026
8 checks passed
@dereuromark
dereuromark deleted the feature/exceptions-and-format-enum branch April 29, 2026 11:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant