Passed
Pull Request — master (#9)
by Shinji
07:44
created

DaemonCommand::execute()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 19
nc 6
nop 2
dl 0
loc 29
c 2
b 0
f 1
cc 6
rs 9.0111
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\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...
17
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...
18
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...
19
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...
20
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...
21
22
class DaemonCommand extends Command
23
{
24
    public function configure(): void
25
    {
26
        $this->setName('inspector:daemon')
27
            ->setDescription('periodically get running function name from an outer process or thread');
28
    }
29
30
    /**
31
     * @param InputInterface $input
32
     * @param OutputInterface $output
33
     * @return int
34
     */
35
    public function execute(InputInterface $input, OutputInterface $output): int
36
    {
37
        $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

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

39
        $searcher_pid = /** @scrutinizer ignore-call */ Promise\wait($context->start());
Loading history...
Unused Code introduced by
The assignment to $searcher_pid is dead and can be removed.
Loading history...
40
        /** @var int[] $pid_list */
41
        $pid_list = Promise\wait($context->receive());
42
        $readers = [];
43
        foreach ($pid_list as $pid) {
44
            $context = Context\create(__DIR__ . '/Worker/php-reader.php');
45
            Promise\wait($context->start());
46
            Promise\wait($context->send($pid));
47
            $readers[$pid] = $context;
48
        }
49
        while (1) {
50
            if ($readers) {
51
                foreach ($readers as $pid => $reader) {
52
                    if (!$reader->isRunning()) {
53
                        unset($readers[$pid]);
54
                        continue;
55
                    }
56
                    /** @var string */
57
                    $result = Promise\wait($reader->receive());
58
                    $output->write($result);
59
                }
60
            }
61
            time_nanosleep(0, 1000 * 1000 * 10);
62
        }
63
        return 0;
64
    }
65
}
66