Completed
Pull Request — master (#1)
by Viacheslav
02:50
created

PhpNamedVar   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Test Coverage

Coverage 55.81%

Importance

Changes 0
Metric Value
dl 0
loc 123
ccs 24
cts 43
cp 0.5581
rs 10
c 0
b 0
f 0
wmc 17

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 3 1
A setName() 0 4 1
A renderArgumentType() 0 6 3
A getName() 0 3 1
A renderDefault() 0 13 3
A __construct() 0 6 1
A setType() 0 4 1
A renderPhpDocValue() 0 13 4
A setDefault() 0 5 1
A clearDefault() 0 4 1
1
<?php
2
3
namespace Swaggest\PhpCodeBuilder;
4
5
6
use Swaggest\PhpCodeBuilder\Traits\Description;
7
8
class PhpNamedVar
9
{
10
    use Description;
11
12
    /** @var string */
13
    private $name;
14
15
    /** @var PhpAnyType|null */
16
    private $type;
17
18
    /** @var bool */
19
    private $hasDefault;
20
21
    /** @var mixed */
22
    private $default;
23
24
    /**
25
     * PhpNamedVar constructor.
26
     * @param string $name
27
     * @param PhpAnyType $type
28
     * @param bool $hasDefault
29
     * @param null $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
30
     */
31 6
    public function __construct($name, PhpAnyType $type = null, $hasDefault = false, $default = null)
32
    {
33 6
        $this->name = $name;
34 6
        $this->type = $type;
35 6
        $this->hasDefault = $hasDefault;
36 6
        $this->default = $default;
37 6
    }
38
39
    /**
40
     * @return string
41
     */
42 6
    public function getName()
43
    {
44 6
        return $this->name;
45
    }
46
47
    /**
48
     * @param string $name
49
     * @return PhpNamedVar
50
     */
51
    public function setName($name)
52
    {
53
        $this->name = $name;
54
        return $this;
55
    }
56
57
    /**
58
     * @return PhpAnyType|null
59
     */
60
    public function getType()
61
    {
62
        return $this->type;
63
    }
64
65
    /**
66
     * @param PhpAnyType $type
67
     * @return PhpNamedVar
68
     */
69
    public function setType($type)
70
    {
71
        $this->type = $type;
72
        return $this;
73
    }
74
75
    /**
76
     * @param mixed $value
77
     * @return $this
78
     */
79
    public function setDefault($value)
80
    {
81
        $this->hasDefault = true;
82
        $this->default = $value;
83
        return $this;
84
    }
85
86
    /**
87
     * @return $this
88
     */
89
    public function clearDefault()
90
    {
91
        $this->hasDefault = false;
92
        return $this;
93
    }
94
95 6
    public function renderDefault()
96
    {
97 6
        if (!$this->hasDefault) {
98 6
            return '';
99
        }
100
101
        if ($this->default instanceof PhpTemplate) {
102
            $result = $this->default->render();
103
        } else {
104
            $result = var_export($this->default, true);
105
        }
106
107
        return ' = ' . $result;
108
    }
109
110 6
    public function renderArgumentType()
111
    {
112 6
        if ($this->type && $type = $this->type->renderArgumentType()) {
113 5
            return $type . ' ';
114
        }
115 5
        return '';
116
    }
117
118 6
    public function renderPhpDocValue($withName = false)
119
    {
120 6
        $tagValue = '';
121 6
        if ($this->type) {
122 6
            $tagValue .= $this->type->renderPhpDocType();
123
        }
124 6
        if ($withName) {
125 6
            $tagValue .= ' $' . $this->name;
126
        }
127 6
        if ($this->description) {
128 3
            $tagValue .= ' ' . $this->description;
129
        }
130 6
        return trim($tagValue);
131
    }
132
}