ReadPostingsCookie   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
dl 0
loc 85
rs 10
c 1
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 20 3
A delete() 0 3 1
A _gc() 0 12 2
A _get() 0 17 4
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace Saito\User\ReadPostings;
14
15
use Saito\User\Cookie\Storage;
16
17
/**
18
 * Handles read posting by a client side cookie. Used for non logged-in
19
 * users.
20
 */
21
class ReadPostingsCookie extends ReadPostingsAbstract
22
{
23
    /**
24
     * Max number of postings in cookie
25
     */
26
    protected $maxPostings = 240;
27
28
    /** @var Storage */
29
    protected $storage;
30
31
    /**
32
     * {@inheritDoc}
33
     */
34
    public function set($entries)
35
    {
36
        $entries = $this->_prepareForSave($entries);
37
        if (empty($entries)) {
38
            return;
39
        }
40
41
        $entries = array_fill_keys($entries, 1);
42
        $new = $this->_get() + $entries;
43
        if (empty($new)) {
44
            return;
45
        }
46
        $this->readPostings = $new;
47
48
        $this->_gc();
49
50
        // make simple string and don't encrypt it to keep cookie small enough
51
        // to fit $this->_maxPostings into 4 kB
52
        $data = implode('.', array_keys($this->readPostings));
53
        $this->storage->write($data);
54
    }
55
56
    /**
57
     * {@inheritDoc}
58
     */
59
    public function delete()
60
    {
61
        $this->storage->delete();
62
    }
63
64
    /**
65
     * limits the number of postings saved in cookie
66
     *
67
     * cookie size should not exceed 4 kB
68
     *
69
     * @return void
70
     */
71
    protected function _gc()
72
    {
73
        $overhead = count($this->readPostings) - $this->maxPostings;
74
        if ($overhead < 0) {
75
            return;
76
        }
77
        ksort($this->readPostings);
78
        $this->readPostings = array_slice(
79
            $this->readPostings,
80
            $overhead,
81
            null,
82
            true
83
        );
84
    }
85
86
    /**
87
     * {@inheritDoc}
88
     */
89
    protected function _get()
90
    {
91
        if ($this->readPostings !== null) {
92
            return $this->readPostings;
93
        }
94
        $this->readPostings = $this->storage->read();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->storage->read() can also be of type string. However, the property $readPostings is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
95
        if (
96
            empty($this->readPostings)
97
            || !preg_match('/^[0-9\.]*$/', $this->readPostings)
0 ignored issues
show
Bug introduced by
It seems like $this->readPostings can also be of type array; however, parameter $subject of preg_match() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
            || !preg_match('/^[0-9\.]*$/', /** @scrutinizer ignore-type */ $this->readPostings)
Loading history...
98
        ) {
99
            $this->readPostings = [];
100
        } else {
101
            $this->readPostings = explode('.', $this->readPostings);
0 ignored issues
show
Bug introduced by
It seems like $this->readPostings can also be of type array; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

101
            $this->readPostings = explode('.', /** @scrutinizer ignore-type */ $this->readPostings);
Loading history...
102
            $this->readPostings = array_fill_keys($this->readPostings, 1);
103
        }
104
105
        return $this->readPostings;
106
    }
107
}
108