Skip to content

Commit 0f1e978

Browse files
committed
Use array_replace where keys must be preserved
1 parent 0eed736 commit 0f1e978

File tree

6 files changed

+15
-14
lines changed

6 files changed

+15
-14
lines changed

src/Eloquent/Builder.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use MongoDB\Model\BSONDocument;
1919

2020
use function array_key_exists;
21-
use function array_merge;
21+
use function array_replace;
2222
use function collect;
2323
use function is_array;
2424
use function is_object;
@@ -270,7 +270,7 @@ public function firstOrCreate(array $attributes = [], array $values = [])
270270

271271
// createOrFirst is not supported in transaction.
272272
if ($this->getConnection()->getSession()?->isInTransaction()) {
273-
return $this->create(array_merge($attributes, $values));
273+
return $this->create(array_replace($attributes, $values));
274274
}
275275

276276
return $this->createOrFirst($attributes, $values);
@@ -284,7 +284,7 @@ public function createOrFirst(array $attributes = [], array $values = [])
284284
}
285285

286286
try {
287-
return $this->create(array_merge($attributes, $values));
287+
return $this->create(array_replace($attributes, $values));
288288
} catch (BulkWriteException $e) {
289289
if ($e->getCode() === self::DUPLICATE_KEY_ERROR) {
290290
return $this->where($attributes)->first() ?? throw $e;

src/Eloquent/DocumentModel.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
use function array_key_exists;
3232
use function array_keys;
33-
use function array_merge;
33+
use function array_replace;
3434
use function array_unique;
3535
use function array_values;
3636
use function class_basename;
@@ -192,7 +192,7 @@ protected function transformModelValue($key, $value)
192192
// to a Carbon or CarbonImmutable instance.
193193
// @see Model::setAttribute()
194194
if ($this->hasCast($key) && $value instanceof CarbonInterface) {
195-
$value->settings(array_merge($value->getSettings(), ['toStringFormat' => $this->getDateFormat()]));
195+
$value->settings(array_replace($value->getSettings(), ['toStringFormat' => $this->getDateFormat()]));
196196

197197
// "date" cast resets the time to 00:00:00.
198198
$castType = $this->getCasts()[$key];

src/Query/Builder.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
use function array_key_exists;
4141
use function array_map;
4242
use function array_merge;
43+
use function array_replace;
4344
use function array_values;
4445
use function assert;
4546
use function blank;
@@ -426,7 +427,7 @@ public function toMql(): array
426427

427428
// Add custom query options
428429
if (count($this->options)) {
429-
$options = array_merge($options, $this->options);
430+
$options = array_replace($options, $this->options);
430431
}
431432

432433
$options = $this->inheritConnectionOptions($options);
@@ -450,7 +451,7 @@ public function toMql(): array
450451

451452
// Add custom projections.
452453
if ($this->projections) {
453-
$projection = array_merge($projection, $this->projections);
454+
$projection = array_replace($projection, $this->projections);
454455
}
455456

456457
$options = [];
@@ -484,7 +485,7 @@ public function toMql(): array
484485

485486
// Add custom query options
486487
if (count($this->options)) {
487-
$options = array_merge($options, $this->options);
488+
$options = array_replace($options, $this->options);
488489
}
489490

490491
$options = $this->inheritConnectionOptions($options);

src/Relations/BelongsToMany.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use function array_diff;
1515
use function array_keys;
1616
use function array_map;
17-
use function array_merge;
17+
use function array_replace;
1818
use function array_values;
1919
use function assert;
2020
use function count;
@@ -164,7 +164,7 @@ public function sync($ids, $detaching = true)
164164
// Now we are finally ready to attach the new records. Note that we'll disable
165165
// touching until after the entire operation is complete so we don't fire a
166166
// ton of touch operations until we are totally done syncing the records.
167-
$changes = array_merge(
167+
$changes = array_replace(
168168
$changes,
169169
$this->attachNew($records, $current, false),
170170
);

src/Relations/MorphToMany.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
use function array_key_exists;
1616
use function array_keys;
1717
use function array_map;
18-
use function array_merge;
1918
use function array_reduce;
19+
use function array_replace;
2020
use function array_values;
2121
use function collect;
2222
use function count;
@@ -190,7 +190,7 @@ public function sync($ids, $detaching = true)
190190
// Now we are finally ready to attach the new records. Note that we'll disable
191191
// touching until after the entire operation is complete so we don't fire a
192192
// ton of touch operations until we are totally done syncing the records.
193-
$changes = array_merge(
193+
$changes = array_replace(
194194
$changes,
195195
$this->attachNew($records, $current, false),
196196
);

src/Scout/ScoutEngine.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
use function array_column;
3030
use function array_flip;
3131
use function array_map;
32-
use function array_merge;
32+
use function array_replace;
3333
use function assert;
3434
use function call_user_func;
3535
use function class_uses_recursive;
@@ -117,7 +117,7 @@ public function update($models)
117117

118118
unset($searchableData['_id']);
119119

120-
$searchableData = array_merge($searchableData, $model->scoutMetadata());
120+
$searchableData = array_replace($searchableData, $model->scoutMetadata());
121121

122122
/** Convert the __soft_deleted set by {@see Searchable::pushSoftDeleteMetadata()}
123123
* into a boolean for efficient storage and indexing. */

0 commit comments

Comments
 (0)