Completed
Push — master ( 79428d...367fd6 )
by Cesar
27s queued 11s
created

UserService::create()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 3
nop 1
1
<?php
2
3
namespace App\Service\Hyperwallet;
4
5
use Exception;
6
use Hyperwallet\Exception\HyperwalletApiException;
7
use Hyperwallet\Model\User;
8
use Hyperwallet\Response\ListResponse;
9
use Hyperwallet\Model\UserStatusTransition;
10
11
/**
12
 * Class UserService
13
 * @package App\Service\Hyperwallet
14
 */
15
class UserService extends AbstractHyperwalletService
16
{
17
    /**
18
     * @return ListResponse
19
     *
20
     * @throws HyperwalletApiException
21
     */
22
    public function list(): ListResponse
23
    {
24
        return $this->client->listUsers([
25
            'status' => ['ACTIVATED', 'PRE_ACTIVATED'],
26
        ]);
27
    }
28
29
    /**
30
     * @param array $userParameters
31
     * @return User | Exception
32
     */
33
    public function create(array $userParameters)
34
    {
35
        try {
36
            $user = new User($userParameters);
37
            return $this->client->createUser($user);
38
        } catch (Exception $exception) {
39
            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\UserService::create of type Hyperwallet\Model\User.

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...
40
        }
41
    }
42
43
    /**
44
     * @param string $userToken
45
     * @return Exception|User
46
     */
47
    public function get(string $userToken)
48
    {
49
        try {
50
            return $this->client->getUser($userToken);
51
        } catch (Exception $exception) {
52
            return $exception;
53
        }
54
    }
55
}
56