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 |
||
| 43 | class Response { |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Headers - defaults to ['Cache-Control' => 'no-cache, no-store, must-revalidate'] |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | private $headers = array( |
||
| 50 | 'Cache-Control' => 'no-cache, no-store, must-revalidate' |
||
| 51 | ); |
||
| 52 | |||
| 53 | |||
| 54 | /** |
||
| 55 | * Cookies that will be need to be constructed as header |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | private $cookies = array(); |
||
| 59 | |||
| 60 | |||
| 61 | /** |
||
| 62 | * HTTP status code - defaults to STATUS OK |
||
| 63 | * @var int |
||
| 64 | */ |
||
| 65 | private $status = Http::STATUS_OK; |
||
| 66 | |||
| 67 | |||
| 68 | /** |
||
| 69 | * Last modified date |
||
| 70 | * @var \DateTime |
||
| 71 | */ |
||
| 72 | private $lastModified; |
||
| 73 | |||
| 74 | |||
| 75 | /** |
||
| 76 | * ETag |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | private $ETag; |
||
| 80 | |||
| 81 | /** @var ContentSecurityPolicy|null Used Content-Security-Policy */ |
||
| 82 | private $contentSecurityPolicy = null; |
||
| 83 | |||
| 84 | /** @var bool */ |
||
| 85 | private $throttled = false; |
||
| 86 | /** @var array */ |
||
| 87 | private $throttleMetadata = []; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Caches the response |
||
| 91 | * @param int $cacheSeconds the amount of seconds that should be cached |
||
| 92 | * if 0 then caching will be disabled |
||
| 93 | * @return $this |
||
| 94 | * @since 6.0.0 - return value was added in 7.0.0 |
||
| 95 | */ |
||
| 96 | public function cacheFor($cacheSeconds) { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Adds a new cookie to the response |
||
| 109 | * @param string $name The name of the cookie |
||
| 110 | * @param string $value The value of the cookie |
||
| 111 | * @param \DateTime|null $expireDate Date on that the cookie should expire, if set |
||
| 112 | * to null cookie will be considered as session |
||
| 113 | * cookie. |
||
| 114 | * @return $this |
||
| 115 | * @since 8.0.0 |
||
| 116 | */ |
||
| 117 | public function addCookie($name, $value, \DateTime $expireDate = null) { |
||
| 121 | |||
| 122 | |||
| 123 | /** |
||
| 124 | * Set the specified cookies |
||
| 125 | * @param array $cookies array('foo' => array('value' => 'bar', 'expire' => null)) |
||
| 126 | * @return $this |
||
| 127 | * @since 8.0.0 |
||
| 128 | */ |
||
| 129 | public function setCookies(array $cookies) { |
||
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * Invalidates the specified cookie |
||
| 137 | * @param string $name |
||
| 138 | * @return $this |
||
| 139 | * @since 8.0.0 |
||
| 140 | */ |
||
| 141 | public function invalidateCookie($name) { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Invalidates the specified cookies |
||
| 148 | * @param array $cookieNames array('foo', 'bar') |
||
| 149 | * @return $this |
||
| 150 | * @since 8.0.0 |
||
| 151 | */ |
||
| 152 | public function invalidateCookies(array $cookieNames) { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Returns the cookies |
||
| 161 | * @return array |
||
| 162 | * @since 8.0.0 |
||
| 163 | */ |
||
| 164 | public function getCookies() { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Adds a new header to the response that will be called before the render |
||
| 170 | * function |
||
| 171 | * @param string $name The name of the HTTP header |
||
| 172 | * @param string $value The value, null will delete it |
||
| 173 | * @return $this |
||
| 174 | * @since 6.0.0 - return value was added in 7.0.0 |
||
| 175 | */ |
||
| 176 | View Code Duplication | public function addHeader($name, $value) { |
|
| 189 | |||
| 190 | |||
| 191 | /** |
||
| 192 | * Set the headers |
||
| 193 | * @param array $headers value header pairs |
||
| 194 | * @return $this |
||
| 195 | * @since 8.0.0 |
||
| 196 | */ |
||
| 197 | public function setHeaders(array $headers) { |
||
| 202 | |||
| 203 | |||
| 204 | /** |
||
| 205 | * Returns the set headers |
||
| 206 | * @return array the headers |
||
| 207 | * @since 6.0.0 |
||
| 208 | */ |
||
| 209 | public function getHeaders() { |
||
| 229 | |||
| 230 | |||
| 231 | /** |
||
| 232 | * By default renders no output |
||
| 233 | * @return string|null |
||
| 234 | * @since 6.0.0 |
||
| 235 | */ |
||
| 236 | public function render() { |
||
| 239 | |||
| 240 | |||
| 241 | /** |
||
| 242 | * Set response status |
||
| 243 | * @param int $status a HTTP status code, see also the STATUS constants |
||
| 244 | * @return Response Reference to this object |
||
| 245 | * @since 6.0.0 - return value was added in 7.0.0 |
||
| 246 | */ |
||
| 247 | public function setStatus($status) { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Set a Content-Security-Policy |
||
| 255 | * @param EmptyContentSecurityPolicy $csp Policy to set for the response object |
||
| 256 | * @return $this |
||
| 257 | * @since 8.1.0 |
||
| 258 | */ |
||
| 259 | public function setContentSecurityPolicy(EmptyContentSecurityPolicy $csp) { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Get the currently used Content-Security-Policy |
||
| 266 | * @return EmptyContentSecurityPolicy|null Used Content-Security-Policy or null if |
||
| 267 | * none specified. |
||
| 268 | * @since 8.1.0 |
||
| 269 | */ |
||
| 270 | public function getContentSecurityPolicy() { |
||
| 273 | |||
| 274 | |||
| 275 | /** |
||
| 276 | * Get response status |
||
| 277 | * @since 6.0.0 |
||
| 278 | */ |
||
| 279 | public function getStatus() { |
||
| 282 | |||
| 283 | |||
| 284 | /** |
||
| 285 | * Get the ETag |
||
| 286 | * @return string the etag |
||
| 287 | * @since 6.0.0 |
||
| 288 | */ |
||
| 289 | public function getETag() { |
||
| 292 | |||
| 293 | |||
| 294 | /** |
||
| 295 | * Get "last modified" date |
||
| 296 | * @return \DateTime RFC2822 formatted last modified date |
||
| 297 | * @since 6.0.0 |
||
| 298 | */ |
||
| 299 | public function getLastModified() { |
||
| 302 | |||
| 303 | |||
| 304 | /** |
||
| 305 | * Set the ETag |
||
| 306 | * @param string $ETag |
||
| 307 | * @return Response Reference to this object |
||
| 308 | * @since 6.0.0 - return value was added in 7.0.0 |
||
| 309 | */ |
||
| 310 | public function setETag($ETag) { |
||
| 315 | |||
| 316 | |||
| 317 | /** |
||
| 318 | * Set "last modified" date |
||
| 319 | * @param \DateTime $lastModified |
||
| 320 | * @return Response Reference to this object |
||
| 321 | * @since 6.0.0 - return value was added in 7.0.0 |
||
| 322 | */ |
||
| 323 | public function setLastModified($lastModified) { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Marks the response as to throttle. Will be throttled when the |
||
| 331 | * @BruteForceProtection annotation is added. |
||
| 332 | * |
||
| 333 | * @param array $metadata |
||
| 334 | * @since 12.0.0 |
||
| 335 | */ |
||
| 336 | public function throttle(array $metadata = []) { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Returns the throttle metadata, defaults to empty array |
||
| 343 | * |
||
| 344 | * @return array |
||
| 345 | * @since 13.0.0 |
||
| 346 | */ |
||
| 347 | public function getThrottleMetadata() { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Whether the current response is throttled. |
||
| 353 | * |
||
| 354 | * @since 12.0.0 |
||
| 355 | */ |
||
| 356 | public function isThrottled() { |
||
| 359 | } |
||
| 360 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..