Completed
Push — master ( 2ce63d...389baa )
by Luis
11:05 queued 03:24
created

Attribute::__toString()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHP version 7.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\Code\Attributes;
9
10
use PhUml\Code\CanBeStatic;
11
use PhUml\Code\HasVisibility;
12
use PhUml\Code\TypeDeclaration;
13
use PhUml\Code\Variable;
14
use PhUml\Code\Visibility;
15
use PhUml\Code\WithStaticModifier;
16
use PhUml\Code\WithVisibility;
17
18
/**
19
 * It represents an instance variable
20
 */
21
class Attribute extends Variable implements HasVisibility, CanBeStatic
22
{
23
    use WithVisibility, WithStaticModifier;
24
25 96
    protected function __construct(string $name, Visibility $modifier, TypeDeclaration $type)
26
    {
27 96
        parent::__construct($name, $type);
28 96
        $this->modifier = $modifier;
29 96
        $this->isStatic = false;
30 96
    }
31
32 60
    public static function public(string $name, TypeDeclaration $type = null): Attribute
33
    {
34 60
        return new static($name, Visibility::public(), $type ?? TypeDeclaration::absent());
35
    }
36
37 48
    public static function protected(string $name, TypeDeclaration $type = null): Attribute
38
    {
39 48
        return new static($name, Visibility::protected(), $type ?? TypeDeclaration::absent());
40
    }
41
42 72
    public static function private(string $name, TypeDeclaration $type = null): Attribute
43
    {
44 72
        return new static($name, Visibility::private(), $type ?? TypeDeclaration::absent());
45
    }
46
47 42
    public function __toString()
48
    {
49 42
        return sprintf('%s%s%s',
50 42
            $this->modifier,
51 42
            $this->name,
52 42
            $this->type->isPresent() ? ": {$this->type}" : ''
53
        );
54
    }
55
}
56