Passed
Push — 0.6.x ( 9a1948...a98caa )
by Shinji
03:23 queued 01:32
created

CommandLineEnumerator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the reliforp/reli-prof 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 Reli\Lib\Process\ProcFileSystem;
15
16
use IteratorAggregate;
17
use Reli\Lib\File\FileReaderInterface;
18
19
final class CommandLineEnumerator implements IteratorAggregate
20
{
21
    public function __construct(
22
        private FileReaderInterface $file_reader
23
    ) {
24
    }
25
26
    /** @return \Generator<int, string> */
27
    public function getIterator()
28
    {
29
        /**
30
         * @var string $full_path
31
         * @var \SplFileInfo $item
32
         */
33
        foreach (new \GlobIterator('/proc/*/cmdline') as $full_path => $item) {
34
            if (file_exists($full_path) === false) {
35
                continue;
36
            }
37
            $command_line = $this->file_reader->readAll($full_path);
38
            if ($command_line === '') {
39
                continue;
40
            }
41
            if (!is_numeric(basename($item->getPath()))) {
42
                continue;
43
            }
44
            yield (int)basename($item->getPath()) => preg_replace('/\0/', ' ', $command_line);
45
        }
46
    }
47
}
48