Passed
Push — master ( 5731b9...6612be )
by Jared
02:33
created

JCRequest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 37.84 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 28
loc 74
c 0
b 0
f 0
wmc 8
lcom 1
cbo 4
ccs 31
cts 31
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A request() 0 4 1
A get() 0 6 1
A post() 7 7 1
A put() 7 7 1
A patch() 7 7 1
A delete() 7 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
        return new JCResponse((new Client())->request($method, $url, $options));
21
    }
22
23
    /**
24
     * @param $url
25
     * @param array $headers
26
     * @param array $params
27
     * @param array $options
28
     * @return JCResponse
29
     */
30 1
    public static function get($url, $params = [], $headers = [], $options = [])
31
    {
32 1
        return static::request(Method::GET, static::manipulateUrl($url, $params), [
33
            'headers' => $headers
34 1
        ]);
35
    }
36
37 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...
38
    {
39 1
        return static::request(Method::POST, $url, [
40 1
            'headers' => $headers,
41
            'form_params' => $params
42 1
        ]);
43
    }
44
45 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...
46
    {
47 1
        return static::request(Method::PUT, $url, [
48 1
            'headers' => $headers,
49
            'form_params' => $params
50 1
        ]);
51
    }
52
53 1 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...
54
    {
55 1
        return static::request(Method::PATCH, $url, [
56 1
            'headers' => $headers,
57
            'form_params' => $params
58 1
        ]);
59
    }
60
61 1 View Code Duplication
    public static function delete($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...
62
    {
63 1
        return static::request(Method::DELETE, $url, [
64 1
            'headers' => $headers,
65 1
            'form_params' => $params
66 1
        ]);
67
    }
68
69 1
    public static function head($url, $params = [], $headers = [], $options = [])
70
    {
71 1
        return static::request(Method::HEAD, $url, [
72 1
            'headers' => $headers,
73 1
        ]);
74
    }
75
76
    /**
77
     * @param string $url
78
     * @param array $params
79
     * @return string
80
     */
81 1
    protected static function manipulateUrl($url, $params = [])
82
    {
83 1
        $urlObject = Url::parse($url);
84 1
        $queryData = array_merge($urlObject->query->getData(), $params);
85 1
        $urlObject->query->setData($queryData);
86
87 1
        return $urlObject->getUrl();
88
    }
89
}