Passed
Pull Request — master (#9)
by Shinji
10:53
created

DaemonCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 58
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 48
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 58
rs 9.1344

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 PhpProfiler\Inspector\Daemon\Dispatcher\DispatchTable;
19
use PhpProfiler\Inspector\Daemon\Dispatcher\Message\TraceMessage;
20
use PhpProfiler\Inspector\Daemon\Dispatcher\WorkerPool;
21
use PhpProfiler\Inspector\Daemon\Reader\Context\PhpReaderContextCreator;
22
use PhpProfiler\Inspector\Daemon\Searcher\Context\PhpSearcherContextCreator;
23
use PhpProfiler\Inspector\Settings\DaemonSettings;
24
use PhpProfiler\Inspector\Settings\GetTraceSettings;
25
use PhpProfiler\Inspector\Settings\TargetPhpSettings;
26
use PhpProfiler\Inspector\Settings\TraceLoopSettings;
27
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...
28
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...
29
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...
30
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...
31
32
final class DaemonCommand extends Command
33
{
34
    private PhpSearcherContextCreator $php_searcher_context_creator;
35
    private PhpReaderContextCreator $php_reader_context_creator;
36
37
    public function __construct(
38
        PhpSearcherContextCreator $php_searcher_context_creator,
39
        PhpReaderContextCreator $php_reader_context_creator
40
    ) {
41
        parent::__construct();
42
        $this->php_reader_context_creator = $php_reader_context_creator;
43
        $this->php_searcher_context_creator = $php_searcher_context_creator;
44
    }
45
46
    public function configure(): void
47
    {
48
        $this->setName('inspector:daemon')
49
            ->setDescription('periodically get running function name from an outer process or thread')
50
            ->addOption(
51
                'target-regex',
52
                'P',
53
                InputOption::VALUE_OPTIONAL,
54
                'regex to find the php binary loaded in the target process'
55
            )
56
            ->addOption('depth', 'd', InputOption::VALUE_OPTIONAL, 'max depth')
57
            ->addOption(
58
                'sleep-ns',
59
                's',
60
                InputOption::VALUE_OPTIONAL,
61
                'nanoseconds between traces (default: 1000 * 1000 * 10)'
62
            )
63
            ->addOption(
64
                'max-retries',
65
                'r',
66
                InputOption::VALUE_OPTIONAL,
67
                'max retries on contiguous errors of read (default: 10)'
68
            )
69
            ->addOption(
70
                'threads',
71
                'T',
72
                InputOption::VALUE_OPTIONAL,
73
                'number of workers (default: 8)'
74
            )
75
            ->addOption(
76
                'php-regex',
77
                null,
78
                InputOption::VALUE_OPTIONAL,
79
                'regex to find the php binary loaded in the target process'
80
            )
81
            ->addOption(
82
                'libpthread-regex',
83
                null,
84
                InputOption::VALUE_OPTIONAL,
85
                'regex to find the libpthread.so loaded in the target process'
86
            )
87
            ->addOption(
88
                'php-version',
89
                null,
90
                InputOption::VALUE_OPTIONAL,
91
                'php version of the target'
92
            )
93
            ->addOption(
94
                'php-path',
95
                null,
96
                InputOption::VALUE_OPTIONAL,
97
                'path to the php binary (only needed in tracing chrooted ZTS target)'
98
            )
99
            ->addOption(
100
                'libpthread-path',
101
                null,
102
                InputOption::VALUE_OPTIONAL,
103
                'path to the libpthread.so (only needed in tracing chrooted ZTS target)'
104
            )
105
        ;
106
    }
107
108
    /**
109
     * @param InputInterface $input
110
     * @param OutputInterface $output
111
     * @return int
112
     */
113
    public function execute(InputInterface $input, OutputInterface $output): int
114
    {
115
        $target_php_settings = TargetPhpSettings::fromConsoleInput($input);
116
        $loop_settings = TraceLoopSettings::fromConsoleInput($input);
117
        $get_trace_settings = GetTraceSettings::fromConsoleInput($input);
118
        $daemon_settings = DaemonSettings::fromConsoleInput($input);
119
120
        $searcher_context = $this->php_searcher_context_creator->create();
121
        Promise\wait($searcher_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

121
        /** @scrutinizer ignore-call */ 
122
        Promise\wait($searcher_context->start());
Loading history...
122
        Promise\wait($searcher_context->sendTargetRegex($daemon_settings->target_regex));
123
124
        $worker_pool = WorkerPool::create(
125
            $this->php_reader_context_creator,
126
            $daemon_settings->threads,
127
            $target_php_settings,
128
            $loop_settings,
129
            $get_trace_settings
130
        );
131
132
        $dispatch_table = new DispatchTable(
133
            $worker_pool,
134
            $target_php_settings,
135
            $loop_settings,
136
            $get_trace_settings
137
        );
138
139
        exec('stty -icanon -echo');
140
141
        Loop::run(function () use ($dispatch_table, $searcher_context, $worker_pool, $output) {
142
            Loop::onReadable(
143
                STDIN,
144
                /** @param resource $stream */
145
                function (string $watcher_id, $stream) {
146
                    $key = fread($stream, 1);
147
                    if ($key === 'q') {
148
                        Loop::cancel($watcher_id);
149
                        Loop::stop();
150
                    }
151
                }
152
            );
153
            Loop::repeat(10, function () use ($dispatch_table, $searcher_context, $worker_pool, $output) {
154
                $promises = [];
155
                static $searcher_on_read = false;
156
                if (!$searcher_on_read) {
157
                    $promises[] = \Amp\call(function () use ($searcher_context, $dispatch_table, &$searcher_on_read) {
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

157
                    $promises[] = /** @scrutinizer ignore-call */ \Amp\call(function () use ($searcher_context, $dispatch_table, &$searcher_on_read) {
Loading history...
158
                        $searcher_on_read = true;
159
                        $update_target_message = yield $searcher_context->receivePidList();
160
                        $dispatch_table->updateTargets($update_target_message->target_process_list);
161
                        $searcher_on_read = false;
162
                    });
163
                }
164
                $readers = $worker_pool->getReadableWorkers();
165
                foreach ($readers as $pid => $reader) {
166
                    $promises[] = \Amp\call(
167
                        function () use ($reader, $pid, $worker_pool, $dispatch_table, $output) {
168
                            $worker_pool->setOnRead($pid);
169
                            $result = yield $reader->receiveTrace();
170
                            $worker_pool->releaseOnRead($pid);
171
                            if ($result instanceof TraceMessage) {
172
                                $output->write($result->trace);
173
                            } else {
174
                                $dispatch_table->releaseOne($result->pid);
175
                            }
176
                        }
177
                    );
178
                }
179
                yield $promises;
180
            });
181
        });
182
183
        return 0;
184
    }
185
}
186