Passed
Pull Request — master (#52)
by Shinji
01:45
created

CommandLineEnumerator::getIterator()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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