Make WordPress Core

Opened 9 years ago

Closed 8 years ago

Last modified 8 years ago

#42742 closed enhancement (fixed)

Add a has_errors() method to WP_Error

Reported by: DrewAPicture Owned by: robdxw
Priority: normal Milestone: 5.1
Component: General Version:
Severity: normal Keywords: has-unit-tests has-dev-note
Cc: Focuses:

Description

We should add a method to easily check whether a WP_Error object contains any errors.

There isn't any one standard way to perform such an operation, and in fact, a cursory search of core yielded a variety of methods for checking whether there are any errors in the object, but nothing really consistent:

  • Checking empty() on the $errors property
  • Checking if the count from get_error_codes() is greater than 0
  • Checking if the value of get_error_code() is an empty string

My proposal would be to introduce a boolean has_errors() method, or perhaps empty() that simply checks whether the $errors property is empty or not.

Attachments (3)

42742-wp-error-tests.diff​ (26.8 KB ) - added by DrewAPicture 9 years ago.
WP_Error full tests coverage
42742_wp_error_has_errors_method.diff​ (1.7 KB ) - added by robdxw 9 years ago.
Add has_errors() method and tests
42742.diff​ (1.6 KB ) - added by DrewAPicture 9 years ago.

Download all attachments as: .zip

Change History (12)

#1 @DrewAPicture
9 years ago

In 42255:

General: Add complete test coverage for WP_Error.

See #42742.

#2 @robdxw
9 years ago

I've submitted a patch adding the has_errors() method & tests. This is my first patch, so please let me know if anything needs changing.

#3 @SergeyBiryukov
9 years ago

  • Milestone Awaiting Review → 5.0

Hi @robdxw, thanks for the patch! It looks good to me.

As a next step, we could update all the instances in WordPress core that currently check if a WP_Error object contains any errors in one way or another (as outlined in the ticket description) to use the new method instead.

#4 @DrewAPicture
9 years ago

In 42646:

Tests: Simplify @covers annotations for WP_Error test coverage. Also fix a typo in a test method name.

See #42742.

#5 @DrewAPicture
9 years ago

  • Keywords has-patch has-unit-tests added; needs-patch needs-unit-tests removed
  • Owner set to robdxw
  • Status new → assigned

42742.diff​ just updates @robdxw's latest patch to fix the simplified @covers annotations introduced in [42646].

Also assigns the ticket to @robdxw to mark the good-first-bug as "claimed".

#6 @SergeyBiryukov
9 years ago

  • Resolution → fixed
  • Status assigned → closed

In 42761:

General: Introduce WP_Error::has_errors() method and use it where appropriate.

Props robdxw, DrewAPicture, SergeyBiryukov.
Fixes #42742.

#7 @johnbillion
8 years ago

  • Keywords needs-patch added; good-first-bug has-patch removed
  • Milestone 5.0 → 5.1
  • Resolution fixed
  • Status closed → reopened

@since needs updating

#8 @desrosj
8 years ago

  • Resolution → fixed
  • Status reopened → closed

In 44527:

Docs: Update since annotations WP_Error::has_errors()

Introduced in [42761].

Fixes #42742.

#9 @desrosj
8 years ago

  • Keywords has-dev-note added; needs-patch removed
Note: See TracTickets for help on using tickets.