Completed
Push — peter279k-master ( fcaf21 )
by Christian
13:01
created

ExampleCacheTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 67
Duplicated Lines 31.34 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 21
loc 67
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A urlToPath() 12 12 2
A isCached() 9 9 3
A getCached() 0 4 1
A setCached() 0 4 1
A setSeconds() 0 4 1
A setTempPath() 0 8 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
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 26 and the first side effect is on line 19.

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
 * 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\AbstractCache;
18
19
require_once __DIR__ . '/bootstrap.php';
20
21
/**
22
 * Example cache implementation for doing unit testing.
23
 *
24
 * @ignore
25
 */
26
class ExampleCacheTest extends AbstractCache
27
{
28
    protected $seconds;
29
    protected $tmp;
30
31 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...
32
    {
33
        $tmp = $this->tmp;
34
        $dir = $tmp . DIRECTORY_SEPARATOR . "OpenWeatherMapPHPAPI";
35
        if (!is_dir($dir)) {
36
            mkdir($dir);
37
        }
38
39
        $path = $dir . DIRECTORY_SEPARATOR . md5($url);
40
41
        return $path;
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47 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...
48
    {
49
        $path = $this->urlToPath($url);
50
        if (!file_exists($path) || filectime($path) + $this->seconds < time()) {
51
            return false;
52
        }
53
54
        return true;
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function getCached($url)
61
    {
62
        return file_get_contents($this->urlToPath($url));
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68
    public function setCached($url, $content)
69
    {
70
        file_put_contents($this->urlToPath($url), $content);
71
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76
    public function setSeconds($seconds)
77
    {
78
        $this->seconds = $seconds;
79
    }
80
81
    /**
82
     * @inheritdoc
83
     */
84
     public function setTempPath($path)
85
     {
86
         if (!is_dir($path)) {
87
             mkdir($path);
88
         }
89
        
90
         $this->tmp = $path;
91
     }
92
}
93