1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ICanBoogie package. |
5
|
|
|
* |
6
|
|
|
* (c) Olivier Laviale <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ICanBoogie\Prototype; |
13
|
|
|
|
14
|
|
|
use BadMethodCallException; |
15
|
|
|
use ICanBoogie\Accessor\AccessorTrait; |
16
|
|
|
use Throwable; |
17
|
|
|
|
18
|
|
|
use function assert; |
19
|
|
|
use function get_class; |
20
|
|
|
use function ICanBoogie\format; |
21
|
|
|
use function is_object; |
22
|
|
|
use function is_string; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Exception thrown in attempt to access a method that is not defined. |
26
|
|
|
* |
27
|
|
|
* @property-read string $method The method that is not defined. |
28
|
|
|
* @property-read class-string $class The class of the instance on which the method was invoked. |
29
|
|
|
* @property-read object|null $instance Instance on which the method was invoked, or `null` if |
30
|
|
|
* only the class is available. |
31
|
|
|
*/ |
|
|
|
|
32
|
|
|
class MethodNotDefined extends BadMethodCallException implements Exception |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @uses get_method |
36
|
|
|
* @uses get_class |
37
|
|
|
* @uses get_instance |
38
|
|
|
*/ |
39
|
|
|
use AccessorTrait; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $method; |
45
|
|
|
|
46
|
|
|
private function get_method(): string |
47
|
|
|
{ |
48
|
|
|
return $this->method; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var class-string |
|
|
|
|
53
|
|
|
*/ |
54
|
|
|
private $class; |
55
|
|
|
|
56
|
|
|
private function get_class(): string |
57
|
|
|
{ |
58
|
|
|
return $this->class; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var object|null |
63
|
|
|
*/ |
64
|
|
|
private $instance; |
65
|
|
|
|
66
|
|
|
private function get_instance(): ?object |
67
|
|
|
{ |
68
|
|
|
return $this->instance; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @inheritdoc |
73
|
|
|
* |
74
|
|
|
* @param string $method The method that is not defined. |
75
|
|
|
* @param class-string|object $class_or_instance The name of the class or one of its instances. |
|
|
|
|
76
|
|
|
* @param string|null $message If `null` a message is formatted with $method and $class. |
77
|
|
|
*/ |
78
|
|
|
public function __construct(string $method, $class_or_instance, string $message = null, Throwable $previous = null) |
79
|
|
|
{ |
80
|
|
|
$class = $class_or_instance; |
81
|
|
|
|
82
|
|
|
if (is_object($class_or_instance)) { |
83
|
|
|
$this->instance = $class_or_instance; |
84
|
|
|
$class = get_class($class_or_instance); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
assert(is_string($class)); |
88
|
|
|
|
89
|
|
|
$this->method = $method; |
90
|
|
|
$this->class = $class; |
91
|
|
|
|
92
|
|
|
parent::__construct($message ?: $this->format_message($method, $class), 0, $previous); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param class-string $class |
|
|
|
|
97
|
|
|
*/ |
98
|
|
|
private function format_message(string $method, string $class): string |
99
|
|
|
{ |
100
|
|
|
return format('The method %method is not defined by the prototype of class %class.', [ |
101
|
|
|
|
102
|
|
|
'method' => $method, |
103
|
|
|
'class' => $class |
104
|
|
|
|
105
|
|
|
]); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|