Passed
Push — master ( 1b8175...e65fee )
by Nico
02:12
created

CoC_Location   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 129
Duplicated Lines 20.16 %

Coupling/Cohesion

Components 1
Dependencies 1
Metric Value
wmc 16
lcom 1
cbo 1
dl 26
loc 129
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getLocation() 0 19 4
A setLocationByName() 13 13 3
A setLocationByCode() 13 13 3
A getLocationId() 0 4 1
A getLocationName() 0 4 1
A isCountry() 0 4 1
A getCountryCode() 0 11 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
3
class CoC_Location
4
{
5
	protected $api;
6
	protected $locationId;
7
	protected $location = NULL;
8
    
9
	/**
10
	 * Constructor of CoC_Location
11
	 * 
12
	 * @param $locationId
13
	 */
14
	public function __construct($locationId)
15
	{
16
		$this->api        = new ClashOfClans();
17
		$this->locationId = $locationId;
18
	}
19
    
20
	/**
21
	 * Gets an stdClass containing location-information
22
	 *
23
	 * @return stdClass, location
0 ignored issues
show
Documentation introduced by
The doc-type stdClass, could not be parsed: Expected "|" or "end of type", but got "," at position 8. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
24
	 */
25
	protected function getLocation()
26
	{
27
		if($this->location == NULL)
28
		{
29
			foreach ($this->api->getLocationList()->items as $location) 
30
			{
31
				if ($location->id == $this->locationId) 
32
				{
33
					$this->location = $location;
34
					return $this->location;
35
				}
36
			}
37
			return 0;
38
		}
39
		else
40
		{
41
			return $this->location;
42
		}
43
	}
44
    
45
	/**
46
	 * Sets the location by providing it's exact name
47
	 *
48
	 * @param string, location name
49
	 * @return bool, success or fail
0 ignored issues
show
Documentation introduced by
The doc-type bool, could not be parsed: Expected "|" or "end of type", but got "," at position 4. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
50
	 */
51 View Code Duplication
	public function setLocationByName($name)
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...
52
	{
53
		foreach ($this->api->getLocationList()->items as $location) 
54
		{
55
			if ($location->name == $name) 
56
			{
57
				$this->locationId = $location->id;
58
				$this->location = $location;
59
				return 1;
60
			}
61
		}
62
		return 0;
63
	}
64
65
	/**
66
	 * Sets the location by providing it's country code
67
	 *
68
     * @param string
69
     * @return bool
70
     */
71 View Code Duplication
	public function setLocationByCode($cc)
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...
72
	{
73
		foreach ($this->api->getLocationList()->items as $location) 
74
		{
75
			if ($location->countryCode == $cc) 
76
			{
77
				$this->locationId = $location->id;
78
				$this->location = $location;
79
				return 1;
80
			}
81
		}
82
		return 0;
83
	}
84
85
	/**
86
	 * Gets the location ID
87
	 *
88
	 * @return int
89
	 */
90
	public function getLocationId()
91
	{
92
		return $this->getLocation()->id;
93
	}
94
95
	/**
96
	 * Gets the location name
97
	 *
98
	 * @return string, name
0 ignored issues
show
Documentation introduced by
The doc-type string, could not be parsed: Expected "|" or "end of type", but got "," at position 6. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
99
	 */
100
	public function getLocationName()
101
	{
102
		return $this->getLocation()->name;
103
	}
104
105
	/**
106
	 * Check whether the given location is a country or a region
107
	 *
108
	 * @return bool
109
	 */
110
	public function isCountry()
111
	{
112
		return $this->getLocation()->isCountry;
113
	}
114
115
	/**
116
	 * Gets the country code for the given location
117
	 *
118
	 * @return string, country code
0 ignored issues
show
Documentation introduced by
The doc-type string, could not be parsed: Expected "|" or "end of type", but got "," at position 6. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
119
	 */
120
	public function getCountryCode()
121
	{
122
		if($this->isCountry())
123
		{
124
			return $this->getLocation()->countryCode;
125
		}
126
		else
127
		{
128
			return "nA";
129
		}
130
	}
131
}
132
133
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...