Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Illuminate/Collections/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ public static function join($array, $glue, $finalGlue = '')
}

if (count($array) === 1) {
return end($array);
return array_last($array);
}

$finalItem = array_pop($array);
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Collections/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ function data_forget(&$target, $key)
*/
function head($array)
{
return reset($array);
return array_first($array);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not exactly the same: reset() updates the internal array pointer to the first element, while array_first() does not update internal pointer. Additionally, reset() returns false if the array is empty, while array_first() returns null.

Same goes to the end() and array_last().

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The concern about the internal pointer changing is irrelevant to most of these changes, as arrays in PHP are passed into functions as copies, and the use of reset() or end() happens, mostly, in the return statement.

I say "most of the cases," as I didn't thoroughly check every change. But regardless of being used on the return statement, all changes seem fine overall.

The internal pointer change would be relevant if arrays were explicitly being passed as references, which is not the case in any of the changes.

<?php

function byCopy($arr) { return reset($arr); }
function byReference(&$arr) { return end($arr); }

$arr = [1, 2, 3];

// move internal pointer to the second element
next($arr);

echo 'current element: ', current($arr), PHP_EOL, PHP_EOL;

echo 'result of byCopy(): ', byCopy($arr), PHP_EOL;
echo 'original array pointer should be unchanged: ', current($arr), PHP_EOL, PHP_EOL;

echo 'result of byReference(): ', byReference($arr), PHP_EOL;
echo 'original array pointer should be changed: ', current($arr), PHP_EOL;

Output:

$ php test.php 
current element: 2

result of byCopy(): 1
original array pointer should be unchanged: 2

result of byReference(): 3
original array pointer should be changed: 3

But indeed the false on an empty array case would be a breaking change for anyone already handling that outcome.

Maybe it is best to send this to master and document the change on the upgrade guide to avoid any surprises for developers handling the false on an empty array case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate the explanations! I now realize that head() and last() are the only real cases that introduce breaking changes. I still think their implementation should use array_first() and array_last(), since the purpose of these helpers is simply to return the first or last element of an array. I agree though that these 2 should target the master branch, and not 12.x. I can create another PR for them.

The other changes don’t introduce breaking changes, since they dont rely on the internal pointer or the special false return value, so I believe those can safely target the 12.x branch.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even in cases where the array is a reference, not manipulating the array or its internal pointer would be the expected default behavior. So if anything, this actually fixes potentially non-apparent behavior. But as always, someone might rely on this and sending it to master instead might make sense here therefore.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally agree with @shaedrich,, that explains basically I still think their implementation should use array_first() and array_last() from my previous message 😅.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for explanation @rodrigopedra. Indeed, I checked other usages and it seems to not impact userland except for false -> null return on empty array.
While the contract is not clearly established (return mixed) in the PHPDoc, documentation states:

The head function returns the first element in the given array. If the array is empty, false will be returned:

https://laravel.com/docs/12.x/helpers#method-head

Which should be considered a minor BC break and requires doc update. It also appears that this edge case is not covered by tests.

}
}

Expand All @@ -216,7 +216,7 @@ function head($array)
*/
function last($array)
{
return end($array);
return array_last($array);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Console/Scheduling/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ protected function mergePendingAttributes(Event $event)
}

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

$group->mergeAttributes($event);
}
Expand Down Expand Up @@ -476,7 +476,7 @@ public function __call($method, $parameters)
}

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

return $this->attributes->$method(...$parameters);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,7 @@ protected function getParameterOverride($dependency)
*/
protected function getLastParameterOverride()
{
return count($this->with) ? end($this->with) : [];
return count($this->with) ? array_last($this->with) : [];
}

/**
Expand Down Expand Up @@ -1646,7 +1646,7 @@ protected function fireCallbackArray($object, array $callbacks)
*/
public function currentlyResolving()
{
return end($this->buildStack) ?: null;
return array_last($this->buildStack) ?: null;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public function scalar($query, $bindings = [], $useReadPdo = true)
throw new MultipleColumnsSelectedException;
}

return reset($record);
return array_last($record);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, reset is replaced with array_last?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it seems to be an oversight. I guess it should be fixed.

Especially as the method description reads as "Run a select statement and return the first column of the first row."

ping @KIKOmanasijev

}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ public function fillForInsert(array $values)
return [];
}

if (! is_array(reset($values))) {
if (! is_array(array_first($values))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, reset is replaced with array_first?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it is fine.

$values = [$values];
}

Expand Down Expand Up @@ -1265,12 +1265,12 @@ public function upsert(array $values, $uniqueBy, $update = null)
return 0;
}

if (! is_array(reset($values))) {
if (! is_array(array_first($values))) {
$values = [$values];
}

if (is_null($update)) {
$update = array_keys(reset($values));
$update = array_keys(array_first($values));
}

return $this->toBase()->upsert(
Expand Down Expand Up @@ -1366,7 +1366,7 @@ protected function addUpdatedAtColumn(array $values)

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

$qualifiedColumn = end($segments).'.'.$column;
$qualifiedColumn = array_last($segments).'.'.$column;

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

Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public function loadMissing($relations)
}

if (is_callable($value)) {
$path[count($segments) - 1][end($segments)] = $value;
$path[count($segments) - 1][array_last($segments)] = $value;
}

$this->loadMissingRelation($this, $path);
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ public function updateOrCreate(array $attributes, array $values = [])
*/
public function upsert(array $values, $uniqueBy, $update = null)
{
if (! empty($values) && ! is_array(reset($values))) {
if (! empty($values) && ! is_array(array_first($values))) {
$values = [$values];
}

Expand Down Expand Up @@ -581,7 +581,7 @@ public function getForeignKeyName()
{
$segments = explode('.', $this->getQualifiedForeignKeyName());

return end($segments);
return array_last($segments);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ protected function setForeignAttributesForCreate(Model $model)
*/
public function upsert(array $values, $uniqueBy, $update = null)
{
if (! empty($values) && ! is_array(reset($values))) {
if (! empty($values) && ! is_array(array_last($values))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it also seems to be swapped. IMO it should be array_first().

$values = [$values];
}

Expand Down
14 changes: 7 additions & 7 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3130,7 +3130,7 @@ public function value($column)
{
$result = (array) $this->first([$column]);

return count($result) > 0 ? reset($result) : null;
return count($result) > 0 ? array_first($result) : null;
}

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

return count($result) > 0 ? reset($result) : null;
return count($result) > 0 ? array_first($result) : null;
}

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

return reset($result);
return array_last($result);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many in this file have had reset changed to array_last instead of array_first.

Is it intentional?

}

/**
Expand Down Expand Up @@ -3781,7 +3781,7 @@ public function insert(array $values)
return true;
}

if (! is_array(reset($values))) {
if (! is_array(array_last($values))) {
$values = [$values];
}

Expand Down Expand Up @@ -3818,7 +3818,7 @@ public function insertOrIgnore(array $values)
return 0;
}

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

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

if (is_null($update)) {
$update = array_keys(reset($values));
$update = array_keys(array_last($values));
}

$this->applyBeforeQueryCallbacks();
Expand Down
16 changes: 8 additions & 8 deletions src/Illuminate/Database/Query/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,9 +430,9 @@ protected function whereBetween(Builder $query, $where)
{
$between = $where['not'] ? 'not between' : 'between';

$min = $this->parameter(is_array($where['values']) ? reset($where['values']) : $where['values'][0]);
$min = $this->parameter(is_array($where['values']) ? array_first($where['values']) : $where['values'][0]);

$max = $this->parameter(is_array($where['values']) ? end($where['values']) : $where['values'][1]);
$max = $this->parameter(is_array($where['values']) ? array_last($where['values']) : $where['values'][1]);

return $this->wrap($where['column']).' '.$between.' '.$min.' and '.$max;
}
Expand All @@ -448,9 +448,9 @@ protected function whereBetweenColumns(Builder $query, $where)
{
$between = $where['not'] ? 'not between' : 'between';

$min = $this->wrap(is_array($where['values']) ? reset($where['values']) : $where['values'][0]);
$min = $this->wrap(is_array($where['values']) ? array_first($where['values']) : $where['values'][0]);

$max = $this->wrap(is_array($where['values']) ? end($where['values']) : $where['values'][1]);
$max = $this->wrap(is_array($where['values']) ? array_last($where['values']) : $where['values'][1]);

return $this->wrap($where['column']).' '.$between.' '.$min.' and '.$max;
}
Expand All @@ -466,9 +466,9 @@ protected function whereValueBetween(Builder $query, $where)
{
$between = $where['not'] ? 'not between' : 'between';

$min = $this->wrap(is_array($where['columns']) ? reset($where['columns']) : $where['columns'][0]);
$min = $this->wrap(is_array($where['columns']) ? array_first($where['columns']) : $where['columns'][0]);

$max = $this->wrap(is_array($where['columns']) ? end($where['columns']) : $where['columns'][1]);
$max = $this->wrap(is_array($where['columns']) ? array_last($where['columns']) : $where['columns'][1]);

return $this->parameter($where['value']).' '.$between.' '.$min.' and '.$max;
}
Expand Down Expand Up @@ -1186,11 +1186,11 @@ public function compileInsert(Builder $query, array $values)
return "insert into {$table} default values";
}

if (! is_array(reset($values))) {
if (! is_array(array_last($values))) {
$values = [$values];
}

$columns = $this->columnize(array_keys(reset($values)));
$columns = $this->columnize(array_keys(array_last($values)));

// We need to build a list of parameter place-holders of values that are bound
// to the query. Each insert should have the exact same number of parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ protected function compileUpdateWithJoins(Builder $query, $table, $columns, $whe
*/
public function compileUpsert(Builder $query, array $values, array $uniqueBy, array $update)
{
$columns = $this->columnize(array_keys(reset($values)));
$columns = $this->columnize(array_keys(array_last($values)));

$sql = 'merge '.$this->wrapTable($query->from).' ';

Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Schema/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ protected function getCommandByName(Blueprint $blueprint, $name)
$commands = $this->getCommandsByName($blueprint, $name);

if (count($commands) > 0) {
return reset($commands);
return array_last($commands);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ protected function injectFrameworkSpecificConfiguration()
file_put_contents($filePath, $newContents);
} else {
// Add Echo configuration after the last import...
$lastImport = end($matches[0]);
$lastImport = array_last($matches[0]);

$positionOfLastImport = strrpos($contents, $lastImport);

Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ function cache($key = null, $default = null)
);
}

return app('cache')->put(key($key), reset($key), ttl: $default);
return app('cache')->put(key($key), array_first($key), ttl: $default);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/Illuminate/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ protected function updateGroupStack(array $attributes)
*/
public function mergeWithLastGroup($new, $prependExistingPrefix = true)
{
return RouteGroup::merge($new, end($this->groupStack), $prependExistingPrefix);
return RouteGroup::merge($new, array_last($this->groupStack), $prependExistingPrefix);
}

/**
Expand All @@ -535,7 +535,7 @@ protected function loadRoutes($routes)
public function getLastGroupPrefix()
{
if ($this->hasGroupStack()) {
$last = end($this->groupStack);
$last = array_last($this->groupStack);

return $last['prefix'] ?? '';
}
Expand Down Expand Up @@ -640,7 +640,7 @@ protected function convertToControllerAction($action)
*/
protected function prependGroupNamespace($class)
{
$group = end($this->groupStack);
$group = array_last($this->groupStack);

return isset($group['namespace']) && ! str_starts_with($class, '\\') && ! str_starts_with($class, $group['namespace'])
? $group['namespace'].'\\'.$class
Expand All @@ -655,7 +655,7 @@ protected function prependGroupNamespace($class)
*/
protected function prependGroupController($class)
{
$group = end($this->groupStack);
$group = array_last($this->groupStack);

if (! isset($group['controller'])) {
return $class;
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Support/Fluent.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public function __call($method, $parameters)
return $this->macroCall($method, $parameters);
}

$this->attributes[$method] = count($parameters) > 0 ? reset($parameters) : true;
$this->attributes[$method] = count($parameters) > 0 ? array_last($parameters) : true;

return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Support/Reflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static function getClassAttributes($objectOrClass, $attribute, $includePa
));
} while ($includeParents && false !== $reflectionClass = $reflectionClass->getParentClass());

return $includeParents ? new Collection($attributes) : reset($attributes);
return $includeParents ? new Collection($attributes) : array_first($attributes);
}

/**
Expand Down