Skip to content

Commit 505698f

Browse files
0.6.0
* [R] Many fixes for PHP8+ compatibility
1 parent ca7a911 commit 505698f

File tree

7 files changed

+63
-60
lines changed

7 files changed

+63
-60
lines changed

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"platform-check": false
1616
},
1717
"require": {
18-
"php": ">=7.4",
18+
"php": ">=7.4 | 8.*",
1919
"ext-pdo": "*",
2020
"ext-mbstring": "*",
2121
"ext-json": "*"
@@ -29,10 +29,11 @@
2929
"sources/helpers.php"
3030
],
3131
"psr-4": {
32-
"Arris\\" : ["interfaces", "sources"]
32+
"Arris\\" : "sources"
3333
}
3434
},
3535
"require-dev": {
36-
"phpunit/phpunit": "^4.8"
36+
"phpunit/phpunit": "^4.8",
37+
"rector/rector": "^1.1"
3738
}
3839
}

sources/Helpers/FS.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ public static function rmdir($directory): bool
4141
*/
4242
public static function read_all_files($root = '.')
4343
{
44-
$directory_content = [ 'files' => [], 'dirs' => [] ];
45-
$directories = array();
46-
$last_letter = $root[ strlen( $root ) - 1 ];
44+
$directory_content = [
45+
'files' => [],
46+
'dirs' => []
47+
];
48+
$directories = [];
49+
$last_letter = $root[ \strlen( $root ) - 1 ];
4750
$root
4851
= ($last_letter === '\\' || $last_letter === '/')
4952
? $root
@@ -81,7 +84,7 @@ public static function read_all_files($root = '.')
8184
*/
8285
public static function checkPath($path)
8386
{
84-
if (!is_dir( $path ) && !mkdir( $path, 0777, true ) && !is_dir( $path )) {
87+
if (!\is_dir( $path ) && !\mkdir( $path, 0777, true ) && !\is_dir( $path )) {
8588
return false;
8689
//throw new \RuntimeException( sprintf( 'Directory "%s" was not created', $path ) );
8790
}

sources/Helpers/HTTP/RemoteAddress.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,8 @@ public function getIpAddress()
9393
if ($ip) {
9494
return $ip;
9595
}
96-
97-
// direct IP address
98-
if (isset($_SERVER['REMOTE_ADDR'])) {
99-
return $_SERVER['REMOTE_ADDR'];
100-
}
101-
102-
return '';
96+
97+
return $_SERVER['REMOTE_ADDR'] ?? '';
10398
}
10499

105100
/**
@@ -122,11 +117,11 @@ protected function getIpAddressFromProxy()
122117
}
123118

124119
// Extract IPs
125-
$ips = explode(',', $_SERVER[$header]);
120+
$ips = \explode(',', $_SERVER[$header]);
126121
// trim, so we can compare against trusted proxies properly
127-
$ips = array_map('trim', $ips);
122+
$ips = \array_map('trim', $ips);
128123
// remove trusted proxy IPs
129-
$ips = array_diff($ips, $this->trustedProxies);
124+
$ips = \array_diff($ips, $this->trustedProxies);
130125

131126
// Any left?
132127
if (empty($ips)) {
@@ -138,7 +133,7 @@ protected function getIpAddressFromProxy()
138133
// not know if it is a proxy server, or a client. As such, we treat it
139134
// as the originating IP.
140135
// @see http://en.wikipedia.org/wiki/X-Forwarded-For
141-
return array_pop($ips);
136+
return \array_pop($ips);
142137
}
143138

144139
/**
@@ -152,9 +147,9 @@ protected function getIpAddressFromProxy()
152147
*/
153148
protected function normalizeProxyHeader($header)
154149
{
155-
$header = strtoupper($header);
156-
$header = str_replace('-', '_', $header);
157-
if (0 !== strpos($header, 'HTTP_')) {
150+
$header = \strtoupper($header);
151+
$header = \str_replace('-', '_', $header);
152+
if (0 !== \strpos($header, 'HTTP_')) {
158153
$header = 'HTTP_' . $header;
159154
}
160155
return $header;

sources/Helpers/HTTP/ResponseCode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public static function header($code = null)
99
static $responseCode = null;
1010

1111
if ($code === null) {
12-
return $responseCode ? $responseCode : 200;
12+
return $responseCode ?: 200;
1313
}
1414

1515
$text = '';

sources/Helpers/Server.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
<?php
22

3-
43
namespace Arris\Helpers;
54

6-
75
class Server
86
{
9-
public const HTTP_CODES = array(
7+
public const HTTP_CODES = [
108
100 => "HTTP/1.1 100 Continue",
119
101 => "HTTP/1.1 101 Switching Protocols",
1210
200 => "HTTP/1.1 200 OK",
@@ -46,7 +44,7 @@ class Server
4644
502 => "HTTP/1.1 502 Bad Gateway",
4745
503 => "HTTP/1.1 503 Service Unavailable",
4846
504 => "HTTP/1.1 504 Gateway Time-out"
49-
);
47+
];
5048

5149
/**
5250
*

sources/Helpers/Strings.php

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,17 @@ public static function trim($input, $length, $ellipses = true, $strip_html = tru
5050
{
5151
//strip tags, if desired
5252
if ($strip_html) {
53-
$input = strip_tags($input);
53+
$input = \strip_tags($input);
5454
}
5555

5656
//no need to trim, already shorter than trim length
57-
if (mb_strlen($input) <= $length) {
57+
if (\mb_strlen($input) <= $length) {
5858
return $input;
5959
}
6060

6161
//find last space within length
62-
$last_space = mb_strrpos(mb_substr($input, 0, $length), ' ');
63-
$trimmed_text = mb_substr($input, 0, $last_space);
62+
$last_space = \mb_strrpos(mb_substr($input, 0, $length), ' ');
63+
$trimmed_text = \mb_substr($input, 0, $last_space);
6464

6565
//add ellipses (...)
6666
if ($ellipses) {
@@ -84,39 +84,39 @@ public static function trim($input, $length, $ellipses = true, $strip_html = tru
8484
*/
8585
public static function str_replace($search, $replace, $subject, &$count = 0)
8686
{
87-
if (!is_array($search) && is_array($replace)) {
87+
if (!\is_array($search) && \is_array($replace)) {
8888
return false;
8989
}
9090

91-
if (is_array($subject)) {
91+
if (\is_array($subject)) {
9292
// call mb_replace for each single string in $subject
9393
foreach ($subject as &$string) {
9494
$string = self::str_replace($search, $replace, $string, $c);
9595
$count += $c;
9696
}
9797
unset($string);
9898

99-
} elseif (is_array($search)) {
99+
} elseif (\is_array($search)) {
100100

101-
if (!is_array($replace)) {
101+
if (!\is_array($replace)) {
102102
foreach ($search as &$string) {
103103
$subject = self::str_replace($string, $replace, $subject, $c);
104104
$count += $c;
105105
}
106106
unset($string);
107107
} else {
108-
$n = max(count($search), count($replace));
108+
$n = \max(\count($search), \count($replace));
109109
while ($n--) {
110-
$subject = self::str_replace(current($search), current($replace), $subject, $c);
110+
$subject = self::str_replace(\current($search), current($replace), $subject, $c);
111111
$count += $c;
112-
next($search);
113-
next($replace);
112+
\next($search);
113+
\next($replace);
114114
}
115115
}
116116
} else {
117-
$parts = mb_split(preg_quote($search), $subject);
118-
$count = count($parts) - 1;
119-
$subject = implode($replace, $parts);
117+
$parts = \mb_split(\preg_quote($search), $subject);
118+
$count = \count($parts) - 1;
119+
$subject = \implode($replace, $parts);
120120
}
121121
return $subject;
122122
}
@@ -138,22 +138,24 @@ public static function str_replace($search, $replace, $subject, &$count = 0)
138138
*/
139139
public static function vksprintf($str, $args)
140140
{
141-
if (is_object($args)) {
142-
$args = get_object_vars($args);
141+
if (\is_object($args)) {
142+
$args = \get_object_vars($args);
143143
}
144-
$map = array_flip(array_keys($args));
145-
$new_str = preg_replace_callback('/(^|[^%])%([a-zA-Z0-9_-]+)\$/',
146-
function($m) use ($map) { return $m[1].'%'.($map[$m[2]] + 1).'$'; },
147-
$str);
148-
return vsprintf($new_str, $args);
144+
145+
$map = \array_flip(\array_keys($args));
146+
$new_str = \preg_replace_callback('/(^|[^%])%([a-zA-Z0-9_-]+)\$/',function($m) use ($map) {
147+
return $m[1].'%'.($map[$m[2]] + 1).'$';
148+
},$str);
149+
150+
return \vsprintf($new_str, $args);
149151
}
150152

151153
/**
152154
* pluralForm - форма числительного (Plural form of number)
153155
*
154156
*
155157
* @param $number
156-
* @param mixed $forms (array or string with glues, x|y|z or [x,y,z]
158+
* @param array|string $forms (array or string with glues, x|y|z or [x,y,z]
157159
* @param string $glue
158160
* @return string
159161
*/
@@ -163,20 +165,20 @@ public static function pluralForm($number, $forms, string $glue = '|'):string
163165
return $number;
164166
}
165167

166-
if (is_string($forms)) {
167-
$forms = explode($forms, $glue);
168-
} elseif (!is_array($forms)) {
168+
if (\is_string($forms)) {
169+
$forms = \explode($forms, $glue);
170+
} elseif (!\is_array($forms)) {
169171
return $number;
170172
}
171173

172-
switch (count($forms)) {
174+
switch (\count($forms)) {
173175
case 1: {
174-
$forms[] = end($forms);
175-
$forms[] = end($forms);
176+
$forms[] = \end($forms);
177+
$forms[] = \end($forms);
176178
break;
177179
}
178180
case 2: {
179-
$forms[] = end($forms);
181+
$forms[] = \end($forms);
180182
}
181183
}
182184

@@ -273,8 +275,8 @@ public static function mb_str_pad($input, $pad_length, $pad_string = ' ', $pad_t
273275
*/
274276
public static function mb_count_chars($string, $mode, $encoding = 'UTF-8')
275277
{
276-
$l = mb_strlen($string, $encoding);
277-
$unique = array();
278+
$l = \mb_strlen($string, $encoding);
279+
$unique = [];
278280
for ($i = 0; $i < $l; $i++) {
279281
$char = mb_substr($string, $i, 1, $encoding);
280282
if (!array_key_exists($char, $unique)) {

sources/helpers.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,17 @@ function d()
3737
function dd()
3838
{
3939
$is_not_cli = php_sapi_name() !== "cli";
40-
if ($is_not_cli) echo '<pre>';
40+
if ($is_not_cli) {
41+
echo '<pre>';
42+
}
4143
if (func_num_args()) {
4244
foreach (func_get_args() as $arg) {
4345
var_dump($arg);
4446
}
4547
}
46-
if ($is_not_cli) echo '</pre>';
48+
if ($is_not_cli) {
49+
echo '</pre>';
50+
}
4751

4852
die;
4953
}

0 commit comments

Comments
 (0)