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

Traitor   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
eloc 13
c 4
b 0
f 1
dl 0
loc 31
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A traitor() 0 18 4
A traitorReturnIndex() 0 3 1
A traitorPattern() 0 3 1
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