Skip to content

Docs: Add missing @return tag to WP_Image_Editor_Imagick::remove_pdf_alpha_channel() - #13200

Closed
csemazharul wants to merge 4 commits into
WordPress:trunkfrom
csemazharul:fix/imagick-pdf-alpha-docblock
Closed

Docs: Add missing @return tag to WP_Image_Editor_Imagick::remove_pdf_alpha_channel()#13200
csemazharul wants to merge 4 commits into
WordPress:trunkfrom
csemazharul:fix/imagick-pdf-alpha-docblock

Conversation

@csemazharul

@csemazharul csemazharul commented Aug 20, 2026

Copy link
Copy Markdown

Trac ticket: https://core.trac.wordpress.org/ticket/65916

WP_Image_Editor_Imagick::remove_pdf_alpha_channel() returns a WP_Error if Imagick::setImageAlphaChannel() throws, but there's no @return tag in the DocBlock. Been like that since the method was added in changeset 56271 (Trac #39216).

/**
 * Removes PDF alpha after it's been read.
 *
 * @since 6.4.0
 */
protected function remove_pdf_alpha_channel() {
	...
	} catch ( Exception $e ) {
		return new WP_Error( 'pdf_alpha_process_failed', $e->getMessage() );
	}
}

Found it while looking at Trac #65817. PHPStan's return.missing rule didn't catch it because there's no declared return type, so it got skipped even though set_imagick_time_limit() in the same file is being fixed for that rule in #13082.

This just adds the tag. Nothing else changes, and PHPCS output for the file is the same before and after.

One other thing I ran into: the only caller, on line 372, throws the return value away, while pdf_load_source() and ->valid() right next to it are both checked. Not sure what the right call is there. Returning the error from load() would kill the PDF preview completely instead of just giving you one with a black background, which seems worse. Or the WP_Error could come out since nothing reads it. Left it alone for now, can do either if someone has a preference.

Drafted Commit Message

Code Quality: Document `WP_Image_Editor_Imagick::remove_pdf_alpha_channel()` return value.

The `WP_Image_Editor_Imagick::remove_pdf_alpha_channel()` method has returned a `WP_Error` object when `Imagick::setImageAlphaChannel()` throws ever since it was introduced in r56271, but its DocBlock declared no return value at all. Document it as `null|WP_Error` and add the explicit `return null;` that the annotation implies.

The union deliberately uses `null` rather than `void`. In PHP's type system `void` means a function returns no value, so it cannot be a member of a union; core removed the construct across `wp-includes` and `wp-admin` in r62178 and r62179. The spelling also matters for static analysis here: PHPStan silently normalizes `void|WP_Error` to `WP_Error|null` and skips the check, whereas `null|WP_Error` correctly reports `return.missing` for a method that otherwise falls off the end.

The sole caller, in `WP_Image_Editor_Imagick::load()`, continues to discard the return value. That is intentional: surfacing the error would abort PDF preview generation outright rather than degrade it to a preview with a black background. No behavior changes.

Developed in https://github.com/WordPress/wordpress-develop/pull/13200.
Follow-up to r56271, r62178.

Props mazharul78, westonruter.
See #65817.
Fixes #65916.

`WP_Image_Editor_Imagick::remove_pdf_alpha_channel()`, introduced in [56271],
returns a `WP_Error` object when `Imagick::setImageAlphaChannel()` throws, but
its DocBlock declares no return value.

This documents the existing return value only; no behavior is changed.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@github-actions

github-actions Bot commented Aug 20, 2026

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props mazharul78, westonruter.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

*
* @since 6.4.0
*
* @return void|WP_Error WP_Error object if the alpha channel could not be removed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to change this to null|WP_Error for the reasons in Core-64694.

…` return type.

`void` cannot be a member of a union type: it means the function returns no
value, which contradicts the `WP_Error` half of the annotation. Core has been
systematically removing this construct — see r61716, r61719, r62178 and r62179,
the latter two of which also add explicit `return null;` statements where
functions previously fell through without a return value. After those sweeps
only one `X|void` annotation remained in `src/`.

The distinction is not cosmetic here. PHPStan normalizes `void|WP_Error` to
`WP_Error|null` and skips the check, so at level 10 the method reports no
errors. Spelled `null|WP_Error` it correctly reports `return.missing`, since
the method falls off the end without returning. The `void` spelling documents
the return value while suppressing the very rule that would enforce it, which
is at odds with the change being made for static analysis in the first place.

Order the union as `null|WP_Error` and describe it as "Null on success" to match
the closest analogues in core, `activate_plugin()` and
`WP_REST_Meta_Fields::update_value()`. Add the explicit `return null;`, the same
shape applied to `wp_dashboard_quota()` in r62179.

The only caller, in `load()`, still discards the return value. That is
deliberate: surfacing the error would fail the PDF preview outright rather than
degrade it to a black background, and r56271 clearly intended the alpha removal
to be best-effort. No behavior changes here — the method already returned `null`
implicitly.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
pento pushed a commit that referenced this pull request Aug 27, 2026
Document `WP_Image_Editor_Imagick::remove_pdf_alpha_channel()` as returning `null|WP_Error` and explicitly return null on success.

Developed in: #13200

Follow-up to r56271.

Props mazharul78, westonruter.
Fixes #65916. See #39216, #65817.


git-svn-id: https://develop.svn.wordpress.org/trunk@63369 602fd350-edb4-49c9-b593-d223f7449a82
@github-actions

Copy link
Copy Markdown

A commit was made that fixes the Trac ticket referenced in the description of this pull request.

SVN changeset: 63369
GitHub commit: 2043dd6

This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.

@github-actions github-actions Bot closed this Aug 27, 2026
markjaquith pushed a commit to markjaquith/WordPress that referenced this pull request Aug 27, 2026
Document `WP_Image_Editor_Imagick::remove_pdf_alpha_channel()` as returning `null|WP_Error` and explicitly return null on success.

Developed in: WordPress/wordpress-develop#13200

Follow-up to r56271.

Props mazharul78, westonruter.
Fixes #65916. See #39216, #65817.

Built from https://develop.svn.wordpress.org/trunk@63369


git-svn-id: http://core.svn.wordpress.org/trunk@62562 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants