Skip to content

Commit

Permalink
0.6.0
Browse files Browse the repository at this point in the history
* [R] Many fixes for PHP8+ compatibility
  • Loading branch information
KarelWintersky committed Jun 21, 2024
1 parent ca7a911 commit 505698f
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 60 deletions.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"platform-check": false
},
"require": {
"php": ">=7.4",
"php": ">=7.4 | 8.*",
"ext-pdo": "*",
"ext-mbstring": "*",
"ext-json": "*"
Expand All @@ -29,10 +29,11 @@
"sources/helpers.php"
],
"psr-4": {
"Arris\\" : ["interfaces", "sources"]
"Arris\\" : "sources"
}
},
"require-dev": {
"phpunit/phpunit": "^4.8"
"phpunit/phpunit": "^4.8",
"rector/rector": "^1.1"
}
}
11 changes: 7 additions & 4 deletions sources/Helpers/FS.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ public static function rmdir($directory): bool
*/
public static function read_all_files($root = '.')
{
$directory_content = [ 'files' => [], 'dirs' => [] ];
$directories = array();
$last_letter = $root[ strlen( $root ) - 1 ];
$directory_content = [
'files' => [],
'dirs' => []
];
$directories = [];
$last_letter = $root[ \strlen( $root ) - 1 ];
$root
= ($last_letter === '\\' || $last_letter === '/')
? $root
Expand Down Expand Up @@ -81,7 +84,7 @@ public static function read_all_files($root = '.')
*/
public static function checkPath($path)
{
if (!is_dir( $path ) && !mkdir( $path, 0777, true ) && !is_dir( $path )) {
if (!\is_dir( $path ) && !\mkdir( $path, 0777, true ) && !\is_dir( $path )) {
return false;
//throw new \RuntimeException( sprintf( 'Directory "%s" was not created', $path ) );
}
Expand Down
23 changes: 9 additions & 14 deletions sources/Helpers/HTTP/RemoteAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,8 @@ public function getIpAddress()
if ($ip) {
return $ip;
}

// direct IP address
if (isset($_SERVER['REMOTE_ADDR'])) {
return $_SERVER['REMOTE_ADDR'];
}

return '';

return $_SERVER['REMOTE_ADDR'] ?? '';
}

/**
Expand All @@ -122,11 +117,11 @@ protected function getIpAddressFromProxy()
}

// Extract IPs
$ips = explode(',', $_SERVER[$header]);
$ips = \explode(',', $_SERVER[$header]);
// trim, so we can compare against trusted proxies properly
$ips = array_map('trim', $ips);
$ips = \array_map('trim', $ips);
// remove trusted proxy IPs
$ips = array_diff($ips, $this->trustedProxies);
$ips = \array_diff($ips, $this->trustedProxies);

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

/**
Expand All @@ -152,9 +147,9 @@ protected function getIpAddressFromProxy()
*/
protected function normalizeProxyHeader($header)
{
$header = strtoupper($header);
$header = str_replace('-', '_', $header);
if (0 !== strpos($header, 'HTTP_')) {
$header = \strtoupper($header);
$header = \str_replace('-', '_', $header);
if (0 !== \strpos($header, 'HTTP_')) {
$header = 'HTTP_' . $header;
}
return $header;
Expand Down
2 changes: 1 addition & 1 deletion sources/Helpers/HTTP/ResponseCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static function header($code = null)
static $responseCode = null;

if ($code === null) {
return $responseCode ? $responseCode : 200;
return $responseCode ?: 200;
}

$text = '';
Expand Down
6 changes: 2 additions & 4 deletions sources/Helpers/Server.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<?php


namespace Arris\Helpers;


class Server
{
public const HTTP_CODES = array(
public const HTTP_CODES = [
100 => "HTTP/1.1 100 Continue",
101 => "HTTP/1.1 101 Switching Protocols",
200 => "HTTP/1.1 200 OK",
Expand Down Expand Up @@ -46,7 +44,7 @@ class Server
502 => "HTTP/1.1 502 Bad Gateway",
503 => "HTTP/1.1 503 Service Unavailable",
504 => "HTTP/1.1 504 Gateway Time-out"
);
];

/**
*
Expand Down
66 changes: 34 additions & 32 deletions sources/Helpers/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ public static function trim($input, $length, $ellipses = true, $strip_html = tru
{
//strip tags, if desired
if ($strip_html) {
$input = strip_tags($input);
$input = \strip_tags($input);
}

//no need to trim, already shorter than trim length
if (mb_strlen($input) <= $length) {
if (\mb_strlen($input) <= $length) {
return $input;
}

//find last space within length
$last_space = mb_strrpos(mb_substr($input, 0, $length), ' ');
$trimmed_text = mb_substr($input, 0, $last_space);
$last_space = \mb_strrpos(mb_substr($input, 0, $length), ' ');
$trimmed_text = \mb_substr($input, 0, $last_space);

//add ellipses (...)
if ($ellipses) {
Expand All @@ -84,39 +84,39 @@ public static function trim($input, $length, $ellipses = true, $strip_html = tru
*/
public static function str_replace($search, $replace, $subject, &$count = 0)
{
if (!is_array($search) && is_array($replace)) {
if (!\is_array($search) && \is_array($replace)) {
return false;
}

if (is_array($subject)) {
if (\is_array($subject)) {
// call mb_replace for each single string in $subject
foreach ($subject as &$string) {
$string = self::str_replace($search, $replace, $string, $c);
$count += $c;
}
unset($string);

} elseif (is_array($search)) {
} elseif (\is_array($search)) {

if (!is_array($replace)) {
if (!\is_array($replace)) {
foreach ($search as &$string) {
$subject = self::str_replace($string, $replace, $subject, $c);
$count += $c;
}
unset($string);
} else {
$n = max(count($search), count($replace));
$n = \max(\count($search), \count($replace));
while ($n--) {
$subject = self::str_replace(current($search), current($replace), $subject, $c);
$subject = self::str_replace(\current($search), current($replace), $subject, $c);
$count += $c;
next($search);
next($replace);
\next($search);
\next($replace);
}
}
} else {
$parts = mb_split(preg_quote($search), $subject);
$count = count($parts) - 1;
$subject = implode($replace, $parts);
$parts = \mb_split(\preg_quote($search), $subject);
$count = \count($parts) - 1;
$subject = \implode($replace, $parts);
}
return $subject;
}
Expand All @@ -138,22 +138,24 @@ public static function str_replace($search, $replace, $subject, &$count = 0)
*/
public static function vksprintf($str, $args)
{
if (is_object($args)) {
$args = get_object_vars($args);
if (\is_object($args)) {
$args = \get_object_vars($args);
}
$map = array_flip(array_keys($args));
$new_str = preg_replace_callback('/(^|[^%])%([a-zA-Z0-9_-]+)\$/',
function($m) use ($map) { return $m[1].'%'.($map[$m[2]] + 1).'$'; },
$str);
return vsprintf($new_str, $args);

$map = \array_flip(\array_keys($args));
$new_str = \preg_replace_callback('/(^|[^%])%([a-zA-Z0-9_-]+)\$/',function($m) use ($map) {
return $m[1].'%'.($map[$m[2]] + 1).'$';
},$str);

return \vsprintf($new_str, $args);
}

/**
* pluralForm - форма числительного (Plural form of number)
*
*
* @param $number
* @param mixed $forms (array or string with glues, x|y|z or [x,y,z]
* @param array|string $forms (array or string with glues, x|y|z or [x,y,z]
* @param string $glue
* @return string
*/
Expand All @@ -163,20 +165,20 @@ public static function pluralForm($number, $forms, string $glue = '|'):string
return $number;
}

if (is_string($forms)) {
$forms = explode($forms, $glue);
} elseif (!is_array($forms)) {
if (\is_string($forms)) {
$forms = \explode($forms, $glue);
} elseif (!\is_array($forms)) {
return $number;
}

switch (count($forms)) {
switch (\count($forms)) {
case 1: {
$forms[] = end($forms);
$forms[] = end($forms);
$forms[] = \end($forms);
$forms[] = \end($forms);
break;
}
case 2: {
$forms[] = end($forms);
$forms[] = \end($forms);
}
}

Expand Down Expand Up @@ -273,8 +275,8 @@ public static function mb_str_pad($input, $pad_length, $pad_string = ' ', $pad_t
*/
public static function mb_count_chars($string, $mode, $encoding = 'UTF-8')
{
$l = mb_strlen($string, $encoding);
$unique = array();
$l = \mb_strlen($string, $encoding);
$unique = [];
for ($i = 0; $i < $l; $i++) {
$char = mb_substr($string, $i, 1, $encoding);
if (!array_key_exists($char, $unique)) {
Expand Down
8 changes: 6 additions & 2 deletions sources/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@ function d()
function dd()
{
$is_not_cli = php_sapi_name() !== "cli";
if ($is_not_cli) echo '<pre>';
if ($is_not_cli) {
echo '<pre>';
}
if (func_num_args()) {
foreach (func_get_args() as $arg) {
var_dump($arg);
}
}
if ($is_not_cli) echo '</pre>';
if ($is_not_cli) {
echo '</pre>';
}

die;
}
Expand Down

0 comments on commit 505698f

Please sign in to comment.