| Conditions | 10 |
| Paths | 9 |
| Total Lines | 48 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 28 | function validate(string $value, \CharlotteDunois\Livia\Commands\Context $context, ?\CharlotteDunois\Livia\Arguments\Argument $arg = null) { |
||
| 29 | if($context->message->guild === null) { |
||
| 30 | return 'Invalid place (not a guild channel) for argument type.'; |
||
| 31 | } |
||
| 32 | |||
| 33 | $prg = \preg_match('/(?:<@!?)?(\d{15,})>?/', $value, $matches); |
||
| 34 | if($prg === 1) { |
||
| 35 | return $context->message->guild->fetchMember($matches[1])->then(function () { |
||
| 36 | return true; |
||
| 37 | }, function () { |
||
| 38 | return false; |
||
| 39 | }); |
||
| 40 | } |
||
| 41 | |||
| 42 | $search = \mb_strtolower($value); |
||
| 43 | |||
| 44 | $inexactMembers = $context->message->guild->members->filter(function ($member) use ($search) { |
||
| 45 | return (\mb_stripos($member->user->tag, $search) !== false || \mb_stripos($member->displayName, $search) !== false); |
||
| 46 | }); |
||
| 47 | $inexactLength = $inexactMembers->count(); |
||
| 48 | |||
| 49 | if($inexactLength === 0) { |
||
| 50 | return false; |
||
| 51 | } |
||
| 52 | if($inexactLength === 1) { |
||
| 53 | return true; |
||
| 54 | } |
||
| 55 | |||
| 56 | $exactMembers = $context->message->guild->members->filter(function ($member) use ($search) { |
||
| 57 | return (\mb_strtolower($member->user->tag) === $search || \mb_strtolower($member->displayName) === $search); |
||
| 58 | }); |
||
| 59 | $exactLength = $exactMembers->count(); |
||
| 60 | |||
| 61 | if($exactLength === 1) { |
||
| 62 | return true; |
||
| 63 | } |
||
| 64 | |||
| 65 | if($exactLength > 0) { |
||
| 66 | $members = $exactMembers; |
||
| 67 | } else { |
||
| 68 | $members = $inexactMembers; |
||
| 69 | } |
||
| 70 | |||
| 71 | if($members->count() >= 15) { |
||
| 72 | return 'Multiple members found. Please be more specific.'; |
||
| 73 | } |
||
| 74 | |||
| 75 | return \CharlotteDunois\Livia\Utils\DataHelpers::disambiguation($members, 'members', null).\PHP_EOL; |
||
| 76 | } |
||
| 114 |