Skip to content

Commit ea88149

Browse files
committed
feat: add exception MissingMethodException
1 parent 14c01fe commit ea88149

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog][keepachangelog] and this project adheres to [Semantic Versioning][semver].
66

7+
## v4.7.0
8+
9+
### Added
10+
11+
- Add exception `MissingMethodException`
12+
- Add global function `remoteStaticCallOrTrow`
13+
714
## v4.6.0
815

916
### Added
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Php\Support\Exceptions;
6+
7+
use BadMethodCallException;
8+
use Php\Support\Traits\Thrower;
9+
10+
class MissingMethodException extends BadMethodCallException
11+
{
12+
use Thrower;
13+
14+
public function __construct(protected ?string $method = null, string $message = null)
15+
{
16+
parent::__construct(
17+
$message ??
18+
($this->getName() . ($this->method ? ': "' . $this->method . '"' : ''))
19+
);
20+
}
21+
22+
/**
23+
* @return string
24+
*/
25+
public function getName(): string
26+
{
27+
return 'Missing method';
28+
}
29+
30+
/**
31+
* @return null|string
32+
*/
33+
public function getMethod(): ?string
34+
{
35+
return $this->method;
36+
}
37+
}

src/Global/base.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,33 @@ function remoteStaticCall(object|string|null $class, string $method, mixed ...$p
198198
}
199199
}
200200

201+
if (!function_exists('remoteStaticCall')) {
202+
/**
203+
* Returns result of an object's method if it exists in the object or trow exception.
204+
*
205+
* @param string|object|null $class
206+
* @param string $method
207+
* @param mixed ...$params
208+
*
209+
* @return mixed
210+
*/
211+
function remoteStaticCallOrTrow(object|string|null $class, string $method, mixed ...$params): mixed
212+
{
213+
if (!$class) {
214+
throw new RuntimeException('Target Class is absent');
215+
}
216+
217+
if (
218+
(is_object($class) || (is_string($class) && class_exists($class))) &&
219+
method_exists($class, $method)
220+
) {
221+
return $class::$method(...$params);
222+
}
223+
224+
\Php\Support\Exceptions\MissingMethodException::throw("$class::$method");
225+
}
226+
}
227+
201228
if (!function_exists('remoteCall')) {
202229
/**
203230
* Returns result of an object's method if it exists in the object.

0 commit comments

Comments
 (0)