|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PHP version 7.1 |
|
4
|
|
|
* |
|
5
|
|
|
* This source file is subject to the license that is bundled with this package in the file LICENSE. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace PhUml\Parser; |
|
9
|
|
|
|
|
10
|
|
|
use PhUml\Code\AbstractMethod; |
|
11
|
|
|
use PhUml\Code\Attribute; |
|
12
|
|
|
use PhUml\Code\Constant; |
|
13
|
|
|
use PhUml\Code\Method; |
|
14
|
|
|
use PhUml\Code\StaticAttribute; |
|
15
|
|
|
use PhUml\Code\StaticMethod; |
|
16
|
|
|
use PhUml\Code\TypeDeclaration; |
|
17
|
|
|
use PhUml\Code\Variable; |
|
18
|
|
|
use PhUml\Parser\Raw\RawDefinition; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* It builds the attributes and methods of both classes and interfaces |
|
22
|
|
|
*/ |
|
23
|
|
|
class DefinitionMembersBuilder |
|
24
|
|
|
{ |
|
25
|
|
|
/** @return Method[] */ |
|
26
|
|
|
public function methods(RawDefinition $definition): array |
|
27
|
|
|
{ |
|
28
|
|
|
return array_map(function (array $method) { |
|
29
|
|
|
return $this->buildMethod($method); |
|
30
|
|
|
}, $definition->methods()); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** @return Attribute[] */ |
|
34
|
|
|
public function attributes(RawDefinition $class): array |
|
35
|
|
|
{ |
|
36
|
|
|
return array_map(function (array $attribute) { |
|
37
|
|
|
[$name, $modifier, $comment, $isStatic] = $attribute; |
|
38
|
|
|
if ($isStatic) { |
|
39
|
|
|
return StaticAttribute::$modifier($name, $this->extractTypeFrom($comment)); |
|
40
|
|
|
} |
|
41
|
|
|
return Attribute::$modifier($name, $this->extractTypeFrom($comment)); |
|
42
|
|
|
}, $class->attributes()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** @return Constant[] */ |
|
46
|
|
|
public function constants(RawDefinition $definition): array |
|
47
|
|
|
{ |
|
48
|
|
|
return array_map(function (array $constant) { |
|
49
|
|
|
[$name, $type] = $constant; |
|
50
|
|
|
return new Constant($name, TypeDeclaration::from($type)); |
|
51
|
|
|
}, $definition->constants()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
private function buildMethod(array $method): Method |
|
55
|
|
|
{ |
|
56
|
|
|
[$name, $modifier, $parameters, $isAbstract, $isStatic] = $method; |
|
57
|
|
|
if ($isAbstract) { |
|
58
|
|
|
return AbstractMethod::$modifier($name, $this->buildParameters($parameters)); |
|
59
|
|
|
} |
|
60
|
|
|
if ($isStatic) { |
|
61
|
|
|
return StaticMethod::$modifier($name, $this->buildParameters($parameters)); |
|
62
|
|
|
} |
|
63
|
|
|
return Method::$modifier($name, $this->buildParameters($parameters)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** @return Variable[] */ |
|
67
|
|
|
private function buildParameters(array $parameters): array |
|
68
|
|
|
{ |
|
69
|
|
|
return array_map(function (array $parameter) { |
|
70
|
|
|
[$name, $type] = $parameter; |
|
71
|
|
|
return Variable::declaredWith($name, TypeDeclaration::from($type)); |
|
72
|
|
|
}, $parameters); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
private function extractTypeFrom(?string $comment): TypeDeclaration |
|
76
|
|
|
{ |
|
77
|
|
|
if ($comment === null) { |
|
78
|
|
|
return TypeDeclaration::absent(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$type = null; // There might be no type information in the comment |
|
82
|
|
|
$matches = []; |
|
83
|
|
|
$arrayExpression = '/^[\s*]*@var\s+array\(\s*(\w+\s*=>\s*)?(\w+)\s*\).*$/m'; |
|
84
|
|
|
if (preg_match($arrayExpression, $comment, $matches)) { |
|
85
|
|
|
$type = $matches[2]; |
|
86
|
|
|
} else { |
|
87
|
|
|
$typeExpression = '/^[\s*]*@var\s+(\S+).*$/m'; |
|
88
|
|
|
if (preg_match($typeExpression, $comment, $matches)) { |
|
89
|
|
|
$type = trim($matches[1]); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
return TypeDeclaration::from($type); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|