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 |
||
| 19 | abstract class AbstractNode implements NodeInterface |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * urlencoded content type |
||
| 23 | * @const array URLENCODED_CONTENT_TYPE |
||
| 24 | */ |
||
| 25 | const URLENCODED_CONTENT_TYPE = [ |
||
| 26 | 'Content-Type' => 'application/x-www-form-urlencoded', |
||
| 27 | ]; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * json content type |
||
| 31 | * @const array JSON_CONTENT_TYPE |
||
| 32 | */ |
||
| 33 | const JSON_CONTENT_TYPE = [ |
||
| 34 | 'Content-Type' => 'application/json', |
||
| 35 | ]; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * client |
||
| 39 | * @var \Unikorp\KongAdminApi\Client $client |
||
| 40 | */ |
||
| 41 | private $client = null; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Constructor |
||
| 45 | * |
||
| 46 | * @param \Unikorp\KongAdminApi\Client $client |
||
| 47 | */ |
||
| 48 | 9 | public function __construct(Client $client) |
|
| 52 | |||
| 53 | /** |
||
| 54 | * get |
||
| 55 | * |
||
| 56 | * @param string $endpoint |
||
| 57 | * @param DocumentInterface $document |
||
| 58 | * |
||
| 59 | * @return \Psr\Http\Message\ResponseInterface |
||
| 60 | */ |
||
| 61 | 20 | protected function get($endpoint, DocumentInterface $document = null) |
|
| 68 | |||
| 69 | /** |
||
| 70 | * post |
||
| 71 | * |
||
| 72 | * @param string $endpoint |
||
| 73 | * @param DocumentInterface $document |
||
| 74 | * |
||
| 75 | * @return \Psr\Http\Message\ResponseInterface |
||
| 76 | */ |
||
| 77 | 8 | View Code Duplication | protected function post($endpoint, DocumentInterface $document = null) |
| 85 | |||
| 86 | /** |
||
| 87 | * put |
||
| 88 | * |
||
| 89 | * @param string $endpoint |
||
| 90 | * @param DocumentInterface $document |
||
| 91 | * |
||
| 92 | * @return \Psr\Http\Message\ResponseInterface |
||
| 93 | */ |
||
| 94 | 6 | View Code Duplication | protected function put($endpoint, DocumentInterface $document = null) |
| 104 | |||
| 105 | /** |
||
| 106 | * patch |
||
| 107 | * |
||
| 108 | * @param string $endpoint |
||
| 109 | * @param DocumentInterface $document |
||
| 110 | * |
||
| 111 | * @return \Psr\Http\Message\ResponseInterface |
||
| 112 | */ |
||
| 113 | 6 | View Code Duplication | protected function patch($endpoint, DocumentInterface $document = null) |
| 121 | |||
| 122 | /** |
||
| 123 | * delete |
||
| 124 | * |
||
| 125 | * @param string $endpoint |
||
| 126 | * @param DocumentInterface $document |
||
| 127 | * |
||
| 128 | * @return \Psr\Http\Message\ResponseInterface |
||
| 129 | */ |
||
| 130 | 8 | View Code Duplication | protected function delete($endpoint, DocumentInterface $document = null) |
| 138 | } |
||
| 139 |