• 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

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

141

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

149

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