Completed
Push — master ( 93b855...faa581 )
by Cesar
21s queued 10s
created

PaymentService::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace App\Service\Hyperwallet;
4
5
use Exception;
6
use Hyperwallet\Exception\HyperwalletApiException;
7
use Hyperwallet\Model\Payment;
8
use Hyperwallet\Response\ListResponse;
9
10
/**
11
 * Class PaymentService
12
 * @package App\Service\Hyperwallet
13
 */
14
class PaymentService extends AbstractHyperwalletService
15
{
16
    /**
17
     * @return ListResponse
18
     *
19
     * @throws HyperwalletApiException
20
     */
21
    public function list(): ListResponse
22
    {
23
        return $this->client->listPayments();
24
    }
25
26
    /**
27
     * @param array $paymentDetails
28
     * @return Payment | Exception
29
     */
30
    public function create(array $paymentDetails)
31
    {
32
        try {
33
            $payment = new Payment($paymentDetails);
34
            return $this->client->createPayment($payment);
35
        } catch (Exception $exception) {
36
            return $exception;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $exception; (Exception) is incompatible with the return type documented by App\Service\Hyperwallet\PaymentService::create of type Hyperwallet\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...
37
        }
38
    }
39
40
    /**
41
     * @param string $paymentToken
42
     * @return Exception|Payment
43
     */
44
    public function get(string $paymentToken)
45
    {
46
        try {
47
            return $this->client->getPayment($paymentToken);
48
        } catch (Exception $exception) {
49
            return $exception;
50
        }
51
    }
52
}
53