Issues (3)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/ApcuCache.php (3 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 Paillechat\ApcuSimpleCache;
4
5
use Paillechat\ApcuSimpleCache\Exception\ApcuInvalidCacheKeyException;
6
use Psr\SimpleCache\CacheInterface;
7
8
class ApcuCache implements CacheInterface
9
{
10
    /**
11
     * @var string
12
     */
13
    private $namespace;
14
    /**
15
     * @var int
16
     */
17
    private $defaultLifetime;
18
19 22
    public function __construct($namespace = '', $defaultLifetime = 0)
20
    {
21 22
        $this->namespace = $namespace;
22 22
        $this->defaultLifetime = $defaultLifetime;
23 22
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 18
    public function get($key, $default = null)
29
    {
30 18
        $this->assertKeyName($key);
31 8
        $key = $this->buildKeyName($key);
32
33 8
        $value = apcu_fetch($key, $success);
34 8
        if ($success === false) {
35 2
            return $default;
36
        }
37
38 8
        return $value;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 8
    public function set($key, $value, $ttl = null)
45
    {
46 8
        $this->assertKeyName($key);
47 8
        $key = $this->buildKeyName($key);
48
49 8
        $ttl = is_null($ttl) ? $this->defaultLifetime : $ttl;
50
51 8
        return apcu_store($key, $value, (int) $ttl);
0 ignored issues
show
Bug Compatibility introduced by
The expression apcu_store($key, $value, (int) $ttl); of type boolean|array adds the type array to the return on line 51 which is incompatible with the return type declared by the interface Psr\SimpleCache\CacheInterface::set of type boolean.
Loading history...
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 2
    public function delete($key)
58
    {
59 2
        $this->assertKeyName($key);
60 2
        $key = $this->buildKeyName($key);
61
62 2
        return apcu_delete($key);
0 ignored issues
show
Bug Compatibility introduced by
The expression apcu_delete($key); of type boolean|string[] adds the type string[] to the return on line 62 which is incompatible with the return type declared by the interface Psr\SimpleCache\CacheInterface::delete of type boolean.
Loading history...
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 4
    public function clear()
69
    {
70 4
        return apcu_clear_cache();
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 2
    public function getMultiple($keys, $default = null)
77
    {
78 2
        $this->assertKeyNames($keys);
79 2
        $keys = $this->buildKeyNames($keys);
80
81 2
        $result = apcu_fetch($keys);
82
83 2
        if (!is_null($default) && is_array($result) && count($keys) > count($result)) {
84 2
            $notFoundKeys = array_diff($keys, array_keys($result));
85 2
            $result = array_merge($result, array_fill_keys($notFoundKeys, $default));
86 1
        }
87
88 2
        $mappedResult = [];
89
90 2
        foreach ($result as $key => $value) {
91 2
            $key = preg_replace("/^$this->namespace/", '', $key);
92
93 2
            $mappedResult[$key] = $value;
94 1
        }
95
96 2
        return $mappedResult;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $mappedResult; (array) is incompatible with the return type declared by the interface Psr\SimpleCache\CacheInterface::getMultiple of type Psr\SimpleCache\iterable.

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...
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 2
    public function setMultiple($values, $ttl = null)
103
    {
104 2
        $this->assertKeyNames(array_keys($values));
105
106 2
        $mappedByNamespaceValues = [];
107
108 2
        foreach ($values as $key => $value) {
109 2
            $mappedByNamespaceValues[$this->buildKeyName($key)] = $value;
110 1
        }
111
112 2
        $ttl = is_null($ttl) ? $this->defaultLifetime : $ttl;
113
114 2
        $result = apcu_store($mappedByNamespaceValues, (int) $ttl);
115
116 2
        return $result === true ? true : (is_array($result) && count($result) == 0 ? true: false);
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 2
    public function deleteMultiple($keys)
123
    {
124 2
        $this->assertKeyNames($keys);
125 2
        $keys = $this->buildKeyNames($keys);
126
127 2
        $result = apcu_delete($keys);
128
129 2
        return count($result) === count($keys) ? false : true;
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135 4
    public function has($key)
136
    {
137 4
        $this->assertKeyName($key);
138 4
        $key = $this->buildKeyName($key);
139
140 4
        return (bool) apcu_exists($key);
141
    }
142
143
    /**
144
     * @param string $key
145
     *
146
     * @return string
147
     */
148 10
    private function buildKeyName($key)
149
    {
150 10
        return $this->namespace . $key;
151
    }
152
153
    /**
154
     * @param string[] $keys
155
     *
156
     * @return string[]
157
     */
158 2
    private function buildKeyNames(array $keys)
159
    {
160
        return array_map(function($key) {
161 2
            return $this->buildKeyName($key);
162 2
        }, $keys);
163
164
    }
165
166
    /**
167
     * @param mixed $key
168
     *
169
     * @throws ApcuInvalidCacheKeyException
170
     */
171 20
    private function assertKeyName($key)
172
    {
173 20
        if (!is_scalar($key) || is_bool($key)) {
174 10
            throw new ApcuInvalidCacheKeyException();
175
        }
176 10
    }
177
178
    /**
179
     * @param string[] $keys
180
     *
181
     * @throws ApcuInvalidCacheKeyException
182
     */
183
    private function assertKeyNames(array $keys)
184
    {
185 2
        array_map(function ($value) {
186 2
            $this->assertKeyName($value);
187 2
        }, $keys);
188 2
    }
189
}
190