swaggest /
php-code-builder
| 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) { |
|
|
0 ignored issues
–
show
|
|||
| 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 "' . $constant->getName() . '" with different value, ' . |
||
| 129 | $constant->getValue() . ' !== ' . $this->constants[$constant->getName()]->getValue()); |
||
| 130 | } |
||
| 131 | } else { |
||
| 132 | $this->constants[$constant->getName()] = $constant; |
||
| 133 | } |
||
| 134 | return $this; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Adds a new trait to the list of traits |
||
| 139 | * |
||
| 140 | * @param PhpTrait $trait |
||
| 141 | * @return self |
||
| 142 | * @throws Exception if a trait already exists with same name |
||
| 143 | */ |
||
| 144 | public function addTrait(PhpTrait $trait) |
||
| 145 | { |
||
| 146 | if (!array_key_exists($trait->getName(), $this->traits)) { |
||
| 147 | $this->traits[$trait->getName()] = $trait; |
||
| 148 | } else { |
||
| 149 | throw new Exception('Duplicate trait'); |
||
| 150 | } |
||
| 151 | |||
| 152 | return $this; |
||
| 153 | } |
||
| 154 | |||
| 155 | private function renderTraits() |
||
| 156 | { |
||
| 157 | $result = ''; |
||
| 158 | foreach ($this->traits as $trait) { |
||
| 159 | $result .= $trait->render(); |
||
| 160 | } |
||
| 161 | return $result; |
||
| 162 | } |
||
| 163 | |||
| 164 | private function renderIsAbstract() |
||
| 165 | { |
||
| 166 | if ($this->isAbstract) { |
||
| 167 | return 'abstract '; |
||
| 168 | } else { |
||
| 169 | return ''; |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | } |
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.