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  | 
            ||
| 16 | trait Rest  | 
            ||
| 17 | { | 
            ||
| 18 | /** @var array */  | 
            ||
| 19 | protected $headers = [];  | 
            ||
| 20 | |||
| 21 | /** @var string */  | 
            ||
| 22 | protected $apiUrl;  | 
            ||
| 23 | |||
| 24 | /** @var string */  | 
            ||
| 25 | protected $apiVersion = '1.0';  | 
            ||
| 26 | |||
| 27 | /**  | 
            ||
| 28 | * @param string $apiUrl  | 
            ||
| 29 | * @return self  | 
            ||
| 30 | */  | 
            ||
| 31 | public function setApiUrl(string $apiUrl)  | 
            ||
| 36 | |||
| 37 | /**  | 
            ||
| 38 | * @param string $apiVersion  | 
            ||
| 39 | * @return self  | 
            ||
| 40 | */  | 
            ||
| 41 | public function setApiVersion(string $apiVersion)  | 
            ||
| 46 | |||
| 47 | /**  | 
            ||
| 48 | * @param array $header  | 
            ||
| 49 | * @return void  | 
            ||
| 50 | */  | 
            ||
| 51 | public function setHeaders($header)  | 
            ||
| 55 | |||
| 56 | /**  | 
            ||
| 57 | * @param string $endpoint  | 
            ||
| 58 | * @return bool  | 
            ||
| 59 | */  | 
            ||
| 60 | private function requireCorrelationId(string $endpoint)  | 
            ||
| 69 | |||
| 70 | /**  | 
            ||
| 71 | * @param string $endpoint  | 
            ||
| 72 | * @param array|string|null $query  | 
            ||
| 73 | * @param string|null $correlationId  | 
            ||
| 74 | * @return array|mixed  | 
            ||
| 75 | * @throws RequestException  | 
            ||
| 76 | */  | 
            ||
| 77 | public function get(string $endpoint, $query = null, $correlationId = null)  | 
            ||
| 95 | |||
| 96 | /**  | 
            ||
| 97 | * @param string $endpoint  | 
            ||
| 98 | * @param array $body  | 
            ||
| 99 | * @param string|null $correlationId  | 
            ||
| 100 | * @param bool $asJson  | 
            ||
| 101 | * @return array|mixed  | 
            ||
| 102 | * @throws RequestException  | 
            ||
| 103 | */  | 
            ||
| 104 | View Code Duplication | private function post(string $endpoint, array $body = [], $correlationId = null, bool $asJson = false)  | 
            |
| 125 | |||
| 126 | /**  | 
            ||
| 127 | * @param string $endpoint  | 
            ||
| 128 | * @param array $body  | 
            ||
| 129 | * @param string|null $correlationId  | 
            ||
| 130 | * @param bool $asJson  | 
            ||
| 131 | * @param bool $attachment  | 
            ||
| 132 | * @param DocumentAnalysis $document  | 
            ||
| 133 | * @return array|mixed  | 
            ||
| 134 | * @throws RequestException  | 
            ||
| 135 | */  | 
            ||
| 136 | private function put(  | 
            ||
| 137 | string $endpoint,  | 
            ||
| 138 | array $body = [],  | 
            ||
| 139 | $correlationId = null,  | 
            ||
| 140 | bool $asJson = false,  | 
            ||
| 141 | bool $attachment = false,  | 
            ||
| 142 | DocumentAnalysis $document = null  | 
            ||
| 143 |     ) { | 
            ||
| 144 |         if (is_null($correlationId) && $this->requireCorrelationId($endpoint)) { | 
            ||
| 145 | $correlationId = Uuid::uuid4()->toString();  | 
            ||
| 146 | }  | 
            ||
| 147 | |||
| 148 | $this->setHeaders([  | 
            ||
| 149 | 'API-Version' => $this->apiVersion,  | 
            ||
| 150 | 'x-correlation-id' => $correlationId  | 
            ||
| 151 | ]);  | 
            ||
| 152 | |||
| 153 | $bodyFormat = $asJson ? 'json' : 'form_params';  | 
            ||
| 154 | $token = Auth::login()->getToken();  | 
            ||
| 155 | |||
| 156 | $request = Http::withToken($token)  | 
            ||
| 157 | ->withHeaders($this->headers)  | 
            ||
| 158 | ->bodyFormat($bodyFormat);  | 
            ||
| 159 | |||
| 160 |         if ($attachment && !is_null($document)) { | 
            ||
| 161 | $request->attach($document->getFieldName(), $document->getFileContents(), $document->getFileName());  | 
            ||
| 162 | }  | 
            ||
| 163 | |||
| 164 | return $request->put($this->getFinalUrl($endpoint), $body)  | 
            ||
| 165 | ->throw()  | 
            ||
| 166 | ->json();  | 
            ||
| 167 | }  | 
            ||
| 168 | |||
| 169 | /**  | 
            ||
| 170 | * @param string $endpoint  | 
            ||
| 171 | * @param array $body  | 
            ||
| 172 | * @param string|null $correlationId  | 
            ||
| 173 | * @param bool $asJson  | 
            ||
| 174 | * @return array|mixed  | 
            ||
| 175 | * @throws RequestException  | 
            ||
| 176 | */  | 
            ||
| 177 | View Code Duplication | private function patch(  | 
            |
| 203 | |||
| 204 | /**  | 
            ||
| 205 | * Http delete method.  | 
            ||
| 206 | *  | 
            ||
| 207 | * @param string $endpoint  | 
            ||
| 208 | * @return array|mixed  | 
            ||
| 209 | * @throws RequestException  | 
            ||
| 210 | */  | 
            ||
| 211 | private function delete(string $endpoint)  | 
            ||
| 221 | |||
| 222 | /**  | 
            ||
| 223 | * @param string $endpoint  | 
            ||
| 224 | * @return string  | 
            ||
| 225 | */  | 
            ||
| 226 | private function getFinalUrl(string $endpoint)  | 
            ||
| 234 | }  | 
            ||
| 235 |