Completed
Push — master ( bc8abc...611a53 )
by Achille
9s
created

Box   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 71
Duplicated Lines 30.99 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 13
c 3
b 0
f 1
lcom 1
cbo 3
dl 22
loc 71
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A all() 0 4 1
B findAll() 0 27 5
A create() 10 10 2
A delete() 0 4 1
A edit() 12 12 3
A find() 0 4 1

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
namespace Streak\Endpoint;
4
5
use Streak\Paginator;
6
7
class Box extends AbstractEndpoint
8
{
9
    const ENDPOINT = 'boxes';
10
11
    public function all()
12
    {
13
        return $this->client->get(self::ENDPOINT);
14
    }
15
16
    public function findAll($pipelineKey, $sortBy = null, Paginator $paginator = null)
17
    {
18
        $options = ['query' => []];
19
20
        if (null !== $sortBy) {
21
            if (!in_array($sortBy, ['creationTimestamp', 'lastUpdatedTimestamp'])) {
22
                throw new \InvalidArgumentException('Invalid sort field.');
23
            }
24
25
            $options['query']['sortBy'] = $sortBy;
26
        }
27
28
        if (null === $paginator) {
29
            $paginator = new Paginator();
30
        }
31
32
        while ($paginator->nextPage()) {
33
            $options['query']['page'] = $paginator->getPage();
34
            $options['query']['limit'] = $paginator->getLimit();
35
36
            $results = $this->client->get(sprintf('pipelines/%s/%s', $pipelineKey, self::ENDPOINT), $options);
37
38
            $paginator->addResults($results);
0 ignored issues
show
Bug introduced by
It seems like $results defined by $this->client->get(sprin...f::ENDPOINT), $options) on line 36 can also be of type string; however, Streak\Paginator::addResults() does only seem to accept array, 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...
39
        }
40
41
        return $paginator->getResults();
42
    }
43
44 View Code Duplication
    public function create($pipelineKey, array $box)
0 ignored issues
show
Duplication introduced by
This method 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...
45
    {
46
        if (!isset($box['name'])) {
47
            throw new \InvalidArgumentException('Missing required fields.');
48
        }
49
50
        return $this->client->put(sprintf('pipelines/%s/%s', $pipelineKey, self::ENDPOINT), [
51
            'form_params' => $box,
52
        ]);
53
    }
54
55
    public function delete($boxKey)
56
    {
57
        return $this->client->delete(sprintf('%s/%s', self::ENDPOINT, $boxKey));
58
    }
59
60 View Code Duplication
    public function edit($boxKey, array $box)
0 ignored issues
show
Duplication introduced by
This method 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...
61
    {
62
        foreach ($box as $field => $value) {
63
            if (!in_array($field, ['name', 'notes', 'stageKey', 'followerKeys', 'fields'])) {
64
                throw new \InvalidArgumentException('Not allowed field.');
65
            }
66
        }
67
68
        return $this->client->post(sprintf('%s/%s', self::ENDPOINT, $boxKey), [
69
            'json' => $box,
70
        ]);
71
    }
72
73
    public function find($boxKey)
74
    {
75
        return $this->client->get(sprintf('%s/%s', self::ENDPOINT, $boxKey));
76
    }
77
}
78