Skip to content

Commit f73517f

Browse files
KIKOmanasijevkikomanasijev
andauthored
[12.x] Use array_first and array_last (#56706)
* wip * fix tests * fix tests * add more cases * add more cases * head and last return false on empty array --------- Co-authored-by: kikomanasijev <[email protected]>
1 parent 3a66665 commit f73517f

File tree

18 files changed

+41
-41
lines changed

18 files changed

+41
-41
lines changed

src/Illuminate/Collections/Arr.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ public static function join($array, $glue, $finalGlue = '')
637637
}
638638

639639
if (count($array) === 1) {
640-
return end($array);
640+
return array_last($array);
641641
}
642642

643643
$finalItem = array_pop($array);

src/Illuminate/Collections/helpers.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ function data_forget(&$target, $key)
203203
*/
204204
function head($array)
205205
{
206-
return reset($array);
206+
return empty($array) ? false : array_first($array);
207207
}
208208
}
209209

@@ -216,7 +216,7 @@ function head($array)
216216
*/
217217
function last($array)
218218
{
219-
return end($array);
219+
return empty($array) ? false : array_last($array);
220220
}
221221
}
222222

src/Illuminate/Console/Scheduling/Schedule.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ protected function mergePendingAttributes(Event $event)
334334
}
335335

336336
if (! empty($this->groupStack)) {
337-
$group = end($this->groupStack);
337+
$group = array_last($this->groupStack);
338338

339339
$group->mergeAttributes($event);
340340
}
@@ -476,7 +476,7 @@ public function __call($method, $parameters)
476476
}
477477

478478
if (method_exists(PendingEventAttributes::class, $method)) {
479-
$this->attributes ??= end($this->groupStack) ?: new PendingEventAttributes($this);
479+
$this->attributes ??= array_last($this->groupStack) ?: new PendingEventAttributes($this);
480480

481481
return $this->attributes->$method(...$parameters);
482482
}

src/Illuminate/Container/Container.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,7 +1286,7 @@ protected function getParameterOverride($dependency)
12861286
*/
12871287
protected function getLastParameterOverride()
12881288
{
1289-
return count($this->with) ? end($this->with) : [];
1289+
return count($this->with) ? array_last($this->with) : [];
12901290
}
12911291

12921292
/**
@@ -1646,7 +1646,7 @@ protected function fireCallbackArray($object, array $callbacks)
16461646
*/
16471647
public function currentlyResolving()
16481648
{
1649-
return end($this->buildStack) ?: null;
1649+
return array_last($this->buildStack) ?: null;
16501650
}
16511651

16521652
/**

src/Illuminate/Database/Connection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ public function scalar($query, $bindings = [], $useReadPdo = true)
369369
throw new MultipleColumnsSelectedException;
370370
}
371371

372-
return reset($record);
372+
return array_last($record);
373373
}
374374

375375
/**

src/Illuminate/Database/Eloquent/Builder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ public function fillForInsert(array $values)
507507
return [];
508508
}
509509

510-
if (! is_array(reset($values))) {
510+
if (! is_array(array_first($values))) {
511511
$values = [$values];
512512
}
513513

@@ -1265,12 +1265,12 @@ public function upsert(array $values, $uniqueBy, $update = null)
12651265
return 0;
12661266
}
12671267

1268-
if (! is_array(reset($values))) {
1268+
if (! is_array(array_first($values))) {
12691269
$values = [$values];
12701270
}
12711271

12721272
if (is_null($update)) {
1273-
$update = array_keys(reset($values));
1273+
$update = array_keys(array_first($values));
12741274
}
12751275

12761276
return $this->toBase()->upsert(
@@ -1366,7 +1366,7 @@ protected function addUpdatedAtColumn(array $values)
13661366

13671367
$segments = preg_split('/\s+as\s+/i', $this->query->from);
13681368

1369-
$qualifiedColumn = end($segments).'.'.$column;
1369+
$qualifiedColumn = array_last($segments).'.'.$column;
13701370

13711371
$values[$qualifiedColumn] = Arr::get($values, $qualifiedColumn, $values[$column]);
13721372

src/Illuminate/Database/Eloquent/Collection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public function loadMissing($relations)
240240
}
241241

242242
if (is_callable($value)) {
243-
$path[count($segments) - 1][end($segments)] = $value;
243+
$path[count($segments) - 1][array_last($segments)] = $value;
244244
}
245245

246246
$this->loadMissingRelation($this, $path);

src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ public function updateOrCreate(array $attributes, array $values = [])
291291
*/
292292
public function upsert(array $values, $uniqueBy, $update = null)
293293
{
294-
if (! empty($values) && ! is_array(reset($values))) {
294+
if (! empty($values) && ! is_array(array_first($values))) {
295295
$values = [$values];
296296
}
297297

@@ -581,7 +581,7 @@ public function getForeignKeyName()
581581
{
582582
$segments = explode('.', $this->getQualifiedForeignKeyName());
583583

584-
return end($segments);
584+
return array_last($segments);
585585
}
586586

587587
/**

src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ protected function setForeignAttributesForCreate(Model $model)
116116
*/
117117
public function upsert(array $values, $uniqueBy, $update = null)
118118
{
119-
if (! empty($values) && ! is_array(reset($values))) {
119+
if (! empty($values) && ! is_array(array_last($values))) {
120120
$values = [$values];
121121
}
122122

src/Illuminate/Database/Query/Builder.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3130,7 +3130,7 @@ public function value($column)
31303130
{
31313131
$result = (array) $this->first([$column]);
31323132

3133-
return count($result) > 0 ? reset($result) : null;
3133+
return count($result) > 0 ? array_first($result) : null;
31343134
}
31353135

31363136
/**
@@ -3142,7 +3142,7 @@ public function rawValue(string $expression, array $bindings = [])
31423142
{
31433143
$result = (array) $this->selectRaw($expression, $bindings)->first();
31443144

3145-
return count($result) > 0 ? reset($result) : null;
3145+
return count($result) > 0 ? array_first($result) : null;
31463146
}
31473147

31483148
/**
@@ -3158,7 +3158,7 @@ public function soleValue($column)
31583158
{
31593159
$result = (array) $this->sole([$column]);
31603160

3161-
return reset($result);
3161+
return array_last($result);
31623162
}
31633163

31643164
/**
@@ -3781,7 +3781,7 @@ public function insert(array $values)
37813781
return true;
37823782
}
37833783

3784-
if (! is_array(reset($values))) {
3784+
if (! is_array(array_last($values))) {
37853785
$values = [$values];
37863786
}
37873787

@@ -3818,7 +3818,7 @@ public function insertOrIgnore(array $values)
38183818
return 0;
38193819
}
38203820

3821-
if (! is_array(reset($values))) {
3821+
if (! is_array(array_last($values))) {
38223822
$values = [$values];
38233823
} else {
38243824
foreach ($values as $key => $value) {
@@ -3976,7 +3976,7 @@ public function upsert(array $values, array|string $uniqueBy, ?array $update = n
39763976
return (int) $this->insert($values);
39773977
}
39783978

3979-
if (! is_array(reset($values))) {
3979+
if (! is_array(array_last($values))) {
39803980
$values = [$values];
39813981
} else {
39823982
foreach ($values as $key => $value) {
@@ -3987,7 +3987,7 @@ public function upsert(array $values, array|string $uniqueBy, ?array $update = n
39873987
}
39883988

39893989
if (is_null($update)) {
3990-
$update = array_keys(reset($values));
3990+
$update = array_keys(array_last($values));
39913991
}
39923992

39933993
$this->applyBeforeQueryCallbacks();

0 commit comments

Comments
 (0)