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); |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|
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:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.