PropertyGenerator::generate()   B
last analyzed

Complexity

Conditions 7
Paths 25

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 8.3306
c 0
b 0
f 0
cc 7
nc 25
nop 0
1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2014-04-27 
5
 */
6
7
namespace Net\Bazzline\Component\CodeGenerator;
8
9
use Net\Bazzline\Component\CodeGenerator\InvalidArgumentException;
10
use Net\Bazzline\Component\CodeGenerator\RuntimeException;
11
12
/**
13
 * Class PropertyGenerator
14
 * @package Net\Bazzline\Component\Locator\LocatorGenerator\Generator
15
 */
16
class PropertyGenerator extends AbstractDocumentedGenerator
17
{
18
    /**
19
     * @param string $typeHint
20
     * @return $this
21
     */
22
    public function addTypeHint($typeHint)
23
    {
24
        $this->addGeneratorProperty('type_hints', (string) $typeHint);
25
26
        return $this;
27
    }
28
29
    /**
30
     * @return $this
31
     */
32
    public function markAsPrivate()
33
    {
34
        $this->addGeneratorProperty('visibility', 'private', false);
35
36
        return $this;
37
    }
38
39
    /**
40
     * @return $this
41
     */
42
    public function markAsProtected()
43
    {
44
        $this->addGeneratorProperty('visibility', 'protected', false);
45
46
        return $this;
47
    }
48
49
    /**
50
     * @return $this
51
     */
52
    public function markAsPublic()
53
    {
54
        $this->addGeneratorProperty('visibility', 'public', false);
55
56
        return $this;
57
    }
58
59
    /**
60
     * @return $this
61
     */
62
    public function markAsStatic()
63
    {
64
        $this->addGeneratorProperty('static', true, false);
65
66
        return $this;
67
    }
68
69
    /**
70
     * @param string $name
71
     * @return $this
72
     */
73
    public function setName($name)
74
    {
75
        $this->addGeneratorProperty('name', (string) $name, false);
76
77
        return $this;
78
    }
79
80
    /**
81
     * @param string $value
82
     * @return $this
83
     */
84
    public function setValue($value)
85
    {
86
        $this->addGeneratorProperty('value', (string) $value, false);
87
88
        return $this;
89
    }
90
91
    /**
92
     * @throws InvalidArgumentException|RuntimeException
93
     * @return string
94
     * @todo implement exception throwing if mandatory parameter is missing
95
     */
96
    public function generate()
97
    {
98
        $this->resetContent();
99
        $documentation  = $this->getGeneratorProperty('documentation');
100
        $isStatic       = $this->getGeneratorProperty('static', false);
101
        $name           = $this->getGeneratorProperty('name');
102
        $typeHints      = $this->getGeneratorProperty('type_hints', []);
103
        $value          = $this->getGeneratorProperty('value');
104
        $visibility     = $this->getGeneratorProperty('visibility');
105
106
        if (is_null($name)) {
107
            throw new RuntimeException('name is mandatory');
108
        }
109
110
        $block = $this->getBlockGenerator();
111
        $line = $this->getLineGenerator();
112
113
        if ($documentation instanceof DocumentationGenerator) {
114
            if ($this->completeDocumentationAutomatically === true) {
115
                $documentation->setVariable($name, $typeHints);
116
            }
117
            $block->add(explode(PHP_EOL, $documentation->generate()));
118
        }
119
120
        if (!is_null($visibility)) {
121
            $line->add($visibility);
122
        }
123
124
        if ($isStatic) {
125
            $line->add('static');
126
        }
127
        if (!is_null($value)) {
128
            $line->add('$' . $name . ' = ' . $value . ';');
129
        } else {
130
            $line->add('$' . $name . ';');
131
        }
132
        $block->add($line);
133
        $this->addContent($block);
134
135
        return $this->generateStringFromContent();
136
    }
137
}
138