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
Pull Request — master (#14)
by
unknown
02:08
created

FilePhpStore::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 17
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 17
loc 17
rs 9.4285
cc 3
eloc 9
nc 3
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 FilePhpStore implements ValueStoreInterface, LockStoreInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    protected $directory;
16
17
    protected static $strDenyAccess =  '<?php return header("HTTP/1.0 404 Not Found"); ?>'; //deny access from web-browser
18
19
    /**
20
     * @param string
21
     */
22
    public function __construct($directory = null)
23
    {
24
        if (!$directory) {
25
            $directory = sys_get_temp_dir();
26
        }
27
28
        $this->directory = $directory;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 View Code Duplication
    public function set($key, $value, $ttl)
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...
35
    {
36
        $fileName = $this->getFileName($key);
37
        $data = self::$strDenyAccess . '|' . $ttl . '|' . serialize($value);
38
39
        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...
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 View Code Duplication
    public function get($key)
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...
46
    {
47
        $fileName = $this->getFileName($key);
48
49
        if (!file_exists($fileName)) {
50
            return false;
51
        }
52
53
        $data = file_get_contents($fileName);
54
        list($strDenyAccess, $ttl, $serializedValue) = explode('|', $data, 3);
0 ignored issues
show
Unused Code introduced by
The assignment to $strDenyAccess is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
55
56
        if ($ttl > time()) {
57
            return false;
58
        }
59
60
        return unserialize($serializedValue);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 View Code Duplication
    public function add($key, $value, $ttl)
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...
67
    {
68
        $fileName = $this->getFileName($key);
69
70
        if (file_exists($fileName)) {
71
            return false;
72
        }
73
74
        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...
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function delete($key)
81
    {
82
        $fileName = $this->getFileName($key);
83
84
        if (file_exists($fileName)) {
85
            unlink($fileName);
86
        }
87
        return true;
88
    }
89
90
    protected function getFileName($key)
91
    {
92
        return $this->directory . DIRECTORY_SEPARATOR . $key . '.php';
93
    }
94
}
95