Passed
Push — master ( e7d3cc...21de9b )
by Maxim
03:42
created

Axessors   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 67
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 0 3 1
A __callStatic() 0 3 1
A __axessorsExecute() 0 12 4
A __axessorsCall() 0 7 2
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
13
/**
14
 * Trait Axessors.
15
 *
16
 * This trait is used to indicate that a class uses the library and provides them with method call handlers.
17
 * If you wouldn't use method handling functionality, you can add trait {@see Axessors\Axs} to your class.
18
 *
19
 * @package NoOne4rever\Axessors
20
 */
21
trait Axessors
22
{
23
    /**
24
     * Redirects an instance method call.
25
     *
26
     * @param string $method the name of the called method
27
     * @param array $args arguments of the called method
28
     * @return mixed result of the called method
29
     * @throws ReThrownError is thrown when there is an error in *injected* callback or condition code
30
     */
31 3
    public function __call(string $method, array $args)
32
    {
33 3
        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
     * @throws ReThrownError is thrown when there is an error in *injected* callback or condition code
43
     */
44 1
    public static function __callStatic(string $method, array $args)
45
    {
46 1
        return self::__axessorsCall($method, $args);
47
    }
48
49
    /**
50
     * Executes *injected* callback or condition.
51
     *
52
     * @param string $code code to execute
53
     * @param mixed $_var value to process
54
     * @param bool $mode mode of execution
55
     * @return mixed the result or condition or callback
56
     * @throws ReThrownError if an error occurred while evaluating *injected* callback or condition
57
     */
58 3
    public function __axessorsExecute(string $code, $_var, bool $mode)
59
    {
60 3
        $var = $_var;
61
        try {
62 3
            $result = eval('return ' . $code . ';'); // evaluation of the code written in Axessors comment
63 1
        } catch (\Throwable $error) {
64 1
            throw new ReThrownError("an error occurred while evaluating executable string \"$code\": {$error->getMessage()}");
65
        }
66 2
        if ($var != $_var) {
67 1
            return $var;
68
        } else {
69 1
            return $mode ? $result : $var;
70
        }
71
    }
72
73
    /**
74
     * Redirects Axessors method call.
75
     *
76
     * @param string $method method name
77
     * @param array $args method arguments
78
     * @param object|null $object object for instance method call
79
     * @return mixed the result of called method
80
     */
81 4
    private static function __axessorsCall(string $method, array $args, $object = null)
82
    {
83 4
        if (method_exists(static::class, $method)) {
84 2
            return call_user_func_array([$object ?? static::class, $method], $args);
85
        } else {
86 2
            $callProcessor = new CallProcessor(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), $object);
87 2
            return $callProcessor->call($args, $method);
88
        }
89
    }
90
}
91