FunctionProxy   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 6
c 1
b 0
f 0
dl 0
loc 26
ccs 6
cts 6
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A proxyFunctionCall() 0 10 3
1
<?php
2
declare(strict_types=1);
3
namespace ZERO2TEN\Observability\APM;
4
5
use BadFunctionCallException;
6
use Throwable;
7
8
use function call_user_func;
9
use function function_exists;
10
use function sprintf;
11
12
/**
13
 * FunctionProxy
14
 *
15
 * @copyright Copyright (c) 2023 0TO10 B.V. <https://0to10.nl>
16
 * @package ZERO2TEN\Observability\APM
17
 */
18
trait FunctionProxy
19
{
20
    /**
21
     * This method serves as a proxy for calling (native) functions for Agents.
22
     *
23
     * Extending classes must ensure that the arguments passed to the function
24
     * are correct, as this method will just pass through all arguments.
25
     *
26
     * Returns the result of the called function (which may be `false`), or `false`
27
     * on failure.
28
     *
29
     * @param string $name
30
     * @param array $arguments
31
     * @throws BadFunctionCallException
32
     * @return mixed|false
33
     */
34 3
    protected function proxyFunctionCall(string $name, array $arguments)
35
    {
36 3
        if (!function_exists($name)) {
37 1
            throw new BadFunctionCallException(sprintf('Function "%s" does not exist.', $name));
38
        }
39
40
        try {
41 2
            return call_user_func($name, ...$arguments);
42 1
        } catch (Throwable $e) {
43 1
            return false;
44
        }
45
    }
46
}
47