Completed
Push — master ( 4bb787...bf6574 )
by Tobias
01:45
created

Customer   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 95
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 95
loc 95
ccs 0
cts 45
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A search() 13 13 3
A fetch() 13 13 3
A create() 13 13 3
A update() 13 13 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Billogram\Api;
6
7
use Billogram\Exception\Domain\NotFoundException;
8
use Billogram\Exception\Domain\ValidationException;
9
use Billogram\Model\Customer\Customer as Model;
10
use Billogram\Model\Customer\CustomerCollection;
11
use Psr\Http\Message\ResponseInterface;
12
13
/**
14
 * @author Ibrahim Hizeoui <[email protected]>
15
 */
16 View Code Duplication
class Customer extends HttpApi
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
{
18
    /**
19
     * @param array $param
20
     *
21
     * @return string|array
22
     *
23
     * @see https://billogram.com/api/documentation#customers_list
24
     */
25
    public function search(array $param = [])
26
    {
27
        $param = array_merge(['page' => 1, 'page_size' => 100], $param);
28
        $response = $this->httpGet('/customer', $param);
29
        if (!$this->hydrator) {
30
            return $response;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $response; (Psr\Http\Message\ResponseInterface) is incompatible with the return type documented by Billogram\Api\Customer::search of type string|array.

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...
31
        }
32
        if ($response->getStatusCode() !== 200) {
33
            $this->handleErrors($response);
34
        }
35
36
        return $this->hydrator->hydrate($response, CustomerCollection::class);
37
    }
38
39
    /**
40
     * @param int   $customerNo
41
     * @param array $param
42
     *
43
     * @return Model|ResponseInterface
44
     *
45
     * @throws NotFoundException
46
     *
47
     * @see https://billogram.com/api/documentation#customers_fetch
48
     */
49
    public function fetch(int $customerNo, array $param = [])
50
    {
51
        $response = $this->httpGet('/customer/'.$customerNo, $param);
52
        if (!$this->hydrator) {
53
            return $response;
54
        }
55
        // Use any valid status code here
56
        if ($response->getStatusCode() !== 200) {
57
            $this->handleErrors($response);
58
        }
59
60
        return $this->hydrator->hydrate($response, Model::class);
61
    }
62
63
    /**
64
     * @param Model $customer
65
     *
66
     * @return Model|ResponseInterface
67
     *
68
     * @throws ValidationException
69
     *
70
     * @see https://billogram.com/api/documentation#customers_create
71
     */
72
    public function create(Model $customer)
73
    {
74
        $response = $this->httpPost('/customer', $customer->toArray());
75
        if (!$this->hydrator) {
76
            return $response;
77
        }
78
        // Use any valid status code here
79
        if ($response->getStatusCode() !== 200) {
80
            $this->handleErrors($response);
81
        }
82
83
        return $this->hydrator->hydrate($response, Model::class);
84
    }
85
86
    /**
87
     * @param int   $customerNo
88
     * @param Model $costumer
89
     *
90
     * @return Model|ResponseInterface
91
     *
92
     * @throws NotFoundException
93
     * @throws ValidationException
94
     *
95
     * @see https://billogram.com/api/documentation#customers_edit
96
     */
97
    public function update(int $customerNo, Model $costumer)
98
    {
99
        $response = $this->httpPut('/customer/'.$customerNo, $costumer->toArray());
100
        if (!$this->hydrator) {
101
            return $response;
102
        }
103
        // Use any valid status code here
104
        if ($response->getStatusCode() !== 200) {
105
            $this->handleErrors($response);
106
        }
107
108
        return $this->hydrator->hydrate($response, Model::class);
109
    }
110
}
111