GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Setup Failed
Pull Request — master (#91)
by
unknown
07:24
created

AbstractRequest::setAccount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Lexik\Bundle\PayboxBundle\Paybox;
4
5
use Lexik\Bundle\PayboxBundle\Paybox\System\Tools;
6
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
7
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
8
9
/**
10
 * Class AbstractRequest
11
 *
12
 * @package Lexik\Bundle\PayboxBundle\Paybox
13
 *
14
 * @author Lexik <[email protected]>
15
 * @author Olivier Maisonneuve <[email protected]>
16
 */
17
abstract class AbstractRequest implements RequestInterface
18
{
19
    /**
20
     * Account attached to the request.
21
     *
22
     * @var array
23
     */
24
    protected $account;
25
26
    /**
27
     * Array of the transaction's parameters.
28
     *
29
     * @var array
30
     */
31
    protected $parameters;
32
33
    /**
34
     * Array of globals parameters.
35
     *
36
     * @var array
37
     */
38
    protected $globals;
39
40
    /**
41
     * Array of servers informations.
42
     *
43
     * @var array
44
     */
45
    protected $servers;
46
47
    /**
48
     * Constructor.
49
     *
50
     * @param string $account
51
     * @param array  $parameters
52
     * @param array  $servers
53
     */
54
    public function __construct($account, array $parameters, array $servers)
55
    {
56
        $this->account    = $account;
0 ignored issues
show
Documentation Bug introduced by
It seems like $account of type string is incompatible with the declared type array of property $account.

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...
57
        $this->parameters = array();
58
        $this->globals    = array();
59
        $this->servers    = $servers;
60
61
        $this->initGlobals($parameters);
62
        $this->initParameters();
63
    }
64
65
    /**
66
     * Initialize the object with the defaults values.
67
     *
68
     * @param array $parameters
69
     */
70
    abstract protected function initGlobals(array $parameters);
71
72
    /**
73
     * Initialize defaults parameters with globals.
74
     */
75
    abstract protected function initParameters();
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function setParameter($name, $value)
81
    {
82
        $this->parameters[$name] = $value;
83
84
        return $this;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function setParameters(array $parameters)
91
    {
92
        foreach ($parameters as $name => $value) {
93
            $this->setParameter($name, $value);
94
        }
95
96
        return $this;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function setAccount($name)
103
    {
104
        $this->account = $name;
105
106
        return $this;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function getAccount()
113
    {
114
        return $this->account;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->account; (array) is incompatible with the return type declared by the interface Lexik\Bundle\PayboxBundl...stInterface::getAccount 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...
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function getParameter($name)
121
    {
122
        return (isset($this->parameters[strtoupper($name)])) ? $this->parameters[strtoupper($name)] : null;
123
    }
124
125
    /**
126
     * Returns all parameters as a querystring.
127
     *
128
     * @return string
129
     */
130
    protected function stringifyParameters()
131
    {
132
        if (isset($this->parameters['PBX_HMAC'])) {
133
            unset($this->parameters['PBX_HMAC']);
134
        }
135
136
        ksort($this->parameters);
137
138
        return Tools::stringify($this->parameters);
139
    }
140
141
    /**
142
     * Computes the hmac hash.
143
     *
144
     * @return string
145
     */
146
    protected function computeHmac()
147
    {
148
        $binKey = pack('H*', $this->globals['hmac_key']);
149
150
        return hash_hmac($this->globals['hmac_algorithm'], $this->stringifyParameters(), $binKey);
151
    }
152
153
    /**
154
     * Returns the url of an available server.
155
     *
156
     * @return array
157
     *
158
     * @throws InvalidArgumentException If the specified environment is not valid (dev/prod).
159
     * @throws RuntimeException         If no server is available.
160
     */
161
    protected function getServer()
162
    {
163
        $servers = array();
164
165
        if (isset($this->globals['production']) && (true === $this->globals['production'])) {
166
            $servers[] = $this->servers['primary'];
167
            $servers[] = $this->servers['secondary'];
168
        } else {
169
            $servers[] = $this->servers['preprod'];
170
        }
171
172
        foreach ($servers as $server) {
173
            $doc = new \DOMDocument();
174
            $doc->loadHTML($this->getWebPage(sprintf(
175
                '%s://%s%s',
176
                $server['protocol'],
177
                $server['host'],
178
                $server['test_path']
179
            )));
180
            $element = $doc->getElementById('server_status');
181
182
            if ($element && 'OK' == $element->textContent) {
183
                return $server;
184
            }
185
        }
186
187
        throw new RuntimeException('No server available.');
188
    }
189
190
    /**
191
     * Returns the content of a web resource.
192
     *
193
     * @param  string $url
194
     *
195
     * @return string
196
     */
197
    protected function getWebPage($url)
198
    {
199
        $curl = curl_init();
200
201
        curl_setopt($curl, CURLOPT_URL,            $url);
202
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
203
        curl_setopt($curl, CURLOPT_HEADER,         false);
204
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
205
        $output = curl_exec($curl);
206
        curl_close($curl);
207
208
        return (string) $output;
209
    }
210
}
211