Conditions | 21 |
Paths | 5122 |
Total Lines | 114 |
Lines | 4 |
Ratio | 3.51 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
80 | public function request($method, UriInterface $uri, $data = null, array $headers = null, $timeout = null, $userAgent = null) |
||
81 | { |
||
82 | $connection = $this->connect($uri, $timeout); |
||
83 | |||
84 | // Make sure the connection is alive and valid. |
||
85 | if (is_resource($connection)) |
||
86 | { |
||
87 | // Make sure the connection has not timed out. |
||
88 | $meta = stream_get_meta_data($connection); |
||
89 | |||
90 | if ($meta['timed_out']) |
||
91 | { |
||
92 | throw new \RuntimeException('Server connection timed out.'); |
||
93 | } |
||
94 | } |
||
95 | else |
||
96 | { |
||
97 | throw new \RuntimeException('Not connected to server.'); |
||
98 | } |
||
99 | |||
100 | // Get the request path from the URI object. |
||
101 | $path = $uri->toString(array('path', 'query')); |
||
102 | |||
103 | // If we have data to send make sure our request is setup for it. |
||
104 | if (!empty($data)) |
||
105 | { |
||
106 | // If the data is not a scalar value encode it to be sent with the request. |
||
107 | if (!is_scalar($data)) |
||
108 | { |
||
109 | $data = http_build_query($data); |
||
110 | } |
||
111 | |||
112 | if (!isset($headers['Content-Type'])) |
||
113 | { |
||
114 | $headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8'; |
||
115 | } |
||
116 | |||
117 | // Add the relevant headers. |
||
118 | $headers['Content-Length'] = strlen($data); |
||
119 | } |
||
120 | |||
121 | // Configure protocol version, use transport's default if not set otherwise. |
||
122 | $protocolVersion = isset($this->options['protocolVersion']) ? $this->options['protocolVersion'] : '1.0'; |
||
123 | |||
124 | // Build the request payload. |
||
125 | $request = array(); |
||
126 | $request[] = strtoupper($method) . ' ' . ((empty($path)) ? '/' : $path) . ' HTTP/' . $protocolVersion; |
||
127 | $request[] = 'Host: ' . $uri->getHost(); |
||
128 | |||
129 | // If an explicit user agent is given use it. |
||
130 | if (isset($userAgent)) |
||
131 | { |
||
132 | $headers['User-Agent'] = $userAgent; |
||
133 | } |
||
134 | |||
135 | // If we have a username then we include basic authentication credentials. |
||
136 | if ($uri->getUser()) |
||
137 | { |
||
138 | $authString = $uri->getUser() . ':' . $uri->getPass(); |
||
139 | $headers['Authorization'] = 'Basic ' . base64_encode($authString); |
||
140 | } |
||
141 | |||
142 | // If there are custom headers to send add them to the request payload. |
||
143 | if (is_array($headers)) |
||
144 | { |
||
145 | foreach ($headers as $k => $v) |
||
146 | { |
||
147 | $request[] = $k . ': ' . $v; |
||
148 | } |
||
149 | } |
||
150 | |||
151 | // Authentication, if needed |
||
152 | View Code Duplication | if (isset($this->options['userauth']) && isset($this->options['passwordauth'])) |
|
|
|||
153 | { |
||
154 | $request[] = 'Authorization: Basic ' . base64_encode($this->options['userauth'] . ':' . $this->options['passwordauth']); |
||
155 | } |
||
156 | |||
157 | // Set any custom transport options |
||
158 | if (isset($this->options['transport.socket'])) |
||
159 | { |
||
160 | foreach ($this->options['transport.socket'] as $value) |
||
161 | { |
||
162 | $request[] = $value; |
||
163 | } |
||
164 | } |
||
165 | |||
166 | // If we have data to send add it to the request payload. |
||
167 | if (!empty($data)) |
||
168 | { |
||
169 | $request[] = null; |
||
170 | $request[] = $data; |
||
171 | } |
||
172 | |||
173 | // Send the request to the server. |
||
174 | fwrite($connection, implode("\r\n", $request) . "\r\n\r\n"); |
||
175 | |||
176 | // Get the response data from the server. |
||
177 | $content = ''; |
||
178 | |||
179 | while (!feof($connection)) |
||
180 | { |
||
181 | $content .= fgets($connection, 4096); |
||
182 | } |
||
183 | |||
184 | $content = $this->getResponse($content); |
||
185 | |||
186 | // Follow Http redirects |
||
187 | if ($content->code >= 301 && $content->code < 400 && isset($content->headers['Location'])) |
||
188 | { |
||
189 | return $this->request($method, new Uri($content->headers['Location']), $data, $headers, $timeout, $userAgent); |
||
190 | } |
||
191 | |||
192 | return $content; |
||
193 | } |
||
194 | |||
359 |
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.