|
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
|
5 |
|
private function renderImplements() |
|
38
|
|
|
{ |
|
39
|
5 |
|
if ($this->implements) { |
|
|
|
|
|
|
40
|
2 |
|
$implements = ''; |
|
41
|
2 |
|
foreach ($this->implements as $implement) { |
|
42
|
2 |
|
$implements .= $implement->getReference() . ', '; |
|
43
|
|
|
} |
|
44
|
2 |
|
$implements = substr($implements, 0, -2); |
|
45
|
|
|
return <<<PHP |
|
46
|
2 |
|
implements {$implements} |
|
47
|
|
|
PHP; |
|
48
|
|
|
} |
|
49
|
5 |
|
return ''; |
|
50
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
5 |
|
protected function toString() |
|
54
|
|
|
{ |
|
55
|
5 |
|
$content = $this->renderConstants() . $this->renderProperties() |
|
56
|
5 |
|
. $this->renderMethods(); |
|
57
|
|
|
|
|
58
|
5 |
|
$content = $this->indentLines(trim($content)); |
|
59
|
|
|
|
|
60
|
|
|
return <<<PHP |
|
61
|
5 |
|
{$this->renderHeadComment()}{$this->renderIsAbstract()}class {$this->name}{$this->renderExtends()}{$this->renderImplements()} { |
|
62
|
5 |
|
$content |
|
63
|
|
|
} |
|
64
|
|
|
PHP; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
5 |
|
private function renderConstants() |
|
68
|
|
|
{ |
|
69
|
5 |
|
$result = ''; |
|
70
|
5 |
|
foreach ($this->constants as $name => $constant) { |
|
71
|
2 |
|
$result .= $constant->render(); |
|
72
|
|
|
} |
|
73
|
5 |
|
return $result; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
5 |
|
private function renderProperties() |
|
77
|
|
|
{ |
|
78
|
5 |
|
$result = ''; |
|
79
|
5 |
|
foreach ($this->properties as $property) { |
|
80
|
5 |
|
$result .= $property->render(); |
|
81
|
|
|
} |
|
82
|
5 |
|
return $result; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
5 |
|
private function renderMethods() |
|
86
|
|
|
{ |
|
87
|
5 |
|
$result = ''; |
|
88
|
5 |
|
foreach ($this->methods as $method) { |
|
89
|
5 |
|
$result .= $method->render(); |
|
90
|
|
|
} |
|
91
|
5 |
|
return $result; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
5 |
|
public function addProperty(PhpClassProperty $property) |
|
95
|
|
|
{ |
|
96
|
5 |
|
$this->properties[] = $property; |
|
97
|
5 |
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
5 |
|
public function addMethod(PhpFunction $function) |
|
101
|
|
|
{ |
|
102
|
5 |
|
$this->methods[] = $function; |
|
103
|
5 |
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
2 |
|
public function addConstant(PhpConstant $constant) |
|
107
|
|
|
{ |
|
108
|
2 |
|
if (array_key_exists($constant->getName(), $this->constants)) { |
|
109
|
1 |
|
if ($this->constants[$constant->getName()]->getValue() !== $constant->getValue()) { |
|
110
|
1 |
|
throw new Exception('Duplicate const with different value'); |
|
111
|
|
|
} |
|
112
|
|
|
} else { |
|
113
|
2 |
|
$this->constants[$constant->getName()] = $constant; |
|
114
|
|
|
} |
|
115
|
2 |
|
return $this; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
5 |
|
private function renderIsAbstract() |
|
119
|
|
|
{ |
|
120
|
5 |
|
if ($this->isAbstract) { |
|
121
|
|
|
return 'abstract '; |
|
122
|
|
|
} else { |
|
123
|
5 |
|
return ''; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
} |
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.