|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* PHP version 7.4 |
|
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\Code\Methods; |
|
9
|
|
|
|
|
10
|
|
|
use PhUml\Code\Variables\TypeDeclaration; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* It extracts the return type and parameters type of a method |
|
14
|
|
|
*/ |
|
15
|
|
|
final class MethodDocBlock |
|
16
|
|
|
{ |
|
17
|
|
|
private const RETURN_EXPRESSION = '/@return\s*([\w]+(\[\])?)/'; |
|
18
|
|
|
|
|
19
|
|
|
private const PARAMETER_EXPRESSION = '/@param\s*([\w]+(?:\[\])?)\s*(\$[\w]+)/'; |
|
20
|
|
|
|
|
21
|
|
|
private TypeDeclaration $returnType; |
|
22
|
|
|
|
|
23
|
|
|
/** @var TypeDeclaration[] */ |
|
24
|
|
|
private array $parametersTypes = []; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(?string $comment) |
|
27
|
93 |
|
{ |
|
28
|
|
|
$this->extractParametersTypes($comment); |
|
29
|
93 |
|
$this->extractReturnType($comment); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
87 |
|
public function hasReturnType(): bool |
|
33
|
|
|
{ |
|
34
|
87 |
|
return $this->returnType->isPresent(); |
|
35
|
87 |
|
} |
|
36
|
63 |
|
|
|
37
|
|
|
public function returnType(): TypeDeclaration |
|
38
|
87 |
|
{ |
|
39
|
|
|
return $this->returnType; |
|
40
|
|
|
} |
|
41
|
81 |
|
|
|
42
|
|
|
public function hasTypeOfParameter(string $parameterName): bool |
|
43
|
81 |
|
{ |
|
44
|
|
|
return isset($this->parametersTypes[$parameterName]); |
|
45
|
|
|
} |
|
46
|
93 |
|
|
|
47
|
|
|
public function typeOfParameter(string $parameterName): TypeDeclaration |
|
48
|
93 |
|
{ |
|
49
|
93 |
|
return $this->parametersTypes[$parameterName] ?? TypeDeclaration::absent(); |
|
50
|
93 |
|
} |
|
51
|
|
|
|
|
52
|
93 |
|
private function extractReturnType(?string $comment): void |
|
53
|
|
|
{ |
|
54
|
93 |
|
if (preg_match(self::RETURN_EXPRESSION, (string) $comment, $matches) === 1) { |
|
55
|
87 |
|
$this->returnType = TypeDeclaration::from(trim($matches[1])); |
|
56
|
|
|
return; |
|
57
|
78 |
|
} |
|
58
|
78 |
|
|
|
59
|
|
|
$this->returnType = TypeDeclaration::absent(); |
|
60
|
78 |
|
} |
|
61
|
|
|
|
|
62
|
78 |
|
private function extractParametersTypes(?string $comment): void |
|
63
|
|
|
{ |
|
64
|
78 |
|
if (preg_match_all(self::PARAMETER_EXPRESSION, (string) $comment, $matches) < 1) { |
|
65
|
78 |
|
$this->parametersTypes = []; |
|
66
|
78 |
|
return; |
|
67
|
|
|
} |
|
68
|
78 |
|
foreach ($matches[0] as $typeHint) { |
|
69
|
|
|
$this->extractDeclarationFrom($typeHint); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
private function extractDeclarationFrom(string $typeHint): void |
|
74
|
|
|
{ |
|
75
|
|
|
if (preg_match(self::PARAMETER_EXPRESSION, $typeHint, $match) === 1) { |
|
76
|
|
|
[$_, $type, $parameterName] = $match; |
|
77
|
|
|
$this->parametersTypes[$parameterName] = TypeDeclaration::from($type); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|