• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dg / dresscode / 41044778467

30 Oct 2026 10:08AM UTC coverage: 97.234% (-0.3%) from 97.495%
41044778467

push

github

dg
upgrade: a decision only the data of the target version turn on belongs to the jump, the project as it is today resolved without narrowing, so that the plan no longer fixes it in the bridge, where nothing runs it

837123ff-7353-45e9-8cd7-ba68b373e93e

5 of 5 new or added lines in 1 file covered. (100.0%)

160 existing lines in 33 files now uncovered.

27315 of 28092 relevant lines covered (97.23%)

0.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.0
/src/Measuring/ProcessProbeRunner.php
1
<?php declare(strict_types=1);
2

3
/**
4
 * This file is part of the DressCode, a coding style and upgrade tool for PHP (https://dresscode.run)
5
 * Copyright (c) 2026 David Grudl (https://davidgrudl.com)
6
 */
7

8
namespace DressCode\Measuring;
9

10
use DressCode\ConfigurationException;
11
use DressCode\Engine\WorkerPool;
12
use Nette\Neon\Neon;
13
use Nette\Utils\FileSystem;
14
use function count, dirname, is_array;
15
use const PHP_OS_FAMILY;
16

17

18
/**
19
 * Runs the probes as processes of `check --format json`, as many at once as the machine has processors, the processors
20
 * shared among those running: a probe over a sample is cheaper alone in a process than with a pool of workers to start,
21
 * and a few probes over many files get the workers between them. The configurations lie in the system temp, never in
22
 * the project, and `--root` makes the project the root of the run. The script is always the one of this DressCode, and a
23
 * probe starts no probes of its own, so the processes never grow beyond one level, however the runner is called.
24
 * @internal
25
 */
26
final readonly class ProcessProbeRunner implements ProbeRunner
27
{
28
        /** how long the loop waits between two looks at the processes, in microseconds */
29
        private const PollInterval = 20_000;
30

31
        /** the variable of the environment a probe runs with */
32
        private const ProbeVariable = 'DRESSCODE_PROBE';
33

34

35
        public function __construct(
36
                private string $root,
1 ✔
37
                /** @var list<string>  the PHP and the script of DressCode */
38
                private array $command,
1 ✔
39
                private int $processors,
1 ✔
40
                /** @var ?\Closure(int, int): void  the probes done and all of them, told at every look while they run */
41
                private ?\Closure $onProgress = null,
1 ✔
42
        ) {
43
        }
1 ✔
44

45

46
        /** @param  ?\Closure(int, int): void  $onProgress */
47
        public static function create(string $root, ?\Closure $onProgress = null): self
48
        {
49
                $script = \Phar::running(returnPhar: false) ?: dirname(__DIR__, 2) . '/bin/dresscode';
1 ✔
50
                return new self($root, [...WorkerPool::buildPhpCommand() ?? [PHP_BINARY], $script], WorkerPool::detectCpuCount(), $onProgress);
1 ✔
51
        }
52

53

54
        public function run(array $probes): array
55
        {
56
                if (getenv(self::ProbeVariable) !== false) {
1 ✔
57
                        throw new \LogicException('A probe of `dresscode init` starts no probes of its own.');
1 ✔
58
                }
59

60
                $dir = sys_get_temp_dir() . '/dresscode-probes-' . uniqid();
1 ✔
61
                FileSystem::createDir($dir);
1 ✔
62
                $parallel = max(1, min($this->processors, count($probes)));
1 ✔
63
                $jobs = max(1, intdiv($this->processors, $parallel));
1 ✔
64
                $queue = array_keys($probes);
1 ✔
65
                $running = $results = [];
1 ✔
66
                try {
67
                        while ($queue || $running) {
1 ✔
68
                                while ($queue && count($running) < $parallel) {
1 ✔
69
                                        $key = array_shift($queue);
1 ✔
70
                                        $running[$key] = $this->start($probes[$key], $dir . '/' . md5((string) $key), $jobs);
1 ✔
71
                                }
72

73
                                foreach ($running as $key => [$process, $file]) {
1 ✔
74
                                        // the exit code is told once, by the first look after the end, and proc_close() then has none
75
                                        $status = proc_get_status($process);
1 ✔
76
                                        if ($status['running']) {
1 ✔
77
                                                continue;
1 ✔
78
                                        }
79

80
                                        proc_close($process);
1 ✔
81
                                        unset($running[$key]);
1 ✔
82
                                        $results[$key] = self::readResult($file, $status['exitcode']);
1 ✔
83
                                }
84

85
                                ($this->onProgress)?->__invoke(count($results), count($probes));
1 ✔
86
                                if ($running) {
1 ✔
87
                                        usleep(self::PollInterval);
1 ✔
88
                                }
89
                        }
90
                } finally {
1 ✔
91
                        foreach ($running as [$process]) {
1 ✔
UNCOV
92
                                proc_terminate($process);
×
93
                        }
94

95
                        FileSystem::delete($dir);
1 ✔
96
                }
97

98
                return $results;
1 ✔
99
        }
100

101

102
        /**
103
         * @param  array<string, mixed>  $config
104
         * @return array{resource, string}  the process and the base name of its files
105
         */
106
        private function start(array $config, string $file, int $jobs): array
107
        {
108
                FileSystem::write("$file.neon", Neon::encode($config, blockMode: true));
1 ✔
109
                $null = PHP_OS_FAMILY === 'Windows' ? 'NUL' : '/dev/null';
1 ✔
110
                $process = proc_open(
1 ✔
111
                        [...$this->command, 'check', '--format=json', "--jobs=$jobs", '--no-cache', "--root=$this->root", "--config=$file.neon"],
1 ✔
112
                        [0 => ['file', $null, 'r'], 1 => ['file', "$file.json", 'w'], 2 => ['file', "$file.err", 'w']],
1 ✔
113
                        $pipes,
114
                        $this->root,
1 ✔
115
                        [...getenv(), self::ProbeVariable => '1'],
1 ✔
116
                );
117
                if ($process === false) {
1 ✔
UNCOV
118
                        throw new \RuntimeException('A process of `check` could not be started.');
×
119
                }
120

121
                return [$process, $file];
1 ✔
122
        }
123

124

125
        /** @throws ConfigurationException */
126
        private static function readResult(string $file, int $exit): ProbeResult
127
        {
128
                $report = json_decode((string) @file_get_contents("$file.json"), associative: true); // @ written by the process
1 ✔
129
                if ($exit > 2 || !is_array($report)) { // 2 is a file a rule failed in, which the report carries
1 ✔
130
                        $error = trim((string) @file_get_contents("$file.err")); // @ written by the process
1 ✔
131
                        throw new ConfigurationException("A check of the measuring ended with exit code $exit: " . ($error ?: 'no output'));
1 ✔
132
                }
133

134
                return ProbeResult::fromJson($report);
1 ✔
135
        }
136
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc