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

ProxyClass   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 84.21%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 19
c 1
b 0
f 0
dl 0
loc 43
ccs 16
cts 19
cp 0.8421
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A define() 0 6 4
A alias() 0 9 3
A _call() 0 17 3
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