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 |
||
| 7 | class CsrfTokenReplacer implements Replacer |
||
| 8 | { |
||
| 9 | protected string $replacementString = '<laravel-responsecache-csrf-token-here>'; |
||
|
|
|||
| 10 | |||
| 11 | public function prepareResponseToCache(Response $response): void |
||
| 12 | { |
||
| 13 | if (! $response->getContent()) { |
||
| 14 | return; |
||
| 15 | } |
||
| 16 | |||
| 17 | $response->setContent(str_replace( |
||
| 18 | csrf_token(), |
||
| 19 | $this->replacementString, |
||
| 20 | $response->getContent() |
||
| 21 | )); |
||
| 22 | } |
||
| 23 | |||
| 24 | public function replaceInCachedResponse(Response $response): void |
||
| 25 | { |
||
| 26 | if (! $response->getContent()) { |
||
| 27 | return; |
||
| 28 | } |
||
| 29 | |||
| 30 | $response->setContent(str_replace( |
||
| 31 | $this->replacementString, |
||
| 32 | csrf_token(), |
||
| 33 | $response->getContent() |
||
| 34 | )); |
||
| 35 | } |
||
| 36 | } |
||
| 37 |