fix: align Media docblock with the nullable migration columns - #722
Merged
Conversation
`directory`, `width`, `height` and `size` are created `nullable()` by stubs/migration.stub, but the model's docblock declared them non-nullable. Static analysis in consuming apps then flagged correct null-handling as a defect — a test asserting `$media->width` is null for a sanitized SVG was reported as an impossible expectation. Also corrects `url`, which resolves through `Media::resolveUrl()` and is already declared `?string` there. Chasing the annotations turned up two views that really do hand a null `size` straight to `sizeForHumans(int $size)`, fatalling the page: the media edit page's details panel and the picker's list display. Both are now guarded, with regression tests covering each. Fixes #721 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
Fixes #721.
The reported bug
Media's docblock declared four properties non-nullable thatstubs/migration.stubcreates asnullable():directory$table->string('directory')->nullable()width$table->unsignedInteger('width')->nullable()height$table->unsignedInteger('height')->nullable()size$table->unsignedInteger('size')->nullable()Larastan believes the annotation, so correct null-handling in a consuming app gets reported as a defect — the reporter's
expect($media->width)->toBeNull()for a sanitized SVG came back aspest.expectation.impossible.All four now carry
|null. I also corrected@property-read string $url→string|null, since it resolves throughMedia::resolveUrl(), which is already declared?string.Per the issue's suggestion I checked the rest of the block against the stub. Everything else lines up:
alt,title,description,caption,exifandcurationsalready have|null;disk,name,path,typeandextare non-nullable columns andvisibilityhas adefault('public');pretty_nameis a nullable column but is in$appendswith an accessor that always returns a string; the tenancy FK has a templated name, so there's nothing to annotate.Two real crashes the annotation was hiding
Chasing the
sizecall sites turned up two views that hand a null straight intosizeForHumans(int $size)and fatal:resources/views/components/forms/details.blade.php— the details panel on the media edit page. The existingfilled($record)guard checks the record, not the size.resources/views/components/forms/picker.blade.php— the picker's list display.Both are guarded now, with a regression test per site in
tests/src/Feature/NullableMediaColumnsTest.php. Both tests fail on5.xwithArgument #1 ($size) must be of type int, null givenand pass here.The picker test needs the list markup, so
PostResourcegains an opt-inlistDisplay()driven by a config flag — the other tests that render that fixture keep the default grid display.Checked and deliberately left alone
MediaTable's size column looks like the same bug but isn't reachable:TextColumnshort-circuits to the placeholder branch on blank state and never callsformatStateUsing. Verified by running a null-size record through the list page unpatched.info-overlay.blade.phpis also safe, for a different reason: Blade's@propsdefault ('size' => 0) replaces an explicitly passed null, so it renders0 Brather than throwing. Cosmetically wrong for an unknown size, but not a crash, and changing it would alter the grid's rendering — happy to do it separately if you want.Notes
3.xhas a wider version of the same problem: its docblock also declaresalt,title,description,caption,exifandcurationsnon-nullable. Separate backport if you want it.composer testis green: 201 passed, PHPStan clean, no Pint or Rector drift.🤖 Generated with Claude Code