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

dg / dresscode / 42928610199

10 Nov 2026 11:53PM UTC coverage: 97.024% (-0.6%) from 97.596%
42928610199

push

github

dg
cli: the hidden option --profile writes where the time of a run went, the workers included

`--profile <file>` writes as JSON where the time of a run went, in nanoseconds: the phases (the start of the process,
the start of the container of PHPStan or of a derived one and its scopes, the parse, the passes, the gaps, the print,
the cycle collection, the run as a whole), every rule by the time of its callbacks, how often they ran and how often
they changed the tree, every gap rule by the time of its claims, every file with its size, rounds and passes, and the
peak memory of every process. A worker measures its own files and hands that over with every result and the parent
adds it up, so the totals do not depend on the number of workers. A run with the option leaves the result cache out,
a file served from the cache having nothing to measure.

It says where the time of a run goes, not only how much of it: a comparison of two revisions then names the rule or
the claim that moved. Without the option the engine only asks whether there is a profiler, and wraps a claim in
a measurement only when there is one.

137 of 148 new or added lines in 14 files covered. (92.57%)

147 existing lines in 29 files now uncovered.

30283 of 31212 relevant lines covered (97.02%)

0.97 hits per line

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

84.38
/src/Engine/ProcessRunner.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\Engine;
9

10
use function count, is_string;
11
use const PHP_OS_FAMILY;
12

13

14
/**
15
 * Runs a command and ends one that does not end in time as a whole tree, the shell, the command and what it started,
16
 * keeping the end of its output where it writes more than is ever read.
17
 * @internal
18
 */
19
final class ProcessRunner
20
{
21
        /** how much of the output is kept, its end */
22
        private const MaxOutput = 8 * 1024 * 1024;
23

24

25
        /**
26
         * The exit code and the output of both streams, or of the standard one alone, the exit code null where the command did
27
         * not end in time.
28
         * @param  string|list<string>  $command  a command line, or its program and arguments to quote
29
         * @param  ?string  $input  what the command reads on its standard input
30
         * @param  array<string, string>  $env  the variables set beside those of this process
31
         * @return array{?int, string}
32
         */
33
        public static function run(
34
                string|array $command,
35
                string $cwd,
36
                int $timeout,
37
                ?string $input = null,
38
                bool $errors = true,
39
                array $env = [],
40
        ): array
41
        {
42
                // the shell of Windows is what finds `composer.bat`
43
                $command = is_string($command) || PHP_OS_FAMILY !== 'Windows'
1 ✔
44
                        ? $command
1 ✔
UNCOV
45
                        : implode(' ', array_map(escapeshellarg(...), $command));
×
46
                // files rather than pipes, which Windows reads only blocking, so that a command holding them open still ends in time
47
                [$stdin, $stdout, $stderr] = [tmpfile(), tmpfile(), $errors ? null : tmpfile()];
1 ✔
48
                if ($stdin === false || $stdout === false || $stderr === false) {
1 ✔
UNCOV
49
                        throw new \RuntimeException('A temporary file could not be created.');
×
50
                }
51

52
                fwrite($stdin, (string) $input);
1 ✔
53
                rewind($stdin);
1 ✔
54
                $process = proc_open(
1 ✔
55
                        $command,
56
                        [0 => $stdin, 1 => $stdout, 2 => $stderr ?? $stdout],
1 ✔
57
                        $pipes,
58
                        $cwd,
59
                        $env === [] ? null : [...getenv(), ...$env],
1 ✔
60
                );
61
                if ($process === false) {
1 ✔
UNCOV
62
                        return [127, 'The command could not be started.'];
×
63
                }
64

65
                $deadline = microtime(as_float: true) + $timeout;
1 ✔
66
                while (($status = proc_get_status($process))['running'] && microtime(as_float: true) < $deadline) {
1 ✔
67
                        usleep(20_000);
1 ✔
68
                }
69

70
                if ($status['running']) {
1 ✔
71
                        self::killTree($status['pid']);
1 ✔
72
                }
73

74
                proc_close($process);
1 ✔
75
                fseek($stdout, max(0, (int) fstat($stdout)['size'] - self::MaxOutput));
1 ✔
76
                return [$status['running'] ? null : $status['exitcode'], (string) stream_get_contents($stdout)];
1 ✔
77
        }
78

79

80
        /**
81
         * Ends the process and every process it started at once, as ending the shell alone would leave what it started
82
         * orphaned.
83
         */
84
        public static function killTree(int $pid): void
85
        {
86
                if (PHP_OS_FAMILY === 'Windows') {
1 ✔
UNCOV
87
                        exec("taskkill /F /T /PID $pid 2>NUL");
×
UNCOV
88
                        return;
×
89
                }
90

91
                $tree = [$pid];
1 ✔
92
                for ($i = 0; $i < count($tree); $i++) {
1 ✔
93
                        exec("pgrep -P {$tree[$i]} 2>/dev/null", $children);
1 ✔
94
                        array_push($tree, ...array_map('intval', $children));
1 ✔
95
                        $children = [];
1 ✔
96
                }
97

98
                exec('kill -9 ' . implode(' ', $tree) . ' 2>/dev/null');
1 ✔
99
        }
1 ✔
100
}
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