PHPDocClass   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 0
loc 97
ccs 43
cts 43
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A docComment() 0 6 1
B createMethods() 0 19 6
B parseParams() 0 18 6
A staticMethods() 0 13 1
A methods() 0 13 1
1
<?php
2
3
namespace Atrapalo\PHPTools\Parser;
4
5
use Atrapalo\PHPTools\Parser\Values\Method;
6
use Atrapalo\PHPTools\Parser\Values\Param;
7
8
/**
9
 * Class PHPDocClass
10
 * @package Atrapalo\PHPTools\Parser
11
 *
12
 * @author Guillermo González <[email protected]>
13
 */
14
class PHPDocClass
15
{
16
    /**
17
     * @param string $className
18
     * @return Method[]
19
     */
20 4
    public static function staticMethods(string $className): array
21
    {
22 4
        $classDoc = self::docComment($className);
23
24 3
        preg_match_all(
25 3
            '/^[* ]+@method(?>\hstatic) ?([a-zA-Z0-9_\-\$\\\ ]*) (\w+)\(([a-zA-Z0-9_\-$,\\\ ]*)\) ?(.*)$/im',
26 3
            $classDoc,
27 3
            $staticMethodsOfDoc,
28 3
            PREG_SET_ORDER
29
        );
30
31 3
        return self::createMethods($staticMethodsOfDoc, true);
0 ignored issues
show
Bug introduced by
It seems like $staticMethodsOfDoc can also be of type null; however, Atrapalo\PHPTools\Parser...cClass::createMethods() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
32
    }
33
34
    /**
35
     * @param string $className
36
     * @return Method[]
37
     */
38 4
    public static function methods(string $className): array
39
    {
40 4
        $classDoc = self::docComment($className);
41
42 3
        preg_match_all(
43 3
            '/^[* ]+@method(?!.*static )([a-zA-Z0-9_\-\$\\\ ]*) (\w+)\(([a-zA-Z0-9_\-$,\\\ ]*)\) ?(.*)$/mi',
44 3
            $classDoc,
45 3
            $staticMethodsOfDoc,
46 3
            PREG_SET_ORDER
47
        );
48
49 3
        return self::createMethods($staticMethodsOfDoc, false);
0 ignored issues
show
Bug introduced by
It seems like $staticMethodsOfDoc can also be of type null; however, Atrapalo\PHPTools\Parser...cClass::createMethods() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
50
    }
51
52
    /**
53
     * @param string $className
54
     * @return string
55
     */
56 8
    private static function docComment(string $className): string
57
    {
58 8
        $rc = new \ReflectionClass($className);
59
60 6
        return $rc->getDocComment();
61
    }
62
63
    /**
64
     * @param array $methodsOfDoc
65
     * @param bool  $areStatics
66
     * @return array
67
     */
68 6
    private static function createMethods(array $methodsOfDoc, bool $areStatics): array
69
    {
70 6
        $methods = [];
71 6
        foreach ($methodsOfDoc as $methodOfDoc) {
72 4
            $return = isset($methodOfDoc[1]) ? trim($methodOfDoc[1]) : '';
73 4
            $name = isset($methodOfDoc[2]) ? trim($methodOfDoc[2]) : '';
74 4
            $params = isset($methodOfDoc[3]) ? self::parseParams($methodOfDoc[3]) : [];
75 4
            $description = isset($methodOfDoc[4]) ? trim($methodOfDoc[4]) : '';
76
77 4
            $method = new Method($areStatics, $name);
78 4
            $method->setReturn($return)
79 4
                ->setDescription($description)
80 4
                ->setParams($params);
81
82 4
            $methods[] = $method;
83
        }
84
85 6
        return $methods;
86
    }
87
88
    /**
89
     * @param string $stringParams
90
     * @return array
91
     */
92 4
    private static function parseParams(string $stringParams): array
93
    {
94 4
        $params = [];
95 4
        if (!empty($stringParams)){
96 2
            $paramsSlice = explode(',', $stringParams);
97 2
            foreach ($paramsSlice as $paramElement) {
98 2
                preg_match('/([\\a-z]*)\h?(\$\w+)/i', $paramElement, $paramData);
99 2
                if (count($paramData) == 3) {
100 2
                    $type = isset($paramData[1]) ? trim($paramData[1]) : '';
101 2
                    $name = isset($paramData[2]) ? trim($paramData[2]) : '';
102
103 2
                    $params[] = new Param($name, $type);
104
                }
105
            }
106
        }
107
108 4
        return $params;
109
    }
110
}
111