Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 20 | class Response extends Base |
||
| 21 | { |
||
| 22 | private $httpStatusCode = 200; |
||
| 23 | private $httpHeaders = []; |
||
| 24 | private $httpBody = ''; |
||
| 25 | private $responseSent = false; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Return true if the response have been sent to the user agent. |
||
| 29 | * |
||
| 30 | * @return bool |
||
| 31 | */ |
||
| 32 | public function isResponseAlreadySent() |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Set HTTP status code. |
||
| 39 | * |
||
| 40 | * @param int $statusCode |
||
| 41 | * |
||
| 42 | * @return $this |
||
| 43 | */ |
||
| 44 | public function withStatusCode($statusCode) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Set HTTP header. |
||
| 53 | * |
||
| 54 | * @param string $header |
||
| 55 | * @param string $value |
||
| 56 | * |
||
| 57 | * @return $this |
||
| 58 | */ |
||
| 59 | public function withHeader($header, $value) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Set content type header. |
||
| 68 | * |
||
| 69 | * @param string $value |
||
| 70 | * |
||
| 71 | * @return $this |
||
| 72 | */ |
||
| 73 | public function withContentType($value) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Set default security headers. |
||
| 82 | * |
||
| 83 | * @return $this |
||
| 84 | */ |
||
| 85 | public function withSecurityHeaders() |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Set header Content-Security-Policy. |
||
| 95 | * |
||
| 96 | * @param array $policies |
||
| 97 | * |
||
| 98 | * @return $this |
||
| 99 | */ |
||
| 100 | public function withContentSecurityPolicy(array $policies = []) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Set header X-Frame-Options. |
||
| 115 | * |
||
| 116 | * @return $this |
||
| 117 | */ |
||
| 118 | public function withXframe() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Set header Strict-Transport-Security (only if we use HTTPS). |
||
| 127 | * |
||
| 128 | * @return $this |
||
| 129 | */ |
||
| 130 | public function withStrictTransportSecurity() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Set HTTP response body. |
||
| 141 | * |
||
| 142 | * @param string $body |
||
| 143 | * |
||
| 144 | * @return $this |
||
| 145 | */ |
||
| 146 | public function withBody($body) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Send headers to cache a resource. |
||
| 155 | * |
||
| 156 | * @param int $duration |
||
| 157 | * @param string $etag |
||
| 158 | * |
||
| 159 | * @return $this |
||
| 160 | */ |
||
| 161 | public function withCache($duration, $etag = '') |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Send no cache headers. |
||
| 177 | * |
||
| 178 | * @return $this |
||
| 179 | */ |
||
| 180 | public function withoutCache() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Force the browser to download an attachment. |
||
| 190 | * |
||
| 191 | * @param string $filename |
||
| 192 | * |
||
| 193 | * @return $this |
||
| 194 | */ |
||
| 195 | public function withFileDownload($filename) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Send headers and body. |
||
| 206 | */ |
||
| 207 | public function send() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Send a custom HTTP status code. |
||
| 227 | * |
||
| 228 | * @param int $statusCode |
||
| 229 | */ |
||
| 230 | public function status($statusCode) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Redirect to another URL. |
||
| 238 | * |
||
| 239 | * @param string $url Redirection URL |
||
| 240 | * @param bool $self If Ajax request and true: refresh the current page |
||
| 241 | */ |
||
| 242 | public function redirect($url, $self = false) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Send a HTML response. |
||
| 255 | * |
||
| 256 | * @param string $data |
||
| 257 | * @param int $statusCode |
||
| 258 | */ |
||
| 259 | public function html($data, $statusCode = 200) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Send a text response. |
||
| 269 | * |
||
| 270 | * @param string $data |
||
| 271 | * @param int $statusCode |
||
| 272 | */ |
||
| 273 | public function text($data, $statusCode = 200) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Send a CSV response. |
||
| 283 | * |
||
| 284 | * @param array $data Data to serialize in csv |
||
| 285 | */ |
||
| 286 | public function csv(array $data) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Send a Json response. |
||
| 296 | * |
||
| 297 | * @param array $data Data to serialize in json |
||
| 298 | * @param int $statusCode HTTP status code |
||
| 299 | */ |
||
| 300 | View Code Duplication | public function json(array $data, $statusCode = 200) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Send a XML response. |
||
| 311 | * |
||
| 312 | * @param string $data |
||
| 313 | * @param int $statusCode |
||
| 314 | */ |
||
| 315 | public function xml($data, $statusCode = 200) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Send a javascript response. |
||
| 326 | * |
||
| 327 | * @param string $data |
||
| 328 | * @param int $statusCode |
||
| 329 | */ |
||
| 330 | public function js($data, $statusCode = 200) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Send a css response. |
||
| 340 | * |
||
| 341 | * @param string $data |
||
| 342 | * @param int $statusCode |
||
| 343 | */ |
||
| 344 | public function css($data, $statusCode = 200) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Send a binary response. |
||
| 354 | * |
||
| 355 | * @param string $data |
||
| 356 | * @param int $statusCode |
||
| 357 | */ |
||
| 358 | View Code Duplication | public function binary($data, $statusCode = 200) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Send a iCal response. |
||
| 370 | * |
||
| 371 | * @param string $data |
||
| 372 | * @param int $statusCode |
||
| 373 | */ |
||
| 374 | public function ical($data, $statusCode = 200) |
||
| 381 | } |
||
| 382 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.