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

DaemonCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
eloc 35
c 4
b 0
f 1
dl 0
loc 67
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 4 1
A execute() 0 54 5
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\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...
22
23
class DaemonCommand extends Command
24
{
25
    public function configure(): void
26
    {
27
        $this->setName('inspector:daemon')
28
            ->setDescription('periodically get running function name from an outer process or thread');
29
    }
30
31
    /**
32
     * @param InputInterface $input
33
     * @param OutputInterface $output
34
     * @return int
35
     */
36
    public function execute(InputInterface $input, OutputInterface $output): int
37
    {
38
        $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

38
        $context = /** @scrutinizer ignore-call */ Context\create(__DIR__ . '/Worker/php-searcher.php');
Loading history...
39
        /** @var int $searcher_pid */
40
        $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

40
        $searcher_pid = /** @scrutinizer ignore-call */ Promise\wait($context->start());
Loading history...
41
        /** @var int[] $pid_list */
42
        $pid_list = Promise\wait($context->receive());
43
        $readers = [];
44
        foreach ($pid_list as $pid) {
45
            $context = Context\create(__DIR__ . '/Worker/php-reader.php');
46
            Promise\wait($context->start());
47
            Promise\wait($context->send($pid));
48
            $readers[$pid] = $context;
49
        }
50
        exec('stty -icanon -echo');
51
52
        Loop::run(function () use (&$readers, $output) {
53
            Loop::onReadable(
54
                STDIN,
55
                /** @param resource $stream */
56
                function (string $watcher_id, $stream) {
57
                    $key = fread($stream, 1);
58
                    if ($key === 'q') {
59
                        Loop::cancel($watcher_id);
60
                        Loop::stop();
61
                    }
62
                }
63
            );
64
            Loop::repeat(10, function () use (&$readers, $output) {
65
                /** @var array<int, Context\Context> $readers */
66
67
                $promises = [];
68
                foreach ($readers as $pid => $reader) {
69
                    if (!$reader->isRunning()) {
70
                        /** @psalm-suppress MixedArrayAccess*/
71
                        unset($readers[$pid]);
72
                        continue;
73
                    }
74
                    $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

74
                    $promises[] = /** @scrutinizer ignore-call */ \Amp\call(
Loading history...
75
                        function () use ($reader, &$readers, $pid, $output) {
76
                            /** @psalm-suppress MixedArrayAccess*/
77
                            unset($readers[$pid]);
78
                            /** @var string $result */
79
                            $result = yield $reader->receive();
80
                            $output->write($result);
81
                            $readers[$pid] = $reader;
82
                        }
83
                    );
84
                }
85
                yield $promises;
86
            });
87
        });
88
89
        return 0;
90
    }
91
}
92