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

dg / dresscode / 39958913551

23 Oct 2026 12:44PM UTC coverage: 96.921% (-0.9%) from 97.773%
39958913551

push

github

dg
uses the Runner of nette/command-line WIP

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

278 existing lines in 25 files now uncovered.

24711 of 25496 relevant lines covered (96.92%)

0.97 hits per line

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

98.65
/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\{FileResult, Risk, Rule, Severity, Violation};
11
use function is_array, is_string;
12

13

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

48

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

87

88
        /** @return array<string, mixed> */
89
        private static function encodeViolation(Violation $violation): array
90
        {
91
                return [
92
                        'decision' => $violation->decision,
1 ✔
93
                        'message' => $violation->message,
1 ✔
94
                        'line' => $violation->line,
1 ✔
95
                        'column' => $violation->column,
1 ✔
96
                        'severity' => $violation->severity->value,
1 ✔
97
                        'fingerprint' => $violation->fingerprint,
1 ✔
98
                        'fixable' => $violation->fixable,
1 ✔
99
                        'risk' => $violation->risk?->value,
1 ✔
100
                        'refused' => $violation->refused,
1 ✔
101
                        'because' => $violation->because,
1 ✔
102
                        'derivedFrom' => $violation->derivedFrom,
1 ✔
103
                        'offset' => $violation->offset,
1 ✔
104
                        'length' => $violation->length,
1 ✔
105
                        'originalLines' => $violation->originalLines,
1 ✔
106
                ];
107
        }
108

109

110
        /** @return list<Violation> */
111
        private static function decodeViolations(mixed $data): array
112
        {
113
                return array_values(array_map(fn(array $item) => new Violation(
1 ✔
114
                        (string) $item['decision'],
1 ✔
115
                        (string) $item['message'],
1 ✔
116
                        (int) $item['line'],
1 ✔
117
                        $item['column'] === null ? null : (int) $item['column'],
1 ✔
118
                        Severity::from((string) $item['severity']),
1 ✔
119
                        (string) $item['fingerprint'],
1 ✔
120
                        (bool) $item['fixable'],
1 ✔
121
                        $item['risk'] === null ? null : Risk::from((string) $item['risk']),
1 ✔
122
                        (bool) $item['refused'],
1 ✔
123
                        $item['because'] === null ? null : (string) $item['because'],
1 ✔
124
                        $item['derivedFrom'] === null ? null : (string) $item['derivedFrom'],
1 ✔
125
                        $item['offset'] === null ? null : (int) $item['offset'],
1 ✔
126
                        $item['length'] === null ? null : (int) $item['length'],
1 ✔
127
                        is_array($item['originalLines']) ? [(int) $item['originalLines'][0], (int) $item['originalLines'][1]] : null,
1 ✔
128
                ), is_array($data) ? $data : []));
1 ✔
129
        }
130

131

132
        /** @phpstan-assert-if-true class-string<Rule> $class */
133
        private static function isRule(mixed $class): bool
134
        {
135
                return is_string($class) && is_a($class, Rule::class, allow_string: true);
1 ✔
136
        }
137
}
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