[2.x] fix: TypeError in Revised event when post content is NULL#4648
Merged
Conversation
CommentPost::revise() forwards $this->content as $oldContent to the Revised event, but the column is nullable while Revised's constructor types $oldContent as non-nullable string. Editing a post that had NULL content (legacy imports, extension-created posts) throws a TypeError on every save and pollutes the log. Coalesce at the call site rather than widening Revised::$oldContent to ?string. Listeners that read the property still get a string — fully backward-compatible for any extension that does string ops on it without a null check. Add a regression test in framework/core/tests/integration/api/posts/UpdateTest.php (file did not exist before — there was no UpdateTest for posts). Fixes #4606.
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.
Summary
Fixes #4606. `CommentPost::revise()` raises a `Flarum\Post\Event\Revised` whose constructor types `$oldContent` as non-nullable `string`. But the `posts.content` column is nullable — legacy/imported posts and extension-created posts can have `NULL` content. Editing such a post throws a `TypeError` on every save and pollutes the log.
Root cause
```php
// CommentPost::revise()
$oldContent = $this->content; // ← can be null
$this->raise(new Revised($this, $actor, $oldContent));
```
```php
// Revised::__construct
public string $oldContent // ← rejects null
```
```
TypeError: Flarum\Post\Event\Revised::__construct(): Argument #3 ($oldContent) must be of type string, null given
```
Fix
Coalesce at the call site:
```php
$oldContent = $this->content ?? '';
```
Chosen over widening `Revised::$oldContent` to `?string` because:
Tests
New `framework/core/tests/integration/api/posts/UpdateTest.php` (the file didn't exist — there was no integration test for post updates):
Both tests fail against pre-fix code with the exact `TypeError` from the bug report; both pass after the fix.