Passed
Push — master ( f58add...4aa999 )
by Luis
54s queued 13s
created

AttributeDocBlock::hasAttributeType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
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\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 69
    public function __construct(?string $comment)
22
    {
23 69
        $this->extractType($comment);
24
    }
25
26 69
    public function attributeType(): TypeDeclaration
27
    {
28 69
        return $this->attributeType;
29 69
    }
30 66
31
    public function hasAttributeType(): bool
32 69
    {
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