Completed
Push — master ( 6612be...325c35 )
by Jared
02:19
created

JCRequest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 83
Duplicated Lines 25.3 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 84.21%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 21
loc 83
ccs 32
cts 38
cp 0.8421
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A request() 0 13 4
A get() 0 6 1
A post() 7 7 1
A put() 7 7 1
A patch() 7 7 1
A delete() 0 7 1
A head() 0 6 1
A manipulateUrl() 0 8 1

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
 * Created by PhpStorm.
4
 * User: jaredchu
5
 * Date: 31/05/2017
6
 * Time: 09:40
7
 */
8
9
namespace JC;
10
11
12
use GuzzleHttp\Client;
13
use Purl\Url;
14
use JC\Enums\Method;
15
16
class JCRequest implements iJCRequest
17
{
18 6
    public static function request($method, $url, $options)
19
    {
20 6
        if (isset($options['headers']['Content-Type']) &&
21
            strpos($options['headers']['Content-Type'], 'application/json') !== false
22 6
        ) {
23
            if (isset($options['form_params'])) {
24
                $options['json'] = $options['form_params'];
25
                unset($options['form_params']);
26
            }
27
        }
28
29 6
        return new JCResponse((new Client())->request($method, $url, $options));
30
    }
31
32
    /**
33
     * @param $url
34
     * @param array $headers
35
     * @param array $params
36
     * @param array $options
37
     * @return JCResponse
38
     */
39 1
    public static function get($url, $params = [], $headers = [], $options = [])
40
    {
41 1
        return static::request(Method::GET, static::manipulateUrl($url, $params), [
42
            'headers' => $headers
43 1
        ]);
44
    }
45
46 1 View Code Duplication
    public static function post($url, $params = [], $headers = [], $options = [])
0 ignored issues
show
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...
47
    {
48 1
        return static::request(Method::POST, $url, [
49 1
            'headers' => $headers,
50
            'form_params' => $params
51 1
        ]);
52
    }
53
54 1 View Code Duplication
    public static function put($url, $params = [], $headers = [], $options = [])
0 ignored issues
show
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...
55
    {
56 1
        return static::request(Method::PUT, $url, [
57 1
            'headers' => $headers,
58
            'form_params' => $params
59 1
        ]);
60
    }
61
62 2 View Code Duplication
    public static function patch($url, $params = [], $headers = [], $options = [])
0 ignored issues
show
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...
63
    {
64 1
        return static::request(Method::PATCH, $url, [
65 2
            'headers' => $headers,
66
            'form_params' => $params
67 1
        ]);
68
    }
69
70 1
    public static function delete($url, $params = [], $headers = [], $options = [])
71
    {
72 1
        return static::request(Method::DELETE, $url, [
73 1
            'headers' => $headers,
74
            'form_params' => $params
75 1
        ]);
76
    }
77
78 1
    public static function head($url, $params = [], $headers = [], $options = [])
79
    {
80 1
        return static::request(Method::HEAD, $url, [
81 1
            'headers' => $headers,
82 1
        ]);
83
    }
84
85
    /**
86
     * @param string $url
87
     * @param array $params
88
     * @return string
89
     */
90 1
    protected static function manipulateUrl($url, $params = [])
91
    {
92 1
        $urlObject = Url::parse($url);
93 1
        $queryData = array_merge($urlObject->query->getData(), $params);
94 1
        $urlObject->query->setData($queryData);
95
96 1
        return $urlObject->getUrl();
97
    }
98
}