| Conditions | 10 |
| Paths | 3 |
| Total Lines | 54 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 __invoke(Request $request) |
||
| 20 | { |
||
| 21 | $attributes = []; |
||
| 22 | |||
| 23 | if ($request->has('info')) { |
||
| 24 | $attributes = explode(',', trim($request->info)); |
||
| 25 | |||
| 26 | if (in_array('user_count', $attributes) && ! Str::startsWith($request->filter_by_prefix, 'presence-')) { |
||
| 27 | throw new HttpException(400, 'Request must be limited to presence channels in order to fetch user_count'); |
||
| 28 | } |
||
| 29 | } |
||
| 30 | |||
| 31 | return $this->channelManager |
||
| 32 | ->getGlobalChannels($request->appId) |
||
| 33 | ->then(function ($channels) use ($request, $attributes) { |
||
| 34 | $channels = collect($channels)->keyBy(function ($channel) { |
||
| 35 | return $channel instanceof Channel |
||
| 36 | ? $channel->getName() |
||
| 37 | : $channel; |
||
| 38 | }); |
||
| 39 | |||
| 40 | if ($request->has('filter_by_prefix')) { |
||
| 41 | $channels = $channels->filter(function ($channel, $channelName) use ($request) { |
||
| 42 | return Str::startsWith($channelName, $request->filter_by_prefix); |
||
| 43 | }); |
||
| 44 | } |
||
| 45 | |||
| 46 | $channelNames = $channels->map(function ($channel) { |
||
| 47 | return $channel instanceof Channel |
||
| 48 | ? $channel->getName() |
||
| 49 | : $channel; |
||
| 50 | })->toArray(); |
||
| 51 | |||
| 52 | return $this->channelManager |
||
| 53 | ->getChannelsMembersCount($request->appId, $channelNames) |
||
| 54 | ->then(function ($counts) use ($channels, $attributes) { |
||
| 55 | $channels = $channels->map(function ($channel) use ($counts, $attributes) { |
||
| 56 | $info = new stdClass; |
||
| 57 | |||
| 58 | $channelName = $channel instanceof Channel |
||
| 59 | ? $channel->getName() |
||
| 60 | : $channel; |
||
| 61 | |||
| 62 | if (in_array('user_count', $attributes)) { |
||
| 63 | $info->user_count = $counts[$channelName]; |
||
| 64 | } |
||
| 65 | |||
| 66 | return $info; |
||
| 67 | })->sortBy(function ($content, $name) { |
||
| 68 | return $name; |
||
| 69 | })->all(); |
||
| 70 | |||
| 71 | return [ |
||
| 72 | 'channels' => $channels ?: new stdClass, |
||
| 73 | ]; |
||
| 78 |