Skip to content
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

Array typehints for Update methods #54352

Closed

Conversation

liamduckett
Copy link
Contributor

@liamduckett liamduckett commented Jan 25, 2025

Hey, this PR aims to improve static analysis around passing numerically keyed arrays to "update" methods. The following situations will now be reported by static analysers:

// 1. Model
$user = User::first();
$user->update(['foo', 'bar']);

// 2. Eloquent Builder
User::query()->update(['foo', 'bar']);

// 3. Query Builder
DB::table('users')->update(['foo', 'bar']);

These are already stubbed in Larastan, but this change will benefit those who do not use it.

@@ -3840,7 +3840,7 @@ public function insertOrIgnoreUsing(array $columns, $query)
/**
* Update records in the database.
*
* @param array $values
* @param array<string, mixed> $values
Copy link
Contributor

Choose a reason for hiding this comment

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

mixed might be improved:

Suggested change
* @param array<string, mixed> $values
* @param array<string, \Illuminate\Database\Query\Builder|\Illuminate\Support\Collection|\UnitEnum|mixed> $values

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey, that feels like a more involved change than I was planning to make here (although I'm happy to implement it if desired by the maintainers).

I'm also not sure of the value of adding other types, in a union with mixed? With it being the top type, I believe X|mixed is equivalent to mixed (for any X).

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I would like to get rid of the mixed altogether but at a quick glance, I didn't know what were possible types for the default match case. I just thought: What good is it to retroactively add doc block types and then still end up with mixed? That might not be your fault, but I would be trying to avoid that to achieve an actually improved typing

Copy link
Contributor

Choose a reason for hiding this comment

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

I think that mixed is enough here for now.

@taylorotwell
Copy link
Member

I think it's fine for now.

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