Skip to content

Commit

Permalink
Merge pull request #320 from diablomedia/missing-functions
Browse files Browse the repository at this point in the history
Adding back some removed functions for PHP 8 (adding as deprecated)
  • Loading branch information
Kharhamel authored Jan 18, 2022
2 parents ad8f37c + e09fab9 commit d48e85e
Show file tree
Hide file tree
Showing 7 changed files with 1,074 additions and 0 deletions.
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
},
"files": [
"deprecated/apc.php",
"deprecated/array.php",
"deprecated/datetime.php",
"deprecated/libevent.php",
"deprecated/password.php",
"deprecated/mssql.php",
"deprecated/stats.php",
"deprecated/strings.php",
"lib/special_cases.php",
"deprecated/mysqli.php",
"generated/apache.php",
Expand Down
15 changes: 15 additions & 0 deletions deprecated/Exceptions/PasswordException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Safe\Exceptions;

/**
* @deprecated This exception is deprecated
*/
class PasswordException extends \ErrorException implements SafeExceptionInterface
{
public static function createFromPhpError(): self
{
$error = error_get_last();
return new self($error['message'] ?? 'An error occured', 0, $error['type'] ?? 1);
}
}
204 changes: 204 additions & 0 deletions deprecated/array.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
<?php

namespace Safe;

use Safe\Exceptions\ArrayException;

/**
* array_flip returns an array in flip
* order, i.e. keys from array become values and values
* from array become keys.
*
* Note that the values of array need to be valid
* keys, i.e. they need to be either integer or
* string. A warning will be emitted if a value has the wrong
* type, and the key/value pair in question will not be included
* in the result.
*
* If a value has several occurrences, the latest key will be
* used as its value, and all others will be lost.
*
* @param array $array An array of key/value pairs to be flipped.
* @return array Returns the flipped array on success.
* @throws ArrayException
* @deprecated The Safe version of this function is no longer needed in PHP 8.0+
*
*/
function array_flip(array $array): array
{
error_clear_last();
$result = \array_flip($array);
if ($result === null) {
throw ArrayException::createFromPhpError();
}
return $result;
}

/**
* This function sorts an array such that array indices maintain their
* correlation with the array elements they are associated with.
*
* This is used mainly when sorting associative arrays where the actual
* element order is significant.
*
* @param array $array The input array.
* @param int $sort_flags You may modify the behavior of the sort using the optional parameter
* sort_flags, for details see
* sort.
* @throws ArrayException
* @deprecated The Safe version of this function is no longer needed in PHP 8.0+
*
*/
function arsort(array &$array, int $sort_flags = SORT_REGULAR): void
{
error_clear_last();
$result = \arsort($array, $sort_flags);
if ($result === false) {
throw ArrayException::createFromPhpError();
}
}

/**
* This function sorts an array such that array indices maintain
* their correlation with the array elements they are associated
* with. This is used mainly when sorting associative arrays where
* the actual element order is significant.
*
* @param array $array The input array.
* @param int $sort_flags You may modify the behavior of the sort using the optional
* parameter sort_flags, for details
* see sort.
* @throws ArrayException
* @deprecated The Safe version of this function is no longer needed in PHP 8.0+
*/
function asort(array &$array, int $sort_flags = SORT_REGULAR): void
{
error_clear_last();
$result = \asort($array, $sort_flags);
if ($result === false) {
throw ArrayException::createFromPhpError();
}
}

/**
* Sorts an array by key in reverse order, maintaining key to data
* correlations. This is useful mainly for associative arrays.
*
* @param array $array The input array.
* @param int $sort_flags You may modify the behavior of the sort using the optional parameter
* sort_flags, for details see
* sort.
* @throws ArrayException
* @deprecated The Safe version of this function is no longer needed in PHP 8.0+
*
*/
function krsort(array &$array, int $sort_flags = SORT_REGULAR): void
{
error_clear_last();
$result = \krsort($array, $sort_flags);
if ($result === false) {
throw ArrayException::createFromPhpError();
}
}

/**
* Sorts an array by key, maintaining key to data correlations. This is
* useful mainly for associative arrays.
*
* @param array $array The input array.
* @param int $sort_flags You may modify the behavior of the sort using the optional
* parameter sort_flags, for details
* see sort.
* @throws ArrayException
* @deprecated The Safe version of this function is no longer needed in PHP 8.0+
*
*/
function ksort(array &$array, int $sort_flags = SORT_REGULAR): void
{
error_clear_last();
$result = \ksort($array, $sort_flags);
if ($result === false) {
throw ArrayException::createFromPhpError();
}
}

/**
* This function sorts an array. Elements will be arranged from
* lowest to highest when this function has completed.
*
* @param array $array The input array.
* @param int $sort_flags The optional second parameter sort_flags
* may be used to modify the sorting behavior using these values:
*
* Sorting type flags:
*
*
* SORT_REGULAR - compare items normally;
* the details are described in the comparison operators section
*
*
* SORT_NUMERIC - compare items numerically
*
*
* SORT_STRING - compare items as strings
*
*
*
* SORT_LOCALE_STRING - compare items as
* strings, based on the current locale. It uses the locale,
* which can be changed using setlocale
*
*
*
*
* SORT_NATURAL - compare items as strings
* using "natural ordering" like natsort
*
*
*
*
* SORT_FLAG_CASE - can be combined
* (bitwise OR) with
* SORT_STRING or
* SORT_NATURAL to sort strings case-insensitively
*
*
*
* @throws ArrayException
* @deprecated The Safe version of this function is no longer needed in PHP 8.0+
*/
function sort(array &$array, int $sort_flags = SORT_REGULAR): void
{
error_clear_last();
$result = \sort($array, $sort_flags);
if ($result === false) {
throw ArrayException::createFromPhpError();
}
}

/**
* This function will sort an array by its values using a user-supplied
* comparison function. If the array you wish to sort needs to be sorted by
* some non-trivial criteria, you should use this function.
*
* @param array $array The input array.
* @param callable $value_compare_func The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
* Note that before PHP 7.0.0 this integer had to be in the range from -2147483648 to 2147483647.
*
* Returning non-integer values from the comparison
* function, such as float, will result in an internal cast to
* integer of the callback's return value. So values such as
* 0.99 and 0.1 will both be cast to an integer value of 0, which will
* compare such values as equal.
* @throws ArrayException
* @deprecated The Safe version of this function is no longer needed in PHP 8.0+
*
*/
function usort(array &$array, callable $value_compare_func): void
{
error_clear_last();
$result = \usort($array, $value_compare_func);
if ($result === false) {
throw ArrayException::createFromPhpError();
}
}
36 changes: 36 additions & 0 deletions deprecated/datetime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Safe;

use Safe\Exceptions\DatetimeException;

/**
* Identical to the date function except that
* the time returned is Greenwich Mean Time (GMT).
*
* @param string $format The format of the outputted date string. See the formatting
* options for the date function.
* @param int $timestamp The optional timestamp parameter is an
* integer Unix timestamp that defaults to the current
* local time if a timestamp is not given. In other
* words, it defaults to the value of time.
* @return string Returns a formatted date string. If a non-numeric value is used for
* timestamp, FALSE is returned and an
* E_WARNING level error is emitted.
* @throws DatetimeException
* @deprecated The Safe version of this function is no longer needed in PHP 8.0+
*
*/
function gmdate(string $format, int $timestamp = null): string
{
error_clear_last();
if ($timestamp !== null) {
$result = \gmdate($format, $timestamp);
} else {
$result = \gmdate($format);
}
if ($result === false) {
throw DatetimeException::createFromPhpError();
}
return $result;
}
11 changes: 11 additions & 0 deletions deprecated/functionsList.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
'apc_inc',
'apc_load_constants',
'apc_sma_info',
'arsort',
'array_flip',
'asort',
'event_add',
'event_base_loopbreak',
'event_base_loopexit',
Expand All @@ -30,10 +33,13 @@
'event_priority_set',
'event_set',
'event_timer_set',
'gmdate',
'imagepsencodefont',
'imagepsextendfont',
'imagepsfreefont',
'imagepsslantfont',
'krsort',
'ksort',
'mssql_bind',
'mssql_close',
'mssql_connect',
Expand All @@ -49,9 +55,14 @@
'mssql_query',
'mssql_select_db',
'mysqli_get_client_stats',
'password_hash',
'sort',
'stats_covariance',
'stats_standard_deviation',
'stats_stat_correlation',
'stats_stat_innerproduct',
'stats_variance',
'substr',
'usort',
'vsprintf',
];
Loading

0 comments on commit d48e85e

Please sign in to comment.