Completed
Push — master ( c30967...3bd52a )
by Andrii
03:06
created

ManualResponse::getReasonPhrase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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\curl;
12
13
use hiqdev\hiart\AbstractRequest;
14
use hiqdev\hiart\AbstractResponse;
15
use hiqdev\hiart\ResponseErrorException;
16
17
/**
18
 * For creating response manually.
19
 */
20
class ManualResponse extends AbstractResponse
21
{
22
    /**
23
     * @var mixed
24
     */
25
    protected $data;
26
27
    /**
28
     * @var array[]
29
     */
30
    protected $headers = [];
31
32
    /**
33
     * @var string
34
     */
35
    protected $statusCode;
36
37
    /**
38
     * @var string
39
     */
40
    protected $reasonPhrase;
41
42
    /**
43
     * @param AbstractRequest $request
44
     * @param mixed $data
45
     * @param array $headers
46
     * @param string $statusCode
47
     * @param string $reasonPhrase
48
     */
49
    public function __construct(RequestInterface $request, $data, array $headers = [], $statusCode = null, $reasonPhrase = null)
50
    {
51
        $this->request = $request;
0 ignored issues
show
Documentation Bug introduced by
It seems like $request of type object<hiqdev\hiart\curl\RequestInterface> is incompatible with the declared type object<hiqdev\hiart\RequestInterface> of property $request.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
52
        $this->data = $data;
53
        $this->headers = $headers;
54
        $this->statusCode = $statusCode;
55
        $this->reasonPhrase = $reasonPhrase;
56
    }
57
58
    /**
59
     * @return mixed
60
     */
61
    public function getData()
62
    {
63
        return $this->data;
64
    }
65
66
    /**
67
     * @return mixed
68
     */
69
    public function getRawData()
70
    {
71
        return $this->data;
72
    }
73
74
    /**
75
     * @param string $name the header name
76
     * @return array|null
77
     */
78
    public function getHeader($name)
79
    {
80
        return isset($this->headers[strtolower($name)]) ? $this->headers[strtolower($name)] : null;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function getStatusCode()
87
    {
88
        return $this->statusCode;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function getReasonPhrase()
95
    {
96
        return $this->reasonPhrase;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->reasonPhrase; (string) is incompatible with the return type declared by the interface hiqdev\hiart\ResponseInterface::getReasonPhrase of type hiqdev\hiart\ResponseInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
97
    }
98
99
    /**
100
     * Returns array of all headers.
101
     * Key - Header name
102
     * Value - array of header values. For example:.
103
     *
104
     * ```php
105
     * ['Location' => ['http://example.com'], 'Expires' => ['Thu, 01 Jan 1970 00:00:00 GMT']]
106
     * @return array
107
     */
108
    public function getHeaders()
109
    {
110
        return $this->headers;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->headers; (array[]) is incompatible with the return type declared by the interface hiqdev\hiart\ResponseInterface::getHeaders of type string[][].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
111
    }
112
}
113