Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConsoleFormatter: display error counts per category #175

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 43 additions & 12 deletions src/Result/ConsoleFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use function array_reduce;
use function count;
use function fnmatch;
use function in_array;
use function round;
use function strlen;
use function strpos;
Expand Down Expand Up @@ -123,60 +124,73 @@ private function printResultErrors(
$prodDependencyOnlyInDevErrors = $result->getProdDependencyOnlyInDevErrors();
$unusedDependencyErrors = $result->getUnusedDependencyErrors();

if (count($unknownClassErrors) > 0) {
$unknownClassErrorsCount = count($unknownClassErrors);
$unknownFunctionErrorsCount = count($unknownFunctionErrors);
$shadowDependencyErrorsCount = count($shadowDependencyErrors);
$devDependencyInProductionErrorsCount = count($devDependencyInProductionErrors);
$prodDependencyOnlyInDevErrorsCount = count($prodDependencyOnlyInDevErrors);
$unusedDependencyErrorsCount = count($unusedDependencyErrors);

if ($unknownClassErrorsCount > 0) {
$hasError = true;
$classes = $this->pluralize($unknownClassErrorsCount, 'class');
$this->printSymbolBasedErrors(
'Unknown classes!',
"Found $unknownClassErrorsCount unknown $classes!",
'unable to autoload those, so we cannot check them',
$unknownClassErrors,
$maxShownUsages
);
}

if (count($unknownFunctionErrors) > 0) {
if ($unknownFunctionErrorsCount > 0) {
$hasError = true;
$functions = $this->pluralize($unknownFunctionErrorsCount, 'function');
$this->printSymbolBasedErrors(
'Unknown functions!',
"Found $unknownFunctionErrorsCount unknown $functions!",
'those are not declared, so we cannot check them',
$unknownFunctionErrors,
$maxShownUsages
);
}

if (count($shadowDependencyErrors) > 0) {
if ($shadowDependencyErrorsCount > 0) {
$hasError = true;
$dependencies = $this->pluralize($shadowDependencyErrorsCount, 'dependency');
$this->printPackageBasedErrors(
'Found shadow dependencies!',
"Found $shadowDependencyErrorsCount shadow $dependencies!",
'those are used, but not listed as dependency in composer.json',
$shadowDependencyErrors,
$maxShownUsages
);
}

if (count($devDependencyInProductionErrors) > 0) {
if ($devDependencyInProductionErrorsCount > 0) {
$hasError = true;
$dependencies = $this->pluralize($devDependencyInProductionErrorsCount, 'dependency');
$this->printPackageBasedErrors(
'Found dev dependencies in production code!',
"Found $devDependencyInProductionErrorsCount dev $dependencies in production code!",
'those should probably be moved to "require" section in composer.json',
$devDependencyInProductionErrors,
$maxShownUsages
);
}

if (count($prodDependencyOnlyInDevErrors) > 0) {
if ($prodDependencyOnlyInDevErrorsCount > 0) {
$hasError = true;
$dependencies = $this->pluralize($prodDependencyOnlyInDevErrorsCount, 'dependency');
$this->printPackageBasedErrors(
'Found prod dependencies used only in dev paths!',
"Found $prodDependencyOnlyInDevErrorsCount prod $dependencies used only in dev paths!",
'those should probably be moved to "require-dev" section in composer.json',
array_fill_keys($prodDependencyOnlyInDevErrors, []),
$maxShownUsages
);
}

if (count($unusedDependencyErrors) > 0) {
if ($unusedDependencyErrorsCount > 0) {
$hasError = true;
$dependencies = $this->pluralize($unusedDependencyErrorsCount, 'dependency');
$this->printPackageBasedErrors(
'Found unused dependencies!',
"Found $unusedDependencyErrorsCount unused $dependencies!",
'those are listed in composer.json, but no usage was found in scanned paths',
array_fill_keys($unusedDependencyErrors, []),
$maxShownUsages
Expand Down Expand Up @@ -433,4 +447,21 @@ private function willLimitUsages(array $usages, int $limit): bool
return false;
}

private function pluralize(int $count, string $singular): string
{
if ($count === 1) {
return $singular;
}

if (substr($singular, -1) === 's' || substr($singular, -1) === 'x' || substr($singular, -2) === 'sh' || substr($singular, -2) === 'ch') {
return $singular . 'es';
}

if (substr($singular, -1) === 'y' && !in_array($singular[strlen($singular) - 2], ['a', 'e', 'i', 'o', 'u'], true)) {
return substr($singular, 0, -1) . 'ies';
}

return $singular . 's';
}

}
24 changes: 12 additions & 12 deletions tests/ConsoleFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,23 @@ public function testPrintResult(): void

$expectedRegularOutput = <<<'OUT'

Unknown classes!
Found 1 unknown class!
(unable to autoload those, so we cannot check them)

• Unknown\Thing
in app/init.php:1093



Unknown functions!
Found 1 unknown function!
(those are not declared, so we cannot check them)

• Unknown\function
in app/foo.php:51



Found shadow dependencies!
Found 2 shadow dependencies!
(those are used, but not listed as dependency in composer.json)

• shadow/another
Expand All @@ -110,21 +110,21 @@ public function testPrintResult(): void



Found dev dependencies in production code!
Found 1 dev dependency in production code!
(those should probably be moved to "require" section in composer.json)

• some/package
e.g. Another\Command in src/ProductGenerator.php:28



Found prod dependencies used only in dev paths!
Found 1 prod dependency used only in dev paths!
(those should probably be moved to "require-dev" section in composer.json)

• misplaced/package


Found unused dependencies!
Found 1 unused dependency!
(those are listed in composer.json, but no usage was found in scanned paths)

• dead/package
Expand All @@ -135,23 +135,23 @@ public function testPrintResult(): void
OUT;
$expectedVerboseOutput = <<<'OUT'

Unknown classes!
Found 1 unknown class!
(unable to autoload those, so we cannot check them)

• Unknown\Thing
app/init.php:1093



Unknown functions!
Found 1 unknown function!
(those are not declared, so we cannot check them)

• Unknown\function
app/foo.php:51



Found shadow dependencies!
Found 2 shadow dependencies!
(those are used, but not listed as dependency in composer.json)

• shadow/another
Expand All @@ -170,21 +170,21 @@ public function testPrintResult(): void
+ 1 more symbol


Found dev dependencies in production code!
Found 1 dev dependency in production code!
(those should probably be moved to "require" section in composer.json)

• some/package
Another\Command
src/ProductGenerator.php:28


Found prod dependencies used only in dev paths!
Found 1 prod dependency used only in dev paths!
(those should probably be moved to "require-dev" section in composer.json)

• misplaced/package


Found unused dependencies!
Found 1 unused dependency!
(those are listed in composer.json, but no usage was found in scanned paths)

• dead/package
Expand Down
Loading