Completed
Push — master ( c13142...c0435c )
by Tobias
02:34
created

Invoice   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 94
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 72.41%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 94
loc 94
ccs 21
cts 29
cp 0.7241
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A search() 15 15 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\ValidationException;
8
use Billogram\Model\Invoice\Invoice as Model;
9
use Billogram\Model\Invoice\InvoiceCollection;
10
11
/**
12
 * @author Ibrahim Hizeoui <[email protected]>
13
 */
14 View Code Duplication
class Invoice extends HttpApi
0 ignored issues
show
Duplication introduced by Ibrahimhizeoui
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...
15
{
16
    /**
17
     * @param array $param
18
     *
19
     * @return string|array
20
     *
21
     * @see https://billogram.com/api/documentation#billogram_call_create
22
     */
23 1
    public function search(array $param = [])
24
    {
25 1
        $param = array_merge(['page' => 1, 'page_size' => 100], $param);
26 1
        $response = $this->httpGet('/billogram', $param);
27 1
        if (!$this->hydrator) {
28
            return $response;
0 ignored issues
show
Bug Best Practice introduced by Ibrahimhizeoui
The return type of return $response; (Psr\Http\Message\ResponseInterface) is incompatible with the return type documented by Billogram\Api\Invoice::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...
29
        }
30
31
        // Use any valid status code here
32 1
        if ($response->getStatusCode() !== 200) {
33
            $this->handleErrors($response);
34
        }
35
36 1
        return $this->hydrator->hydrate($response, InvoiceCollection::class);
37
    }
38
39
    /**
40
     * @param string $invoiceId
41
     * @param array  $param
42
     *
43
     * @return mixed|\Psr\Http\Message\ResponseInterface
44
     *
45
     * @see https://billogram.com/api/documentation#billogram_call_create
46
     */
47 2
    public function fetch(string $invoiceId, array $param = [])
48
    {
49 2
        $response = $this->httpGet('/billogram/'.$invoiceId, $param);
50 2
        if (!$this->hydrator) {
51
            return $response;
52
        }
53
        // Use any valid status code here
54 2
        if ($response->getStatusCode() !== 200) {
55
            $this->handleErrors($response);
56
        }
57
58 2
        return $this->hydrator->hydrate($response, Model::class);
59
    }
60
61
    /**
62
     * @param Model $invoice
63
     *
64
     * @return mixed|\Psr\Http\Message\ResponseInterface
65
     *
66
     * @see https://billogram.com/api/documentation#billogram_call_create
67
     *
68
     * @throws ValidationException
69
     */
70 1
    public function create(Model $invoice)
71
    {
72 1
        $response = $this->httpPost('/billogram', $invoice->toArray());
73 1
        if (!$this->hydrator) {
74
            return $response;
75
        }
76
        // Use any valid status code here
77 1
        if ($response->getStatusCode() !== 200) {
78
            $this->handleErrors($response);
79
        }
80
81 1
        return $this->hydrator->hydrate($response, Model::class);
82
    }
83
84
    /**
85
     * @param string $invoiceId
86
     * @param Model  $invoice
87
     *
88
     * @return mixed|\Psr\Http\Message\ResponseInterface
89
     *
90
     * @see https://billogram.com/api/documentation#billogram_call_create
91
     *
92
     * @throws ValidationException
93
     */
94 1
    public function update(string $invoiceId, Model $invoice)
95
    {
96 1
        $response = $this->httpPut('/billogram/'.$invoiceId, $invoice->toArray());
97 1
        if (!$this->hydrator) {
98
            return $response;
99
        }
100
        // Use any valid status code here
101 1
        if ($response->getStatusCode() !== 200) {
102
            $this->handleErrors($response);
103
        }
104
105 1
        return $this->hydrator->hydrate($response, Model::class);
106
    }
107
}
108