| Conditions | 2 |
| Paths | 1 |
| Total Lines | 53 |
| 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 |
||
| 21 | public function subscribe(ConnectionInterface $connection, stdClass $payload) |
||
| 22 | { |
||
| 23 | $this->verifySignature($connection, $payload); |
||
| 24 | |||
| 25 | $this->saveConnection($connection); |
||
| 26 | |||
| 27 | $this->channelManager->userJoinedPresenceChannel( |
||
|
|
|||
| 28 | $connection, |
||
| 29 | $user = json_decode($payload->channel_data), |
||
| 30 | $this->getName(), |
||
| 31 | $payload |
||
| 32 | ); |
||
| 33 | |||
| 34 | $this->channelManager |
||
| 35 | ->getChannelMembers($connection->app->id, $this->getName()) |
||
| 36 | ->then(function ($users) use ($connection) { |
||
| 37 | $hash = []; |
||
| 38 | |||
| 39 | foreach ($users as $socketId => $user) { |
||
| 40 | $hash[$user->user_id] = $user->user_info ?? []; |
||
| 41 | } |
||
| 42 | |||
| 43 | $connection->send(json_encode([ |
||
| 44 | 'event' => 'pusher_internal:subscription_succeeded', |
||
| 45 | 'channel' => $this->getName(), |
||
| 46 | 'data' => json_encode([ |
||
| 47 | 'presence' => [ |
||
| 48 | 'ids' => collect($users)->map(function ($user) { |
||
| 49 | return (string) $user->user_id; |
||
| 50 | })->values(), |
||
| 51 | 'hash' => $hash, |
||
| 52 | 'count' => count($users), |
||
| 53 | ], |
||
| 54 | ]), |
||
| 55 | ])); |
||
| 56 | }); |
||
| 57 | |||
| 58 | $memberAddedPayload = [ |
||
| 59 | 'event' => 'pusher_internal:member_added', |
||
| 60 | 'channel' => $this->getName(), |
||
| 61 | 'data' => $payload->channel_data, |
||
| 62 | ]; |
||
| 63 | |||
| 64 | $this->broadcastToEveryoneExcept( |
||
| 65 | (object) $memberAddedPayload, $connection->socketId, |
||
| 66 | $connection->app->id |
||
| 67 | ); |
||
| 68 | |||
| 69 | DashboardLogger::log($connection->app->id, DashboardLogger::TYPE_SUBSCRIBED, [ |
||
| 70 | 'socketId' => $connection->socketId, |
||
| 71 | 'channel' => $this->getName(), |
||
| 72 | ]); |
||
| 73 | } |
||
| 74 | |||
| 113 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: