Completed
Push — master ( a823ea...a54744 )
by Shinji
19s queued 12s
created

GetEgAddressCommand::execute()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 12
nc 5
nop 2
dl 0
loc 18
c 1
b 0
f 1
cc 5
rs 9.5555
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 PhpProfiler\Lib\Elf\Parser\ElfParserException;
17
use PhpProfiler\Lib\Elf\Tls\TlsFinderException;
18
use PhpProfiler\Lib\Process\MemoryReader\MemoryReaderException;
19
use PhpProfiler\ProcessReader\PhpGlobalsFinder;
20
use PhpProfiler\Lib\Elf\Process\ProcessSymbolReaderException;
21
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...
22
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...
23
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...
24
use Symfony\Component\Console\Output\ConsoleOutputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Consol...\ConsoleOutputInterface 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...
25
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...
26
27
/**
28
 * Class GetEgAddressCommand
29
 * @package PhpProfiler\Command\Inspector
30
 */
31
final class GetEgAddressCommand extends Command
32
{
33
    private PhpGlobalsFinder $php_globals_finder;
34
35
    /**
36
     * GetEgAddressCommand constructor.
37
     *
38
     * @param PhpGlobalsFinder $php_globals_finder
39
     * @param string|null $name
40
     */
41
    public function __construct(
42
        PhpGlobalsFinder $php_globals_finder,
43
        string $name = null
44
    ) {
45
        parent::__construct($name);
46
        $this->php_globals_finder = $php_globals_finder;
47
    }
48
49
    public function configure(): void
50
    {
51
        $this->setName('inspector:eg_address')
52
            ->setDescription('get EG address from an outer process or thread')
53
            ->addOption('pid', 'p', InputOption::VALUE_REQUIRED, 'process id');
54
    }
55
56
    /**
57
     * @param InputInterface $input
58
     * @param OutputInterface $output
59
     * @return int
60
     * @throws MemoryReaderException
61
     * @throws ProcessSymbolReaderException
62
     * @throws ElfParserException
63
     * @throws TlsFinderException
64
     */
65
    public function execute(InputInterface $input, OutputInterface $output): int
66
    {
67
        $pid = $input->getOption('pid');
68
        if (is_null($pid)) {
69
            $error_output = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
70
            $error_output->writeln('pid is not specified');
71
            return 1;
72
        }
73
        $pid = filter_var($pid, FILTER_VALIDATE_INT);
74
        if ($pid === false) {
75
            $error_output = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
76
            $error_output->writeln('pid is not integer');
77
            return 2;
78
        }
79
80
        $output->writeln('0x' . dechex($this->php_globals_finder->findExecutorGlobals($pid)));
81
82
        return 0;
83
    }
84
}
85