Skip to content

Commit fac38fc

Browse files
committed
refactor!: remove param types for traditional validation rules
There is no guarantee that a string will be passed, so declaring strict types may result in a TypeError.
1 parent 365e4c1 commit fac38fc

File tree

2 files changed

+170
-38
lines changed

2 files changed

+170
-38
lines changed

system/Validation/FormatRules.php

Lines changed: 163 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@ class FormatRules
2222
{
2323
/**
2424
* Alpha
25+
*
26+
* @param string|null $str
2527
*/
26-
public function alpha(?string $str = null): bool
28+
public function alpha($str = null): bool
2729
{
28-
return ctype_alpha($str ?? '');
30+
if (! is_string($str)) {
31+
$str = (string) $str;
32+
}
33+
34+
return ctype_alpha($str);
2935
}
3036

3137
/**
@@ -35,12 +41,16 @@ public function alpha(?string $str = null): bool
3541
*
3642
* @return bool True if alpha with spaces, else false.
3743
*/
38-
public function alpha_space(?string $value = null): bool
44+
public function alpha_space($value = null): bool
3945
{
4046
if ($value === null) {
4147
return true;
4248
}
4349

50+
if (! is_string($value)) {
51+
$value = (string) $value;
52+
}
53+
4454
// @see https://regex101.com/r/LhqHPO/1
4555
return (bool) preg_match('/\A[A-Z ]+\z/i', $value);
4656
}
@@ -49,13 +59,19 @@ public function alpha_space(?string $value = null): bool
4959
* Alphanumeric with underscores and dashes
5060
*
5161
* @see https://regex101.com/r/XfVY3d/1
62+
*
63+
* @param string|null $str
5264
*/
53-
public function alpha_dash(?string $str = null): bool
65+
public function alpha_dash($str = null): bool
5466
{
5567
if ($str === null) {
5668
return false;
5769
}
5870

71+
if (! is_string($str)) {
72+
$str = (string) $str;
73+
}
74+
5975
return preg_match('/\A[a-z0-9_-]+\z/i', $str) === 1;
6076
}
6177

@@ -78,24 +94,40 @@ public function alpha_numeric_punct($str)
7894
return false;
7995
}
8096

97+
if (! is_string($str)) {
98+
$str = (string) $str;
99+
}
100+
81101
return preg_match('/\A[A-Z0-9 ~!#$%\&\*\-_+=|:.]+\z/i', $str) === 1;
82102
}
83103

84104
/**
85105
* Alphanumeric
106+
*
107+
* @param string|null $str
86108
*/
87-
public function alpha_numeric(?string $str = null): bool
109+
public function alpha_numeric($str = null): bool
88110
{
89-
return ctype_alnum($str ?? '');
111+
if (! is_string($str)) {
112+
$str = (string) $str;
113+
}
114+
115+
return ctype_alnum($str);
90116
}
91117

92118
/**
93119
* Alphanumeric w/ spaces
120+
*
121+
* @param string|null $str
94122
*/
95-
public function alpha_numeric_space(?string $str = null): bool
123+
public function alpha_numeric_space($str = null): bool
96124
{
125+
if (! is_string($str)) {
126+
$str = (string) $str;
127+
}
128+
97129
// @see https://regex101.com/r/0AZDME/1
98-
return (bool) preg_match('/\A[A-Z0-9 ]+\z/i', $str ?? '');
130+
return (bool) preg_match('/\A[A-Z0-9 ]+\z/i', $str);
99131
}
100132

101133
/**
@@ -113,109 +145,175 @@ public function string($str = null): bool
113145

114146
/**
115147
* Decimal number
148+
*
149+
* @param string|null $str
116150
*/
117-
public function decimal(?string $str = null): bool
151+
public function decimal($str = null): bool
118152
{
153+
if (! is_string($str)) {
154+
$str = (string) $str;
155+
}
156+
119157
// @see https://regex101.com/r/HULifl/2/
120-
return (bool) preg_match('/\A[-+]?\d{0,}\.?\d+\z/', $str ?? '');
158+
return (bool) preg_match('/\A[-+]?\d{0,}\.?\d+\z/', $str);
121159
}
122160

123161
/**
124162
* String of hexidecimal characters
163+
*
164+
* @param string|null $str
125165
*/
126-
public function hex(?string $str = null): bool
166+
public function hex($str = null): bool
127167
{
128-
return ctype_xdigit($str ?? '');
168+
if (! is_string($str)) {
169+
$str = (string) $str;
170+
}
171+
172+
return ctype_xdigit($str);
129173
}
130174

131175
/**
132176
* Integer
177+
*
178+
* @param string|null $str
133179
*/
134-
public function integer(?string $str = null): bool
180+
public function integer($str = null): bool
135181
{
136-
return (bool) preg_match('/\A[\-+]?\d+\z/', $str ?? '');
182+
if (! is_string($str)) {
183+
$str = (string) $str;
184+
}
185+
186+
return (bool) preg_match('/\A[\-+]?\d+\z/', $str);
137187
}
138188

139189
/**
140190
* Is a Natural number (0,1,2,3, etc.)
191+
*
192+
* @param string|null $str
141193
*/
142-
public function is_natural(?string $str = null): bool
194+
public function is_natural($str = null): bool
143195
{
144-
return ctype_digit($str ?? '');
196+
if (! is_string($str)) {
197+
$str = (string) $str;
198+
}
199+
200+
return ctype_digit($str);
145201
}
146202

147203
/**
148204
* Is a Natural number, but not a zero (1,2,3, etc.)
205+
*
206+
* @param string|null $str
149207
*/
150-
public function is_natural_no_zero(?string $str = null): bool
208+
public function is_natural_no_zero($str = null): bool
151209
{
152-
return $str !== '0' && ctype_digit($str ?? '');
210+
if (! is_string($str)) {
211+
$str = (string) $str;
212+
}
213+
214+
return $str !== '0' && ctype_digit($str);
153215
}
154216

155217
/**
156218
* Numeric
219+
*
220+
* @param string|null $str
157221
*/
158-
public function numeric(?string $str = null): bool
222+
public function numeric($str = null): bool
159223
{
224+
if (! is_string($str)) {
225+
$str = (string) $str;
226+
}
227+
160228
// @see https://regex101.com/r/bb9wtr/2
161-
return (bool) preg_match('/\A[\-+]?\d*\.?\d+\z/', $str ?? '');
229+
return (bool) preg_match('/\A[\-+]?\d*\.?\d+\z/', $str);
162230
}
163231

164232
/**
165233
* Compares value against a regular expression pattern.
234+
*
235+
* @param string|null $str
166236
*/
167-
public function regex_match(?string $str, string $pattern): bool
237+
public function regex_match($str, string $pattern): bool
168238
{
239+
if (! is_string($str)) {
240+
$str = (string) $str;
241+
}
242+
169243
if (strpos($pattern, '/') !== 0) {
170244
$pattern = "/{$pattern}/";
171245
}
172246

173-
return (bool) preg_match($pattern, $str ?? '');
247+
return (bool) preg_match($pattern, $str);
174248
}
175249

176250
/**
177251
* Validates that the string is a valid timezone as per the
178252
* timezone_identifiers_list function.
179253
*
180254
* @see http://php.net/manual/en/datetimezone.listidentifiers.php
255+
*
256+
* @param string|null $str
181257
*/
182-
public function timezone(?string $str = null): bool
258+
public function timezone($str = null): bool
183259
{
184-
return in_array($str ?? '', timezone_identifiers_list(), true);
260+
if (! is_string($str)) {
261+
$str = (string) $str;
262+
}
263+
264+
return in_array($str, timezone_identifiers_list(), true);
185265
}
186266

187267
/**
188268
* Valid Base64
189269
*
190270
* Tests a string for characters outside of the Base64 alphabet
191271
* as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
272+
*
273+
* @param string|null $str
192274
*/
193-
public function valid_base64(?string $str = null): bool
275+
public function valid_base64($str = null): bool
194276
{
195277
if ($str === null) {
196278
return false;
197279
}
198280

281+
if (! is_string($str)) {
282+
$str = (string) $str;
283+
}
284+
199285
return base64_encode(base64_decode($str, true)) === $str;
200286
}
201287

202288
/**
203289
* Valid JSON
290+
*
291+
* @param string|null $str
204292
*/
205-
public function valid_json(?string $str = null): bool
293+
public function valid_json($str = null): bool
206294
{
207-
json_decode($str ?? '');
295+
if (! is_string($str)) {
296+
$str = (string) $str;
297+
}
298+
299+
json_decode($str);
208300

209301
return json_last_error() === JSON_ERROR_NONE;
210302
}
211303

212304
/**
213305
* Checks for a correctly formatted email address
306+
*
307+
* @param string|null $str
214308
*/
215-
public function valid_email(?string $str = null): bool
309+
public function valid_email($str = null): bool
216310
{
311+
if (! is_string($str)) {
312+
$str = (string) $str;
313+
}
314+
217315
// @see https://regex101.com/r/wlJG1t/1/
218-
if (function_exists('idn_to_ascii') && defined('INTL_IDNA_VARIANT_UTS46') && preg_match('#\A([^@]+)@(.+)\z#', $str ?? '', $matches)) {
316+
if (function_exists('idn_to_ascii') && defined('INTL_IDNA_VARIANT_UTS46') && preg_match('#\A([^@]+)@(.+)\z#', $str, $matches)) {
219317
$str = $matches[1] . '@' . idn_to_ascii($matches[2], 0, INTL_IDNA_VARIANT_UTS46);
220318
}
221319

@@ -227,10 +325,16 @@ public function valid_email(?string $str = null): bool
227325
*
228326
* Example:
229327
328+
*
329+
* @param string|null $str
230330
*/
231-
public function valid_emails(?string $str = null): bool
331+
public function valid_emails($str = null): bool
232332
{
233-
foreach (explode(',', $str ?? '') as $email) {
333+
if (! is_string($str)) {
334+
$str = (string) $str;
335+
}
336+
337+
foreach (explode(',', $str) as $email) {
234338
$email = trim($email);
235339

236340
if ($email === '') {
@@ -248,11 +352,16 @@ public function valid_emails(?string $str = null): bool
248352
/**
249353
* Validate an IP address (human readable format or binary string - inet_pton)
250354
*
355+
* @param string|null $ip
251356
* @param string|null $which IP protocol: 'ipv4' or 'ipv6'
252357
*/
253-
public function valid_ip(?string $ip = null, ?string $which = null): bool
358+
public function valid_ip($ip = null, ?string $which = null): bool
254359
{
255-
if (empty($ip)) {
360+
if (! is_string($ip)) {
361+
$ip = (string) $ip;
362+
}
363+
364+
if ($ip === '') {
256365
return false;
257366
}
258367

@@ -278,13 +387,19 @@ public function valid_ip(?string $ip = null, ?string $which = null): bool
278387
*
279388
* Warning: this rule will pass basic strings like
280389
* "banana"; use valid_url_strict for a stricter rule.
390+
*
391+
* @param string|null $str
281392
*/
282-
public function valid_url(?string $str = null): bool
393+
public function valid_url($str = null): bool
283394
{
284395
if (empty($str)) {
285396
return false;
286397
}
287398

399+
if (! is_string($str)) {
400+
$str = (string) $str;
401+
}
402+
288403
if (preg_match('/\A(?:([^:]*)\:)?\/\/(.+)\z/', $str, $matches)) {
289404
if (! in_array($matches[1], ['http', 'https'], true)) {
290405
return false;
@@ -301,14 +416,19 @@ public function valid_url(?string $str = null): bool
301416
/**
302417
* Checks a URL to ensure it's formed correctly.
303418
*
419+
* @param string|null $str
304420
* @param string|null $validSchemes comma separated list of allowed schemes
305421
*/
306-
public function valid_url_strict(?string $str = null, ?string $validSchemes = null): bool
422+
public function valid_url_strict($str = null, ?string $validSchemes = null): bool
307423
{
308424
if (empty($str)) {
309425
return false;
310426
}
311427

428+
if (! is_string($str)) {
429+
$str = (string) $str;
430+
}
431+
312432
// parse_url() may return null and false
313433
$scheme = strtolower((string) parse_url($str, PHP_URL_SCHEME));
314434
$validSchemes = explode(
@@ -322,10 +442,16 @@ public function valid_url_strict(?string $str = null, ?string $validSchemes = nu
322442

323443
/**
324444
* Checks for a valid date and matches a given date format
445+
*
446+
* @param string|null $str
325447
*/
326-
public function valid_date(?string $str = null, ?string $format = null): bool
448+
public function valid_date($str = null, ?string $format = null): bool
327449
{
328-
if ($str === null) {
450+
if (! is_string($str)) {
451+
$str = (string) $str;
452+
}
453+
454+
if ($str === '') {
329455
return false;
330456
}
331457

0 commit comments

Comments
 (0)