These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace Redaxscript; |
||
3 | |||
4 | use function is_numeric; |
||
5 | use function random_int; |
||
6 | |||
7 | /** |
||
8 | * parent class to provide a mathematical task |
||
9 | * |
||
10 | * @since 2.0.0 |
||
11 | * |
||
12 | * @package Redaxscript |
||
13 | * @category Captcha |
||
14 | * @author Henry Ruhs |
||
15 | */ |
||
16 | |||
17 | class Captcha |
||
18 | { |
||
19 | /** |
||
20 | * instance of the language class |
||
21 | * |
||
22 | * @var Language |
||
23 | */ |
||
24 | |||
25 | protected $_language; |
||
26 | |||
27 | /** |
||
28 | * captcha operator mode |
||
29 | * |
||
30 | * @var int |
||
31 | */ |
||
32 | |||
33 | protected $_mode; |
||
34 | |||
35 | /** |
||
36 | * task to be solved |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | |||
41 | protected $_task; |
||
42 | |||
43 | /** |
||
44 | * solution to the task |
||
45 | * |
||
46 | * @var int |
||
47 | */ |
||
48 | |||
49 | protected $_solution; |
||
50 | |||
51 | /** |
||
52 | * allowed range for the task |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | |||
57 | protected $_rangeArray = |
||
58 | [ |
||
59 | 'min' => 1, |
||
60 | 'max' => 10 |
||
61 | ]; |
||
62 | |||
63 | /** |
||
64 | * array of mathematical operators used for the task |
||
65 | * |
||
66 | * @var array |
||
67 | */ |
||
68 | |||
69 | protected $_operatorArray = |
||
70 | [ |
||
71 | 1 => 'plus', |
||
72 | -1 => 'minus' |
||
73 | ]; |
||
74 | |||
75 | /** |
||
76 | * constructor of the class |
||
77 | * |
||
78 | * @since 2.4.0 |
||
79 | * |
||
80 | * @param Language $language instance of the language class |
||
81 | */ |
||
82 | |||
83 | 5 | public function __construct(Language $language) |
|
84 | { |
||
85 | 5 | $this->_language = $language; |
|
86 | 5 | } |
|
87 | |||
88 | /** |
||
89 | * init the class |
||
90 | * |
||
91 | * @since 2.4.0 |
||
92 | * |
||
93 | * @param int $mode captcha operator mode |
||
94 | */ |
||
95 | |||
96 | 5 | public function init(int $mode = null) : void |
|
97 | { |
||
98 | 5 | if (is_numeric($mode)) |
|
99 | { |
||
100 | 2 | $this->_mode = $mode; |
|
0 ignored issues
–
show
|
|||
101 | } |
||
102 | else |
||
103 | { |
||
104 | 3 | $settingModel = new Model\Setting(); |
|
105 | 3 | $this->_mode = $settingModel->get('captcha'); |
|
0 ignored issues
–
show
It seems like
$settingModel->get('captcha') can also be of type string . However, the property $_mode is declared as type integer . 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...
|
|||
106 | } |
||
107 | 5 | $this->_create(); |
|
108 | 5 | } |
|
109 | |||
110 | /** |
||
111 | * get the task |
||
112 | * |
||
113 | * @since 2.0.0 |
||
114 | * |
||
115 | * @return string |
||
116 | */ |
||
117 | |||
118 | 1 | public function getTask() : string |
|
119 | { |
||
120 | 1 | return $this->_task; |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * get the solution |
||
125 | * |
||
126 | * @since 2.6.0 |
||
127 | * |
||
128 | * @return int |
||
129 | */ |
||
130 | |||
131 | 3 | public function getSolution() : int |
|
132 | { |
||
133 | 3 | return $this->_solution; |
|
134 | } |
||
135 | |||
136 | /** |
||
137 | * get the minimum range |
||
138 | * |
||
139 | * @since 2.6.0 |
||
140 | * |
||
141 | * @return int |
||
142 | */ |
||
143 | |||
144 | 5 | public function getMin() : int |
|
145 | { |
||
146 | 5 | return $this->_rangeArray['min']; |
|
147 | } |
||
148 | |||
149 | /** |
||
150 | * get the maximum range |
||
151 | * |
||
152 | * @since 2.6.0 |
||
153 | * |
||
154 | * @return int |
||
155 | */ |
||
156 | |||
157 | 5 | public function getMax() : int |
|
158 | { |
||
159 | 5 | return $this->_rangeArray['max']; |
|
160 | } |
||
161 | |||
162 | /** |
||
163 | * get the mathematical operator used for the task |
||
164 | * |
||
165 | * @since 2.0.0 |
||
166 | * |
||
167 | * @return int |
||
168 | */ |
||
169 | |||
170 | 5 | protected function _getOperator() : int |
|
171 | { |
||
172 | 5 | if ($this->_mode === 2) |
|
173 | { |
||
174 | 1 | return 1; |
|
175 | } |
||
176 | 4 | if ($this->_mode === 3) |
|
177 | { |
||
178 | 1 | return -1; |
|
179 | } |
||
180 | 3 | return random_int(0, 1) * 2 - 1; |
|
181 | } |
||
182 | |||
183 | /** |
||
184 | * create a task of two numbers between allowable range |
||
185 | * |
||
186 | * @since 2.0.0 |
||
187 | */ |
||
188 | |||
189 | 5 | protected function _create() : void |
|
190 | { |
||
191 | /* range */ |
||
192 | |||
193 | 5 | $min = $this->getMin(); |
|
194 | 5 | $max = $this->getMax(); |
|
195 | |||
196 | /* random numbers */ |
||
197 | |||
198 | 5 | $a = random_int($min + 1, $max); |
|
199 | 5 | $b = random_int($min, $a - 1); |
|
200 | |||
201 | /* operator */ |
||
202 | |||
203 | 5 | $c = $this->_getOperator(); |
|
204 | 5 | $operator = $this->_operatorArray[$c]; |
|
205 | |||
206 | /* solution and task */ |
||
207 | |||
208 | 5 | $this->_solution = $a + $b * $c; |
|
209 | 5 | $this->_task = $this->_language->get($a, '_number') . ' ' . $this->_language->get($operator) . ' ' . $this->_language->get($b, '_number'); |
|
210 | 5 | } |
|
211 | } |
||
212 |
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.