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.

PaymentClient   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 7
dl 0
loc 90
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createPayment() 0 11 1
A executePayment() 0 4 1
A executePaymentByIds() 0 8 1
A lookupPayment() 0 7 1
A listPayments() 0 11 1
A factoryPaymentResponse() 0 4 1
1
<?php
2
3
namespace Onend\PayPal\Payment\Client;
4
5
use Guzzle\Http\Message\Response;
6
7
use Onend\PayPal\Common\Client\AbstractAuthenticatedClient;
8
use Onend\PayPal\Common\Enum\Endpoint;
9
use Onend\PayPal\Common\Enum\RequestFormat;
10
use Onend\PayPal\Payment\Model\Payment;
11
use Onend\PayPal\Payment\Model\PaymentList;
12
13
class PaymentClient extends AbstractAuthenticatedClient
14
{
15
    /**
16
     * Create a payment
17
     *
18
     * @param Payment $payment
19
     *
20
     * @return Payment
21
     */
22
    public function createPayment(Payment $payment)
23
    {
24
        $request = $this->post(
25
            Endpoint::CREATE_PAYMENT,
26
            [],
27
            $this->getSerializer()->serialize($payment, RequestFormat::JSON)
28
        );
29
        $response = $this->send($request);
30
31
        return $this->factoryPaymentResponse($response);
0 ignored issues
show
Bug introduced by
It seems like $response defined by $this->send($request) on line 29 can also be of type array; however, Onend\PayPal\Payment\Cli...actoryPaymentResponse() does only seem to accept object<Guzzle\Http\Message\Response>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
32
    }
33
34
    /**
35
     * Execute an approved PayPal payment by Payment
36
     *
37
     * @param Payment $payment
38
     *
39
     * @return Payment
40
     */
41
    public function executePayment(Payment $payment)
42
    {
43
        return $this->executePaymentByIds($payment->getId(), $payment->getPayer()->getPayerInfo()->getPayerId());
44
    }
45
46
    /**
47
     * Execute an approved PayPal payment by payerId and paymentId
48
     *
49
     * @param string $paymentId
50
     * @param string $payerId
51
     *
52
     * @return Payment
53
     */
54
    public function executePaymentByIds($paymentId, $payerId)
55
    {
56
        $data = json_encode(["payer_id" => $payerId]);
57
        $request = $this->post(Endpoint::EXECUTE_PAYMENT, null, $data, ["paymenId" => $paymentId]);
58
        $response = $this->send($request);
59
60
        return $this->factoryPaymentResponse($response);
0 ignored issues
show
Bug introduced by
It seems like $response defined by $this->send($request) on line 58 can also be of type array; however, Onend\PayPal\Payment\Cli...actoryPaymentResponse() does only seem to accept object<Guzzle\Http\Message\Response>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
61
    }
62
63
    /**
64
     * Look up a payment resource
65
     *
66
     * @param string $paymentId
67
     *
68
     * @return Payment
69
     */
70
    public function lookupPayment($paymentId)
71
    {
72
        $request = $this->get(Endpoint::LOOKUP_PAYMENT, null, ["paymenId" => $paymentId]);
73
        $response = $this->send($request);
74
75
        return $this->factoryPaymentResponse($response);
0 ignored issues
show
Bug introduced by
It seems like $response defined by $this->send($request) on line 73 can also be of type array; however, Onend\PayPal\Payment\Cli...actoryPaymentResponse() does only seem to accept object<Guzzle\Http\Message\Response>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
76
    }
77
78
    /**
79
     * @return PaymentList
80
     */
81
    public function listPayments()
82
    {
83
        $request = $this->get(Endpoint::LIST_PAYMENTS);
84
        $response = $this->send($request);
85
86
        return $this->getSerializer()->deserialize(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getSeriali...m\RequestFormat::JSON); (object|array|integer|double|string|boolean) is incompatible with the return type documented by Onend\PayPal\Payment\Cli...entClient::listPayments of type Onend\PayPal\Payment\Model\PaymentList.

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...
87
            $response->getBody(true),
88
            PaymentList::getClass(),
89
            RequestFormat::JSON
90
        );
91
    }
92
93
    /**
94
     * @param $response
95
     *
96
     * @return Payment
97
     */
98
    protected function factoryPaymentResponse(Response $response)
99
    {
100
        return $this->getSerializer()->deserialize($response->getBody(true), Payment::getClass(), RequestFormat::JSON);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getSeriali...m\RequestFormat::JSON); (object|array|integer|double|string|boolean) is incompatible with the return type documented by Onend\PayPal\Payment\Cli...:factoryPaymentResponse of type Onend\PayPal\Payment\Model\Payment.

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...
101
    }
102
}
103