diff --git a/src/Result/ConsoleFormatter.php b/src/Result/ConsoleFormatter.php index 345a97d..67402dc 100644 --- a/src/Result/ConsoleFormatter.php +++ b/src/Result/ConsoleFormatter.php @@ -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; @@ -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 @@ -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'; + } + } diff --git a/tests/ConsoleFormatterTest.php b/tests/ConsoleFormatterTest.php index 0f200be..19bbea2 100644 --- a/tests/ConsoleFormatterTest.php +++ b/tests/ConsoleFormatterTest.php @@ -83,7 +83,7 @@ public function testPrintResult(): void $expectedRegularOutput = <<<'OUT' -Unknown classes! +Found 1 unknown class! (unable to autoload those, so we cannot check them) • Unknown\Thing @@ -91,7 +91,7 @@ public function testPrintResult(): void -Unknown functions! +Found 1 unknown function! (those are not declared, so we cannot check them) • Unknown\function @@ -99,7 +99,7 @@ public function testPrintResult(): void -Found shadow dependencies! +Found 2 shadow dependencies! (those are used, but not listed as dependency in composer.json) • shadow/another @@ -110,7 +110,7 @@ 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 @@ -118,13 +118,13 @@ public function testPrintResult(): void -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 @@ -135,7 +135,7 @@ public function testPrintResult(): void OUT; $expectedVerboseOutput = <<<'OUT' -Unknown classes! +Found 1 unknown class! (unable to autoload those, so we cannot check them) • Unknown\Thing @@ -143,7 +143,7 @@ public function testPrintResult(): void -Unknown functions! +Found 1 unknown function! (those are not declared, so we cannot check them) • Unknown\function @@ -151,7 +151,7 @@ public function testPrintResult(): void -Found shadow dependencies! +Found 2 shadow dependencies! (those are used, but not listed as dependency in composer.json) • shadow/another @@ -170,7 +170,7 @@ 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 @@ -178,13 +178,13 @@ public function testPrintResult(): void 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