Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 13 | class Help extends SignatureHandler |
||
| 14 | { |
||
| 15 | protected $signature = '* help {command? : The command you want information about}'; |
||
| 16 | |||
| 17 | protected $description = 'List all commands or provide information about all commands'; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Handle the given request. |
||
| 21 | * |
||
| 22 | * @param \Spatie\SlashCommand\Request $request |
||
| 23 | * |
||
| 24 | * @return \Spatie\SlashCommand\Response |
||
| 25 | */ |
||
| 26 | public function handle(Request $request): Response |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Find all handlers that are available for the current SlashCommand |
||
| 39 | * and have a signature. |
||
| 40 | * |
||
| 41 | * @return Collection|SignatureHandler[] |
||
| 42 | */ |
||
| 43 | protected function findAvailableHandlers(): Collection |
||
| 44 | { |
||
| 45 | return collect(config('laravel-slack-slash-command.handlers')) |
||
| 46 | ->map(function (string $handlerClassName) { |
||
| 47 | return new $handlerClassName($this->request); |
||
| 48 | }) |
||
| 49 | ->filter(function (HandlesSlashCommand $handler) { |
||
| 50 | return $handler instanceof SignatureHandler; |
||
| 51 | }) |
||
| 52 | View Code Duplication | ->filter(function (SignatureHandler $handler) { |
|
| 53 | $signatureParts = new SignatureParts($handler->getSignature()); |
||
| 54 | |||
| 55 | return Str::is($signatureParts->getSlashCommandName(), $this->request->command); |
||
| 56 | }); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Show the help information for a single SignatureHandler. |
||
| 61 | * |
||
| 62 | * @param Collection|SignatureHandler[] $handlers |
||
| 63 | * @param string $command |
||
| 64 | * @return Response |
||
| 65 | */ |
||
| 66 | protected function displayHelpForCommand(Collection $handlers, string $command): Response |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Show a list of all available handlers. |
||
| 88 | * |
||
| 89 | * @param Collection|SignatureHandler[] $handlers |
||
| 90 | * @return Response |
||
| 91 | */ |
||
| 92 | View Code Duplication | protected function displayListOfAllCommands(Collection $handlers): Response |
|
| 107 | } |
||
| 108 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.