Completed
Push — master ( f5580e...4e6c29 )
by Beñat
01:39
created

GuzzleHttpClient   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 39.13 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 18
loc 46
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A get() 9 9 2
A post() 0 4 1
A put() 0 4 1
A delete() 0 4 1
A internalPost() 9 9 2

How to fix   Duplicated Code   

Duplicated Code

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
2
3
/*
4
 * This file is part of the Stack Exchange Api Client library.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace BenatEspina\StackExchangeApiClient\Http;
15
16
use GuzzleHttp\Client;
17
use Psr\Http\Message\ResponseInterface;
18
19
/**
20
 * @author Beñat Espiña <[email protected]>
21
 */
22
final class GuzzleHttpClient implements HttpClient
23
{
24
    private $client;
25
26
    public function __construct()
27
    {
28
        $this->client = new Client([
29
            'base_uri' => HttpClient::BASE_URL,
30
        ]);
31
    }
32
33 View Code Duplication
    public function get(string $url, array $parameters)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35
        $response = $this->client->get($url, ['query' => $parameters]);
36
        if (!$response instanceof ResponseInterface) {
37
            throw new \Exception('The response must be implements the ResponseInterface');
38
        }
39
40
        return json_decode($response->getBody()->getContents(), true);
41
    }
42
43
    public function post(string $url, array $parameters)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
44
    {
45
        return $this->internalPost($url, $parameters);
46
    }
47
48
    public function put(string $url, array $parameters)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
49
    {
50
        return $this->internalPost($url, $parameters);
51
    }
52
53
    public function delete(string $url, array $parameters)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
54
    {
55
        return $this->internalPost($url, $parameters);
56
    }
57
58 View Code Duplication
    private function internalPost(string $url, array $parameters)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
        $response = $this->client->post($url, ['form_params' => $parameters]);
61
        if (!$response instanceof ResponseInterface) {
62
            throw new \Exception('The response must be implements the ResponseInterface');
63
        }
64
65
        return json_decode($response->getBody()->getContents(), true);
66
    }
67
}
68