| Conditions | 2 |
| Paths | 2 |
| Total Lines | 52 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 19 | public function query_user_with_relationships() { |
||
| 20 | $users = factory(User::class)->times(10)->create(); |
||
| 21 | |||
| 22 | $users[0]->sendFriendRequestTo($users[1]); |
||
| 23 | $users[1]->acceptFriendRequestFrom($users[0]); |
||
| 24 | |||
| 25 | $users[0]->sendFriendRequestTo($users[2]); |
||
| 26 | $users[2]->acceptFriendRequestFrom($users[0]); |
||
| 27 | |||
| 28 | $users[0]->sendFriendRequestTo($users[3]); |
||
| 29 | |||
| 30 | $users[4]->sendFriendRequestTo($users[0]); |
||
| 31 | |||
| 32 | $results = User::includeRelationshipsWith($users[0])->get(); |
||
| 33 | |||
| 34 | $users_with_relationships = collect([]); |
||
| 35 | foreach ($results as $key => $result) { |
||
| 36 | $users_with_relationships->put($key, $results->where('name', $users[$key]->name)->first()); |
||
| 37 | } |
||
| 38 | |||
| 39 | // User 0 : No relations with self |
||
| 40 | $this->assertCount(0, $users_with_relationships[0]->friends_i_am_recipient); |
||
| 41 | $this->assertCount(0, $users_with_relationships[0]->friends_i_am_sender); |
||
| 42 | |||
| 43 | // User 1 : Friends with User 0 - Is Recipient |
||
| 44 | $this->assertCount(1, $users_with_relationships[1]->friends_i_am_recipient); |
||
| 45 | $this->assertCount(0, $users_with_relationships[1]->friends_i_am_sender); |
||
| 46 | $this->assertEquals($users[1]->id, $users_with_relationships[1]->friends_i_am_recipient->first()->pivot->recipient_id); |
||
| 47 | $this->assertEquals($users[0]->id, $users_with_relationships[1]->friends_i_am_recipient->first()->pivot->sender_id); |
||
| 48 | $this->assertEquals(FriendshipStatus::ACCEPTED, $users_with_relationships[1]->friends_i_am_recipient->first()->pivot->friendship_status); |
||
| 49 | |||
| 50 | // User 2 : Friends with User 0 - Is Recipient |
||
| 51 | $this->assertCount(1, $users_with_relationships[2]->friends_i_am_recipient); |
||
| 52 | $this->assertCount(0, $users_with_relationships[2]->friends_i_am_sender); |
||
| 53 | $this->assertEquals($users[2]->id, $users_with_relationships[2]->friends_i_am_recipient->first()->pivot->recipient_id); |
||
| 54 | $this->assertEquals($users[0]->id, $users_with_relationships[2]->friends_i_am_recipient->first()->pivot->sender_id); |
||
| 55 | $this->assertEquals(FriendshipStatus::ACCEPTED, $users_with_relationships[2]->friends_i_am_recipient->first()->pivot->friendship_status); |
||
| 56 | |||
| 57 | // User 3 : Pending Request from User 0 - Is Recipient |
||
| 58 | $this->assertCount(1, $users_with_relationships[3]->friends_i_am_recipient); |
||
| 59 | $this->assertCount(0, $users_with_relationships[3]->friends_i_am_sender); |
||
| 60 | $this->assertEquals($users[3]->id, $users_with_relationships[3]->friends_i_am_recipient->first()->pivot->recipient_id); |
||
| 61 | $this->assertEquals($users[0]->id, $users_with_relationships[3]->friends_i_am_recipient->first()->pivot->sender_id); |
||
| 62 | $this->assertEquals(FriendshipStatus::PENDING, $users_with_relationships[3]->friends_i_am_recipient->first()->pivot->friendship_status); |
||
| 63 | |||
| 64 | // User 4 : Pending Request for User 0 - Is Sender |
||
| 65 | $this->assertCount(0, $users_with_relationships[4]->friends_i_am_recipient); |
||
| 66 | $this->assertCount(1, $users_with_relationships[4]->friends_i_am_sender); |
||
| 67 | $this->assertEquals($users[4]->id, $users_with_relationships[4]->friends_i_am_sender->first()->pivot->sender_id); |
||
| 68 | $this->assertEquals($users[0]->id, $users_with_relationships[4]->friends_i_am_sender->first()->pivot->recipient_id); |
||
| 69 | $this->assertEquals(FriendshipStatus::PENDING, $users_with_relationships[4]->friends_i_am_sender->first()->pivot->friendship_status); |
||
| 70 | } |
||
| 71 | |||
| 73 | } |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.