Skip to content

Commit 02d4640

Browse files
arraypressclaude
andcommitted
Stub enough of WP_Error for the suites that load these stubs first
The sibling libraries require this file before their own stubs and skip theirs when a class already exists, so the minimal WP_Error added with validation left reports without get_error_data(). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015Kwx2STJ4EaVaJofEJJ9Zo
1 parent 524b8ca commit 02d4640

1 file changed

Lines changed: 48 additions & 3 deletions

File tree

‎tests/stubs.php‎

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,15 +231,60 @@ function get_term( $term, $taxonomy = '' ) {
231231
* data, so the stub carries neither.
232232
*/
233233
if ( ! class_exists( 'WP_Error' ) ) {
234+
/*
235+
* Enough of core's WP_Error for a consumer's suite as well as this one:
236+
* the sibling libraries load these stubs first and skip their own, so a
237+
* method missing here is a method missing from every test that runs
238+
* after it.
239+
*/
234240
class WP_Error {
235-
public string $message;
241+
public $errors = [];
242+
public $error_data = [];
236243

237244
public function __construct( $code = '', $message = '', $data = '' ) {
238-
$this->message = (string) $message;
245+
if ( '' !== $code ) {
246+
$this->add( $code, $message, $data );
247+
}
248+
}
249+
250+
public function add( $code, $message, $data = '' ) {
251+
$this->errors[ $code ][] = $message;
252+
253+
if ( '' !== $data ) {
254+
$this->error_data[ $code ] = $data;
255+
}
256+
}
257+
258+
public function add_data( $data, $code = '' ) {
259+
$this->error_data[ '' === $code ? $this->get_error_code() : $code ] = $data;
260+
}
261+
262+
public function has_errors() {
263+
return [] !== $this->errors;
264+
}
265+
266+
public function get_error_codes() {
267+
return array_keys( $this->errors );
268+
}
269+
270+
public function get_error_code() {
271+
return $this->get_error_codes()[0] ?? '';
272+
}
273+
274+
public function get_error_messages( $code = '' ) {
275+
if ( '' === $code ) {
276+
return array_merge( ...array_values( $this->errors ) ?: [ [] ] );
277+
}
278+
279+
return $this->errors[ $code ] ?? [];
239280
}
240281

241282
public function get_error_message( $code = '' ) {
242-
return $this->message;
283+
return $this->get_error_messages( $code )[0] ?? '';
284+
}
285+
286+
public function get_error_data( $code = '' ) {
287+
return $this->error_data[ '' === $code ? $this->get_error_code() : $code ] ?? null;
243288
}
244289
}
245290
}

0 commit comments

Comments
 (0)