Skip to content

Commit f61a643

Browse files
authored
ConsoleFormatter: display error counts per category (#175)
1 parent 7feaa97 commit f61a643

File tree

2 files changed

+55
-24
lines changed

2 files changed

+55
-24
lines changed

src/Result/ConsoleFormatter.php

+43-12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use function array_reduce;
1313
use function count;
1414
use function fnmatch;
15+
use function in_array;
1516
use function round;
1617
use function strlen;
1718
use function strpos;
@@ -123,60 +124,73 @@ private function printResultErrors(
123124
$prodDependencyOnlyInDevErrors = $result->getProdDependencyOnlyInDevErrors();
124125
$unusedDependencyErrors = $result->getUnusedDependencyErrors();
125126

126-
if (count($unknownClassErrors) > 0) {
127+
$unknownClassErrorsCount = count($unknownClassErrors);
128+
$unknownFunctionErrorsCount = count($unknownFunctionErrors);
129+
$shadowDependencyErrorsCount = count($shadowDependencyErrors);
130+
$devDependencyInProductionErrorsCount = count($devDependencyInProductionErrors);
131+
$prodDependencyOnlyInDevErrorsCount = count($prodDependencyOnlyInDevErrors);
132+
$unusedDependencyErrorsCount = count($unusedDependencyErrors);
133+
134+
if ($unknownClassErrorsCount > 0) {
127135
$hasError = true;
136+
$classes = $this->pluralize($unknownClassErrorsCount, 'class');
128137
$this->printSymbolBasedErrors(
129-
'Unknown classes!',
138+
"Found $unknownClassErrorsCount unknown $classes!",
130139
'unable to autoload those, so we cannot check them',
131140
$unknownClassErrors,
132141
$maxShownUsages
133142
);
134143
}
135144

136-
if (count($unknownFunctionErrors) > 0) {
145+
if ($unknownFunctionErrorsCount > 0) {
137146
$hasError = true;
147+
$functions = $this->pluralize($unknownFunctionErrorsCount, 'function');
138148
$this->printSymbolBasedErrors(
139-
'Unknown functions!',
149+
"Found $unknownFunctionErrorsCount unknown $functions!",
140150
'those are not declared, so we cannot check them',
141151
$unknownFunctionErrors,
142152
$maxShownUsages
143153
);
144154
}
145155

146-
if (count($shadowDependencyErrors) > 0) {
156+
if ($shadowDependencyErrorsCount > 0) {
147157
$hasError = true;
158+
$dependencies = $this->pluralize($shadowDependencyErrorsCount, 'dependency');
148159
$this->printPackageBasedErrors(
149-
'Found shadow dependencies!',
160+
"Found $shadowDependencyErrorsCount shadow $dependencies!",
150161
'those are used, but not listed as dependency in composer.json',
151162
$shadowDependencyErrors,
152163
$maxShownUsages
153164
);
154165
}
155166

156-
if (count($devDependencyInProductionErrors) > 0) {
167+
if ($devDependencyInProductionErrorsCount > 0) {
157168
$hasError = true;
169+
$dependencies = $this->pluralize($devDependencyInProductionErrorsCount, 'dependency');
158170
$this->printPackageBasedErrors(
159-
'Found dev dependencies in production code!',
171+
"Found $devDependencyInProductionErrorsCount dev $dependencies in production code!",
160172
'those should probably be moved to "require" section in composer.json',
161173
$devDependencyInProductionErrors,
162174
$maxShownUsages
163175
);
164176
}
165177

166-
if (count($prodDependencyOnlyInDevErrors) > 0) {
178+
if ($prodDependencyOnlyInDevErrorsCount > 0) {
167179
$hasError = true;
180+
$dependencies = $this->pluralize($prodDependencyOnlyInDevErrorsCount, 'dependency');
168181
$this->printPackageBasedErrors(
169-
'Found prod dependencies used only in dev paths!',
182+
"Found $prodDependencyOnlyInDevErrorsCount prod $dependencies used only in dev paths!",
170183
'those should probably be moved to "require-dev" section in composer.json',
171184
array_fill_keys($prodDependencyOnlyInDevErrors, []),
172185
$maxShownUsages
173186
);
174187
}
175188

176-
if (count($unusedDependencyErrors) > 0) {
189+
if ($unusedDependencyErrorsCount > 0) {
177190
$hasError = true;
191+
$dependencies = $this->pluralize($unusedDependencyErrorsCount, 'dependency');
178192
$this->printPackageBasedErrors(
179-
'Found unused dependencies!',
193+
"Found $unusedDependencyErrorsCount unused $dependencies!",
180194
'those are listed in composer.json, but no usage was found in scanned paths',
181195
array_fill_keys($unusedDependencyErrors, []),
182196
$maxShownUsages
@@ -433,4 +447,21 @@ private function willLimitUsages(array $usages, int $limit): bool
433447
return false;
434448
}
435449

450+
private function pluralize(int $count, string $singular): string
451+
{
452+
if ($count === 1) {
453+
return $singular;
454+
}
455+
456+
if (substr($singular, -1) === 's' || substr($singular, -1) === 'x' || substr($singular, -2) === 'sh' || substr($singular, -2) === 'ch') {
457+
return $singular . 'es';
458+
}
459+
460+
if (substr($singular, -1) === 'y' && !in_array($singular[strlen($singular) - 2], ['a', 'e', 'i', 'o', 'u'], true)) {
461+
return substr($singular, 0, -1) . 'ies';
462+
}
463+
464+
return $singular . 's';
465+
}
466+
436467
}

tests/ConsoleFormatterTest.php

+12-12
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,23 @@ public function testPrintResult(): void
8383

8484
$expectedRegularOutput = <<<'OUT'
8585
86-
Unknown classes!
86+
Found 1 unknown class!
8787
(unable to autoload those, so we cannot check them)
8888
8989
• Unknown\Thing
9090
in app/init.php:1093
9191
9292
9393
94-
Unknown functions!
94+
Found 1 unknown function!
9595
(those are not declared, so we cannot check them)
9696
9797
• Unknown\function
9898
in app/foo.php:51
9999
100100
101101
102-
Found shadow dependencies!
102+
Found 2 shadow dependencies!
103103
(those are used, but not listed as dependency in composer.json)
104104
105105
• shadow/another
@@ -110,21 +110,21 @@ public function testPrintResult(): void
110110
111111
112112
113-
Found dev dependencies in production code!
113+
Found 1 dev dependency in production code!
114114
(those should probably be moved to "require" section in composer.json)
115115
116116
• some/package
117117
e.g. Another\Command in src/ProductGenerator.php:28
118118
119119
120120
121-
Found prod dependencies used only in dev paths!
121+
Found 1 prod dependency used only in dev paths!
122122
(those should probably be moved to "require-dev" section in composer.json)
123123
124124
• misplaced/package
125125
126126
127-
Found unused dependencies!
127+
Found 1 unused dependency!
128128
(those are listed in composer.json, but no usage was found in scanned paths)
129129
130130
• dead/package
@@ -135,23 +135,23 @@ public function testPrintResult(): void
135135
OUT;
136136
$expectedVerboseOutput = <<<'OUT'
137137
138-
Unknown classes!
138+
Found 1 unknown class!
139139
(unable to autoload those, so we cannot check them)
140140
141141
• Unknown\Thing
142142
app/init.php:1093
143143
144144
145145
146-
Unknown functions!
146+
Found 1 unknown function!
147147
(those are not declared, so we cannot check them)
148148
149149
• Unknown\function
150150
app/foo.php:51
151151
152152
153153
154-
Found shadow dependencies!
154+
Found 2 shadow dependencies!
155155
(those are used, but not listed as dependency in composer.json)
156156
157157
• shadow/another
@@ -170,21 +170,21 @@ public function testPrintResult(): void
170170
+ 1 more symbol
171171
172172
173-
Found dev dependencies in production code!
173+
Found 1 dev dependency in production code!
174174
(those should probably be moved to "require" section in composer.json)
175175
176176
• some/package
177177
Another\Command
178178
src/ProductGenerator.php:28
179179
180180
181-
Found prod dependencies used only in dev paths!
181+
Found 1 prod dependency used only in dev paths!
182182
(those should probably be moved to "require-dev" section in composer.json)
183183
184184
• misplaced/package
185185
186186
187-
Found unused dependencies!
187+
Found 1 unused dependency!
188188
(those are listed in composer.json, but no usage was found in scanned paths)
189189
190190
• dead/package

0 commit comments

Comments
 (0)