Traitor::traitorPattern()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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