Skip to content

Commit d48e85e

Browse files
authored
Merge pull request #320 from diablomedia/missing-functions
Adding back some removed functions for PHP 8 (adding as deprecated)
2 parents ad8f37c + e09fab9 commit d48e85e

File tree

7 files changed

+1074
-0
lines changed

7 files changed

+1074
-0
lines changed

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@
1212
},
1313
"files": [
1414
"deprecated/apc.php",
15+
"deprecated/array.php",
16+
"deprecated/datetime.php",
1517
"deprecated/libevent.php",
18+
"deprecated/password.php",
1619
"deprecated/mssql.php",
1720
"deprecated/stats.php",
21+
"deprecated/strings.php",
1822
"lib/special_cases.php",
1923
"deprecated/mysqli.php",
2024
"generated/apache.php",
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Safe\Exceptions;
4+
5+
/**
6+
* @deprecated This exception is deprecated
7+
*/
8+
class PasswordException extends \ErrorException implements SafeExceptionInterface
9+
{
10+
public static function createFromPhpError(): self
11+
{
12+
$error = error_get_last();
13+
return new self($error['message'] ?? 'An error occured', 0, $error['type'] ?? 1);
14+
}
15+
}

deprecated/array.php

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
<?php
2+
3+
namespace Safe;
4+
5+
use Safe\Exceptions\ArrayException;
6+
7+
/**
8+
* array_flip returns an array in flip
9+
* order, i.e. keys from array become values and values
10+
* from array become keys.
11+
*
12+
* Note that the values of array need to be valid
13+
* keys, i.e. they need to be either integer or
14+
* string. A warning will be emitted if a value has the wrong
15+
* type, and the key/value pair in question will not be included
16+
* in the result.
17+
*
18+
* If a value has several occurrences, the latest key will be
19+
* used as its value, and all others will be lost.
20+
*
21+
* @param array $array An array of key/value pairs to be flipped.
22+
* @return array Returns the flipped array on success.
23+
* @throws ArrayException
24+
* @deprecated The Safe version of this function is no longer needed in PHP 8.0+
25+
*
26+
*/
27+
function array_flip(array $array): array
28+
{
29+
error_clear_last();
30+
$result = \array_flip($array);
31+
if ($result === null) {
32+
throw ArrayException::createFromPhpError();
33+
}
34+
return $result;
35+
}
36+
37+
/**
38+
* This function sorts an array such that array indices maintain their
39+
* correlation with the array elements they are associated with.
40+
*
41+
* This is used mainly when sorting associative arrays where the actual
42+
* element order is significant.
43+
*
44+
* @param array $array The input array.
45+
* @param int $sort_flags You may modify the behavior of the sort using the optional parameter
46+
* sort_flags, for details see
47+
* sort.
48+
* @throws ArrayException
49+
* @deprecated The Safe version of this function is no longer needed in PHP 8.0+
50+
*
51+
*/
52+
function arsort(array &$array, int $sort_flags = SORT_REGULAR): void
53+
{
54+
error_clear_last();
55+
$result = \arsort($array, $sort_flags);
56+
if ($result === false) {
57+
throw ArrayException::createFromPhpError();
58+
}
59+
}
60+
61+
/**
62+
* This function sorts an array such that array indices maintain
63+
* their correlation with the array elements they are associated
64+
* with. This is used mainly when sorting associative arrays where
65+
* the actual element order is significant.
66+
*
67+
* @param array $array The input array.
68+
* @param int $sort_flags You may modify the behavior of the sort using the optional
69+
* parameter sort_flags, for details
70+
* see sort.
71+
* @throws ArrayException
72+
* @deprecated The Safe version of this function is no longer needed in PHP 8.0+
73+
*/
74+
function asort(array &$array, int $sort_flags = SORT_REGULAR): void
75+
{
76+
error_clear_last();
77+
$result = \asort($array, $sort_flags);
78+
if ($result === false) {
79+
throw ArrayException::createFromPhpError();
80+
}
81+
}
82+
83+
/**
84+
* Sorts an array by key in reverse order, maintaining key to data
85+
* correlations. This is useful mainly for associative arrays.
86+
*
87+
* @param array $array The input array.
88+
* @param int $sort_flags You may modify the behavior of the sort using the optional parameter
89+
* sort_flags, for details see
90+
* sort.
91+
* @throws ArrayException
92+
* @deprecated The Safe version of this function is no longer needed in PHP 8.0+
93+
*
94+
*/
95+
function krsort(array &$array, int $sort_flags = SORT_REGULAR): void
96+
{
97+
error_clear_last();
98+
$result = \krsort($array, $sort_flags);
99+
if ($result === false) {
100+
throw ArrayException::createFromPhpError();
101+
}
102+
}
103+
104+
/**
105+
* Sorts an array by key, maintaining key to data correlations. This is
106+
* useful mainly for associative arrays.
107+
*
108+
* @param array $array The input array.
109+
* @param int $sort_flags You may modify the behavior of the sort using the optional
110+
* parameter sort_flags, for details
111+
* see sort.
112+
* @throws ArrayException
113+
* @deprecated The Safe version of this function is no longer needed in PHP 8.0+
114+
*
115+
*/
116+
function ksort(array &$array, int $sort_flags = SORT_REGULAR): void
117+
{
118+
error_clear_last();
119+
$result = \ksort($array, $sort_flags);
120+
if ($result === false) {
121+
throw ArrayException::createFromPhpError();
122+
}
123+
}
124+
125+
/**
126+
* This function sorts an array. Elements will be arranged from
127+
* lowest to highest when this function has completed.
128+
*
129+
* @param array $array The input array.
130+
* @param int $sort_flags The optional second parameter sort_flags
131+
* may be used to modify the sorting behavior using these values:
132+
*
133+
* Sorting type flags:
134+
*
135+
*
136+
* SORT_REGULAR - compare items normally;
137+
* the details are described in the comparison operators section
138+
*
139+
*
140+
* SORT_NUMERIC - compare items numerically
141+
*
142+
*
143+
* SORT_STRING - compare items as strings
144+
*
145+
*
146+
*
147+
* SORT_LOCALE_STRING - compare items as
148+
* strings, based on the current locale. It uses the locale,
149+
* which can be changed using setlocale
150+
*
151+
*
152+
*
153+
*
154+
* SORT_NATURAL - compare items as strings
155+
* using "natural ordering" like natsort
156+
*
157+
*
158+
*
159+
*
160+
* SORT_FLAG_CASE - can be combined
161+
* (bitwise OR) with
162+
* SORT_STRING or
163+
* SORT_NATURAL to sort strings case-insensitively
164+
*
165+
*
166+
*
167+
* @throws ArrayException
168+
* @deprecated The Safe version of this function is no longer needed in PHP 8.0+
169+
*/
170+
function sort(array &$array, int $sort_flags = SORT_REGULAR): void
171+
{
172+
error_clear_last();
173+
$result = \sort($array, $sort_flags);
174+
if ($result === false) {
175+
throw ArrayException::createFromPhpError();
176+
}
177+
}
178+
179+
/**
180+
* This function will sort an array by its values using a user-supplied
181+
* comparison function. If the array you wish to sort needs to be sorted by
182+
* some non-trivial criteria, you should use this function.
183+
*
184+
* @param array $array The input array.
185+
* @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.
186+
* Note that before PHP 7.0.0 this integer had to be in the range from -2147483648 to 2147483647.
187+
*
188+
* Returning non-integer values from the comparison
189+
* function, such as float, will result in an internal cast to
190+
* integer of the callback's return value. So values such as
191+
* 0.99 and 0.1 will both be cast to an integer value of 0, which will
192+
* compare such values as equal.
193+
* @throws ArrayException
194+
* @deprecated The Safe version of this function is no longer needed in PHP 8.0+
195+
*
196+
*/
197+
function usort(array &$array, callable $value_compare_func): void
198+
{
199+
error_clear_last();
200+
$result = \usort($array, $value_compare_func);
201+
if ($result === false) {
202+
throw ArrayException::createFromPhpError();
203+
}
204+
}

deprecated/datetime.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Safe;
4+
5+
use Safe\Exceptions\DatetimeException;
6+
7+
/**
8+
* Identical to the date function except that
9+
* the time returned is Greenwich Mean Time (GMT).
10+
*
11+
* @param string $format The format of the outputted date string. See the formatting
12+
* options for the date function.
13+
* @param int $timestamp The optional timestamp parameter is an
14+
* integer Unix timestamp that defaults to the current
15+
* local time if a timestamp is not given. In other
16+
* words, it defaults to the value of time.
17+
* @return string Returns a formatted date string. If a non-numeric value is used for
18+
* timestamp, FALSE is returned and an
19+
* E_WARNING level error is emitted.
20+
* @throws DatetimeException
21+
* @deprecated The Safe version of this function is no longer needed in PHP 8.0+
22+
*
23+
*/
24+
function gmdate(string $format, int $timestamp = null): string
25+
{
26+
error_clear_last();
27+
if ($timestamp !== null) {
28+
$result = \gmdate($format, $timestamp);
29+
} else {
30+
$result = \gmdate($format);
31+
}
32+
if ($result === false) {
33+
throw DatetimeException::createFromPhpError();
34+
}
35+
return $result;
36+
}

deprecated/functionsList.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
'apc_inc',
1212
'apc_load_constants',
1313
'apc_sma_info',
14+
'arsort',
15+
'array_flip',
16+
'asort',
1417
'event_add',
1518
'event_base_loopbreak',
1619
'event_base_loopexit',
@@ -30,10 +33,13 @@
3033
'event_priority_set',
3134
'event_set',
3235
'event_timer_set',
36+
'gmdate',
3337
'imagepsencodefont',
3438
'imagepsextendfont',
3539
'imagepsfreefont',
3640
'imagepsslantfont',
41+
'krsort',
42+
'ksort',
3743
'mssql_bind',
3844
'mssql_close',
3945
'mssql_connect',
@@ -49,9 +55,14 @@
4955
'mssql_query',
5056
'mssql_select_db',
5157
'mysqli_get_client_stats',
58+
'password_hash',
59+
'sort',
5260
'stats_covariance',
5361
'stats_standard_deviation',
5462
'stats_stat_correlation',
5563
'stats_stat_innerproduct',
5664
'stats_variance',
65+
'substr',
66+
'usort',
67+
'vsprintf',
5768
];

0 commit comments

Comments
 (0)