|
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\Code\Builders\Members; |
|
9
|
|
|
|
|
10
|
|
|
use PhpParser\Node\Param; |
|
11
|
|
|
use PhpParser\Node\Stmt\ClassMethod; |
|
12
|
|
|
use PhUml\Code\Methods\AbstractMethod; |
|
13
|
|
|
use PhUml\Code\Methods\Method; |
|
14
|
|
|
use PhUml\Code\Methods\MethodDocBlock; |
|
15
|
|
|
use PhUml\Code\Methods\StaticMethod; |
|
16
|
|
|
use PhUml\Code\Variables\TypeDeclaration; |
|
17
|
|
|
use PhUml\Code\Variables\Variable; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* It builds an array with `Method`s for either a `ClassDefinition` or an `InterfaceDefinition` |
|
21
|
|
|
* |
|
22
|
|
|
* It can run one or more `VisibilityFilter`s |
|
23
|
|
|
* |
|
24
|
|
|
* @see PrivateVisibilityFilter |
|
25
|
|
|
* @see ProtectedVisibilityFilter |
|
26
|
|
|
*/ |
|
27
|
|
|
class MethodsBuilder extends MembersBuilder |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @param ClassMethod[] $classMethods |
|
31
|
|
|
* @return Method[] |
|
32
|
|
|
*/ |
|
33
|
|
|
public function build(array $classMethods): array |
|
34
|
|
|
{ |
|
35
|
99 |
|
return array_map(function (ClassMethod $method) { |
|
36
|
81 |
|
return $this->buildMethod($method); |
|
37
|
99 |
|
}, $this->runFilters($classMethods)); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
81 |
|
private function buildMethod(ClassMethod $method): Method |
|
41
|
|
|
{ |
|
42
|
81 |
|
$name = $method->name; |
|
43
|
81 |
|
$modifier = $this->resolveVisibility($method); |
|
44
|
81 |
|
$comment = $method->getDocComment(); |
|
45
|
81 |
|
$returnType = MethodDocBlock::from($comment)->returnType(); |
|
46
|
81 |
|
$parameters = $method->params; |
|
47
|
81 |
|
if ($method->isAbstract()) { |
|
48
|
27 |
|
return AbstractMethod::$modifier($name, $this->buildParameters($parameters, $comment), $returnType); |
|
49
|
|
|
} |
|
50
|
81 |
|
if ($method->isStatic()) { |
|
51
|
60 |
|
return StaticMethod::$modifier($name, $this->buildParameters($parameters, $comment), $returnType); |
|
52
|
|
|
} |
|
53
|
81 |
|
return Method::$modifier($name, $this->buildParameters($parameters, $comment), $returnType); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
private function buildParameters(array $parameters, ?string $docBlock): array |
|
57
|
|
|
{ |
|
58
|
81 |
|
return array_map(function (Param $parameter) use ($docBlock) { |
|
59
|
72 |
|
$name = "\${$parameter->name}"; |
|
60
|
72 |
|
$type = $parameter->type; |
|
61
|
72 |
|
if ($type !== null) { |
|
62
|
36 |
|
$typeDeclaration = TypeDeclaration::from($type); |
|
|
|
|
|
|
63
|
|
|
} else { |
|
64
|
72 |
|
$typeDeclaration = MethodDocBlock::from($docBlock)->typeOfParameter($name); |
|
65
|
|
|
} |
|
66
|
72 |
|
return Variable::declaredWith($name, $typeDeclaration); |
|
67
|
81 |
|
}, $parameters); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|