1<?php
2
3namespace WpOrg\Requests\Exception;
4
5use WpOrg\Requests\Exception;
6
7/**
8 * Exception for when an incorrect number of arguments are passed to a method.
9 *
10 * Typically, this exception is used when all arguments for a method are optional,
11 * but certain arguments need to be passed together, i.e. a method which can be called
12 * with no arguments or with two arguments, but not with one argument.
13 *
14 * Along the same lines, this exception is also used if a method expects an array
15 * with a certain number of elements and the provided number of elements does not comply.
16 *
17 * @package Requests\Exceptions
18 * @since 2.0.0
19 */
20final class ArgumentCount extends Exception {
21
22 /**
23 * Create a new argument count exception with a standardized text.
24 *
25 * @param string $expected The argument count expected as a phrase.
26 * For example: `at least 2 arguments` or `exactly 1 argument`.
27 * @param int $received The actual argument count received.
28 * @param string $type Exception type.
29 *
30 * @return \WpOrg\Requests\Exception\ArgumentCount
31 */
32 public static function create($expected, $received, $type) {
33 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace
34 $stack = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
35
36 return new self(
37 sprintf(
38 '%s::%s() expects %s, %d given',
39 $stack[1]['class'],
40 $stack[1]['function'],
41 $expected,
42 $received
43 ),
44 $type
45 );
46 }
47}
48