GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 78ed93...4166df )
by Przemek
12s
created

FileStore::prepareTtl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace Metaphore\Store;
3
4
use Metaphore\Store\ValueStoreInterface;
5
use Metaphore\Store\LockStoreInterface;
6
7
/**
8
 * Think again if you really truly want to use filesystem for caching any data ;-)
9
 */
10
class FileStore implements ValueStoreInterface, LockStoreInterface
11
{   
12
    /**
13
     * @var string
14
     */
15
    protected $directory;
16
17
    /**
18
     * @param string
19
     */
20
    public function __construct($directory = null)
21
    {
22
        if (!$directory) {
23
            $directory = sys_get_temp_dir();
24
        }
25
26
        $this->directory = $directory;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function set($key, $value, $ttl)
33
    {
34
        $fileName = $this->getFileName($key);
35
        $data = $this->prepareTtl($ttl).'|'.serialize($value);
36
37
        return file_put_contents($fileName, $data);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return file_put_contents($fileName, $data); (integer) is incompatible with the return type declared by the interface Metaphore\Store\ValueStoreInterface::set of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function get($key)
44
    {
45
        $fileName = $this->getFileName($key);
46
47
        if (!file_exists($fileName)) {
48
            return false;
49
        }
50
51
        $data = file_get_contents($fileName);
52
        list($ttl, $serializedValue) = explode('|', $data, 2);
53
54
        if ($ttl < time()) {
55
            return false;
56
        }
57
58
        return unserialize($serializedValue);
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function add($key, $value, $ttl)
65
    {
66
        if ($this->get($key) !== false) {
67
            return false;
68
        }
69
70
        return $this->set($key, $value, $ttl);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->set($key, $value, $ttl); (integer) is incompatible with the return type declared by the interface Metaphore\Store\LockStoreInterface::add of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function delete($key)
77
    {
78
        $fileName = $this->getFileName($key);
79
80
        if (file_exists($fileName)) {
81
            unlink($fileName);
82
        }
83
84
        return true;
85
    }
86
87
    protected function getFileName($key)
88
    {
89
        return $this->directory.DIRECTORY_SEPARATOR.$key;
90
    }
91
92
    protected function prepareTtl($ttl)
93
    {
94
        return (time() + $ttl);
95
    }
96
}
97