Debian Codegraph @

lib/3rdParty/composer/illuminate/support/Traits/ForwardsCalls.php

raw symbols SCIP
1 <?php
2
3 namespace Illuminate\Support\Traits;
4
5 use BadMethodCallException;
6 use Error;
7
8 trait ForwardsCalls
9 {
10 /**
11 * Forward a method call to the given object.
12 *
13 * @param mixed $object
14 * @param string $method
15 * @param array $parameters
16 * @return mixed
17 *
18 * @throws \BadMethodCallException
19 */
20 protected function forwardCallTo($object, $method, $parameters)
21 {
22 try {
23 return $object->{$method}(...$parameters);
24 } catch (Error|BadMethodCallException $e) {
25 $pattern = '~^Call to undefined method (?P<class>[^:]+)::(?P<method>[^\(]+)\(\)$~';
26
27 if (! preg_match($pattern, $e->getMessage(), $matches)) {
28 throw $e;
29 }
30
31 if ($matches['class'] != get_class($object) ||
32 $matches['method'] != $method) {
33 throw $e;
34 }
35
36 static::throwBadMethodCallException($method);
37 }
38 }
39
40 /**
41 * Forward a method call to the given object, returning $this if the forwarded call returned itself.
42 *
43 * @param mixed $object
44 * @param string $method
45 * @param array $parameters
46 * @return mixed
47 *
48 * @throws \BadMethodCallException
49 */
50 protected function forwardDecoratedCallTo($object, $method, $parameters)
51 {
52 $result = $this->forwardCallTo($object, $method, $parameters);
53
54 return $result === $object ? $this : $result;
55 }
56
57 /**
58 * Throw a bad method call exception for the given method.
59 *
60 * @param string $method
61 * @return never
62 *
63 * @throws \BadMethodCallException
64 */
65 protected static function throwBadMethodCallException($method)
66 {
67 throw new BadMethodCallException(sprintf(
68 'Call to undefined method %s::%s()', static::class, $method
69 ));
70 }
71 }