Base   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 99
Duplicated Lines 8.08 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 28.85%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 3
dl 8
loc 99
ccs 15
cts 52
cp 0.2885
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 2
A setApiRequest() 0 4 1
getRessource() 0 1 ?
A getParametters() 0 4 1
A loadInformation() 0 4 1
A getEntityInformations() 0 11 3
A __toString() 0 4 1
A retreiveField() 0 7 2
A checkIfFieldExist() 0 10 4
A addField() 0 10 2
A getHost() 0 4 1
A getRegion() 0 4 1
A getApiRequest() 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 namespace GameScan\WoW\Entity;
2
3
use GameScan\WoW\EntityInformation;
4
use GameScan\WoW\Exceptions\EntityInformationDecodeException;
5
use GameScan\WoW\WowApiRequest;
6
7
abstract class Base
8
{
9
10
    /**
11
     * @type WowApiRequest
12
     */
13
    protected $apiRequest = null;
14
    protected $entityInformation = null;
15
    protected $parameters = null;
16
17
18 2 View Code Duplication
    public function __construct(WowApiRequest $api, $locale = null)
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...
19
    {
20 2
        $this->apiRequest = $api;
21 2
        if ($locale !== null) {
22
            $this->apiRequest->setLocale($locale);
23
        }
24 2
        $this->loadInformation();
25 2
    }
26
27
    public function setApiRequest(WowApiRequest $apiRequest)
28
    {
29
        $this->apiRequest = $apiRequest;
30
    }
31
32
    abstract public function getRessource();
33
34 2
    public function getParametters()
35
    {
36 2
        return $this->parameters;
37
    }
38
39 2
    public function loadInformation()
40
    {
41 2
        $this->entityInformation = $this->apiRequest->get($this->getRessource(), $this->getParametters());
42 2
    }
43
44 2
    protected function getEntityInformations()
45
    {
46 2
        if ($this->entityInformation === null) {
47
            $this->loadInformation();
48
        }
49 2
        $entityInformation = json_decode($this->entityInformation);
50 2
        if (JSON_ERROR_NONE !== json_last_error()) {
51
            throw new EntityInformationDecodeException(json_last_error_msg());
52
        }
53 2
        return (new EntityInformation($entityInformation));
54
    }
55
56
    public function __toString()
57
    {
58
        return $this->entityInformation;
59
    }
60
61
    public function retreiveField($fieldName)
62
    {
63
        if (!$this->checkIfFieldExist($fieldName)) {
64
            $this->addField($fieldName);
65
            $this->loadInformation();
66
        }
67
    }
68
69
    protected function checkIfFieldExist($fieldName)
70
    {
71
        if ($this->parameters === null || empty($this->parameters['fields'])) {
72
            return false;
73
        }
74
        if (strpos($this->parameters['fields'], $fieldName) !== false) {
75
            return true;
76
        }
77
        return false;
78
    }
79
80
    protected function addField($fieldName)
81
    {
82
        $fields = explode(',', $this->parameters['fields']);
83
        if ($fields === false) {
84
            $fields = array();
85
        }
86
        $fields[] = $fieldName;
87
        $fields = array_unique($fields);
88
        $this->parameters['fields'] = implode(",", $fields);
89
    }
90
91
    protected function getHost()
92
    {
93
        return $this->apiRequest->getHost();
94
    }
95
96
    protected function getRegion()
97
    {
98
        return $this->apiRequest->getRegion();
99
    }
100
101
    protected function getApiRequest()
102
    {
103
        return $this->apiRequest;
104
    }
105
}
106