swaggest /
php-code-builder
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Swaggest\PhpCodeBuilder; |
||
| 4 | |||
| 5 | |||
| 6 | use Swaggest\PhpCodeBuilder\Traits\Description; |
||
| 7 | use Swaggest\PhpCodeBuilder\Traits\StaticFlag; |
||
| 8 | use Swaggest\PhpCodeBuilder\Traits\Visibility; |
||
| 9 | |||
| 10 | class PhpFunction extends PhpTemplate |
||
| 11 | { |
||
| 12 | use Visibility; |
||
| 13 | use StaticFlag; |
||
| 14 | use Description; |
||
| 15 | |||
| 16 | private $name; |
||
| 17 | |||
| 18 | /** @var PhpNamedVar[] */ |
||
| 19 | private $arguments = array(); |
||
| 20 | |||
| 21 | /** @var PhpAnyType|null */ |
||
| 22 | private $result; |
||
| 23 | |||
| 24 | /** @var PhpAnyType[] */ |
||
| 25 | private $throws; |
||
| 26 | |||
| 27 | private $body; |
||
| 28 | protected $outputArgumentsWithDefaults = true; |
||
| 29 | public $skipCodeCoverage = false; |
||
| 30 | |||
| 31 | |||
| 32 | /** |
||
| 33 | * PhpFunction constructor. |
||
| 34 | * @param string $name |
||
| 35 | * @param string|null $visibility |
||
| 36 | * @param bool $isStatic |
||
| 37 | 14 | */ |
|
| 38 | public function __construct($name, $visibility = null, $isStatic = false) |
||
| 39 | 14 | { |
|
| 40 | 14 | $this->name = $name; |
|
| 41 | 14 | $this->visibility = $visibility; |
|
| 42 | 14 | $this->isStatic = $isStatic; |
|
| 43 | } |
||
| 44 | 14 | ||
| 45 | protected function toString() |
||
| 46 | 14 | { |
|
| 47 | 14 | $tail = ''; |
|
| 48 | 6 | if ($this->skipCodeCoverage) { |
|
| 49 | $tail = (new PhpDocTag(PhpDoc::TAG_CODE_COVERAGE_IGNORE_END))->render() . "\n"; |
||
| 50 | } |
||
| 51 | 14 | ||
| 52 | 14 | $body = $this->body; |
|
| 53 | 12 | if ($body === null) { |
|
| 54 | $body = ''; |
||
| 55 | } |
||
| 56 | 14 | ||
| 57 | $body = trim($body); |
||
| 58 | 14 | ||
| 59 | 14 | if ($body !== '') { |
|
| 60 | $body = $this->indentLines($body . "\n"); |
||
| 61 | } |
||
| 62 | return <<<PHP |
||
| 63 | {$this->headToString()} |
||
| 64 | 14 | { |
|
| 65 | {$body}} |
||
| 66 | $tail |
||
| 67 | 14 | ||
| 68 | PHP; |
||
| 69 | } |
||
| 70 | |||
| 71 | public function headToString() |
||
| 72 | { |
||
| 73 | 14 | return <<<PHP |
|
| 74 | {$this->renderPhpDoc()}{$this->renderVisibility()}{$this->renderIsStatic()}function {$this->name}({$this->renderArguments()}) |
||
| 75 | 14 | PHP; |
|
| 76 | 14 | ||
| 77 | 1 | ||
| 78 | } |
||
| 79 | 14 | ||
| 80 | 13 | private function renderPhpDoc() |
|
| 81 | { |
||
| 82 | 14 | $result = new PhpDoc(); |
|
| 83 | 7 | if ($this->description) { |
|
| 84 | $result->add(null, $this->description); |
||
| 85 | 14 | } |
|
| 86 | 4 | foreach ($this->arguments as $argument) { |
|
| 87 | 4 | $result->add(PhpDoc::TAG_PARAM, $argument->renderPhpDocValue(true)); |
|
| 88 | 4 | } |
|
| 89 | if ($this->result && $returnType = $this->result->renderPhpDocType()) { |
||
| 90 | $result->add(PhpDoc::TAG_RETURN, $returnType); |
||
| 91 | } |
||
| 92 | 14 | if ($this->throws) { |
|
|
0 ignored issues
–
show
|
|||
| 93 | 6 | foreach ($this->throws as $throws) { |
|
| 94 | if ($throwsType = $throws->renderPhpDocType()) { |
||
| 95 | 14 | $result->add(PhpDoc::TAG_THROWS, $throwsType); |
|
| 96 | } |
||
| 97 | } |
||
| 98 | 14 | } |
|
| 99 | if ($this->skipCodeCoverage) { |
||
| 100 | 14 | $result->add(PhpDoc::TAG_CODE_COVERAGE_IGNORE_START); |
|
| 101 | 14 | } |
|
| 102 | 13 | return (string)$result; |
|
| 103 | } |
||
| 104 | 14 | ||
| 105 | 13 | private function renderArguments() |
|
| 106 | { |
||
| 107 | 14 | $result = ''; |
|
| 108 | foreach ($this->arguments as $argument) { |
||
| 109 | $default = $this->outputArgumentsWithDefaults ? $argument->renderDefault() : ''; |
||
| 110 | $result .= "{$argument->renderArgumentType()}\${$argument->getName()}{$default}, "; |
||
| 111 | 13 | } |
|
| 112 | if ($result) { |
||
| 113 | 13 | $result = substr($result, 0, -2); |
|
| 114 | 13 | } |
|
| 115 | return $result; |
||
| 116 | } |
||
| 117 | |||
| 118 | |||
| 119 | public function addArgument(PhpNamedVar $argument) |
||
| 120 | { |
||
| 121 | $this->arguments[] = $argument; |
||
| 122 | return $this; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @return mixed |
||
| 127 | */ |
||
| 128 | public function getBody() |
||
| 129 | 12 | { |
|
| 130 | return $this->body; |
||
| 131 | 12 | } |
|
| 132 | 12 | ||
| 133 | /** |
||
| 134 | * @param mixed $body |
||
| 135 | * @return PhpFunction |
||
| 136 | */ |
||
| 137 | public function setBody($body) |
||
| 138 | { |
||
| 139 | 7 | $this->body = $body; |
|
| 140 | return $this; |
||
| 141 | 7 | } |
|
| 142 | 7 | ||
| 143 | /** |
||
| 144 | * @param PhpAnyType|null $result |
||
| 145 | * @return PhpFunction |
||
| 146 | */ |
||
| 147 | public function setResult(PhpAnyType $result = null) |
||
| 148 | { |
||
| 149 | $this->result = $result; |
||
| 150 | return $this; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param PhpAnyType $throws |
||
| 155 | * @return PhpFunction |
||
| 156 | */ |
||
| 157 | public function setThrows($throws) |
||
| 158 | { |
||
| 159 | 4 | $this->throws = array($throws); |
|
| 160 | return $this; |
||
| 161 | 4 | } |
|
| 162 | 4 | ||
| 163 | /** |
||
| 164 | * @param PhpAnyType $throws |
||
| 165 | * @return PhpFunction |
||
| 166 | */ |
||
| 167 | public function addThrows($throws) |
||
| 168 | { |
||
| 169 | $this->throws []= $throws; |
||
| 170 | return $this; |
||
| 171 | } |
||
| 172 | |||
| 173 | |||
| 174 | |||
| 175 | } |
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.