Passed
Push — master ( de9f16...6836c0 )
by Aleksandr
02:35
created

ModuleHelper::getModuleNameByClass()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 7
nop 2
dl 0
loc 14
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
4
namespace carono\exchange1c\helpers;
5
6
7
class ModuleHelper
8
{
9
    /**
10
     * @param string $class
11
     * @param null $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
12
     * @return int|null|string
13
     */
14
    public static function getModuleNameByClass($class = 'carono\exchange1c\ExchangeModule', $default = null)
15
    {
16
        foreach (\Yii::$app->modules as $name => $module) {
17
            $result = '';
18
            if ((is_array($module))) {
19
                $result = ltrim($module['class'], '\\');
20
            } elseif (is_object($module)) {
21
                $result = get_class($module);
22
            }
23
            if ($result == ltrim($class, '\\')) {
24
                return $name;
25
            }
26
        }
27
        return $default;
28
    }
29
30
    /**
31
     * @param $variable
32
     * @param string $class
33
     * @return string|null
34
     */
35
    public static function getPhpDocInterfaceProperty($variable, $class = 'carono\exchange1c\ExchangeModule')
36
    {
37
        $reflection = new \ReflectionClass($class);
38
        $property = $reflection->getProperty($variable);
39
        if (preg_match('#@var\s+([\w\\\]+)#iu', $property->getDocComment(), $match)) {
40
            return $match[1];
41
        } else {
42
            return null;
43
        }
44
    }
45
}