|
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(); |
|
|
|
|
|
|
95
|
|
|
if ( |
|
96
|
|
|
empty($this->readPostings) |
|
97
|
|
|
|| !preg_match('/^[0-9\.]*$/', $this->readPostings) |
|
|
|
|
|
|
98
|
|
|
) { |
|
99
|
|
|
$this->readPostings = []; |
|
100
|
|
|
} else { |
|
101
|
|
|
$this->readPostings = explode('.', $this->readPostings); |
|
|
|
|
|
|
102
|
|
|
$this->readPostings = array_fill_keys($this->readPostings, 1); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $this->readPostings; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
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
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. 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.