Axessors::__axessorsCall()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is a part of "Axessors" library.
4
 *
5
 * @author <[email protected]>
6
 * @license GPL
7
 */
8
9
namespace NoOne4rever\Axessors;
10
11
use NoOne4rever\Axessors\Exceptions\ReThrownError;
12
use NoOne4rever\Axessors\Exceptions\TypeError;
13
14
/**
15
 * Trait Axessors.
16
 *
17
 * This trait is used to indicate that a class uses the library and provides them with method call handlers.
18
 * If you wouldn't use method handling functionality, you can add trait {@see Axessors\Axs} to your class.
19
 *
20
 * @package NoOne4rever\Axessors
21
 */
22
trait Axessors
23
{
24
    /**
25
     * Redirects an instance method call.
26
     *
27
     * @param string $method the name of the called method
28
     * @param array $args arguments of the called method
29
     * @return mixed result of the called method
30
     */
31 101
    public function __call(string $method, array $args)
32
    {
33 101
        return self::__axessorsCall($method, $args, $this);
34
    }
35
36
    /**
37
     * Redirects a static method call.
38
     *
39
     * @param string $method the name of the called method
40
     * @param array $args arguments of the called method
41
     * @return mixed result of the called method
42
     */
43 1
    public static function __callStatic(string $method, array $args)
44
    {
45 1
        return self::__axessorsCall($method, $args);
46
    }
47
48
    /**
49
     * Executes *injected* callback or condition.
50
     *
51
     * @param string $code code to execute
52
     * @param $var mixed value to process
53
     * @param bool $mode mode of execution
54
     * @return mixed the result or condition or callback
55
     * @throws ReThrownError if an error occurred while evaluating *injected* callback or condition
56
     * @throws TypeError if non-countable value supplied to ConditionsRunner::count()
57
     */
58 81
    public function __axessorsExecute(string $code, $var, bool $mode)
59
    {
60
        try {
61 81
            $result = (bool)eval('return ' . $code . ';'); // evaluation of the code written in Axessors comment
62 4
        } catch (TypeError $error) {
63 1
            throw $error;
64 3
        } catch (\Throwable $error) {
65 3
            throw new ReThrownError("an error occurred while evaluating executable string \"$code\": {$error->getMessage()}");
66
        }
67 79
        return $mode ? $result : $var;
68
    }
69
70
    /**
71
     * Executes *injected* callback or condition.
72
     *
73
     * @param string $code code to execute
74
     * @param $var mixed value to process
75
     * @param bool $mode mode of execution
76
     * @return mixed the result or condition or callback
77
     * @throws ReThrownError if an error occurred while evaluating *injected* callback or condition
78
     * @throws TypeError if non-countable value supplied to ConditionsRunner::count()
79
     */
80 1
    public static function __axessorsExecuteStatic(string $code, $var, bool $mode)
81
    {
82
        try {
83 1
            $result = (bool)eval('return ' . $code . ';'); // evaluation of the code written in Axessors comment
84
        } catch (TypeError $error) {
85
            throw $error;
86
        } catch (\Throwable $error) {
87
            throw new ReThrownError("an error occurred while evaluating executable string \"$code\": {$error->getMessage()}");
88
        }
89 1
        return $mode ? $result : $var;
90
    }
91
92
    /**
93
     * Redirects Axessors method call.
94
     *
95
     * @param string $method method name
96
     * @param array $args method arguments
97
     * @param object|null $object object for instance method call
98
     * @return mixed the result of called method
99
     */
100 102
    private static function __axessorsCall(string $method, array $args, $object = null)
101
    {
102 102
        $callProcessor = new CallProcessor(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), $object);
103 102
        return $callProcessor->call($args, $method);
104
    }
105
}
106