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

dg / dresscode / 42439021063

08 Nov 2026 02:24AM UTC coverage: 96.937% (-0.002%) from 96.939%
42439021063

push

github

dg
long lines: the head of a foreach too wide spreads what it iterates before what it assigns

Of two constructs on a line with no common one to spread the last spreads first, so a `foreach` iterating a long
array literal spread the destructuring after `as` and kept the array whole, `[…] as [` above `$location,`. What
a `foreach` iterates now spreads first, its measure not looking past the `as`, and what it assigns waits while the
iterated expression on its line has something to spread, spreading only where the line left, `] as [$a, $b]) {`, is
still too wide; what follows the iterated expression no longer keeps it from spreading, the destructuring spreading
next. The search for a split beginning at a token is shared, `NodeHelpers::beginsSplit()`.

b243d2e8-3e44-4152-8c16-82d9156e6db2

33 of 36 new or added lines in 1 file covered. (91.67%)

96 existing lines in 23 files now uncovered.

30032 of 30981 relevant lines covered (96.94%)

0.97 hits per line

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

97.44
/src/Domain.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;
9

10
use DressCode\Domains\{Count, Words};
11
use function count, is_array, is_bool, is_string;
12
use const JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE;
13

14

15
/**
16
 * What a decision takes: the values it accepts, how a value is normalized, how the values of two layers merge,
17
 * and a description of itself as data and as prose, which the catalogue exports and the reference and `explain`
18
 * read. The domains are those of `Domains\`, which a plugin declares its decisions with; it writes none of its own.
19
 */
20
abstract readonly class Domain
21
{
22
        /** @internal the item of a list standing for the list of the layer below it */
23
        public const Spread = '...';
24

25
        private const StateWords = [
26
                'forbidden' => 'never there',
27
                'required' => 'always there',
28
                'optional' => 'there or not, as the author wrote it',
29
        ];
30

31
        private const PlacementWords = [
32
                'sameLine' => 'on the line of what comes before',
33
                'nextLine' => 'on the next line',
34
        ];
35

36
        private const AlignmentWords = [
37
                'forbidden' => 'never there, more whitespace collapsing to a single space',
38
                'spaces' => 'spaces aligning a column staying, tabs not',
39
                'tabs' => 'tabs aligning a column staying, spaces not',
40
                'optional' => 'there or not, as the author wrote it',
41
        ];
42

43

44
        /** The words of a state, `forbidden`, `required`, `optional` or several of them, `forbidden` unless some are named. */
45
        public static function state(string ...$words): Words
46
        {
47
                $picked = self::pickWords(self::StateWords, $words ?: ['forbidden']);
1 ✔
48
                return new Words($picked);
1 ✔
49
        }
50

51

52
        /** The words of where a token stands, `sameLine` and `nextLine` unless others are named. */
53
        public static function placement(string ...$words): Words
54
        {
55
                return new Words(self::pickWords(self::PlacementWords, $words ?: ['sameLine', 'nextLine']));
1 ✔
56
        }
57

58

59
        /** The words of where an operator stands at a line break, opening the line after it or ending the line before it. */
60
        public static function operatorPosition(): Words
61
        {
62
                return new Words([
1 ✔
63
                        'lineStart' => 'one ending a line opens the next, unless a comment follows it',
1 ✔
64
                        'lineEnd' => 'one opening a line ends the one before, unless a comment precedes it',
65
                ]);
66
        }
67

68

69
        /** The words of which whitespace wider than a single space stays, aligning a column; all four unless some are named. */
70
        public static function alignment(string ...$words): Words
71
        {
72
                return new Words(self::pickWords(self::AlignmentWords, $words ?: array_keys(self::AlignmentWords)));
1 ✔
73
        }
74

75

76
        /** A count of blank lines, or a range of them. */
77
        public static function blankLines(): Count
78
        {
79
                return new Count;
1 ✔
80
        }
81

82

83
        /**
84
         * The value as NEON or PHP gives it, normalized into the Value of this domain; `keep` is taken where `$keep`
85
         * allows it, which a requirement does and a parameter does not.
86
         * @throws ConfigurationException  naming what the domain accepts
87
         */
88
        final public function accept(mixed $raw, string $path, bool $keep = false): Value
89
        {
90
                return $raw === 'keep' && $keep
1 ✔
91
                        ? Value::keep($this)
1 ✔
92
                        : $this->normalize($raw, $path, $keep);
1 ✔
93
        }
94

95

96
        /**
97
         * The value of two layers merged by the law of the domain: `keep` above wins, and a value above replaces the
98
         * one below, unless the domain merges its maps or the list above holds `...` for the one below.
99
         * @throws ConfigurationException
100
         */
101
        public function merge(Value $below, Value $above, string $path = ''): Value
102
        {
103
                return $above;
1 ✔
104
        }
105

106

107
        /**
108
         * The list above with its `...` replaced by the list below in its place; an item both give is merged in a set and
109
         * refused in an order or a tolerance.
110
         * @param  list<string>  $below
111
         * @param  list<string>  $above
112
         * @return list<string>
113
         * @throws ConfigurationException
114
         */
115
        protected static function spread(array $below, array $above, bool $set, string $path): array
116
        {
117
                $at = array_search(self::Spread, $above, strict: true);
1 ✔
118
                if ($at === false) {
1 ✔
UNCOV
119
                        return $above;
×
120
                }
121

122
                $list = [...array_slice($above, 0, $at), ...$below, ...array_slice($above, $at + 1)];
1 ✔
123
                $twice = array_diff_key($list, array_unique($list));
1 ✔
124
                if ($twice !== [] && !$set) {
1 ✔
125
                        throw new ConfigurationException("Key `$path` gives `" . reset($twice) . '` itself and in the list `...` stands for; write it once.');
1 ✔
126
                }
127

128
                return array_values(array_unique($list));
1 ✔
129
        }
130

131

132
        /**
133
         * The domain as data: its kind, its words or shapes with their meanings, bounds, children, whether a
134
         * tolerance or a map of names is taken.
135
         * @return array<string, mixed>
136
         */
137
        abstract public function toArray(): array;
138

139

140
        /** The values the domain takes in prose, as the comment of an export lists them after `Also:`. */
141
        abstract public function describe(): string;
142

143

144
        /**
145
         * The one value the domain takes, as a layer writes it; null for a domain of several.
146
         * @internal
147
         */
148
        public function findSoleValue(): mixed
149
        {
150
                return null;
1 ✔
151
        }
152

153

154
        /**
155
         * @internal
156
         * @throws ConfigurationException
157
         */
158
        abstract protected function normalize(mixed $raw, string $path, bool $keep): Value;
159

160

161
        /** The name of a symbol of PHP as PHP reads it, in any letter case and with or without its leading backslash. */
162
        protected static function canonicalizeName(string $name): string
163
        {
164
                return strtolower(ltrim($name, '\\'));
1 ✔
165
        }
166

167

168
        /**
169
         * @param  list<string>  $expected  what the domain takes, each already in backticks
170
         * @throws ConfigurationException
171
         */
172
        protected static function refuse(mixed $raw, string $path, array $expected, bool $keep, string $why = ''): never
173
        {
174
                if ($keep) {
1 ✔
175
                        $expected[] = '`keep`';
1 ✔
176
                }
177

178
                $list = count($expected) > 1
1 ✔
179
                        ? implode(', ', array_slice($expected, 0, -1)) . ' or ' . end($expected)
1 ✔
180
                        : ($expected[0] ?? 'nothing');
1 ✔
181
                throw new ConfigurationException("Key `$path` does not take " . self::formatRaw($raw) . ($why === '' ? '' : ", $why") . "; write $list.");
1 ✔
182
        }
183

184

185
        /** The value as the reader wrote it, in backticks. */
186
        protected static function formatRaw(mixed $raw): string
187
        {
188
                return Violation::formatCode(match (true) {
1 ✔
189
                        is_string($raw) => $raw,
1 ✔
190
                        is_bool($raw) => $raw ? 'yes' : 'no',
1 ✔
191
                        $raw === null => 'null',
1 ✔
192
                        is_array($raw) && $raw === [] => '[]',
1 ✔
193
                        default => (string) json_encode($raw, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE),
1 ✔
194
                });
195
        }
196

197

198
        /**
199
         * @param  array<string, string>  $known
200
         * @param  array<string>  $words
201
         * @return array<string, string>
202
         */
203
        private static function pickWords(array $known, array $words): array
204
        {
205
                $picked = [];
1 ✔
206
                foreach ($words as $word) {
1 ✔
207
                        $picked[$word] = $known[$word]
1 ✔
208
                                ?? throw new \InvalidArgumentException("Word `$word` is none of `" . implode('`, `', array_keys($known)) . '`.');
1 ✔
209
                }
210

211
                return $picked;
1 ✔
212
        }
213
}
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