Passed
Push — attribute-types ( 3de1eb )
by Luis
10:25
created

MethodDocBlock::from()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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
    {
28
        $this->extractParametersTypes($comment);
29
        $this->extractReturnType($comment);
30
    }
31
32
    public function hasReturnType(): bool
33
    {
34
        return $this->returnType->isPresent();
35
    }
36
37
    public function returnType(): TypeDeclaration
38
    {
39
        return $this->returnType;
40
    }
41
42
    public function hasTypeOfParameter(string $parameterName): bool
43
    {
44
        return isset($this->parametersTypes[$parameterName]);
45
    }
46
47
    public function typeOfParameter(string $parameterName): TypeDeclaration
48
    {
49
        return $this->parametersTypes[$parameterName] ?? TypeDeclaration::absent();
50
    }
51
52
    private function extractReturnType(?string $comment): void
53
    {
54
        if (preg_match(self::RETURN_EXPRESSION, (string) $comment, $matches) === 1) {
55
            $this->returnType = TypeDeclaration::from(trim($matches[1]));
56
            return;
57
        }
58
59
        $this->returnType = TypeDeclaration::absent();
60
    }
61
62
    private function extractParametersTypes(?string $comment): void
63
    {
64
        if (preg_match_all(self::PARAMETER_EXPRESSION, (string) $comment, $matches) < 1) {
65
            $this->parametersTypes = [];
66
            return;
67
        }
68
        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