Addon   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 41.67 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 1
dl 25
loc 60
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A listAddons() 7 24 4
A getAddon() 18 18 2

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
declare(strict_types=1);
3
4
namespace Zoho\Subscription\Api;
5
6
use Zoho\Subscription\Client\Client;
7
8
/**
9
 * Addon.
10
 *
11
 * @author Tristan Perchec <[email protected]>
12
 * @author Tristan Bessoussa <[email protected]>
13
 *
14
 * @link https://www.zoho.com/subscriptions/api/v1/#addons
15
 */
16
class Addon extends Client
17
{
18
    /**
19
     * @param array $filters associative array of filters
20
     *
21
     * @throws \Exception
22
     *
23
     * @return array
24
     */
25
    public function listAddons(array $filters = []): array
26
    {
27
        $cacheKey = 'addons';
28
        $hit = $this->getFromCache($cacheKey);
29
30
        if (false === $hit) {
31
            $response = $this->sendRequest('GET', $cacheKey);
32
33
            $addons = $this->processResponse($response);
34
            $hit = $addons['addons'];
35
36
            $this->saveToCache($cacheKey, $hit);
37
        }
38
39 View Code Duplication
        foreach ($filters as $key => $filter) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
40
            if (array_key_exists($key, current($hit))) {
41
                $hit = array_filter($hit, function ($element) use ($key, $filter) {
42
                    return $element[$key] == $filter;
43
                });
44
            }
45
        }
46
47
        return $hit;
48
    }
49
50
    /**
51
     * @param string $addonCode
52
     *
53
     * @throws \Exception
54
     *
55
     * @return array
56
     */
57 View Code Duplication
    public function getAddon(string $addonCode): array
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...
58
    {
59
        $cacheKey = sprintf('addon_%s', $addonCode);
60
        $hit = $this->getFromCache($cacheKey);
61
62
        if (false === $hit) {
63
            $response = $this->sendRequest('GET', sprintf('addons/%s', $addonCode));
64
65
            $data = $this->processResponse($response);
66
            $addon = $data['addon'];
67
68
            $this->saveToCache($cacheKey, $addon);
69
70
            return $addon;
71
        }
72
73
        return $hit;
74
    }
75
}
76