InvalidDelegateException   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
dl 0
loc 77
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A forNonCallable() 0 17 2
1
<?php
2
/**
3
 * Incoming
4
 *
5
 * @author    Trevor Suarez (Rican7)
6
 * @copyright (c) Trevor Suarez
7
 * @link      https://github.com/Rican7/incoming
8
 * @license   MIT
9
 */
10
11
declare(strict_types=1);
12
13
namespace Incoming\Hydrator\Exception;
14
15
use BadFunctionCallException;
16
use Throwable;
17
18
/**
19
 * An exception to be thrown when an invalid delegate method, function, or
20
 * callback is provided to a caller.
21
 */
22
class InvalidDelegateException extends BadFunctionCallException
23
{
24
25
    /**
26
     * Constants
27
     */
28
29
    /**
30
     * The default exception message.
31
     *
32
     * @var string
33
     */
34
    const DEFAULT_MESSAGE = 'Invalid delegate';
35
36
    /**
37
     * The exception code for when a delegate isn't callable.
38
     *
39
     * @var int
40
     */
41
    const CODE_FOR_NON_CALLABLE = 1;
42
43
    /**
44
     * The message extension for when a delegate isn't callable.
45
     *
46
     * @var string
47
     */
48
    const MESSAGE_EXTENSION_FOR_NON_CALLABLE = ' is unable to be called';
49
50
    /**
51
     * The message extension format for when a delegate's name is provided.
52
     *
53
     * @var string
54
     */
55
    const MESSAGE_EXTENSION_NAME_FORMAT = ' named `%s`';
56
57
58
    /**
59
     * Properties
60
     */
61
62
    /**
63
     * The exception message.
64
     *
65
     * @var string
66
     */
67
    protected $message = self::DEFAULT_MESSAGE;
68
69
70
    /**
71
     * Methods
72
     */
73
74
    /**
75
     * Create an exception instance for a delegate that isn't callable.
76
     *
77
     * @param string|null $name The name of the delegate.
78
     * @param int $code The exception code.
79
     * @param Throwable|null $previous A previous exception used for chaining.
80
     * @return static The newly created exception.
81
     */
82 21
    public static function forNonCallable(
83
        string $name = null,
84
        int $code = self::CODE_FOR_NON_CALLABLE,
85
        Throwable $previous = null
86
    ): self {
87 21
        $message = self::DEFAULT_MESSAGE;
88
89 21
        if (null !== $name) {
90 18
            $message .= sprintf(
91 18
                self::MESSAGE_EXTENSION_NAME_FORMAT,
92 18
                $name
93
            );
94
        }
95
96 21
        $message .= self::MESSAGE_EXTENSION_FOR_NON_CALLABLE;
97
98 21
        return new static($message, $code, $previous);
99
    }
100
}
101