ClassHelper   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 44
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getImplementedMethods() 0 9 3
A getMethods() 0 6 1
A getInterfaceMethods() 0 10 3
1
<?php
2
3
4
namespace carono\exchange1c\helpers;
5
6
7
use yii\helpers\ArrayHelper;
8
use yii\helpers\StringHelper;
9
10
class ClassHelper
11
{
12
    /**
13
     * @param $class
14
     * @return string[]
15
     */
16
    public static function getMethods($class)
17
    {
18
        $path = \Yii::getAlias('@' . str_replace('\\', '/', ltrim($class, '\\'))) . '.php';
19
        $content = file_get_contents($path);
20
        preg_match_all('#^[\s\w]+function\s+(.+)\s*\(#im', $content, $match);
21
        return ArrayHelper::getValue($match, 1, []);
22
    }
23
24
    /**
25
     * @param $interface
26
     * @return string[]
27
     */
28
    public static function getInterfaceMethods($interface)
29
    {
30
        $interface = new \ReflectionClass($interface);
31
        $result = [];
32
        foreach ($interface->getMethods(\ReflectionMethod::IS_ABSTRACT) as $method) {
33
            if (StringHelper::startsWith($method->class, 'carono\exchange1c\interfaces')) {
34
                $result[] = $method;
35
            }
36
        }
37
        return array_values(ArrayHelper::map($result, 'name', 'name'));
38
    }
39
40
    /**
41
     * @param $class
42
     * @param $interface
43
     * @return boolean[]
44
     */
45
    public static function getImplementedMethods($class, $interface)
46
    {
47
        $methods = $class ? ClassHelper::getMethods($class) : [];
48
        $interfaceMethods = self::getInterfaceMethods($interface);
49
        $result = [];
50
        foreach ($interfaceMethods as $interfaceMethod) {
51
            $result[$interfaceMethod] = array_search($interfaceMethod, $methods) !== false;
52
        }
53
        return $result;
54
    }
55
}