Completed
Push — master ( c8064d...729f46 )
by Dmitry
02:44
created

Request   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 92
Duplicated Lines 16.3 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 76.32%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 15
loc 92
ccs 29
cts 38
cp 0.7632
rs 10
c 1
b 0
f 0

4 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

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
 * Tools to use API as ActiveRecord for Yii2
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 2
    public function send($options = [])
42
    {
43
        try {
44 2
            $this->build();
45 2
            $context = stream_context_create($this->prepareContextOptions($options));
46 2
            $stream = fopen($this->getFullUri(), 'rb', false, $context);
47 2
            $responseContent = stream_get_contents($stream);
48
            // see http://php.net/manual/en/reserved.variables.httpresponseheader.php
49 2
            $responseHeaders = $http_response_header;
50 2
            fclose($stream);
51
        } catch (\Exception $e) {
52
            throw new RequestErrorException($e->getMessage(), $this, $e->getCode(), $e);
53
        }
54
55 2
        return new $this->responseClass($this, $responseContent, $responseHeaders);
56
    }
57
58 2
    protected function prepareContextOptions($options)
59
    {
60
        $requestOptions = [
61
            'http' => [
62 2
                'protocol_version' => $this->version,
63 2
                'method' => $this->method,
64 2
                'header' => static::composeHeaderLines($this->headers),
65
            ],
66
        ];
67
68 2
        if (isset($this->body)) {
69
            $requestOptions['http']['content'] = $this->body;
70
        }
71
72 2
        $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 2
        $sendOptions = $this->convertContextOptions($options);
74
75 2
        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 2
    protected function convertContextOptions(array $options)
84
    {
85 2
        $contextOptions = [];
86 2
        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 2
        return $contextOptions;
97
    }
98
99 2 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 2
        $result = [];
102 2
        foreach ($headers as $name => $values) {
103 2
            $name = str_replace(' ', '-', ucwords(str_replace('-', ' ', $name)));
104 2
            if (is_string($values)) {
105 2
                $values = [$values];
106
            }
107 2
            foreach ($values as $value) {
108 2
                $result[] = "$name: $value";
109
            }
110
        }
111
112 2
        return $result;
113
    }
114
}
115