1 | <?php |
||
2 | |||
3 | namespace Bavix\Context; |
||
4 | |||
5 | use Bavix\Exceptions\Invalid; |
||
6 | use Bavix\Exceptions\Runtime; |
||
7 | use Bavix\Slice\Slice; |
||
8 | use Carbon\Carbon; |
||
9 | |||
10 | /** |
||
11 | * Class Configure |
||
12 | * |
||
13 | * @package Bavix\Context |
||
14 | * |
||
15 | * @property-read int $expire |
||
16 | * @property-read string|null $domain |
||
17 | * @property-read bool $secure |
||
18 | * @property-read string $path |
||
19 | * @property-read bool $httpOnly |
||
20 | */ |
||
21 | class Configure |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * default value two week |
||
26 | * |
||
27 | * @var int |
||
28 | */ |
||
29 | protected $expire = 1209600; |
||
30 | |||
31 | /** |
||
32 | * @var null|string |
||
33 | */ |
||
34 | protected $domain = null; |
||
35 | |||
36 | /** |
||
37 | * @var bool |
||
38 | */ |
||
39 | protected $secure = false; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $path = '/'; |
||
45 | |||
46 | /** |
||
47 | * @var bool |
||
48 | */ |
||
49 | protected $httpOnly = false; |
||
50 | |||
51 | /** |
||
52 | * Configure constructor. |
||
53 | * |
||
54 | * @param Slice|array $slice |
||
55 | */ |
||
56 | 17 | public function __construct($slice = null) |
|
57 | { |
||
58 | 17 | if ($slice) |
|
59 | { |
||
60 | 1 | $this->update($slice); |
|
61 | } |
||
62 | 17 | } |
|
63 | |||
64 | /** |
||
65 | * @param Slice|array $slice |
||
66 | * |
||
67 | * @return self |
||
68 | */ |
||
69 | 7 | protected function update($slice): self |
|
70 | { |
||
71 | 7 | if (is_array($slice)) |
|
72 | { |
||
73 | 5 | $slice = new Slice($slice); |
|
74 | } |
||
75 | |||
76 | 7 | $this->expire = $slice->getData('expire', $this->expire); |
|
0 ignored issues
–
show
|
|||
77 | 7 | $this->domain = $slice->getData('domain', $this->domain); |
|
0 ignored issues
–
show
It seems like
$slice->getData('domain', $this->domain) can also be of type array . However, the property $domain is declared as type null|string . 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 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...
|
|||
78 | 7 | $this->secure = $slice->getData('secure', $this->secure); |
|
0 ignored issues
–
show
It seems like
$slice->getData('secure', $this->secure) can also be of type array . However, the property $secure is declared as type boolean . 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 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...
|
|||
79 | 7 | $this->path = $slice->getData('path', $this->path); |
|
0 ignored issues
–
show
It seems like
$slice->getData('path', $this->path) can also be of type array . However, the property $path is declared as type string . 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 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...
|
|||
80 | 7 | $this->httpOnly = $slice->getData('httpOnly', $this->httpOnly); |
|
0 ignored issues
–
show
It seems like
$slice->getData('httpOnly', $this->httpOnly) can also be of type array . However, the property $httpOnly is declared as type boolean . 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 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...
|
|||
81 | |||
82 | 7 | if ($this->expire instanceof Carbon) |
|
83 | { |
||
84 | 1 | $this->expire = $this->expire->timestamp; |
|
85 | } |
||
86 | |||
87 | 7 | if ($this->expire > \time()) |
|
88 | { |
||
89 | 1 | $this->expire -= \time(); |
|
90 | } |
||
91 | |||
92 | 7 | return $this; |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * @param Slice|array $slice |
||
97 | * |
||
98 | * @return Configure |
||
99 | */ |
||
100 | 6 | public function modify($slice): self |
|
101 | { |
||
102 | 6 | return (clone $this)->update($slice); |
|
103 | } |
||
104 | |||
105 | /** |
||
106 | * @param string $name |
||
107 | * |
||
108 | * @return mixed |
||
109 | * |
||
110 | * @throws Invalid |
||
111 | */ |
||
112 | 6 | public function __get(string $name) |
|
113 | { |
||
114 | 6 | if (property_exists($this, $name)) |
|
115 | { |
||
116 | 5 | return $this->{$name}; |
|
117 | } |
||
118 | |||
119 | 1 | throw new Invalid('Property not found'); |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * @param string $name |
||
124 | * @param mixed $value |
||
125 | * |
||
126 | * @throws Invalid |
||
127 | * @throws Runtime |
||
128 | */ |
||
129 | 2 | public function __set(string $name, $value) |
|
130 | { |
||
131 | 2 | if (\property_exists($this, $name)) |
|
132 | { |
||
133 | 1 | throw new Runtime('Property is readOnly!'); |
|
134 | } |
||
135 | |||
136 | 1 | throw new Invalid('Property not found!'); |
|
137 | } |
||
138 | |||
139 | /** |
||
140 | * @param string $name |
||
141 | * |
||
142 | * @return bool |
||
143 | */ |
||
144 | 1 | public function __isset(string $name): bool |
|
145 | { |
||
146 | 1 | return \property_exists($this, $name); |
|
147 | } |
||
148 | |||
149 | /** |
||
150 | * @return array |
||
151 | */ |
||
152 | 12 | public function asArray(): array |
|
153 | { |
||
154 | return [ |
||
155 | // expire |
||
156 | 12 | \time() + (int) $this->expire, |
|
157 | |||
158 | // path |
||
159 | 12 | (string) $this->path, |
|
160 | |||
161 | // domain |
||
162 | 12 | $this->domain, |
|
163 | |||
164 | // secure |
||
165 | 12 | (bool) $this->secure, |
|
166 | |||
167 | // httpOnly |
||
168 | 12 | (bool) $this->httpOnly, |
|
169 | ]; |
||
170 | } |
||
171 | |||
172 | } |
||
173 |
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 theid
property of an instance of theAccount
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.