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

CommandLineEnumerator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 10
c 1
b 0
f 1
dl 0
loc 28
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getIterator() 0 18 5
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