Skip to content

Method chaining indentation #20

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

Closed
khalyomede opened this issue Jun 10, 2022 · 4 comments
Closed

Method chaining indentation #20

khalyomede opened this issue Jun 10, 2022 · 4 comments

Comments

@khalyomede
Copy link

khalyomede commented Jun 10, 2022

Currently I will write method chaining like this:

final class User
{
  public function scopeAuthor(Builder $query): Builder
  {
    return $query->whereIn("status", [
        User::STATUS_SUSPENDED, 
        User::STATUS_BANNED, 
      ])
      ->where("active", true) 
      ->whereHas("books");
  } 
} 

I know I was writing the methods chaining like this a while ago

final class User extends Model
{
  public function scopeAuthor(Builder $query): Builder
  {
    return $query->whereIn("status", [
      User::STATUS_SUSPENDED, 
      User::STATUS_BANNED, 
    ])
    ->where("active", true) 
    ->whereHas("books");
  } 
}

I've seen on Twitter people use a trick when the first method call is multiline

final class User
{
  public function scopeAuthor(Builder $query): Builder
  {
    return $query->new()
      ->whereIn("status", [
        User::STATUS_SUSPENDED, 
        User::STATUS_BANNED, 
      ])
      ->where("active", true) 
      ->whereHas("books");
  } 
} 

Maybe it's worth considering adding a word about it.

@andrewprofile
Copy link

andrewprofile commented Jun 10, 2022

the first and second versions are identical👯

@khalyomede
Copy link
Author

Pay attention to spaces 😜

@samdark
Copy link
Member

samdark commented Jun 17, 2022

Related to #16

@KorvinSzanto
Copy link
Contributor

#16 resolves this by requiring the first method be placed on the next line when breaking chained function calls across multiple lines.

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

No branches or pull requests

4 participants