Skip to content

Commit 753599a

Browse files
authored
Merge pull request #145 from Kharhamel/array_replace
Array replace
2 parents 071c938 + cb2752e commit 753599a

File tree

7 files changed

+91
-3
lines changed

7 files changed

+91
-3
lines changed

generated/apcu.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,19 @@ function apcu_dec(string $key, int $step = 1, ?bool &$success = null, int $ttl =
7676
* string for a single key,
7777
* or as an array of strings for several keys,
7878
* or as an APCUIterator object.
79-
* @return bool|array Returns TRUE on success.
79+
* @return bool|array If key is an array, an indexed array of the keys is returned.
80+
* Otherwise TRUE is returned on success.
8081
* @throws ApcuException
8182
*
8283
*/
83-
function apcu_delete($key): void
84+
function apcu_delete($key)
8485
{
8586
error_clear_last();
8687
$result = \apcu_delete($key);
8788
if ($result === false) {
8889
throw ApcuException::createFromPhpError();
8990
}
91+
return $result;
9092
}
9193

9294

generated/array.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,81 @@ function array_multisort(array &$array1, $array1_sort_order = SORT_ASC, $array1_
132132
}
133133

134134

135+
/**
136+
* array_replace_recursive replaces the values of
137+
* array1 with the same values from all the following
138+
* arrays. If a key from the first array exists in the second array, its value
139+
* will be replaced by the value from the second array. If the key exists in the
140+
* second array, and not the first, it will be created in the first array.
141+
* If a key only exists in the first array, it will be left as is.
142+
* If several arrays are passed for replacement, they will be processed
143+
* in order, the later array overwriting the previous values.
144+
*
145+
* array_replace_recursive is recursive : it will recurse into
146+
* arrays and apply the same process to the inner value.
147+
*
148+
* When the value in the first array is scalar, it will be replaced
149+
* by the value in the second array, may it be scalar or array.
150+
* When the value in the first array and the second array
151+
* are both arrays, array_replace_recursive will replace
152+
* their respective value recursively.
153+
*
154+
* @param array $array1 The array in which elements are replaced.
155+
* @param array $params Optional. Arrays from which elements will be extracted.
156+
* @return array Returns an array, or NULL if an error occurs.
157+
* @throws ArrayException
158+
*
159+
*/
160+
function array_replace_recursive(array $array1, array ...$params): array
161+
{
162+
error_clear_last();
163+
if ($params !== []) {
164+
$result = \array_replace_recursive($array1, ...$params);
165+
} else {
166+
$result = \array_replace_recursive($array1);
167+
}
168+
if ($result === null) {
169+
throw ArrayException::createFromPhpError();
170+
}
171+
return $result;
172+
}
173+
174+
175+
/**
176+
* array_replace replaces the values of
177+
* array1 with values having the same keys in each of the following
178+
* arrays. If a key from the first array exists in the second array, its value
179+
* will be replaced by the value from the second array. If the key exists in the
180+
* second array, and not the first, it will be created in the first array.
181+
* If a key only exists in the first array, it will be left as is.
182+
* If several arrays are passed for replacement, they will be processed
183+
* in order, the later arrays overwriting the previous values.
184+
*
185+
* array_replace is not recursive : it will replace
186+
* values in the first array by whatever type is in the second array.
187+
*
188+
* @param array $array1 The array in which elements are replaced.
189+
* @param array $params Arrays from which elements will be extracted.
190+
* Values from later arrays overwrite the previous values.
191+
* @return array Returns an array, or NULL if an error occurs.
192+
* @throws ArrayException
193+
*
194+
*/
195+
function array_replace(array $array1, array ...$params): array
196+
{
197+
error_clear_last();
198+
if ($params !== []) {
199+
$result = \array_replace($array1, ...$params);
200+
} else {
201+
$result = \array_replace($array1);
202+
}
203+
if ($result === null) {
204+
throw ArrayException::createFromPhpError();
205+
}
206+
return $result;
207+
}
208+
209+
135210
/**
136211
* Applies the user-defined callback function to each
137212
* element of the array. This function will recurse

generated/eio.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function eio_busy(int $delay, int $pri = EIO_PRI_DEFAULT, callable $callback = n
3131

3232

3333
/**
34-
* eio_chmod changes file, or direcrory permissions. The
34+
* eio_chmod changes file, or directory permissions. The
3535
* new permissions are specified by mode.
3636
*
3737
* @param string $path Path to the target file or directory

generated/functionsList.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
'array_combine',
2929
'array_flip',
3030
'array_multisort',
31+
'array_replace_recursive',
32+
'array_replace',
3133
'array_walk_recursive',
3234
'arsort',
3335
'asort',

generator/src/DocPage.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ public function detectNullsyFunction(): bool
124124
if (preg_match('/&null;\s+on\s+failure/', $file)) {
125125
return true;
126126
}
127+
if (preg_match('/&null;\s+if\s+an\s+error\s+occurs/', $file)) {
128+
return true;
129+
}
127130

128131
return false;
129132
}

generator/tests/DocPageTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function testDetectFalsyFunction()
1616
$sessionRegister = new DocPage(__DIR__ . '/../doc/doc-en/en/reference/session/functions/session-register.xml');
1717
$mcryptDecrypt = new DocPage(__DIR__ . '/../doc/doc-en/en/reference/mcrypt/functions/mcrypt-decrypt.xml');
1818
$fsockopen = new DocPage(__DIR__ . '/../doc/doc-en/en/reference/network/functions/fsockopen.xml');
19+
$arrayReplace = new DocPage(__DIR__ . '/../doc/doc-en/en/reference/array/functions/array-replace.xml');
1920

2021
$this->assertTrue($pregMatch->detectFalsyFunction());
2122
$this->assertFalse($implode->detectFalsyFunction());
@@ -25,14 +26,17 @@ public function testDetectFalsyFunction()
2526
$this->assertFalse($sessionRegister->detectFalsyFunction());
2627
$this->assertTrue($mcryptDecrypt->detectFalsyFunction());
2728
$this->assertTrue($fsockopen->detectFalsyFunction());
29+
$this->assertFalse($arrayReplace->detectFalsyFunction());
2830
}
2931

3032
public function testDetectNullsyFunction()
3133
{
3234
$pregMatch = new DocPage(__DIR__ . '/../doc/doc-en/en/reference/array/functions/array-flip.xml');
3335
$implode = new DocPage(__DIR__ . '/../doc/doc-en/en/reference/strings/functions/implode.xml');
36+
$arrayReplace = new DocPage(__DIR__ . '/../doc/doc-en/en/reference/array/functions/array-replace.xml');
3437

3538
$this->assertTrue($pregMatch->detectNullsyFunction());
3639
$this->assertFalse($implode->detectNullsyFunction());
40+
$this->assertTrue($arrayReplace->detectNullsyFunction());
3741
}
3842
}

rector-migrate.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ services:
3131
array_combine: 'Safe\array_combine'
3232
array_flip: 'Safe\array_flip'
3333
array_multisort: 'Safe\array_multisort'
34+
array_replace_recursive: 'Safe\array_replace_recursive'
35+
array_replace: 'Safe\array_replace'
3436
array_walk_recursive: 'Safe\array_walk_recursive'
3537
arsort: 'Safe\arsort'
3638
asort: 'Safe\asort'

0 commit comments

Comments
 (0)