HttpClient   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 49
Duplicated Lines 26.53 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 13
loc 49
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 13 13 1
A request() 0 8 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 PHP Generics package.
5
 *
6
 * @package Generics
7
 */
8
namespace Generics\Client;
9
10
use Generics\Socket\ClientSocket;
11
use Generics\Socket\Url;
12
use Generics\Streams\HttpStream;
13
14
/**
15
 * This class implements a HttpStream as client
16
 *
17
 * @author Maik Greubel <[email protected]>
18
 */
19
class HttpClient extends ClientSocket implements HttpStream
20
{
21
    use HttpClientTrait;
22
    
23
    /**
24
     * Whether to use https instead of http
25
     *
26
     * @var boolean
27
     */
28
    private $secure;
29
30
    /**
31
     * Create a new http client
32
     *
33
     * @param Url $url
34
     *            The url for http request
35
     * @param string $proto
36
     *            The protocol to use (default = HTTP/1.1)
37
     * @param integer $timeout
38
     *            Optional timeout for request (default = 10 seconds)
39
     */
40 13 View Code Duplication
    public function __construct(Url $url, $proto = 'HTTP/1.1', $timeout = 10)
41
    {
42 13
        parent::__construct($url);
43
        
44 13
        $this->secure = $url->getScheme() == 'https';
45
46 13
        $this->setTimeout($timeout);
47 13
        $this->setPath($url->getPath());
48 13
        $this->setProtocol($proto);
49 13
        $this->setQueryString($url->getQueryString());
50 13
        $this->reset();
51 13
        $this->resetHeaders();
52 13
    }
53
54
    /**
55
     *
56
     * {@inheritdoc}
57
     * @see \Generics\Streams\HttpStream::request()
58
     */
59 12
    public function request(string $requestType)
60
    {
61 12
        if ($this->secure) {
62 1
            throw new HttpException("Secure connection using HTTPs is not supported!");
63
        }
64
        
65 11
        $this->requestImpl($requestType);
66 9
    }
67
}
68