Skip to content

Commit 4c50b10

Browse files
committed
Fix deprecated usage of null on string params of internal functions
1 parent 8b01f90 commit 4c50b10

File tree

15 files changed

+78
-143
lines changed

15 files changed

+78
-143
lines changed

app/Config/Mimes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ public static function guessExtensionFromType(string $type, ?string $proposedExt
509509
{
510510
$type = trim(strtolower($type), '. ');
511511

512-
$proposedExtension = trim(strtolower($proposedExtension));
512+
$proposedExtension = trim(strtolower($proposedExtension ?? ''));
513513

514514
if ($proposedExtension !== '') {
515515
if (array_key_exists($proposedExtension, static::$mimes) && in_array($type, is_string(static::$mimes[$proposedExtension]) ? [static::$mimes[$proposedExtension]] : static::$mimes[$proposedExtension], true)) {

system/CLI/CLI.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -865,10 +865,12 @@ public static function getOptionString(bool $useLongOpts = false, bool $trim = f
865865
$out .= "-{$name} ";
866866
}
867867

868-
// If there's a space, we need to group
869-
// so it will pass correctly.
868+
if ($value === null) {
869+
continue;
870+
}
871+
870872
if (mb_strpos($value, ' ') !== false) {
871-
$out .= '"' . $value . '" ';
873+
$out .= "\"{$value}\" ";
872874
} elseif ($value !== null) {
873875
$out .= "{$value} ";
874876
}

system/Database/BaseUtils.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public function repairTable(string $tableName)
200200
public function getCSVFromResult(ResultInterface $query, string $delim = ',', string $newline = "\n", string $enclosure = '"')
201201
{
202202
$out = '';
203-
// First generate the headings from the table column names
203+
204204
foreach ($query->getFieldNames() as $name) {
205205
$out .= $enclosure . str_replace($enclosure, $enclosure . $enclosure, $name) . $enclosure . $delim;
206206
}
@@ -212,7 +212,7 @@ public function getCSVFromResult(ResultInterface $query, string $delim = ',', st
212212
$line = [];
213213

214214
foreach ($row as $item) {
215-
$line[] = $enclosure . str_replace($enclosure, $enclosure . $enclosure, $item) . $enclosure;
215+
$line[] = $enclosure . str_replace($enclosure, $enclosure . $enclosure, $item ?? '') . $enclosure;
216216
}
217217

218218
$out .= implode($delim, $line) . $newline;

system/Database/Forge.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ public function dropKey(string $table, string $keyName)
462462
public function dropForeignKey(string $table, string $foreignName)
463463
{
464464
$sql = sprintf(
465-
$this->dropConstraintStr,
465+
(string) $this->dropConstraintStr,
466466
$this->db->escapeIdentifiers($this->db->DBPrefix . $table),
467467
$this->db->escapeIdentifiers($this->db->DBPrefix . $foreignName)
468468
);

system/Database/Postgre/Connection.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,11 @@ protected function buildDSN()
433433
$this->DSN = "host={$this->hostname} ";
434434
}
435435

436-
if (! empty($this->port) && ctype_digit($this->port)) {
437-
$this->DSN .= "port={$this->port} ";
436+
// ctype_digit only accepts strings
437+
$port = (string) $this->port;
438+
439+
if ($port !== '' && ctype_digit($port)) {
440+
$this->DSN .= "port={$port} ";
438441
}
439442

440443
if ($this->username !== '') {

system/Filters/Filters.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -397,13 +397,10 @@ protected function processGlobals(?string $uri = null)
397397
return;
398398
}
399399

400-
$uri = strtolower(trim($uri, '/ '));
400+
$uri = strtolower(trim($uri ?? '', '/ '));
401401

402402
// Add any global filters, unless they are excluded for this URI
403-
$sets = [
404-
'before',
405-
'after',
406-
];
403+
$sets = ['before', 'after'];
407404

408405
foreach ($sets as $set) {
409406
if (isset($this->config->globals[$set])) {

system/HTTP/CLIRequest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,13 @@ public function getOptionString(bool $useLongOpts = false): string
139139
$out .= "-{$name} ";
140140
}
141141

142-
// If there's a space, we need to group
143-
// so it will pass correctly.
142+
if ($value === null) {
143+
continue;
144+
}
145+
144146
if (mb_strpos($value, ' ') !== false) {
145147
$out .= '"' . $value . '" ';
146-
} elseif ($value !== null) {
148+
} else {
147149
$out .= "{$value} ";
148150
}
149151
}

system/HTTP/CURLRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ protected function applyMethod(string $method, array $curlOptions): array
424424
$this->method = $method;
425425
$curlOptions[CURLOPT_CUSTOMREQUEST] = $method;
426426

427-
$size = strlen($this->body);
427+
$size = strlen($this->body ?? '');
428428

429429
// Have content?
430430
if ($size > 0) {

system/HTTP/ResponseTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ public function send()
435435
if ($this->CSPEnabled === true) {
436436
$this->CSP->finalize($this);
437437
} else {
438-
$this->body = str_replace(['{csp-style-nonce}', '{csp-script-nonce}'], '', $this->body);
438+
$this->body = str_replace(['{csp-style-nonce}', '{csp-script-nonce}'], '', $this->body ?? '');
439439
}
440440

441441
$this->sendHeaders();

system/I18n/Time.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,25 +75,23 @@ class Time extends DateTime
7575
*/
7676
public function __construct(?string $time = null, $timezone = null, ?string $locale = null)
7777
{
78-
// If no locale was provided, grab it from Locale (set by IncomingRequest for web requests)
79-
$this->locale = ! empty($locale) ? $locale : Locale::getDefault();
78+
$this->locale = $locale ?: Locale::getDefault();
8079

81-
// If a test instance has been provided, use it instead.
82-
if ($time === null && static::$testNow instanceof self) {
83-
if (empty($timezone)) {
84-
$timezone = static::$testNow->getTimezone();
85-
}
80+
$time = $time ?? '';
8681

87-
$time = static::$testNow->toDateTimeString();
82+
// If a test instance has been provided, use it instead.
83+
if ($time === '' && static::$testNow instanceof self) {
84+
$timezone = $timezone ?: static::$testNow->getTimezone();
85+
$time = (string) static::$testNow->toDateTimeString();
8886
}
8987

90-
$timezone = ! empty($timezone) ? $timezone : date_default_timezone_get();
88+
$timezone = $timezone ?: date_default_timezone_get();
9189
$this->timezone = $timezone instanceof DateTimeZone ? $timezone : new DateTimeZone($timezone);
9290

9391
// If the time string was a relative string (i.e. 'next Tuesday')
9492
// then we need to adjust the time going in so that we have a current
9593
// timezone to work with.
96-
if (! empty($time) && (is_string($time) && static::hasRelativeKeywords($time))) {
94+
if ($time !== '' && static::hasRelativeKeywords($time)) {
9795
$instance = new DateTime('now', $this->timezone);
9896
$instance->modify($time);
9997
$time = $instance->format('Y-m-d H:i:s');

system/Security/Security.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public function verify(RequestInterface $request)
288288
throw SecurityException::forDisallowedAction();
289289
}
290290

291-
$json = json_decode($request->getBody());
291+
$json = json_decode($request->getBody() ?? '');
292292

293293
if (isset($_POST[$this->tokenName])) {
294294
// We kill this since we're done and we don't want to pollute the POST array.
@@ -323,9 +323,10 @@ private function getPostedToken(RequestInterface $request): ?string
323323
if ($request->hasHeader($this->headerName) && ! empty($request->header($this->headerName)->getValue())) {
324324
$tokenName = $request->header($this->headerName)->getValue();
325325
} else {
326-
$json = json_decode($request->getBody());
326+
$body = (string) $request->getBody();
327+
$json = json_decode($body);
327328

328-
if (! empty($request->getBody()) && ! empty($json) && json_last_error() === JSON_ERROR_NONE) {
329+
if ($body !== '' && ! empty($json) && json_last_error() === JSON_ERROR_NONE) {
329330
$tokenName = $json->{$this->tokenName} ?? null;
330331
} else {
331332
$tokenName = null;

system/Validation/FormatRules.php

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class FormatRules
2323
*/
2424
public function alpha(?string $str = null): bool
2525
{
26-
return ctype_alpha($str);
26+
return ctype_alpha($str ?? '');
2727
}
2828

2929
/**
@@ -74,7 +74,7 @@ public function alpha_numeric_punct($str)
7474
*/
7575
public function alpha_numeric(?string $str = null): bool
7676
{
77-
return ctype_alnum($str);
77+
return ctype_alnum($str ?? '');
7878
}
7979

8080
/**
@@ -83,7 +83,7 @@ public function alpha_numeric(?string $str = null): bool
8383
public function alpha_numeric_space(?string $str = null): bool
8484
{
8585
// @see https://regex101.com/r/0AZDME/1
86-
return (bool) preg_match('/\A[A-Z0-9 ]+\z/i', $str);
86+
return (bool) preg_match('/\A[A-Z0-9 ]+\z/i', $str ?? '');
8787
}
8888

8989
/**
@@ -105,39 +105,39 @@ public function string($str = null): bool
105105
public function decimal(?string $str = null): bool
106106
{
107107
// @see https://regex101.com/r/HULifl/2/
108-
return (bool) preg_match('/\A[-+]?\d{0,}\.?\d+\z/', $str);
108+
return (bool) preg_match('/\A[-+]?\d{0,}\.?\d+\z/', $str ?? '');
109109
}
110110

111111
/**
112112
* String of hexidecimal characters
113113
*/
114114
public function hex(?string $str = null): bool
115115
{
116-
return ctype_xdigit($str);
116+
return ctype_xdigit($str ?? '');
117117
}
118118

119119
/**
120120
* Integer
121121
*/
122122
public function integer(?string $str = null): bool
123123
{
124-
return (bool) preg_match('/\A[\-+]?\d+\z/', $str);
124+
return (bool) preg_match('/\A[\-+]?\d+\z/', $str ?? '');
125125
}
126126

127127
/**
128128
* Is a Natural number (0,1,2,3, etc.)
129129
*/
130130
public function is_natural(?string $str = null): bool
131131
{
132-
return ctype_digit($str);
132+
return ctype_digit($str ?? '');
133133
}
134134

135135
/**
136136
* Is a Natural number, but not a zero (1,2,3, etc.)
137137
*/
138138
public function is_natural_no_zero(?string $str = null): bool
139139
{
140-
return $str !== '0' && ctype_digit($str);
140+
return $str !== '0' && ctype_digit($str ?? '');
141141
}
142142

143143
/**
@@ -146,7 +146,7 @@ public function is_natural_no_zero(?string $str = null): bool
146146
public function numeric(?string $str = null): bool
147147
{
148148
// @see https://regex101.com/r/bb9wtr/2
149-
return (bool) preg_match('/\A[\-+]?\d*\.?\d+\z/', $str);
149+
return (bool) preg_match('/\A[\-+]?\d*\.?\d+\z/', $str ?? '');
150150
}
151151

152152
/**
@@ -158,7 +158,7 @@ public function regex_match(?string $str, string $pattern): bool
158158
$pattern = "/{$pattern}/";
159159
}
160160

161-
return (bool) preg_match($pattern, $str);
161+
return (bool) preg_match($pattern, $str ?? '');
162162
}
163163

164164
/**
@@ -171,7 +171,7 @@ public function regex_match(?string $str, string $pattern): bool
171171
*/
172172
public function timezone(?string $str = null): bool
173173
{
174-
return in_array($str, timezone_identifiers_list(), true);
174+
return in_array($str ?? '', timezone_identifiers_list(), true);
175175
}
176176

177177
/**
@@ -184,6 +184,10 @@ public function timezone(?string $str = null): bool
184184
*/
185185
public function valid_base64(?string $str = null): bool
186186
{
187+
if ($str === null) {
188+
return false;
189+
}
190+
187191
return base64_encode(base64_decode($str, true)) === $str;
188192
}
189193

@@ -194,7 +198,7 @@ public function valid_base64(?string $str = null): bool
194198
*/
195199
public function valid_json(?string $str = null): bool
196200
{
197-
json_decode($str);
201+
json_decode($str ?? '');
198202

199203
return json_last_error() === JSON_ERROR_NONE;
200204
}
@@ -207,7 +211,7 @@ public function valid_json(?string $str = null): bool
207211
public function valid_email(?string $str = null): bool
208212
{
209213
// @see https://regex101.com/r/wlJG1t/1/
210-
if (function_exists('idn_to_ascii') && defined('INTL_IDNA_VARIANT_UTS46') && preg_match('#\A([^@]+)@(.+)\z#', $str, $matches)) {
214+
if (function_exists('idn_to_ascii') && defined('INTL_IDNA_VARIANT_UTS46') && preg_match('#\A([^@]+)@(.+)\z#', $str ?? '', $matches)) {
211215
$str = $matches[1] . '@' . idn_to_ascii($matches[2], 0, INTL_IDNA_VARIANT_UTS46);
212216
}
213217

@@ -224,8 +228,9 @@ public function valid_email(?string $str = null): bool
224228
*/
225229
public function valid_emails(?string $str = null): bool
226230
{
227-
foreach (explode(',', $str) as $email) {
231+
foreach (explode(',', $str ?? '') as $email) {
228232
$email = trim($email);
233+
229234
if ($email === '') {
230235
return false;
231236
}
@@ -240,17 +245,14 @@ public function valid_emails(?string $str = null): bool
240245

241246
/**
242247
* Validate an IP address (human readable format or binary string - inet_pton)
243-
*
244-
* @param string $ip IP Address
245-
* @param string $which IP protocol: 'ipv4' or 'ipv6'
246248
*/
247249
public function valid_ip(?string $ip = null, ?string $which = null): bool
248250
{
249251
if (empty($ip)) {
250252
return false;
251253
}
252254

253-
switch (strtolower($which)) {
255+
switch (strtolower($which ?? '')) {
254256
case 'ipv4':
255257
$which = FILTER_FLAG_IPV4;
256258
break;
@@ -260,20 +262,18 @@ public function valid_ip(?string $ip = null, ?string $which = null): bool
260262
break;
261263

262264
default:
263-
$which = null;
264-
break;
265+
$which = 0;
265266
}
266267

267-
return (bool) filter_var($ip, FILTER_VALIDATE_IP, $which) || (! ctype_print($ip) && (bool) filter_var(inet_ntop($ip), FILTER_VALIDATE_IP, $which));
268+
return filter_var($ip, FILTER_VALIDATE_IP, $which) !== false
269+
|| (! ctype_print($ip) && filter_var(inet_ntop($ip), FILTER_VALIDATE_IP, $which) !== false);
268270
}
269271

270272
/**
271273
* Checks a string to ensure it is (loosely) a URL.
272274
*
273275
* Warning: this rule will pass basic strings like
274276
* "banana"; use valid_url_strict for a stricter rule.
275-
*
276-
* @param string $str
277277
*/
278278
public function valid_url(?string $str = null): bool
279279
{
@@ -317,18 +317,16 @@ public function valid_url_strict(?string $str = null, ?string $validSchemes = nu
317317

318318
/**
319319
* Checks for a valid date and matches a given date format
320-
*
321-
* @param string $str
322-
* @param string $format
323320
*/
324321
public function valid_date(?string $str = null, ?string $format = null): bool
325322
{
326323
if (empty($format)) {
327-
return (bool) strtotime($str);
324+
return strtotime($str) !== false;
328325
}
329326

330-
$date = DateTime::createFromFormat($format, $str);
327+
$date = DateTime::createFromFormat($format, $str);
328+
$errors = DateTime::getLastErrors();
331329

332-
return (bool) $date && DateTime::getLastErrors()['warning_count'] === 0 && DateTime::getLastErrors()['error_count'] === 0;
330+
return $date !== false && $errors !== false && $errors['warning_count'] === 0 && $errors['error_count'] === 0;
333331
}
334332
}

0 commit comments

Comments
 (0)