Completed
Pull Request — master (#93)
by lee
12:27
created

OpenWeatherMapExceptionTest   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 227
Duplicated Lines 55.07 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 29
lcom 1
cbo 0
dl 125
loc 227
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 7 7 1
A testCacheException() 8 8 2
A testSecondNotNumbericException() 8 8 2
A testGetWeatherForecastException() 11 11 2
A testGetDailyWeatherForecastException() 11 11 2
A testGetWeatherHistoryException() 0 10 2
A testGetWeatherHistoryWithEndException() 0 10 2
A testGetWeatherHistoryInvalidArgumentException() 10 10 2
A testGetRawDailyForecastDataInvalidArgumentException() 10 10 2
A testGetRawWeatherHistoryException() 10 10 2
A testGetRawWeatherHistoryWithEndDateException() 10 10 2
A testBuildQueryUrlParameterException() 10 10 2
A testParseXMLException() 15 15 2
A testParseXMLWithIsJsonException() 0 16 2
A testParseJsonException() 15 15 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
 * Copyright Zikula Foundation 2014 - Zikula Application Framework
4
 *
5
 * This work is contributed to the Zikula Foundation under one or more
6
 * Contributor Agreements and licensed to You under the following license:
7
 *
8
 * @license GNU/LGPv3 (or at your option any later version).
9
 * @package OpenWeatherMap-PHP-Api
10
 *
11
 * Please see the NOTICE file distributed with this source code for further
12
 * information regarding copyright and licensing.
13
 */
14
15
namespace Cmfcmf\OpenWeatherMap\Tests\OpenWeatherMap;
16
17
use \Cmfcmf\OpenWeatherMap;
18
19
class OpenWeatherMapExceptionTest extends \PHPUnit_Framework_TestCase
20
{
21
    /**
22
     * @var apiKey
23
     * @var weather
24
     */
25
    protected $apiKey;
26
    protected $weather;
27
28 View Code Duplication
    protected function setUp()
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...
29
    {
30
        $ini = parse_ini_file(__DIR__ . '/../ApiKey.ini');
31
        $apiKey = $ini['api_key'];
32
        $this->apiKey = $apiKey;
33
        $this->weather = new OpenWeatherMap($this->apiKey, null, false, 600);
34
    }
35
36
    /**
37
     * @expectedException Exception
38
     */
39 View Code Duplication
    public function testCacheException()
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...
40
    {
41
        try {
42
            $exception = new OpenWeatherMap($this->apiKey, null, true, 600);
0 ignored issues
show
Unused Code introduced by
$exception is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
43
        } catch (\Eception $e) {
0 ignored issues
show
Bug introduced by
The class Eception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
44
            throw $e;
45
        }
46
    }
47
48
    /**
49
     * @expectedException Exception
50
     */
51 View Code Duplication
    public function testSecondNotNumbericException()
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
        try {
54
            $exception = new OpenWeatherMap($this->apiKey, null, false, 'I am not numberic');
0 ignored issues
show
Unused Code introduced by
$exception is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
55
        } catch (\Exception $e) {
56
            throw $e;
57
        }
58
    }
59
60
    /**
61
     * @expectedException InvalidArgumentException
62
     */
63 View Code Duplication
    public function testGetWeatherForecastException()
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...
64
    {
65
        $days = 20;
66
        $weather = $this->weather;
67
68
        try {
69
            $argException = $weather->getWeatherForecast('Berlin', 'imperial', 'en', '', $days);
0 ignored issues
show
Unused Code introduced by
$argException is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
70
        } catch (\InvalidArgumentException $e) {
71
            throw $e;
72
        }
73
    }
74
75
    /**
76
     * @expectedException InvalidArgumentException
77
     */
78 View Code Duplication
    public function testGetDailyWeatherForecastException()
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...
79
    {
80
        $days = 20;
81
        $weather = $this->weather;
82
83
        try {
84
            $argException = $weather->getDailyWeatherForecast('Berlin', 'imperial', 'en', '', $days);
0 ignored issues
show
Unused Code introduced by
$argException is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
85
        } catch (\InvalidArgumentException $e) {
86
            throw $e;
87
        }
88
    }
89
90
    /**
91
     * @expectedException \Cmfcmf\OpenWeatherMap\Exception
92
     */
93
    public function testGetWeatherHistoryException()
94
    {
95
        $weather = $this->weather;
96
97
        try {
98
            $oWMException = $weather->getWeatherHistory('Berlin', new \DateTime('2015-11-01 00:00:00'), 1, 'hour', 'imperial', 'en', '');
0 ignored issues
show
Unused Code introduced by
$oWMException is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
99
        } catch (\Exception $e) {
100
            throw $e;
101
        }
102
    }
103
104
    /**
105
     * @expectedException \Cmfcmf\OpenWeatherMap\Exception
106
     */
107
    public function testGetWeatherHistoryWithEndException()
108
    {
109
        $weather = $this->weather;
110
111
        try {
112
            $oWMException = $weather->getWeatherHistory('Berlin', new \DateTime('2015-11-01 00:00:00'), new \DateTime('NOW'), 'hour', 'imperial', 'en', '');
0 ignored issues
show
Documentation introduced by
new \DateTime('NOW') is of type object<DateTime>, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
$oWMException is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
113
        } catch (\Exception $e) {
114
            throw $e;
115
        }
116
    }
117
118
     /**
119
      * @expectedException InvalidArgumentException
120
      */
121 View Code Duplication
    public function testGetWeatherHistoryInvalidArgumentException()
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...
122
    {
123
        $weather = $this->weather;
124
125
        try {
126
            $argException = $weather->getWeatherHistory('Berlin', new \DateTime('NOW'), 1, 'wrong-type', 'imperial', 'en', '');
0 ignored issues
show
Unused Code introduced by
$argException is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
127
        } catch (\InvalidArgumentException $e) {
128
            throw $e;
129
        }
130
    }
131
132
    /**
133
     * @expectedException InvalidArgumentException
134
     */
135 View Code Duplication
    public function testGetRawDailyForecastDataInvalidArgumentException()
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...
136
    {
137
        $weather = $this->weather;
138
        
139
        try {
140
            $argException = $weather->getRawDailyForecastData('Berlin', 'imperial', 'en', '', 'xml', 20);
0 ignored issues
show
Unused Code introduced by
$argException is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
141
        } catch (\InvalidArgumentException $e) {
142
            throw $e;
143
        }
144
    }
145
146
    /**
147
     * @expectedException InvalidArgumentException
148
     */
149 View Code Duplication
    public function testGetRawWeatherHistoryException()
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...
150
    {
151
        $weather = $this->weather;
152
        
153
        try {
154
            $argException = $weather->getRawWeatherHistory('Berlin', new \DateTime('NOW'), 1, 'wrong-type', 'imperial', 'en', '');
0 ignored issues
show
Unused Code introduced by
$argException is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
155
        } catch (\InvalidArgumentException $e) {
156
            throw $e;
157
        }
158
    }
159
160
    /**
161
     * @expectedException InvalidArgumentException
162
     */
163 View Code Duplication
    public function testGetRawWeatherHistoryWithEndDateException()
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...
164
    {
165
        $weather = $this->weather;
166
        
167
        try {
168
            $argException = $weather->getRawWeatherHistory('Berlin', new \DateTime('NOW'), 'wrong-endOrCount', 'hour', 'imperial', 'en', '');
0 ignored issues
show
Unused Code introduced by
$argException is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
169
        } catch (\InvalidArgumentException $e) {
170
            throw $e;
171
        }
172
    }
173
     
174
    /**
175
     * @expectedException InvalidArgumentException
176
     */
177 View Code Duplication
    public function testBuildQueryUrlParameterException()
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...
178
    {
179
        $weather = $this->weather;
180
        
181
        try {
182
            $argException = $weather->getWeather(true, new \DateTime('NOW'), 'wrong-endOrCount', 'hour', 'imperial', 'en', '');
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a array|integer|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
new \DateTime('NOW') is of type object<DateTime>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
The call to OpenWeatherMap::getWeather() has too many arguments starting with 'imperial'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Unused Code introduced by
$argException is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
183
        } catch (\InvalidArgumentException $e) {
184
            throw $e;
185
        }
186
    }
187
188
    /**
189
     * @expectedException Exception
190
     */
191 View Code Duplication
    public function testParseXMLException()
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...
192
    {
193
        $answer = 'I am not a XML fromat data';
194
        $weather = $this->weather;
195
        $method = new \ReflectionMethod(
196
            $weather, 'parseXML'
197
        );
198
        $method->setAccessible(true);
199
        
200
        try {
201
            $method->invoke($weather, $answer);
202
        } catch (\Exception $e) {
203
            throw $e;
204
        }
205
    }
206
207
    /**
208
     * @expectedException Exception
209
     */
210
    public function testParseXMLWithIsJsonException()
211
    {
212
        $answer = array('message' => 'simple json data');
213
        $answer = json_encode($answer);
214
        $weather = $this->weather;
215
        $method = new \ReflectionMethod(
216
            $weather, 'parseXML'
217
        );
218
        $method->setAccessible(true);
219
220
        try {
221
            $method->invoke($weather, $answer);
222
        } catch (\Exception $e) {
223
            throw $e;
224
        }
225
    }
226
227
    /**
228
     * @expectedException \Cmfcmf\OpenWeatherMap\Exception
229
     */
230 View Code Duplication
    public function testParseJsonException()
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...
231
    {
232
        $weather = $this->weather;
233
        $answer = 'I am not a json format data';
234
        $method = new \ReflectionMethod(
235
            $weather, 'parseJson'
236
        );
237
        $method->setAccessible(true);
238
        
239
        try {
240
            $method->invoke($weather, $answer);
241
        } catch (\Exception $e) {
242
            throw $e;
243
        }
244
    }
245
}
246