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
|
96 |
|
return array_map(function (array $method) { |
29
|
63 |
|
return $this->buildMethod($method); |
30
|
96 |
|
}, $definition->methods()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** @return Attribute[] */ |
34
|
|
|
public function attributes(RawDefinition $class): array |
35
|
|
|
{ |
36
|
87 |
|
return array_map(function (array $attribute) { |
37
|
54 |
|
[$name, $modifier, $comment, $isStatic] = $attribute; |
38
|
54 |
|
if ($isStatic) { |
39
|
36 |
|
return StaticAttribute::$modifier($name, $this->extractTypeFrom($comment)); |
40
|
|
|
} |
41
|
54 |
|
return Attribute::$modifier($name, $this->extractTypeFrom($comment)); |
42
|
87 |
|
}, $class->attributes()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** @return Constant[] */ |
46
|
|
|
public function constants(RawDefinition $definition): array |
47
|
|
|
{ |
48
|
96 |
|
return array_map(function (array $constant) { |
49
|
33 |
|
[$name, $type] = $constant; |
50
|
33 |
|
return new Constant($name, TypeDeclaration::from($type)); |
51
|
96 |
|
}, $definition->constants()); |
52
|
|
|
} |
53
|
|
|
|
54
|
63 |
|
private function buildMethod(array $method): Method |
55
|
|
|
{ |
56
|
63 |
|
[$name, $modifier, $parameters, $isAbstract, $isStatic] = $method; |
57
|
63 |
|
if ($isAbstract) { |
58
|
21 |
|
return AbstractMethod::$modifier($name, $this->buildParameters($parameters)); |
59
|
|
|
} |
60
|
63 |
|
if ($isStatic) { |
61
|
45 |
|
return StaticMethod::$modifier($name, $this->buildParameters($parameters)); |
62
|
|
|
} |
63
|
63 |
|
return Method::$modifier($name, $this->buildParameters($parameters)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** @return Variable[] */ |
67
|
|
|
private function buildParameters(array $parameters): array |
68
|
|
|
{ |
69
|
63 |
|
return array_map(function (array $parameter) { |
70
|
63 |
|
[$name, $type] = $parameter; |
71
|
63 |
|
return Variable::declaredWith($name, TypeDeclaration::from($type)); |
72
|
63 |
|
}, $parameters); |
73
|
|
|
} |
74
|
|
|
|
75
|
54 |
|
private function extractTypeFrom(?string $comment): TypeDeclaration |
76
|
|
|
{ |
77
|
54 |
|
if ($comment === null) { |
78
|
51 |
|
return TypeDeclaration::absent(); |
79
|
|
|
} |
80
|
|
|
|
81
|
9 |
|
$type = null; // There might be no type information in the comment |
82
|
9 |
|
$matches = []; |
83
|
9 |
|
$arrayExpression = '/^[\s*]*@var\s+array\(\s*(\w+\s*=>\s*)?(\w+)\s*\).*$/m'; |
84
|
9 |
|
if (preg_match($arrayExpression, $comment, $matches)) { |
85
|
9 |
|
$type = $matches[2]; |
86
|
|
|
} else { |
87
|
9 |
|
$typeExpression = '/^[\s*]*@var\s+(\S+).*$/m'; |
88
|
9 |
|
if (preg_match($typeExpression, $comment, $matches)) { |
89
|
9 |
|
$type = trim($matches[1]); |
90
|
|
|
} |
91
|
|
|
} |
92
|
9 |
|
return TypeDeclaration::from($type); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|