Completed
Push — master ( e9b1ca...cbd317 )
by Henry
07:52
created

includes/Captcha.php (2 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
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 5
	 * @param Language $language instance of the language class
81
	 */
82 5
83 5
	public function __construct(Language $language)
84
	{
85
		$this->_language = $language;
86
	}
87
88
	/**
89
	 * init the class
90
	 *
91
	 * @since 2.4.0
92
	 *
93 5
	 * @param int $mode captcha operator mode
94
	 */
95 5
96
	public function init(int $mode = null)
97 2
	{
98
		if (is_numeric($mode))
99
		{
100
			$this->_mode = $mode;
0 ignored issues
show
Documentation Bug introduced by
It seems like $mode can also be of type double or 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 $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...
101 3
		}
102 3
		else
103
		{
104 5
			$settingModel = new Model\Setting();
105 5
			$this->_mode = $settingModel->get('captcha');
0 ignored issues
show
Documentation Bug introduced by
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 $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...
106
		}
107
		$this->_create();
108
	}
109
110
	/**
111
	 * get the task
112
	 *
113
	 * @since 2.0.0
114
	 *
115 1
	 * @return string
116
	 */
117 1
118
	public function getTask() : string
119
	{
120
		return $this->_task;
121
	}
122
123
	/**
124
	 * get the solution
125
	 *
126
	 * @since 2.6.0
127
	 *
128 3
	 * @return int
129
	 */
130 3
131
	public function getSolution() : int
132
	{
133
		return $this->_solution;
134
	}
135
136
	/**
137
	 * get the minimum range
138
	 *
139
	 * @since 2.6.0
140
	 *
141 5
	 * @return int
142
	 */
143 5
144
	public function getMin() : int
145
	{
146
		return $this->_rangeArray['min'];
147
	}
148
149
	/**
150
	 * get the maximum range
151
	 *
152
	 * @since 2.6.0
153
	 *
154 5
	 * @return int
155
	 */
156 5
157
	public function getMax() : int
158
	{
159
		return $this->_rangeArray['max'];
160
	}
161
162
	/**
163
	 * get the mathematical operator used for the task
164
	 *
165
	 * @since 2.0.0
166
	 *
167 5
	 * @return int
168
	 */
169 5
170
	protected function _getOperator() : int
171 1
	{
172
		if ($this->_mode === 2)
173 4
		{
174
			return 1;
175 1
		}
176
		if ($this->_mode === 3)
177 3
		{
178
			return -1;
179
		}
180
		return random_int(0, 1) * 2 - 1;
181
	}
182
183
	/**
184
	 * create a task of two numbers between allowable range
185
	 *
186 5
	 * @since 2.0.0
187
	 */
188
189
	protected function _create()
190 5
	{
191 5
		/* range */
192
193
		$min = $this->getMin();
194
		$max = $this->getMax();
195 5
196 5
		/* random numbers */
197
198
		$a = random_int($min + 1, $max);
199
		$b = random_int($min, $a - 1);
200 5
201 5
		/* operator */
202
203
		$c = $this->_getOperator();
204
		$operator = $this->_operatorArray[$c];
205 5
206 5
		/* solution and task */
207 5
208
		$this->_solution = $a + $b * $c;
209
		$this->_task = $this->_language->get($a, '_number') . ' ' . $this->_language->get($operator) . ' ' . $this->_language->get($b, '_number');
210
	}
211
}
212