Passed
Pull Request — master (#9)
by Shinji
01:23
created

DaemonCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 7
nc 1
nop 0
dl 0
loc 9
c 1
b 0
f 1
cc 1
rs 10
1
<?php
2
3
/**
4
 * This file is part of the sj-i/php-profiler package.
5
 *
6
 * (c) sji <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace PhpProfiler\Command\Inspector;
15
16
use Amp\Loop;
0 ignored issues
show
Bug introduced by
The type Amp\Loop was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Amp\Promise;
0 ignored issues
show
Bug introduced by
The type Amp\Promise was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Amp\Parallel\Context;
0 ignored issues
show
Bug introduced by
The type Amp\Parallel\Context was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use Symfony\Component\Console\Command\Command;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Command\Command was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use Symfony\Component\Console\Input\InputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use Symfony\Component\Console\Input\InputOption;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputOption was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use Symfony\Component\Console\Output\OutputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Output\OutputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
24
class DaemonCommand extends Command
25
{
26
    public function configure(): void
27
    {
28
        $this->setName('inspector:daemon')
29
            ->setDescription('periodically get running function name from an outer process or thread')
30
            ->addOption(
31
                'target-regex',
32
                'P',
33
                InputOption::VALUE_OPTIONAL,
34
                'regex to find the php binary loaded in the target process'
35
            )
36
        ;
37
    }
38
39
    /**
40
     * @param InputInterface $input
41
     * @param OutputInterface $output
42
     * @return int
43
     */
44
    public function execute(InputInterface $input, OutputInterface $output): int
45
    {
46
        /** @var string $target_regex */
47
        $target_regex = $input->getOption('target-regex') ?? '/php-fpm/';
48
        $context = Context\create(__DIR__ . '/Worker/php-searcher.php');
0 ignored issues
show
Bug introduced by
The function create was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

48
        $context = /** @scrutinizer ignore-call */ Context\create(__DIR__ . '/Worker/php-searcher.php');
Loading history...
49
        /** @var int $searcher_pid */
50
        $searcher_pid = Promise\wait($context->start());
0 ignored issues
show
Unused Code introduced by
The assignment to $searcher_pid is dead and can be removed.
Loading history...
Bug introduced by
The function wait was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

50
        $searcher_pid = /** @scrutinizer ignore-call */ Promise\wait($context->start());
Loading history...
51
        Promise\wait($context->send($target_regex));
52
        /** @var int[] $pid_list */
53
        $pid_list = Promise\wait($context->receive());
54
        $readers = [];
55
        foreach ($pid_list as $pid) {
56
            $context = Context\create(__DIR__ . '/Worker/php-reader.php');
57
            Promise\wait($context->start());
58
            Promise\wait($context->send($pid));
59
            $readers[$pid] = $context;
60
        }
61
        exec('stty -icanon -echo');
62
63
        Loop::run(function () use (&$readers, $output) {
64
            Loop::onReadable(
65
                STDIN,
66
                /** @param resource $stream */
67
                function (string $watcher_id, $stream) {
68
                    $key = fread($stream, 1);
69
                    if ($key === 'q') {
70
                        Loop::cancel($watcher_id);
71
                        Loop::stop();
72
                    }
73
                }
74
            );
75
            Loop::repeat(10, function () use (&$readers, $output) {
76
                /** @var array<int, Context\Context> $readers */
77
78
                $promises = [];
79
                foreach ($readers as $pid => $reader) {
80
                    if (!$reader->isRunning()) {
81
                        /** @psalm-suppress MixedArrayAccess*/
82
                        unset($readers[$pid]);
83
                        continue;
84
                    }
85
                    $promises[] = \Amp\call(
0 ignored issues
show
Bug introduced by
The function call was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

85
                    $promises[] = /** @scrutinizer ignore-call */ \Amp\call(
Loading history...
86
                        function () use ($reader, &$readers, $pid, $output) {
87
                            /** @psalm-suppress MixedArrayAccess*/
88
                            unset($readers[$pid]);
89
                            /** @var string $result */
90
                            $result = yield $reader->receive();
91
                            $output->write($result);
92
                            $readers[$pid] = $reader;
93
                        }
94
                    );
95
                }
96
                yield $promises;
97
            });
98
        });
99
100
        return 0;
101
    }
102
}
103