Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[12.x] Fix PHPDoc for Arr::sole method #56096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 12.x
Choose a base branch
from

Conversation

AhmedAlaa4611
Copy link
Contributor

Description

This PR corrects the PHPDoc comment for the Illuminate\Support\Arr::sole method.

  • Updates the word collection to array, which better reflects the actual data type the method operates on.
  • Updates the @param tag for the $callback argument to callable|null to match the method signature.

These changes improve code clarity and align the documentation with the method's actual behavior.

Copy link
Contributor

@shaedrich shaedrich left a comment

Choose a reason for hiding this comment

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

If you wanted, you could significantly shorten the method body as well

    public static function sole($array, ?callable $callback = null)
    {
        if ($callback) {
            $array = static::where($array, $callback);
        }

-		$count = count($array);
-
-       if ($count === 0) {
-           throw new ItemNotFoundException;
-       }
-
-       if ($count > 1) {
-           throw new MultipleItemsFoundException($count);
-       }
-
-       return static::first($array);
+       return match ($count = count($array)) {
+       	0 => new ItemNotFoundException,
+			1 => static::first($array),
+			default => new MultipleItemsFoundException($count),
+       };
	}

but that would probably be a bit out of scope

*
* @param array $array
* @param callable $callback
* @param callable|null $callback
Copy link
Contributor

Choose a reason for hiding this comment

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

You could arrow this down even further:

Suggested change
* @param callable|null $callback
* @param callable(mixed, array-key): array|null $callback

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.

2 participants