1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Bright Nucleus Shortcode Component. |
4
|
|
|
* |
5
|
|
|
* @package BrightNucleus\Shortcode |
6
|
|
|
* @author Alain Schlesser <[email protected]> |
7
|
|
|
* @license MIT |
8
|
|
|
* @link http://www.brightnucleus.com/ |
9
|
|
|
* @copyright 2015-2016 Alain Schlesser, Bright Nucleus |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace BrightNucleus\Shortcode\Exception; |
13
|
|
|
|
14
|
|
|
use BrightNucleus\Exception\RuntimeException; |
15
|
|
|
use Exception; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class FailedToInstantiateObject. |
19
|
|
|
* |
20
|
|
|
* @since 0.3.0 |
21
|
|
|
* |
22
|
|
|
* @package BrightNucleus\Shortcode\Exception |
23
|
|
|
* @author Alain Schlesser <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class FailedToInstantiateObject extends RuntimeException implements ShortcodeException { |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Create a new instance from a passed-in class name or factory callable. |
29
|
|
|
* |
30
|
|
|
* @since 0.3.0 |
31
|
|
|
* |
32
|
|
|
* @param mixed $factory Class name or factory callable. |
33
|
|
|
* @param string $interface Interface name that the object should have |
34
|
|
|
* implemented. |
35
|
|
|
* @param Exception $exception Exception that was caught. |
|
|
|
|
36
|
|
|
* @return static Instance of an exception. |
37
|
|
|
*/ |
38
|
|
|
public static function fromFactory( $factory, $interface, $exception = null ) { |
39
|
|
|
$reason = $exception instanceof Exception |
40
|
|
|
? " Reason: {$exception->getMessage()}" |
41
|
|
|
: ''; |
42
|
|
|
|
43
|
|
|
if ( is_callable( $factory ) ) { |
44
|
|
|
$message = sprintf( |
45
|
|
|
'Could not instantiate object of type "%1$s" from factory of type: "%2$s".%3$s', |
46
|
|
|
$interface, |
47
|
|
|
gettype( $factory ), |
48
|
|
|
$reason |
49
|
|
|
); |
50
|
|
|
} elseif ( is_string( $factory ) ) { |
51
|
|
|
$message = sprintf( |
52
|
|
|
'Could not instantiate object of type "%1$s" from class name: "%2$s".%3$s', |
53
|
|
|
$interface, |
54
|
|
|
$factory, |
55
|
|
|
$reason |
56
|
|
|
); |
57
|
|
|
} else { |
58
|
|
|
$message = sprintf( |
59
|
|
|
'Could not instantiate object of type "%1$s" from invalid argument of type: "%2$s".%3$s', |
60
|
|
|
$interface, |
61
|
|
|
$factory, |
62
|
|
|
$reason |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return new static( $message, 0, $exception ); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Create a new instance from a passed-in object that does not implement the |
71
|
|
|
* correct interface. |
72
|
|
|
* |
73
|
|
|
* @since 0.3.0 |
74
|
|
|
* |
75
|
|
|
* @param mixed $factory Class name or factory callable. |
76
|
|
|
* @param string $interface Interface name that the object should have |
77
|
|
|
* implemented. |
78
|
|
|
* @return static Instance of an exception. |
79
|
|
|
*/ |
80
|
|
|
public static function fromInvalidObject( $factory, $interface ) { |
81
|
|
|
$message = sprintf( |
82
|
|
|
'Could not instantiate object of type "%1$s", got "%2$s" instead.', |
83
|
|
|
$interface, |
84
|
|
|
$factory |
85
|
|
|
); |
86
|
|
|
return new static( $message ); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.