Make WordPress Core

Opened 3 months ago

Closed 3 months ago

#65916 closed defect (bug) (fixed)

Docs: WP_Image_Editor_Imagick::remove_pdf_alpha_channel() is missing an @return tag

Reported by: mazharul78 Owned by: jorbin
Priority: normal Milestone: 7.2
Component: Media Version: 6.4
Severity: trivial Keywords: has-patch
Cc: Focuses: docs

Description

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. It's been like that since the method was added in [56271] (#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() );
        }

I noticed it while looking at #65817. PHPStan's return.missing rule didn't flag 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.

PR just adds the tag, nothing else changes. 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 just come out since nothing reads it. Left it alone for now, can do either if someone has a preference.

PR: ​https://github.com/WordPress/wordpress-develop/pull/13200

Change History (3)

This ticket was mentioned in ​PR #13200 on ​WordPress/wordpress-develop by ​@mazharul78.


3 months ago
#1

## Problem

WP_Image_Editor_Imagick::remove_pdf_alpha_channel() was introduced in [56271] (#39216, WordPress 6.4). It returns a WP_Error object when Imagick::setImageAlphaChannel() throws:

} catch ( Exception $e ) {
    return new WP_Error( 'pdf_alpha_process_failed', $e->getMessage() );
}

Its DocBlock declares no return value at all:

/**
 * Removes PDF alpha after it's been read.
 *
 * @since 6.4.0
 */
protected function remove_pdf_alpha_channel() {

## Why this was not caught by the existing sweeps

PHPStan's return.missing rule only fires when a return type is declared. Because this method declares none, it stayed silent — while its sibling set_imagick_time_limit(), in this same file, is being fixed for that exact rule in #13082 (Trac #65817).

## This PR

Documents the existing return value. No behavior change, no new PHPCS errors (WordPress-Docs commenting sniffs report the same 13 pre-existing errors in this file before and after).

## Open question for a committer

The sole caller discards the return value:

​https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/class-wp-image-editor-imagick.php#L371-L373

...even though the adjacent pdf_load_source() and ->valid() results are both error-checked. Two possible follow-ups, both behavior-affecting and deliberately left out of this PR:

  1. Propagate the error from load() — but a failed alpha strip would then abort PDF preview generation entirely, rather than yielding a preview with a black background. That may be worse for users.
  2. Drop the WP_Error return as dead code.

Happy to follow up with whichever is preferred.

#2 @jorbin
3 months ago

  • Milestone Awaiting Review → 7.2
  • Owner set to jorbin
  • Status new → assigned

#3 @jorbin
3 months ago

  • Resolution → fixed
  • Status assigned → closed

In 63369:

Media: Document PDF alpha channel return value.

Document WP_Image_Editor_Imagick::remove_pdf_alpha_channel() as returning null|WP_Error and explicitly return null on success.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/13200

Follow-up to r56271.

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

Note: See TracTickets for help on using tickets.