1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swaggest\PhpCodeBuilder; |
4
|
|
|
|
5
|
|
|
class PhpClass extends PhpClassTraitInterface |
6
|
|
|
{ |
7
|
|
|
/** @var PhpInterface[] */ |
8
|
|
|
private $implements; |
9
|
|
|
|
10
|
|
|
|
11
|
2 |
|
public function addImplements(PhpInterface $implements) |
12
|
|
|
{ |
13
|
2 |
|
$this->implements[] = $implements; |
14
|
2 |
|
return $this; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** @var boolean */ |
18
|
|
|
private $isAbstract = false; |
19
|
|
|
|
20
|
|
|
/** @var PhpClassProperty[] */ |
21
|
|
|
private $properties = array(); |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return PhpClassProperty[] |
25
|
|
|
*/ |
26
|
2 |
|
public function getProperties() |
27
|
|
|
{ |
28
|
2 |
|
return $this->properties; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** @var PhpFunction[] */ |
32
|
|
|
private $methods = array(); |
33
|
|
|
|
34
|
|
|
/** @var PhpConstant[] */ |
35
|
|
|
private $constants = array(); |
36
|
|
|
|
37
|
12 |
|
/** @var PhpTrait[] */ |
38
|
|
|
private $traits = array(); |
39
|
12 |
|
|
40
|
2 |
|
private function renderImplements() |
41
|
2 |
|
{ |
42
|
2 |
|
if ($this->implements) { |
|
|
|
|
43
|
|
|
$implements = ''; |
44
|
2 |
|
foreach ($this->implements as $implement) { |
45
|
|
|
$implements .= $implement->getReference() . ', '; |
46
|
2 |
|
} |
47
|
|
|
$implements = substr($implements, 0, -2); |
48
|
|
|
return <<<PHP |
49
|
12 |
|
implements {$implements} |
50
|
|
|
PHP; |
51
|
|
|
} |
52
|
|
|
return ''; |
53
|
12 |
|
|
54
|
|
|
} |
55
|
12 |
|
|
56
|
12 |
|
protected function toString() |
57
|
|
|
{ |
58
|
12 |
|
$content = $this->renderTraits() . $this->renderConstants() |
59
|
|
|
. $this->renderProperties() . $this->renderMethods(); |
60
|
|
|
|
61
|
12 |
|
$content = $this->indentLines(trim($content)); |
62
|
|
|
|
63
|
12 |
|
return <<<PHP |
64
|
|
|
{$this->renderHeadComment()}{$this->renderIsAbstract()}class {$this->name}{$this->renderExtends()}{$this->renderImplements()} |
65
|
|
|
{ |
66
|
|
|
$content |
67
|
|
|
} |
68
|
12 |
|
PHP; |
69
|
|
|
} |
70
|
12 |
|
|
71
|
12 |
|
private function renderConstants() |
72
|
5 |
|
{ |
73
|
|
|
$result = ''; |
74
|
12 |
|
foreach ($this->constants as $name => $constant) { |
75
|
|
|
$result .= $constant->render(); |
76
|
|
|
} |
77
|
12 |
|
return $result; |
78
|
|
|
} |
79
|
12 |
|
|
80
|
12 |
|
private function renderProperties() |
81
|
11 |
|
{ |
82
|
|
|
$result = ''; |
83
|
12 |
|
foreach ($this->properties as $property) { |
84
|
|
|
if ($property->isMagical()) { |
85
|
|
|
$nv = $property->getNamedVar(); |
86
|
12 |
|
$typeString = PhpStdType::TYPE_MIXED; |
87
|
|
|
$type = $nv->getType(); |
88
|
12 |
|
if ($type !== null) { |
89
|
12 |
|
$typeString = $type->renderPhpDocType(); |
90
|
12 |
|
} |
91
|
|
|
|
92
|
12 |
|
$this->getPhpDoc()->add( |
93
|
|
|
PhpDoc::TAG_PROPERTY, |
94
|
|
|
trim($typeString . ' $' . $nv->getName() . ' ' . $nv->getDescription()) |
95
|
11 |
|
); |
96
|
|
|
continue; |
97
|
11 |
|
} |
98
|
11 |
|
$result .= $property->render(); |
99
|
|
|
} |
100
|
|
|
return $result; |
101
|
12 |
|
} |
102
|
|
|
|
103
|
12 |
|
private function renderMethods() |
104
|
12 |
|
{ |
105
|
|
|
$result = ''; |
106
|
|
|
foreach ($this->methods as $method) { |
107
|
5 |
|
$result .= $method->render(); |
108
|
|
|
} |
109
|
5 |
|
return $result; |
110
|
1 |
|
} |
111
|
1 |
|
|
112
|
|
|
public function addProperty(PhpClassProperty $property) |
113
|
|
|
{ |
114
|
5 |
|
$this->properties[] = $property; |
115
|
|
|
return $this; |
116
|
5 |
|
} |
117
|
|
|
|
118
|
|
|
public function addMethod(PhpFunction $function) |
119
|
12 |
|
{ |
120
|
|
|
$this->methods[] = $function; |
121
|
12 |
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
12 |
|
public function addConstant(PhpConstant $constant) |
125
|
|
|
{ |
126
|
|
|
if (array_key_exists($constant->getName(), $this->constants)) { |
127
|
|
|
if ($this->constants[$constant->getName()]->getValue() !== $constant->getValue()) { |
128
|
|
|
throw new Exception('Duplicate const with different value'); |
129
|
|
|
} |
130
|
|
|
} else { |
131
|
|
|
$this->constants[$constant->getName()] = $constant; |
132
|
|
|
} |
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Adds a new trait to the list of traits |
138
|
|
|
* |
139
|
|
|
* @param PhpTrait $trait |
140
|
|
|
* @throws Exception if a trait already exists with same name |
141
|
|
|
* @return self |
142
|
|
|
*/ |
143
|
|
|
public function addTrait(PhpTrait $trait) |
144
|
|
|
{ |
145
|
|
|
if (!array_key_exists($trait->getName(), $this->traits)) { |
146
|
|
|
$this->traits[$trait->getName()] = $trait; |
147
|
|
|
} else { |
148
|
|
|
throw new Exception('Duplicate trait'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
private function renderTraits() |
155
|
|
|
{ |
156
|
|
|
$result = ''; |
157
|
|
|
foreach ($this->traits as $trait) { |
158
|
|
|
$result .= $trait->render(); |
159
|
|
|
} |
160
|
|
|
return $result; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
private function renderIsAbstract() |
164
|
|
|
{ |
165
|
|
|
if ($this->isAbstract) { |
166
|
|
|
return 'abstract '; |
167
|
|
|
} else { |
168
|
|
|
return ''; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.