|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* PHP version 8.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\Stmt\ClassMethod; |
|
11
|
|
|
use PhUml\Code\Methods\Method; |
|
12
|
|
|
use PhUml\Code\UseStatements; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* It builds an array with `Method`s for a `ClassDefinition`, an `InterfaceDefinition` or a |
|
16
|
|
|
* `TraitDefinition` |
|
17
|
|
|
*/ |
|
18
|
|
|
final class ParsedMethodsBuilder implements MethodsBuilder |
|
19
|
|
|
{ |
|
20
|
45 |
|
public function __construct( |
|
21
|
|
|
private readonly ParametersBuilder $parametersBuilder, |
|
|
|
|
|
|
22
|
|
|
private readonly TypeBuilder $typeBuilder, |
|
23
|
|
|
private readonly VisibilityBuilder $visibilityBuilder, |
|
24
|
|
|
) { |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param ClassMethod[] $methods |
|
29
|
|
|
* @return Method[] |
|
30
|
|
|
*/ |
|
31
|
34 |
|
public function build(array $methods, UseStatements $useStatements): array |
|
32
|
|
|
{ |
|
33
|
34 |
|
return array_map( |
|
34
|
34 |
|
fn (ClassMethod $method): Method => $this->buildMethod($method, $useStatements), |
|
35
|
|
|
$methods |
|
36
|
|
|
); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
22 |
|
private function buildMethod(ClassMethod $method, UseStatements $useStatements): Method |
|
40
|
|
|
{ |
|
41
|
22 |
|
$name = $method->name->name; |
|
42
|
22 |
|
$visibility = $this->visibilityBuilder->build($method); |
|
43
|
22 |
|
$docBlock = $method->getDocComment(); |
|
44
|
22 |
|
$returnType = $this->typeBuilder->fromMethodReturnType($method->returnType, $docBlock, $useStatements); |
|
45
|
22 |
|
$parameters = $this->parametersBuilder->build($method->params, $docBlock, $useStatements); |
|
46
|
|
|
return match (true) { |
|
47
|
22 |
|
$method->isAbstract() => new Method($name, $visibility, $returnType, $parameters, isAbstract: true), |
|
48
|
22 |
|
$method->isStatic() => new Method($name, $visibility, $returnType, $parameters, isStatic: true), |
|
49
|
22 |
|
default => new Method($name, $visibility, $returnType, $parameters), |
|
50
|
|
|
}; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|