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

ExampleCache::setTempPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
nc 2
cc 2
eloc 4
nop 1
crap 6
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 28 and the first side effect is on line 21.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * OpenWeatherMap-PHP-API — A php api to parse weather data from http://www.OpenWeatherMap.org .
4
 *
5
 * @license MIT
6
 *
7
 * Please see the LICENSE file distributed with this source code for further
8
 * information regarding copyright and licensing.
9
 *
10
 * Please visit the following links to read about the usage policies and the license of
11
 * OpenWeatherMap before using this class:
12
 *
13
 * @see http://www.OpenWeatherMap.org
14
 * @see http://www.OpenWeatherMap.org/terms
15
 * @see http://openweathermap.org/appid
16
 */
17
18
use Cmfcmf\OpenWeatherMap;
19
use Cmfcmf\OpenWeatherMap\AbstractCache;
20
21
require_once __DIR__ . '/bootstrap.php';
22
23
/**
24
 * Example cache implementation.
25
 *
26
 * @ignore
27
 */
28
class ExampleCache extends AbstractCache
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
29
{
30
    protected $tmp;
31
32 View Code Duplication
    private function urlToPath($url)
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...
33
    {
34
        $tmp = $this->tmp;
35
        $dir = $tmp . DIRECTORY_SEPARATOR . "OpenWeatherMapPHPAPI";
36
        if (!is_dir($dir)) {
37
            mkdir($dir);
38
        }
39
40
        $path = $dir . DIRECTORY_SEPARATOR . md5($url);
41
42
        return $path;
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48 View Code Duplication
    public function isCached($url)
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...
49
    {
50
        $path = $this->urlToPath($url);
51
        if (!file_exists($path) || filectime($path) + $this->seconds < time()) {
52
            echo "Weather data is NOT cached!\n";
53
54
            return false;
55
        }
56
57
        echo "Weather data is cached!\n";
58
59
        return true;
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65
    public function getCached($url)
66
    {
67
        return file_get_contents($this->urlToPath($url));
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73
    public function setCached($url, $content)
74
    {
75
        file_put_contents($this->urlToPath($url), $content);
76
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81
    public function setTempPath($path)
82
    {
83
        if (!is_dir($path)) {
84
            mkdir($path);
85
        }
86
        
87
        $this->tmp = $path;
88
    }
89
}
90
91
// Language of data (try your own language here!):
92
$lang = 'de';
93
94
// Units (can be 'metric' or 'imperial' [default]):
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
95
$units = 'metric';
96
97
// Example 1: Use your own cache implementation. Cache for 10 seconds only in this example.
98
$cache = new ExampleCache();
99
$cache->setTempPath(__DIR__.'/temps');
100
$owm = new OpenWeatherMap($myApiKey, null, $cache, 10);
0 ignored issues
show
Documentation introduced by
$cache is of type object<ExampleCache>, but the function expects a boolean|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...
101
102
$weather = $owm->getWeather('Berlin', $units, $lang);
103
echo "EXAMPLE 1<hr />\n\n\n";
104
echo $weather->temperature;
105