Passed
Push — master ( c977af...6ee3f7 )
by Nico
02:53 queued 59s
created

CallableUserMethod::getCallable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 14
ccs 7
cts 7
cp 1
crap 2
rs 10
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
3
/**
4
 * @license     http://opensource.org/licenses/mit-license.php MIT
5
 * @link        https://github.com/nicoSWD
6
 * @author      Nicolas Oelgart <[email protected]>
7
 */
8
namespace nicoSWD\Rule\TokenStream;
9
10
use nicoSWD\Rule\Grammar\CallableUserFunctionInterface;
11
use nicoSWD\Rule\TokenStream\Token\BaseToken;
12
use nicoSWD\Rule\TokenStream\Token\TokenFactory;
13
14
final class CallableUserMethod implements CallableUserFunctionInterface
15
{
16
    const MAGIC_METHOD_PREFIX = '__';
17
18
    /** @var TokenFactory */
19
    private $tokenFactory;
20
    /** @var callable */
21
    private $callable;
22
    /** @var string[] */
23
    private $methodPrefixes = ['', 'get', 'is', 'get_', 'is_'];
24
25 52
    public function __construct(BaseToken $token, TokenFactory $tokenFactory, string $methodName)
26
    {
27 52
        $this->tokenFactory = $tokenFactory;
28 52
        $this->callable = $this->getCallable($token, $methodName);
29 18
    }
30
31 18
    public function call(?BaseToken ...$param): BaseToken
32
    {
33 18
        $callable = $this->callable;
34
35 18
        return $this->tokenFactory->createFromPHPType(
36 18
            $callable(...$param)
37
        );
38
    }
39
40 52
    private function getCallable(BaseToken $token, string $methodName): callable
41
    {
42 52
        $object = $token->getValue();
43
44 52
        if (property_exists($object, $methodName)) {
45
            return function () use ($object, $methodName) {
46 4
                return $object->{$methodName};
47 4
            };
48
        }
49
50 48
        $method = $this->findCallableMethod($object, $methodName);
51
52
        return function (?BaseToken ...$params) use ($method) {
53 14
            return $method(...$this->getTokenValues($params));
54 14
        };
55
    }
56
57 48
    private function findCallableMethod($object, string $methodName): callable
58
    {
59 48
        $this->assertNonMagicMethod($methodName);
60
61 18
        $callable = [$object, $methodName];
62 18
        $index = 0;
63
64
        do {
65 18
            if (!isset($this->methodPrefixes[$index])) {
66 4
                throw new Exception\UndefinedMethodException();
67
            }
68
69 18
            $callable[1] = $this->methodPrefixes[$index++] . $methodName;
70 18
        } while (!is_callable($callable));
71
72 14
        return $callable;
73
    }
74
75 14
    private function getTokenValues(array $params): array
76
    {
77
        $callback = function (BaseToken $token) {
78 4
            return $token->getValue();
79 14
        };
80
81 14
        return array_map($callback, $params);
82
    }
83
84 48
    private function assertNonMagicMethod(string $methodName): void
85
    {
86 48
        if (substr($methodName, 0, 2) === self::MAGIC_METHOD_PREFIX) {
87 30
            throw new Exception\ForbiddenMethodException();
88
        }
89 18
    }
90
}
91