TraitGenerator::setName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2014-04-26 
5
 */
6
7
namespace Net\Bazzline\Component\CodeGenerator;
8
9
/**
10
 * Class TraitGenerator
11
 * @package Net\Bazzline\Component\Locator\LocatorGenerator\Generator
12
 */
13
class TraitGenerator extends AbstractDocumentedGenerator
14
{
15
    /**
16
     * @param ConstantGenerator $constant
17
     * @return $this
18
     */
19
    public function addConstant(ConstantGenerator $constant)
20
    {
21
        $this->addGeneratorProperty('constants', $constant);
22
23
        return $this;
24
    }
25
26
    /**
27
     * @param PropertyGenerator $property
28
     * @return $this
29
     */
30
    public function addProperty(PropertyGenerator $property)
31
    {
32
        $this->addGeneratorProperty('properties', $property);
33
34
        return $this;
35
    }
36
37
    /**
38
     * @param MethodGenerator $method
39
     * @return $this
40
     */
41
    public function addMethod(MethodGenerator $method)
42
    {
43
        $this->addGeneratorProperty('methods', $method);
44
45
        return $this;
46
    }
47
48
    /**
49
     * @param string $name
50
     * @return $this
51
     */
52
    public function setName($name)
53
    {
54
        $this->addGeneratorProperty('name', (string) $name, false);
55
56
        return $this;
57
    }
58
59
    /**
60
     * @return null|string
61
     */
62
    public function getName()
63
    {
64
        return $this->getGeneratorProperty('name');
65
    }
66
67
    /**
68
     * @throws InvalidArgumentException|RuntimeException
69
     * @return string
70
     * @todo implement exception throwing if mandatory parameter is missing
71
     */
72
    public function generate()
73
    {
74
        if ($this->canBeGenerated()) {
75
            if (is_null($this->getGeneratorProperty('name'))) {
76
                throw new RuntimeException('name is mandatory');
77
            }
78
79
            $this->resetContent();
80
            $this->generateDocumentation();
81
            $this->generateSignature();
82
            $this->generateBody();
83
        }
84
85
        return $this->generateStringFromContent();
86
    }
87
88
    private function generateBody()
89
    {
90
        $addEmptyLine = false;
91
        $this->addContent('{');
92
        /** @var null|ConstantGenerator[] $constants */
93
        $constants = $this->getGeneratorProperty('constants');
94
        /** @var null|MethodGenerator[] $methods */
95
        $methods = $this->getGeneratorProperty('methods');
96
        /** @var null|PropertyGenerator[] $properties */
97
        $properties = $this->getGeneratorProperty('properties');
98
99
        if (is_array($constants)) {
100
            $lastArrayKey = $this->getLastArrayKey($constants);
101
            foreach($constants as $key => $constant) {
102
                $this->addGeneratorAsContent($constant, true);
103
                if ($key !== $lastArrayKey) {
104
                    $this->addContent('');
105
                }
106
            }
107
            $addEmptyLine = true;
108
        }
109 View Code Duplication
        if (is_array($properties)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
            if ($addEmptyLine) {
111
                $this->addContent('');
112
            }
113
            $lastArrayKey = $this->getLastArrayKey($properties);
114
            foreach($properties as $key => $property) {
115
                $this->addGeneratorAsContent($property, true);
116
                if ($key !== $lastArrayKey) {
117
                    $this->addContent('');
118
                }
119
            }
120
            $addEmptyLine = true;
121
        }
122
        if (is_array($methods)) {
123
            if ($addEmptyLine) {
124
                $this->addContent('');
125
            }
126
            $lastArrayKey = $this->getLastArrayKey($methods);
127
            foreach($methods as $key => $method) {
128
                $this->addGeneratorAsContent($method, true);
129
                if ($key !== $lastArrayKey) {
130
                    $this->addContent('');
131
                }
132
            }
133
        }
134
135
        $this->addContent('}');
136
    }
137
138
    private function generateDocumentation()
139
    {
140
        $documentation = $this->getGeneratorProperty('documentation');
141
142
        if ($documentation instanceof DocumentationGenerator) {
143
            if ($this->completeDocumentationAutomatically === true) {
144
                $name = $this->getGeneratorProperty('name');
145
146
                if (is_string($name)) {
147
                    $documentation->setClass($name);
148
                }
149
            }
150
            $this->addGeneratorAsContent($documentation);
151
        }
152
    }
153
154
    /**
155
     * @throws \Net\Bazzline\Component\CodeGenerator\RuntimeException
156
     */
157
    private function generateSignature()
158
    {
159
        $name = $this->getGeneratorProperty('name');
160
161
        if (is_null($name)) {
162
            throw new RuntimeException('name is mandatory');
163
        }
164
165
        $line = $this->getLineGenerator('trait ' . $name);
166
        $this->addContent($line);
167
    }
168
}
169