Completed
Push — master ( f4889a...d7a000 )
by Andrii
05:14
created

Request   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 97
Duplicated Lines 15.46 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 15
loc 97
ccs 0
cts 62
cp 0
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A send() 0 16 2
A prepareContextOptions() 0 19 2
A convertContextOptions() 0 15 3
A composeHeaderLines() 15 15 4
A isSupported() 0 4 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
 * ActiveRecord for API
4
 *
5
 * @link      https://github.com/hiqdev/yii2-hiart
6
 * @package   yii2-hiart
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\hiart\stream;
12
13
use hiqdev\hiart\AbstractRequest;
14
use hiqdev\hiart\RequestErrorException;
15
use yii\helpers\ArrayHelper;
16
use yii\helpers\Inflector;
17
18
/**
19
 * PHP stream request implementation.
20
 *
21
 * @author Andrii Vasyliev <[email protected]>
22
 */
23
class Request extends AbstractRequest
24
{
25
    protected $responseClass = Response::class;
26
27
    public $defaultOptions = [
28
        'http' => [
29
            'ignore_errors' => true,
30
        ],
31
        'ssl' => [
32
            'verify_peer' => false,
33
        ],
34
    ];
35
36
    /**
37
     * @param array $options
38
     * @throws RequestErrorException
39
     * @return mixed
40
     */
41
    public function send($options = [])
42
    {
43
        try {
44
            $this->build();
45
            $context = stream_context_create($this->prepareContextOptions($options));
46
            $stream = fopen($this->getFullUri(), 'rb', false, $context);
47
            $responseContent = stream_get_contents($stream);
48
            // see http://php.net/manual/en/reserved.variables.httpresponseheader.php
49
            $responseHeaders = $http_response_header;
50
            fclose($stream);
51
        } catch (\Exception $e) {
52
            throw new RequestErrorException($e->getMessage(), $this, $e->getCode(), $e);
53
        }
54
55
        return new $this->responseClass($this, $responseContent, $responseHeaders);
56
    }
57
58
    protected function prepareContextOptions($options)
59
    {
60
        $requestOptions = [
61
            'http' => [
62
                'protocol_version' => $this->version,
63
                'method' => $this->method,
64
                'header' => static::composeHeaderLines($this->headers),
65
            ],
66
        ];
67
68
        if (isset($this->body)) {
69
            $requestOptions['http']['content'] = $this->body;
70
        }
71
72
        $dbOptions = $this->convertContextOptions($this->getDb()->config);
0 ignored issues
show
Bug introduced by
Accessing config on the interface hiqdev\hiart\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
73
        $sendOptions = $this->convertContextOptions($options);
74
75
        return ArrayHelper::merge($this->defaultOptions, $dbOptions, $requestOptions, $sendOptions);
76
    }
77
78
    /**
79
     * Composes stream context options from raw options.
80
     * @param array $options raw options
81
     * @return array stream context options
82
     */
83
    protected function convertContextOptions(array $options)
84
    {
85
        $contextOptions = [];
86
        foreach ($options as $key => $value) {
87
            $section = 'http';
88
            if (strpos($key, 'ssl') === 0) {
89
                $section = 'ssl';
90
                $key = substr($key, 3);
91
            }
92
            $key = Inflector::underscore($key);
93
            $contextOptions[$section][$key] = $value;
94
        }
95
96
        return $contextOptions;
97
    }
98
99 View Code Duplication
    public static function composeHeaderLines($headers)
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...
100
    {
101
        $result = [];
102
        foreach ($headers as $name => $values) {
103
            $name = str_replace(' ', '-', ucwords(str_replace('-', ' ', $name)));
104
            if (is_string($values)) {
105
                $values = [$values];
106
            }
107
            foreach ($values as $value) {
108
                $result[] = "$name: $value";
109
            }
110
        }
111
112
        return $result;
113
    }
114
115
    public static function isSupported()
116
    {
117
        return true;
118
    }
119
}
120