Completed
Push — master ( a62027...79a6e0 )
by Maxim
03:54 queued 01:49
created

Axessors::__axessorsExecuteStatic()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 6.9849

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 2
nop 3
dl 0
loc 10
ccs 3
cts 7
cp 0.4286
crap 6.9849
rs 9.2
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
     * @throws ReThrownError is thrown when there is an error in *injected* callback or condition code
31
     */
32 101
    public function __call(string $method, array $args)
33
    {
34 101
        return self::__axessorsCall($method, $args, $this);
35
    }
36
37
    /**
38
     * Redirects a static method call.
39
     *
40
     * @param string $method the name of the called method
41
     * @param array $args arguments of the called method
42
     * @return mixed result of the called method
43
     * @throws ReThrownError is thrown when there is an error in *injected* callback or condition code
44
     */
45 1
    public static function __callStatic(string $method, array $args)
46
    {
47 1
        return self::__axessorsCall($method, $args);
48
    }
49
50
    /**
51
     * Executes *injected* callback or condition.
52
     *
53
     * @param string $code code to execute
54
     * @param $var mixed value to process
55
     * @param bool $mode mode of execution
56
     * @return mixed the result or condition or callback
57
     * @throws ReThrownError if an error occurred while evaluating *injected* callback or condition
58
     * @throws TypeError if non-countable value supplied to ConditionsRunner::count()
59
     */
60 81
    public function __axessorsExecute(string $code, $var, bool $mode)
61
    {
62
        try {
63 81
            $result = (bool)eval('return ' . $code . ';'); // evaluation of the code written in Axessors comment
1 ignored issue
show
introduced by
The use of eval() is discouraged.
Loading history...
64 4
        } catch (TypeError $error) {
1 ignored issue
show
Unused Code introduced by
catch (\NoOne4rever\Axes...tions\TypeError $error) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
65 1
            throw $error;
66 3
        } catch (\Throwable $error) {
67 3
            throw new ReThrownError("an error occurred while evaluating executable string \"$code\": {$error->getMessage()}");
68
        }
69 79
        return $mode ? $result : $var;
70
    }
71
72
    /**
73
     * Executes *injected* callback or condition.
74
     *
75
     * @param string $code code to execute
76
     * @param $var mixed value to process
77
     * @param bool $mode mode of execution
78
     * @return mixed the result or condition or callback
79
     * @throws ReThrownError if an error occurred while evaluating *injected* callback or condition
80
     * @throws TypeError if non-countable value supplied to ConditionsRunner::count()
81
     */
82 1
    public static function __axessorsExecuteStatic(string $code, $var, bool $mode)
83
    {
84
        try {
85 1
            $result = (bool)eval('return ' . $code . ';'); // evaluation of the code written in Axessors comment
86
        } catch (TypeError $error) {
1 ignored issue
show
Unused Code introduced by
catch (\NoOne4rever\Axes...tions\TypeError $error) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
87
            throw $error;
88
        } catch (\Throwable $error) {
89
            throw new ReThrownError("an error occurred while evaluating executable string \"$code\": {$error->getMessage()}");
90
        }
91 1
        return $mode ? $result : $var;
92
    }
93
94
    /**
95
     * Redirects Axessors method call.
96
     *
97
     * @param string $method method name
98
     * @param array $args method arguments
99
     * @param object|null $object object for instance method call
100
     * @return mixed the result of called method
101
     */
102 102
    private static function __axessorsCall(string $method, array $args, $object = null)
103
    {
104 102
        $callProcessor = new CallProcessor(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), $object);
105 102
        return $callProcessor->call($args, $method);
106
    }
107
}
108