Passed
Push — main ( 791a81...73981b )
by Sammy
07:46
created

Traitor::traitor()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 4
eloc 10
c 2
b 0
f 1
nc 4
nop 1
dl 0
loc 18
rs 9.9332
1
<?php
2
3
namespace HexMakina\Traitor;
4
5
trait Traitor
6
{
7
8
    public function traitor($method_name)
9
    {
10
        $ret = [];
11
12
        $regex = sprintf($this->traitorPattern(), $method_name);
13
14
        $traits = (new \ReflectionClass($this))->getTraitNames();
15
        foreach ($traits as $trait_name) {
16
            $trait_methods = (new \ReflectionClass($trait_name))->getMethods();
17
            foreach ($trait_methods as $method) {
18
                if (preg_match($regex, $method->name, $match) === 1) {
19
                    $callable = current($match);
20
                    $ret[$this->traitorReturnIndex($trait_name, $method->name)] = $this->$callable();
21
                }
22
            }
23
        }
24
25
        return $ret;
26
    }
27
28
    public function traitorReturnIndex($trait_name, $method_name)
29
    {
30
      return $trait_name . '::' . $method_name;
31
    }
32
33
    public function traitorPattern()
34
    {
35
      return '/.+Traitor_%s$/';
36
    }
37
38
}
39