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

dg / dresscode / 42928114183

10 Nov 2026 11:30PM UTC coverage: 97.596% (+0.6%) from 97.024%
42928114183

push

github

dg
released version 1.4.0

27609 of 28289 relevant lines covered (97.6%)

0.98 hits per line

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

83.33
/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;
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  list<string>  $command  the program and its arguments
29
         * @param  ?string  $input  what the command reads on its standard input
30
         * @return array{?int, string}
31
         */
32
        public static function run(
33
                array $command,
34
                string $cwd,
35
                int $timeout,
36
                ?string $input = null,
37
                bool $errors = true,
38
        ): array
39
        {
40
                // the shell of Windows is what finds a program that is a `.bat`
41
                $command = PHP_OS_FAMILY !== 'Windows'
1 ✔
42
                        ? $command
1 ✔
43
                        : implode(' ', array_map(escapeshellarg(...), $command));
×
44
                // files rather than pipes, which Windows reads only blocking, so that a command holding them open still ends in time
45
                [$stdin, $stdout, $stderr] = [tmpfile(), tmpfile(), $errors ? null : tmpfile()];
1 ✔
46
                if ($stdin === false || $stdout === false || $stderr === false) {
1 ✔
47
                        throw new \RuntimeException('A temporary file could not be created.');
×
48
                }
49

50
                fwrite($stdin, (string) $input);
1 ✔
51
                rewind($stdin);
1 ✔
52
                $process = proc_open($command, [0 => $stdin, 1 => $stdout, 2 => $stderr ?? $stdout], $pipes, $cwd);
1 ✔
53
                if ($process === false) {
1 ✔
54
                        return [127, 'The command could not be started.'];
×
55
                }
56

57
                $deadline = microtime(as_float: true) + $timeout;
1 ✔
58
                while (($status = proc_get_status($process))['running'] && microtime(as_float: true) < $deadline) {
1 ✔
59
                        usleep(20_000);
1 ✔
60
                }
61

62
                if ($status['running']) {
1 ✔
63
                        self::killTree($status['pid']);
1 ✔
64
                }
65

66
                proc_close($process);
1 ✔
67
                fseek($stdout, max(0, (int) fstat($stdout)['size'] - self::MaxOutput));
1 ✔
68
                return [$status['running'] ? null : $status['exitcode'], (string) stream_get_contents($stdout)];
1 ✔
69
        }
70

71

72
        /**
73
         * Ends the process and every process it started at once, as ending the shell alone would leave what it started
74
         * orphaned.
75
         */
76
        public static function killTree(int $pid): void
77
        {
78
                if (PHP_OS_FAMILY === 'Windows') {
1 ✔
79
                        exec("taskkill /F /T /PID $pid 2>NUL");
×
80
                        return;
×
81
                }
82

83
                $tree = [$pid];
1 ✔
84
                for ($i = 0; $i < count($tree); $i++) {
1 ✔
85
                        exec("pgrep -P {$tree[$i]} 2>/dev/null", $children);
1 ✔
86
                        array_push($tree, ...array_map('intval', $children));
1 ✔
87
                        $children = [];
1 ✔
88
                }
89

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