Passed
Push — master ( d75285...c2a6de )
by Shinji
12:57
created

GetEgAddressCommand::writeError()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 4
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 PhpProfiler\Lib\Elf\Parser\ElfParserException;
17
use PhpProfiler\Lib\Elf\Tls\TlsFinderException;
18
use PhpProfiler\Lib\PhpProcessReader\PhpGlobalsFinder;
19
use PhpProfiler\Lib\Process\MemoryReader\MemoryReaderException;
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
            $this->writeError('pid is not specified', $output);
70
            return 1;
71
        }
72
        $pid = filter_var($pid, FILTER_VALIDATE_INT);
73
        if ($pid === false) {
74
            $this->writeError('pid is not integer', $output);
75
            return 2;
76
        }
77
78
        $output->writeln('0x' . dechex($this->php_globals_finder->findExecutorGlobals($pid)));
79
80
        return 0;
81
    }
82
83
    public function writeError(string $message, OutputInterface $output): void
84
    {
85
        $error_output = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
86
        $error_output->writeln($message);
87
    }
88
}
89