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

dg / dresscode / 41357673455

01 Nov 2026 10:32PM UTC coverage: 96.734%. Remained the same
41357673455

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%)

64 existing lines in 10 files now uncovered.

29297 of 30286 relevant lines covered (96.73%)

0.97 hits per line

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

98.81
/src/Engine/WorkerCodec.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 DressCode\Config\{Layer, LayerKind};
11
use DressCode\{FileResult, Risk, Rule, Severity, Violation};
12
use function is_array, is_string;
13

14

15
/**
16
 * The result of a file as a worker sends it to the parent, with what it measured for `--profile`: the output only
17
 * where it differs from the code, which the parent has, the output and the path base64-encoded since neither need
18
 * be UTF-8.
19
 * @internal
20
 */
21
final class WorkerCodec
22
{
23
        /**
24
         * @param  ?array<string, mixed>  $profile  see `Profiler::takeRecords()`
25
         * @return array<string, mixed>
26
         */
27
        public static function encode(FileResult $result, ?array $profile): array
28
        {
29
                return [
30
                        'path' => base64_encode($result->path),
1 ✔
31
                        'output' => $result->output === $result->code ? true : ($result->output === null ? null : base64_encode($result->output)),
1 ✔
32
                        'violations' => array_map(self::encodeViolation(...), $result->violations),
1 ✔
33
                        'warnings' => $result->warnings,
1 ✔
34
                        'syntaxError' => $result->syntaxError,
1 ✔
35
                        'syntaxErrorLine' => $result->syntaxErrorLine,
1 ✔
36
                        'passes' => $result->passes,
1 ✔
37
                        'failure' => $result->failure,
1 ✔
38
                        'failureDocs' => $result->failureDocs,
1 ✔
39
                        'baselined' => $result->baselined,
1 ✔
40
                        'remaining' => array_map(self::encodeViolation(...), $result->remaining),
1 ✔
41
                        'written' => $result->written,
1 ✔
42
                        'cached' => $result->cached,
1 ✔
43
                        'removedImports' => $result->removedImports,
1 ✔
44
                        'mutatedRules' => $result->mutatedRules,
1 ✔
45
                        'profile' => $profile,
1 ✔
46
                ];
47
        }
48

49

50
        /**
51
         * @param  array<string, mixed>  $data  as `encode()` made it
52
         * @param  string  $code  the content the parent handed the worker
53
         * @return array{FileResult, ?array<string, mixed>}  the result and what the worker measured with it
54
         */
55
        public static function decode(array $data, string $code): array
56
        {
57
                $output = $data['output'];
1 ✔
58
                $result = new FileResult(
1 ✔
59
                        path: (string) base64_decode((string) $data['path'], strict: true),
1 ✔
60
                        code: $code,
61
                        output: $output === true ? $code : ($output === null ? null : (string) base64_decode((string) $output, strict: true)),
1 ✔
62
                        violations: self::decodeViolations($data['violations']),
1 ✔
63
                        warnings: is_array($data['warnings']) ? array_values(array_map(strval(...), $data['warnings'])) : [],
1 ✔
64
                        syntaxError: $data['syntaxError'] === null ? null : (string) $data['syntaxError'],
1 ✔
65
                        syntaxErrorLine: $data['syntaxErrorLine'] === null ? null : (int) $data['syntaxErrorLine'],
1 ✔
66
                        passes: (int) $data['passes'],
1 ✔
67
                        failure: $data['failure'] === null ? null : (string) $data['failure'],
1 ✔
68
                        failureDocs: $data['failureDocs'] === null ? null : (string) $data['failureDocs'],
1 ✔
69
                        baselined: is_array($data['baselined']) ? array_values(array_map(strval(...), $data['baselined'])) : [],
1 ✔
70
                        remaining: self::decodeViolations($data['remaining']),
1 ✔
71
                        written: (bool) $data['written'],
1 ✔
72
                        cached: (bool) $data['cached'],
1 ✔
73
                        removedImports: array_values(array_map(fn(array $import) => [
1 ✔
74
                                'kind' => match ($import['kind']) {
1 ✔
75
                                        'function' => 'function',
1 ✔
UNCOV
76
                                        'const' => 'const',
×
77
                                        default => 'class',
1 ✔
78
                                },
79
                                'name' => (string) $import['name'],
1 ✔
80
                                'alias' => $import['alias'] === null ? null : (string) $import['alias'],
1 ✔
81
                                'namespace' => (string) $import['namespace'],
1 ✔
82
                        ], is_array($data['removedImports']) ? $data['removedImports'] : [])),
1 ✔
83
                        mutatedRules: is_array($data['mutatedRules']) ? array_values(array_filter($data['mutatedRules'], self::isRule(...))) : [],
1 ✔
84
                );
85
                return [$result, is_array($data['profile']) ? $data['profile'] : null];
1 ✔
86
        }
87

88

89
        /** @return array<string, mixed> */
90
        public static function encodeViolation(Violation $violation): array
91
        {
92
                return [
93
                        'decision' => $violation->decision,
1 ✔
94
                        'message' => $violation->message,
1 ✔
95
                        'line' => $violation->line,
1 ✔
96
                        'column' => $violation->column,
1 ✔
97
                        'severity' => $violation->severity->value,
1 ✔
98
                        'fingerprint' => $violation->fingerprint,
1 ✔
99
                        'fixable' => $violation->fixable,
1 ✔
100
                        'risk' => $violation->risk?->value,
1 ✔
101
                        'refused' => $violation->refused,
1 ✔
102
                        'because' => $violation->because,
1 ✔
103
                        'derivedFrom' => $violation->derivedFrom,
1 ✔
104
                        'offset' => $violation->offset,
1 ✔
105
                        'length' => $violation->length,
1 ✔
106
                        'originalLines' => $violation->originalLines,
1 ✔
107
                        'source' => $violation->source === null ? null : [
1 ✔
108
                                $violation->source->name,
1 ✔
109
                                $violation->source->package,
1 ✔
110
                                $violation->source->decision,
1 ✔
111
                                $violation->source->library,
1 ✔
112
                                $violation->source->since,
1 ✔
113
                                $violation->source->from,
1 ✔
114
                        ],
115
                ];
116
        }
117

118

119
        /**
120
         * @param  mixed  $data  a list of what `encodeViolation()` made
121
         * @return list<Violation>
122
         */
123
        public static function decodeViolations(mixed $data): array
124
        {
125
                return array_values(array_map(fn(array $item) => new Violation(
1 ✔
126
                        (string) $item['decision'],
1 ✔
127
                        (string) $item['message'],
1 ✔
128
                        (int) $item['line'],
1 ✔
129
                        $item['column'] === null ? null : (int) $item['column'],
1 ✔
130
                        Severity::from((string) $item['severity']),
1 ✔
131
                        (string) $item['fingerprint'],
1 ✔
132
                        (bool) $item['fixable'],
1 ✔
133
                        $item['risk'] === null ? null : Risk::from((string) $item['risk']),
1 ✔
134
                        (bool) $item['refused'],
1 ✔
135
                        $item['because'] === null ? null : (string) $item['because'],
1 ✔
136
                        $item['derivedFrom'] === null ? null : (string) $item['derivedFrom'],
1 ✔
137
                        $item['offset'] === null ? null : (int) $item['offset'],
1 ✔
138
                        $item['length'] === null ? null : (int) $item['length'],
1 ✔
139
                        is_array($item['originalLines']) ? [(int) $item['originalLines'][0], (int) $item['originalLines'][1]] : null,
1 ✔
140
                        is_array($item['source']) ? self::decodeSource($item['source']) : null,
1 ✔
141
                ), is_array($data) ? $data : []));
1 ✔
142
        }
143

144

145
        /** @param  array<mixed>  $data */
146
        private static function decodeSource(array $data): Layer
147
        {
148
                [$name, $package, $decision, $library, $since, $from] = array_map(fn($value) => $value === null ? null : (string) $value, $data);
1 ✔
149
                return new Layer(LayerKind::Package, $name, $package, $decision, $library, $since, $from);
1 ✔
150
        }
151

152

153
        /** @phpstan-assert-if-true class-string<Rule> $class */
154
        private static function isRule(mixed $class): bool
155
        {
156
                return is_string($class) && is_a($class, Rule::class, allow_string: true);
1 ✔
157
        }
158
}
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