Skip to content

Conversation

alipowerful7
Copy link
Contributor

@alipowerful7 alipowerful7 commented Sep 27, 2025

last() method return TValue but in join method just write string

before:

/**
     * Join all items from the collection using a string. The final items can use a separate glue string.
     *
     * @param  string  $glue
     * @param  string  $finalGlue
     * @return string
     */
    public function join($glue, $finalGlue = '')
    {
        if ($finalGlue === '') {
            return $this->implode($glue);
        }

        $count = $this->count();

        if ($count === 0) {
            return '';
        }

        if ($count === 1) {
            return $this->last();
        }

        $collection = new static($this->items);

        $finalItem = $collection->pop();

        return $collection->implode($glue).$finalGlue.$finalItem;
    }

after:

/**
     * Join all items from the collection using a string. The final items can use a separate glue string.
     *
     * @param  string  $glue
     * @param  string  $finalGlue
     * @return TValue|string
     */
    public function join($glue, $finalGlue = '')
    {
        if ($finalGlue === '') {
            return $this->implode($glue);
        }

        $count = $this->count();

        if ($count === 0) {
            return '';
        }

        if ($count === 1) {
            return $this->last();
        }

        $collection = new static($this->items);

        $finalItem = $collection->pop();

        return $collection->implode($glue).$finalGlue.$finalItem;
    }

@shaedrich
Copy link
Contributor

shaedrich commented Sep 27, 2025

Where did you get that from? value() is used on $default to make sure to call the $default in case it's a callable:

return $key !== null ? $array[$key] : value($default);

@rodrigopedra
Copy link
Contributor

last() method return \Closure()

No, it doesn't. It returns whatever the collection holds (TValue), or whatever is passed as default, or if the default is a closure, whatever that closure returns (TLastDefault)

/**
* Get the last item from the collection.
*
* @template TLastDefault
*
* @param (callable(TValue, TKey): bool)|null $callback
* @param TLastDefault|(\Closure(): TLastDefault) $default
* @return TValue|TLastDefault
*/
public function last(?callable $callback = null, $default = null)
{
return Arr::last($this->items, $callback, $default);
}

And TValue is defined class wise here:

/**
* @template TKey of array-key
*
* @template-covariant TValue
*
* @implements \ArrayAccess<TKey, TValue>
* @implements \Illuminate\Support\Enumerable<TKey, TValue>
*/
class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerable

@rodrigopedra
Copy link
Contributor

I'd say you could change it to:

/**
 * Join all items from the collection using a string. The final items can use a separate glue string.
 *
 * @param  string  $glue
 * @param  string  $finalGlue
 * @return TValue|string
 */
public function join($glue, $finalGlue = '')

Or maybe add a cast to string when $count === 1, to ensure Collection@join always return a string:

if ($count === 1) {
    return strval($this->last());
}

@alipowerful7
Copy link
Contributor Author

alipowerful7 commented Sep 28, 2025

@rodrigopedra thank you.
i changed to this

@return TValue|string

@taylorotwell taylorotwell merged commit 3d8ed4e into laravel:12.x Sep 28, 2025
63 checks passed
@alipowerful7 alipowerful7 deleted the fix/return-type-on-join-function branch September 28, 2025 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants