Passed
Pull Request — master (#225)
by Dmitriy
02:36
created

DebugServerBroadcastCommand::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 2
dl 0
loc 25
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
declare(ticks=1);
5
6
namespace Yiisoft\Yii\Debug\Command;
7
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Style\SymfonyStyle;
13
use Yiisoft\Yii\Console\ExitCode;
14
use Yiisoft\Yii\Debug\DebugServer\Connection;
15
16
final class DebugServerBroadcastCommand extends Command
17
{
18
    public const COMMAND_NAME = 'dev:broadcast';
19
    protected static $defaultName = self::COMMAND_NAME;
20
21
    protected static $defaultDescription = 'Runs PHP built-in web server';
22
23
    public function configure(): void
24
    {
25
        $this
26
            ->setHelp(
27
                'Broadcasts a message to all connected clients.'
28
            )
29
            ->addOption('message', 'm', InputOption::VALUE_OPTIONAL, 'A text to broadcast', 'Test message')
30
            ->addOption('env', 'e', InputOption::VALUE_OPTIONAL, 'It is only used for testing.');
31
    }
32
33
    protected function execute(InputInterface $input, OutputInterface $output): int
34
    {
35
        $io = new SymfonyStyle($input, $output);
36
        $io->title('Yii3 Debug Server');
37
        $io->writeln('https://yiiframework.com' . "\n");
38
39
        $env = $input->getOption('env');
40
        if ($env === 'test') {
41
            return ExitCode::OK;
42
        }
43
44
        $socket = Connection::create();
45
        if (\function_exists('pcntl_signal')) {
46
            $io->success('Quit the server with CTRL-C or COMMAND-C.');
47
48
            \pcntl_signal(\SIGINT, static function () use ($socket): void {
49
                $socket->close();
50
                exit(1);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
51
            });
52
        }
53
54
        $data = $input->getOption('message');
55
        $socket->broadcast($data);
0 ignored issues
show
Bug introduced by
The call to Yiisoft\Yii\Debug\DebugS...Connection::broadcast() has too few arguments starting with data. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
        $socket->/** @scrutinizer ignore-call */ 
56
                 broadcast($data);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
56
57
        return ExitCode::OK;
58
    }
59
}
60