Completed
Push — master ( c70864...b6d4e5 )
by Dmitry
03:30 queued 27s
created

src/Cache.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Basis;
4
5
use Carbon\Carbon;
6
7
class Cache
8
{
9
    private $cache = [];
10
    private $converter;
11
12
    public function __construct(Filesystem $fs, Converter $converter)
13
    {
14
        $this->converter = $converter;
15
        $this->path = $fs->getPath('.cache');
0 ignored issues
show
The property path does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
16
        if (!is_dir($this->path)) {
17
            mkdir($this->path);
18
            @chmod($this->path, 0777);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
19
        }
20
    }
21
22
23 2
    public function clear()
24
    {
25 2
        foreach (scandir($this->path) as $f) {
26 2
            if ($f != '.' && $f != '..') {
27 2
                unlink($this->path.'/'.$f);
28
            }
29
        }
30 2
    }
31
32 45
    public function exists($key)
33
    {
34 45
        if (array_key_exists($key, $this->cache)) {
35 5
            return $this->cache[$key]['expire'] > Carbon::now()->getTimestamp();
0 ignored issues
show
The method getTimestamp does only exist in Carbon\CarbonInterface, but not in Carbon\Traits\Creator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
36
        }
37 45
        $filename = $this->path . '/' . $key;
38 45
        if (file_exists($filename) && filesize($filename)) {
39 2
            if (!array_key_exists($key, $this->cache)) {
40 2
                $this->cache[$key] = include $filename;
41
            }
42 2
            $value = $this->cache[$key];
43 2
            return !array_key_exists('expire', $value) || $value['expire'] > Carbon::now()->getTimestamp();
44
        }
45 45
    }
46
47 5
    public function get($key)
48
    {
49 5
        if ($this->exists($key)) {
50 5
            return $this->converter->toObject($this->cache[$key]);
51
        }
52
    }
53
54 8
    public function set($key, $value)
55
    {
56 8
        $filename = $this->path . '/' . $key;
57 8
        $data = $this->converter->toArray($value);
58 8
        $string = '<?php return '.var_export($data, true).';';
59
60 8
        file_put_contents($filename, $string);
61 8
        @chmod($filename, 0777);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
62
63 8
        $this->cache[$key] = $data;
64 8
    }
65
66 45
    public function wrap($key, $callback)
67
    {
68 45
        if (!is_string($key)) {
69 45
            $key = md5(json_encode($key));
70
        }
71 45
        if ($this->exists($key)) {
72 5
            return $this->get($key);
73
        }
74 45
        $result = call_user_func($callback);
75 45
        $expire = null;
76 45
        if (is_array($result) && array_key_exists('expire', $result)) {
77
            $expire = $result['expire'];
78 45
        } elseif (is_object($result) && property_exists($result, 'expire')) {
79 4
            $expire = $result->expire;
80
        }
81 45
        if ($expire && $expire > Carbon::now()->getTimestamp()) {
0 ignored issues
show
The method getTimestamp does only exist in Carbon\CarbonInterface, but not in Carbon\Traits\Creator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
82 4
            $this->set($key, $result);
83
        }
84 45
        return $result;
85
    }
86
}
87