Passed
Push — develop-3.3.x ( c9dc78...05ea88 )
by Mario
02:41
created

post_data_helper::apply_check()   B

Complexity

Conditions 10
Paths 8

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 17
c 0
b 0
f 0
nc 8
nop 3
dl 0
loc 31
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 *
4
 * PayPal Donation extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2015-2025 Skouat
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace skouat\ppde\helpers;
12
13
use phpbb\language\language;
14
use phpbb\request\request;
15
16
/**
17
 * Helper for handling post data
18
 */
19
class post_data_helper
20
{
21
	/** @var language */
22
	protected $language;
23
24
	/** @var request */
25
	protected $request;
26
27
	/**
28
	 * Constructor
29
	 *
30
	 * @param language $language Language object
31
	 * @param request  $request  Request object
32
	 */
33
	public function __construct(
34
		language $language,
35
		request $request
36
	)
37
	{
38
		$this->language = $language;
39
		$this->request = $request;
40
	}
41
42
	/**
43
	 * Get post data from request
44
	 *
45
	 * @param array $data_ary Data array
46
	 * @return array Post data
47
	 */
48
	public function get_post_data(array $data_ary): array
49
	{
50
		$name = $data_ary['name'];
51
		$default = $data_ary['default'];
52
53
		// Handle default value
54
		if (is_array($default))
55
		{
56
			$default = $default[0];
57
			$multibyte = $default[1] ?? false;
58
		}
59
		else
60
		{
61
			$multibyte = false;
62
		}
63
64
		// Get value from request
65
		$value = $this->request->variable($name, $default, $multibyte);
66
67
		return [
68
			'name'            => $name,
69
			'value'           => $value,
70
			'condition_check' => $data_ary['condition_check'] ?? [],
71
			'force_settings'  => $data_ary['force_settings'] ?? [],
72
			'txn_errors'      => '',
73
		];
74
	}
75
76
	/**
77
	 * Check post data against conditions
78
	 *
79
	 * @param array $post_data Post data
80
	 * @return array Checked post data
81
	 */
82
	public function check_post_data(array $post_data): array
83
	{
84
		if (empty($post_data['condition_check']))
85
		{
86
			return $post_data;
87
		}
88
89
		foreach ($post_data['condition_check'] as $check_type => $check_value)
90
		{
91
			$post_data = $this->apply_check($post_data, $check_type, $check_value);
92
		}
93
94
		return $post_data;
95
	}
96
97
	/**
98
	 * Apply check to post data
99
	 *
100
	 * @param array  $post_data   Post data
101
	 * @param string $check_type  Check type
102
	 * @param mixed  $check_value Check value
103
	 * @return array Updated post data
104
	 */
105
	private function apply_check(array $post_data, string $check_type, $check_value): array
106
	{
107
		switch ($check_type)
108
		{
109
			case 'ascii':
110
				if ($check_value && !$this->is_ascii($post_data['value']))
111
				{
112
					$post_data['txn_errors'] .= '<br>' . $this->language->lang('INVALID_TXN_ASCII', $post_data['name']);
113
				}
114
			break;
115
116
			case 'content':
117
				if (!in_array($post_data['value'], $check_value, true))
118
				{
119
					$post_data['txn_errors'] .= '<br>' . $this->language->lang('INVALID_TXN_CONTENT', $post_data['name']);
120
				}
121
			break;
122
123
			case 'empty':
124
				if ($check_value === false && empty($post_data['value']))
125
				{
126
					$post_data['txn_errors'] .= '<br>' . $this->language->lang('INVALID_TXN_EMPTY', $post_data['name']);
127
				}
128
			break;
129
130
			case 'length':
131
				$post_data = $this->check_length($post_data, $check_value);
132
			break;
133
		}
134
135
		return $post_data;
136
	}
137
138
	/**
139
	 * Check if string contains only ASCII characters
140
	 *
141
	 * @param string $string String to check
142
	 * @return bool Whether string contains only ASCII characters
143
	 */
144
	private function is_ascii(string $string): bool
145
	{
146
		return (bool) preg_match('/^[\x00-\x7F]*$/', $string);
147
	}
148
149
	/**
150
	 * Check length of value
151
	 *
152
	 * @param array $post_data   Post data
153
	 * @param array $check_value Check value
154
	 * @return array Updated post data
155
	 */
156
	private function check_length(array $post_data, array $check_value): array
157
	{
158
		$value_length = utf8_strlen($post_data['value']);
0 ignored issues
show
Bug introduced by
The function utf8_strlen was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

158
		$value_length = /** @scrutinizer ignore-call */ utf8_strlen($post_data['value']);
Loading history...
159
		$expected_length = $check_value['value'];
160
		$operator = $check_value['operator'] ?? '==';
161
162
		$valid = false;
163
		switch ($operator)
164
		{
165
			case '==':
166
				$valid = $value_length == $expected_length;
167
			break;
168
			case '<=':
169
				$valid = $value_length <= $expected_length;
170
			break;
171
			case '>=':
172
				$valid = $value_length >= $expected_length;
173
			break;
174
		}
175
176
		if (!$valid)
177
		{
178
			$post_data['txn_errors'] .= '<br>' . $this->language->lang('INVALID_TXN_LENGTH', $post_data['name']);
179
		}
180
181
		return $post_data;
182
	}
183
184
	/**
185
	 * Set function to apply to post data
186
	 *
187
	 * @param array $post_data Post data
188
	 * @return mixed Modified value
189
	 */
190
	public function set_func(array $post_data)
191
	{
192
		$value = $post_data['value'];
193
194
		foreach ($post_data['force_settings'] as $func => $func_value)
195
		{
196
			switch ($func)
197
			{
198
				case 'length':
199
					$value = utf8_substr($value, 0, (int) $func_value);
0 ignored issues
show
Bug introduced by
The function utf8_substr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

199
					$value = /** @scrutinizer ignore-call */ utf8_substr($value, 0, (int) $func_value);
Loading history...
200
				break;
201
202
				case 'lowercase':
203
					$value = utf8_strtolower($value);
0 ignored issues
show
Bug introduced by
The function utf8_strtolower was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

203
					$value = /** @scrutinizer ignore-call */ utf8_strtolower($value);
Loading history...
204
				break;
205
206
				case 'strtotime':
207
					$value = strtotime($value);
208
				break;
209
			}
210
		}
211
212
		return $value;
213
	}
214
}
215