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.
Completed
Push — 1.x ( 51d58d...7f0130 )
by Jakub
02:20
created

VippsException::getErrorCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * Vipps exception.
5
 *
6
 * Provides and handles vipps exception.
7
 */
8
9
namespace Vipps\Exceptions;
10
11
use JMS\Serializer\Serializer;
12
use Psr\Http\Message\ResponseInterface;
13
use Vipps\Model\Error\AuthorizationError;
14
use Vipps\Model\Error\ErrorInterface;
15
use Vipps\Model\Error\PaymentError;
16
17
/**
18
 * Class VippsException
19
 * @package Vipps\Exceptions
20
 */
21
class VippsException extends \Exception
22
{
23
24
    /**
25
     * @var \Vipps\Model\Error\ErrorInterface
26
     */
27
    protected $error;
28
29
    /**
30
     * VippsException constructor.
31
     *
32
     * @param string $message
33
     * @param int $code
34
     * @param \Exception|null $previous
35
     * @param \Vipps\Model\Error\ErrorInterface|null $error
36
     */
37
    public function __construct($message = '', $code = 0, \Exception $previous = null, ErrorInterface $error = null)
38
    {
39
        parent::__construct($message, $code, $previous);
40
        $this->error = $error;
41
    }
42
43
    /**
44
     * @param $phrase
45
     * @param $serializer
46
     *
47
     * @return string|\JMS\Serializer\
48
     */
49
    protected static function parsePhrase($phrase, $serializer = null)
50
    {
51
        if (!($serializer instanceof Serializer)) {
52
            return $phrase;
53
        }
54
55
        try {
56
            $decoded = json_decode($phrase, true);
57
            // Match AuthorizationError.
58
            if (isset($decoded['error'])) {
59
                return $serializer->deserialize(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $serializer->dese...nError::class, 'json'); (object|array|integer|double|string|boolean) is incompatible with the return type documented by Vipps\Exceptions\VippsException::parsePhrase of type string|JMS\Serializer\.

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...
60
                    $phrase,
61
                    AuthorizationError::class,
62
                    'json'
63
                );
64
            }
65
            // Match PaymentError collection.
66
            if (isset($decoded[0]['errorGroup'])) {
67
                $phrase = $serializer->deserialize(
68
                    $phrase,
69
                    'array<' . PaymentError::class . '>',
70
                    'json'
71
                );
72
                return reset($phrase);
73
            }
74
        } catch (\Exception $exception) {
75
            // Mute exceptions.
76
        }
77
78
        return $phrase;
79
    }
80
81
    /**
82
     * @return mixed
83
     */
84
    public function getError()
85
    {
86
        return $this->error;
87
    }
88
89
    /**
90
     * Create new Exception from Response.
91
     *
92
     * @param ResponseInterface $response
93
     * @param \JMS\Serializer\Serializer|null $serializer
94
     * @param bool $force
95
     *
96
     * @return null|\Vipps\Exceptions\VippsException
97
     */
98
    public static function createFromResponse(
99
        ResponseInterface $response,
100
        $serializer = null,
101
        $force = true
102
    ) {
103
104
        $phrase = $response->getBody()->getContents();
105
        $phrase = self::parsePhrase($phrase, $serializer);
106
107
        // If error code tells us that something went wrong we must accept it.
108
        if (!$force && $response->getStatusCode() >= 400) {
109
            $force = true;
110
        }
111
112
        // If not an instance of ErrorInterface we must assume everything is ok.
113
        if (!$force && !($phrase instanceof ErrorInterface)) {
114
            // Rewind content pointer.
115
            $response->getBody()->rewind();
116
            return null;
117
        }
118
119
        // If Error can be parsed.
120
        if ($phrase instanceof ErrorInterface) {
121
            return new static(
122
                $phrase->getMessage(),
123
                $response->getStatusCode(),
124
                null,
125
                $phrase
126
            );
127
        }
128
129
        // If Error cannot be parsed.
130
        return new static(
131
            $phrase ?: $response->getReasonPhrase(),
132
            $response->getStatusCode()
133
        );
134
    }
135
}
136