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

AttributeDocBlock   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 28
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A attributeType() 0 3 1
A __construct() 0 3 1
A extractType() 0 7 2
A hasAttributeType() 0 3 1
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\Attributes;
9
10
use PhUml\Code\Variables\TypeDeclaration;
11
12
/**
13
 * It creates a type declaration for an attribute from its `@var` tag
14
 */
15
final class AttributeDocBlock
16
{
17
    private const VAR_EXPRESSION = '/@var\s*([\w]+(\[\])?)/';
18
19
    private TypeDeclaration $attributeType;
20
21
    public function __construct(?string $comment)
22
    {
23
        $this->extractType($comment);
24
    }
25
26
    public function attributeType(): TypeDeclaration
27
    {
28
        return $this->attributeType;
29
    }
30
31
    public function hasAttributeType(): bool
32
    {
33
        return $this->attributeType->isPresent();
34
    }
35
36
    private function extractType(?string $comment): void
37
    {
38
        if (preg_match(self::VAR_EXPRESSION, (string) $comment, $matches) === 1) {
39
            $this->attributeType = TypeDeclaration::from(trim($matches[1]));
40
            return;
41
        }
42
        $this->attributeType = TypeDeclaration::absent();
43
    }
44
}
45