Completed
Push — master ( 2a0a89...468a99 )
by Christian
02:41
created

ExampleCache::getCached()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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 35 and the first side effect is on line 23.

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
if (file_exists('../vendor/autoload.php')) {
22
    // Library is not part of a project. "composer install" was executed directly on this library's composer file.
23
    require('../vendor/autoload.php');
24
} else {
25
    // Library is part of a project.
26
    /** @noinspection PhpIncludeInspection */
27
    require('../../../autoload.php');
28
}
29
30
/**
31
 * Example cache implementation.
32
 *
33
 * @ignore
34
 */
35
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...
36
{
37
    private function urlToPath($url)
38
    {
39
        $tmp = sys_get_temp_dir();
40
        $dir = $tmp . DIRECTORY_SEPARATOR . "OpenWeatherMapPHPAPI";
41
        if (!is_dir($dir)) {
42
            mkdir($dir);
43
        }
44
45
        $path = $dir . DIRECTORY_SEPARATOR . md5($url);
46
47
        return $path;
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function isCached($url)
54
    {
55
        $path = $this->urlToPath($url);
56
        if (!file_exists($path) || filectime($path) + $this->seconds < time()) {
57
            echo "Weather data is NOT cached!\n";
58
59
            return false;
60
        }
61
62
        echo "Weather data is cached!\n";
63
64
        return true;
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70
    public function getCached($url)
71
    {
72
        return file_get_contents($this->urlToPath($url));
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78
    public function setCached($url, $content)
79
    {
80
        file_put_contents($this->urlToPath($url), $content);
81
    }
82
}
83
84
// Language of data (try your own language here!):
85
$lang = 'de';
86
87
// 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...
88
$units = 'metric';
89
90
// Example 1: Use your own cache implementation. Cache for 10 seconds only in this example.
91
$owm = new OpenWeatherMap(null, new ExampleCache(), 10);
0 ignored issues
show
Documentation introduced by
new \ExampleCache() 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...
92
93
$weather = $owm->getWeather('Berlin', $units, $lang);
94
echo "EXAMPLE 1<hr />\n\n\n";
95
echo $weather->temperature;
96