Passed
Push — master ( 378557...7db6a0 )
by Iman
02:39
created

ProxyClass::define()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.25

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 4
cp 0.75
rs 10
cc 4
nc 2
nop 2
crap 4.25
1
<?php
2
3
namespace Imanghafoori\HeyMan\Core;
4
5
class ProxyClass
6
{
7
    private $methods = [];
8
9
    private $aliases = [];
10
11 112
    public function _call($method, $param)
12
    {
13 112
        $method = $this->aliases[$method] ?? $method;
14
15 112
        if (! isset($this->methods[$method])) {
16
            throw new \BadMethodCallException($method.' does not exist');
17
        }
18
19 112
        $condition = $this->methods[$method];
20
21 112
        if (is_callable($condition)) {
22 1
            return call_user_func_array($condition, $param);
23
        }
24
25 111
        [$class, $method] = explode('@', $condition);
26
27 111
        return call_user_func_array([new $class, $method], $param);
28
    }
29
30 117
    public function define($methodName, $callable)
31
    {
32 117
        if (is_callable($callable) || (is_string($callable) and mb_strpos($callable, '@'))) {
33 117
            $this->methods[$methodName] = $callable;
34
        } else {
35
            throw new \InvalidArgumentException("$callable should be string Class@method or a php callable");
36
        }
37 117
    }
38
39 117
    public function alias(string $currentName, string $newName, $override = true)
40
    {
41 117
        if ($override || ! isset($this->aliases[$newName])) {
42 117
            $this->aliases[$newName] = $currentName;
43
44 117
            return true;
45
        }
46
47
        return false;
48
    }
49
}
50