Completed
Push — master ( 9db202...d3eb33 )
by Dmitry
02:11
created

JsonTransportRequest::fromArray()   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 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * @author Dmitry Gladyshev <[email protected]>
4
 * @date 17/08/2016 13:52
5
 */
6
7
namespace Yandex\Direct\Transport;
8
9
use Yandex\Direct\ConfigurableTrait;
10
use Yandex\Direct\CredentialsInterface;
11
use Yandex\Direct\Exception\InvalidArgumentException;
12
13
/**
14
 * Class TransportRequest
15
 * @package Yandex\Direct
16
 */
17
class JsonTransportRequest implements RequestInterface
18
{
19
    use ConfigurableTrait;
20
21
    /**
22
     * @var bool
23
     */
24
    protected $useOperatorUnits = true;
25
26
    /**
27
     * @var string
28
     */
29
    protected $method;
30
31
    /**
32
     * @var string
33
     */
34
    protected $service;
35
36
    /**
37
     * @var string
38
     */
39
    protected $language = 'ru';
40
41
    /**
42
     * @var array
43
     */
44
    protected $params = [];
45
46
    /**
47
     * Custom headers
48
     * @var array
49
     */
50
    protected $headers = [];
51
52
    /**
53
     * @var CredentialsInterface
54
     */
55
    protected $credentials;
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public static function fromArray(array $request)
61
    {
62
        return new self($request);
63
    }
64
65
66 1
    public function __construct(array $options = [])
67
    {
68 1
        $this->setOptions($options);
69 1
    }
70
71
    /**
72
     * @return string
73
     */
74 1
    public function getService()
75
    {
76 1
        return $this->service;
77
    }
78
79
    /**
80
     * @return string
81
     */
82 1
    public function getMethod()
83
    {
84 1
        return $this->method;
85
    }
86
87
    /**
88
     * @return array
89
     */
90 1
    public function getParams()
91
    {
92 1
        return $this->params;
93
    }
94
95
    /**
96
     * @return string
97
     */
98 1
    public function getUseOperatorUnits()
99
    {
100 1
        return $this->useOperatorUnits;
101
    }
102
103
    /**
104
     * @param bool $useOperatorUnits
105
     * @throws InvalidArgumentException
106
     */
107
    public function setUseOperatorUnits($useOperatorUnits)
108
    {
109
        $this->useOperatorUnits = $useOperatorUnits;
110
    }
111
112
    /**
113
     * @return CredentialsInterface
114
     */
115 1
    public function getCredentials()
116
    {
117 1
        return $this->credentials;
118
    }
119
120
    /**
121
     * @return string
122
     */
123 1
    public function getLanguage()
124
    {
125 1
        return $this->language;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->language; (string) is incompatible with the return type declared by the interface Yandex\Direct\Transport\...tInterface::getLanguage of type boolean.

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...
126
    }
127
}
128