HttpHelper::sendSimpleRequest()   F
last analyzed

Complexity

Conditions 10
Paths 512

Size

Total Lines 41
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 14.9309

Importance

Changes 0
Metric Value
dl 0
loc 41
c 0
b 0
f 0
ccs 19
cts 30
cp 0.6333
rs 3.2187
cc 10
eloc 26
nc 512
nop 12
crap 14.9309

How to fix   Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * Copyright (c) Padosoft.com 2016.
4
 */
5
6
namespace Padosoft\HTTPClient;
7
8
use Padosoft\HTTPClient\MethodHttpHelper;
9
10
/**
11
 * Class HttpHelper
12
 * HTTP Helper class
13
 * @package Padosoft\HTTPClient
14
 */
15
class HttpHelper
16
{
17
    /**
18
     * @var Response
19
     */
20
    protected $response;
21
    /**
22
     * @var HTTPClient
23
     */
24
    protected $httpclient;
25
    /**
26
     * @var \Padosoft\HTTPClient\RequestHelper
27
     */
28
    protected $requestHelper;
29
30
    /**
31
     * HttpHelper constructor.
32
     * @param HTTPClient $httpclient
33
     */
34 3
    public function __construct(\Padosoft\HTTPClient\HTTPClient $httpclient)
35
    {
36 3
        $this->httpclient = $httpclient;
37 3
        $this->requestHelper = $httpclient->requestHelper;
0 ignored issues
show
Documentation introduced by
The property $requestHelper is declared protected in Padosoft\HTTPClient\HTTPClient. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
38 3
        $this->response = new Response();
39 3
    }
40
41
    /**
42
     * Send HTTP Request
43
     *
44
     * @param $method
45
     * @param $uri
46
     * @param array $getParams
47
     * @param array $postParams
48
     * @param string $user
49
     * @param string $password
50
     * @param array $jsonParams
51
     * @param int $requesTimeout
52
     * @param bool $SSLVerify
53
     * @param array $customHeaders
54
     * @param string $accept
55
     * @param string $protocolVersion
56
     * @return Response
57
     */
58 3
    public function sendSimpleRequest($method, $uri
59
                                    , array $getParams=[], array $postParams=[]
60
                                    , $user="", $password=""
61
                                    , array $jsonParams=[]
62
                                    , $requesTimeout=0, $SSLVerify=true
63
                                    , array $customHeaders=[], $accept="", $protocolVersion=""
64
                                    )
65
    {
66
67 3
        if(!empty($getParams)) {
68
            $this->requestHelper->setQuery($getParams);
69
        }
70 3
        if(!empty($postParams)) {
71
            $this->requestHelper->setFormParams($postParams);
72
        }
73 3
        if(!empty($user)) {
74 3
            $this->requestHelper->setAuth($user,$password);
75 3
        }
76 3
        if(!empty($requesTimeout)) {
77
            $this->requestHelper->setTimeout($requesTimeout);
78
        }
79 3
        if(!empty($SSLVerify)) {
80 3
            $this->requestHelper->setVerify($SSLVerify);
81 3
        }
82 3
        if(!empty($accept)) {
83
            $this->requestHelper['accept'] = $accept;
84
        }
85 3
        if(!empty($customHeaders)) {
86
            $this->requestHelper->setHeaders($customHeaders);
87
        }
88 3
        if(!empty($protocolVersion)) {
89
            $this->requestHelper->setVerify($protocolVersion);
90 3
        }
91 3
        if(!empty($jsonParams)) {
92 3
            $this->requestHelper->setJson($jsonParams);
93 3
        }
94
95 3
        $this->response = $this->httpclient->sendRequest($method, $uri);
96
97 3
        return $this->response;
98
    }
99
100
101
102
    /**
103
     * @param $uri
104
     * @param array $getParams
105
     * @param array $postParams
106
     * @return Response
107
     */
108
    public function sendGet($uri, array $getParams=[], array $postParams=[])
109
    {
110
        $this->response =$this->sendSimpleRequest(MethodHttpHelper::GET ,$uri ,$getParams,$postParams,"" ,"",[],0, true, [],  "", "" );
111
        return $this->response;
112
    }
113
114
    /**
115
     * @param $uri
116
     * @param array $getParams
117
     * @param array $postParams
118
     * @param $user
119
     * @param $password
120
     * @return Response
121
     */
122
    public function sendGetWithAuth($uri, array $getParams=[], array $postParams=[], $user, $password)
123
    {
124
        $this->response = $this->sendSimpleRequest(MethodHttpHelper::GET ,$uri ,$getParams,$postParams,$user ,$password,[],0, true, [],  "", "" );
125
        return $this->response;
126
    }
127
128
    /**
129
     * @param $uri
130
     * @param array $getParams
131
     * @param array $postParams
132
     * @return Response
133
     */
134
    public function sendPost($uri, array $getParams=[], array $postParams=[])
135
    {
136
        $this->response = $this->sendSimpleRequest(MethodHttpHelper::POST ,$uri ,$getParams,$postParams,"" ,"",[],0, true, [],  "", "" );
137
        return $this->response;
138
    }
139
140
    /**
141
     * @param $uri
142
     * @param array $getParams
143
     * @param array $postParams
144
     * @param $user
145
     * @param $password
146
     * @return Response
147
     */
148
    public function sendPostWithAuth($uri, array $getParams=[], array $postParams=[], $user, $password)
149
    {
150
        $this->response = $this->sendSimpleRequest(MethodHttpHelper::POST ,$uri ,$getParams,$postParams,$user ,$password,[],0, true, [],  "", "" );
151
        return $this->response;
152
    }
153
154
    /**
155
     * @param $uri
156
     * @param array $jsonParams
157
     * @param $user
158
     * @param $password
159 3
     * @return Response
160
     */
161 3
    public function sendPostJsonWithAuth($uri, array $jsonParams=[], $user, $password)
162 3
    {
163
        $this->response = $this->sendSimpleRequest(MethodHttpHelper::POST ,$uri ,[],[],$user ,$password,$jsonParams,0, true, [],  "", "" );
164
        return $this->response;
165
    }
166
167
}
168