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.

ResponseFactoryTrait   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A throwExceptionIf() 0 4 2
A setSerializer() 0 3 1
A factory() 0 19 4
1
<?php
2
/**
3
 * This file is part of Poloniex PHP SDK.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright 2017-2018 Chasovskih Grisha <[email protected]>
9
 * @license https://github.com/signulls/poloniex-php-sdk/blob/master/LICENSE MIT
10
 */
11
12
namespace Poloniex\Api\Traits;
13
14
use Poloniex\Exception\PoloniexException;
15
use Poloniex\Response\ResponseInterface;
16
use Symfony\Component\Serializer\{Serializer, SerializerInterface};
17
18
/**
19
 * Trait ResponseFactoryTrait
20
 *
21
 * @author Grisha Chasovskih <[email protected]>
22
 */
23
trait ResponseFactoryTrait
24
{
25
    /**
26
     * @var Serializer
27
     */
28
    protected $serializer;
29
30
    /**
31
     * Sets the serializer.
32
     *
33
     * @param SerializerInterface $serializer A SerializerInterface instance
34
     */
35
    public function setSerializer(SerializerInterface $serializer): void
36
    {
37
        $this->serializer = $serializer;
0 ignored issues
show
Documentation Bug introduced by
$serializer is of type Symfony\Component\Serializer\SerializerInterface, but the property $serializer was declared to be of type Symfony\Component\Serializer\Serializer. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
38
    }
39
40
    /**
41
     * @param string $responseClass
42
     * @param array  $data
43
     *
44
     * @return ResponseInterface
45
     */
46
    protected function factory(string $responseClass, array $data): ResponseInterface
47
    {
48
        /* @var $response ResponseInterface */
49
        $response = $this->serializer->denormalize($data, $responseClass);
50
        $this->throwExceptionIf(!$response instanceof $responseClass, 'Poloniex response is not valid.');
51
52
        if (isset($data['globalTradeID'])) {
53
            $response->globalTradeId = (int) $data['globalTradeID'];
0 ignored issues
show
Bug introduced by
Accessing globalTradeId on the interface Poloniex\Response\ResponseInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
54
        }
55
56
        if (isset($data['tradeID'])) {
57
            $response->tradeId = (int) $data['tradeID'];
0 ignored issues
show
Bug introduced by
Accessing tradeId on the interface Poloniex\Response\ResponseInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
58
        }
59
60
        if (isset($data['orderID'])) {
61
            $response->orderId = (int) $data['orderID'];
0 ignored issues
show
Bug introduced by
Accessing orderId on the interface Poloniex\Response\ResponseInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
62
        }
63
64
        return $response;
65
    }
66
67
    /**
68
     * Throw exception by condition
69
     *
70
     * @param bool $condition
71
     * @param string $message
72
     *
73
     * @throws PoloniexException
74
     */
75
    protected function throwExceptionIf(bool $condition, string $message): void
76
    {
77
        if ($condition) {
78
            throw new PoloniexException($message);
79
        }
80
    }
81
}