diff --git a/DeprecationErrorHandler.php b/DeprecationErrorHandler.php index d72d0b9..00453b7 100644 --- a/DeprecationErrorHandler.php +++ b/DeprecationErrorHandler.php @@ -200,7 +200,7 @@ public function shutdown() // store failing status $isFailing = !$configuration->tolerates($this->deprecationGroups); - $this->displayDeprecations($groups, $configuration, $isFailing); + $this->displayDeprecations($groups, $configuration); $this->resetDeprecationGroups(); @@ -213,7 +213,7 @@ public function shutdown() } $isFailingAtShutdown = !$configuration->tolerates($this->deprecationGroups); - $this->displayDeprecations($groups, $configuration, $isFailingAtShutdown); + $this->displayDeprecations($groups, $configuration); if ($configuration->isGeneratingBaseline()) { $configuration->writeBaseline(); @@ -289,11 +289,10 @@ private static function colorize($str, $red) /** * @param string[] $groups * @param Configuration $configuration - * @param bool $isFailing * * @throws \InvalidArgumentException */ - private function displayDeprecations($groups, $configuration, $isFailing) + private function displayDeprecations($groups, $configuration) { $cmp = function ($a, $b) { return $b->count() - $a->count(); @@ -320,7 +319,8 @@ private function displayDeprecations($groups, $configuration, $isFailing) fwrite($handle, "\n".self::colorize($deprecationGroupMessage, 'legacy' !== $group && 'indirect' !== $group)."\n"); } - if ('legacy' !== $group && !$configuration->verboseOutput($group) && !$isFailing) { + // Skip the verbose output if the group is quiet and not failing according to its threshold: + if ('legacy' !== $group && !$configuration->verboseOutput($group) && $configuration->toleratesForGroup($group, $this->deprecationGroups)) { continue; } $notices = $this->deprecationGroups[$group]->notices(); diff --git a/DeprecationErrorHandler/Configuration.php b/DeprecationErrorHandler/Configuration.php index bc46e4f..6e9f0e4 100644 --- a/DeprecationErrorHandler/Configuration.php +++ b/DeprecationErrorHandler/Configuration.php @@ -165,6 +165,32 @@ public function tolerates(array $deprecationGroups) return true; } + /** + * @param array $deprecationGroups + * + * @return bool true if the threshold is not reached for the deprecation type nor for the total + */ + public function toleratesForGroup(string $groupName, array $deprecationGroups): bool + { + $grandTotal = 0; + + foreach ($deprecationGroups as $type => $group) { + if ('legacy' !== $type) { + $grandTotal += $group->count(); + } + } + + if ($grandTotal > $this->thresholds['total']) { + return false; + } + + if (\in_array($groupName, ['self', 'direct', 'indirect'], true) && $deprecationGroups[$groupName]->count() > $this->thresholds[$groupName]) { + return false; + } + + return true; + } + /** * @return bool */ diff --git a/Tests/DeprecationErrorHandler/ConfigurationTest.php b/Tests/DeprecationErrorHandler/ConfigurationTest.php index 5d36a43..3e3a831 100644 --- a/Tests/DeprecationErrorHandler/ConfigurationTest.php +++ b/Tests/DeprecationErrorHandler/ConfigurationTest.php @@ -234,6 +234,103 @@ public function testOutputIsNotVerboseInWeakMode() $this->assertFalse($configuration->verboseOutput('other')); } + /** + * @dataProvider provideDataForToleratesForGroup + */ + public function testToleratesForIndividualGroups(string $deprecationsHelper, array $deprecationsPerType, array $expected) + { + $configuration = Configuration::fromUrlEncodedString($deprecationsHelper); + + $groups = $this->buildGroups($deprecationsPerType); + + foreach ($expected as $groupName => $tolerates) { + $this->assertSame($tolerates, $configuration->toleratesForGroup($groupName, $groups), sprintf('Deprecation type "%s" is %s', $groupName, $tolerates ? 'tolerated' : 'not tolerated')); + } + } + + public function provideDataForToleratesForGroup() { + + yield 'total threshold not reached' => ['max[total]=1', [ + 'unsilenced' => 0, + 'self' => 0, + 'legacy' => 1, // Legacy group is ignored in total threshold + 'other' => 0, + 'direct' => 1, + 'indirect' => 0, + ], [ + 'unsilenced' => true, + 'self' => true, + 'legacy' => true, + 'other' => true, + 'direct' => true, + 'indirect' => true, + ]]; + + yield 'total threshold reached' => ['max[total]=1', [ + 'unsilenced' => 0, + 'self' => 0, + 'legacy' => 1, + 'other' => 0, + 'direct' => 1, + 'indirect' => 1, + ], [ + 'unsilenced' => false, + 'self' => false, + 'legacy' => false, + 'other' => false, + 'direct' => false, + 'indirect' => false, + ]]; + + yield 'direct threshold reached' => ['max[total]=99&max[direct]=0', [ + 'unsilenced' => 0, + 'self' => 0, + 'legacy' => 1, + 'other' => 0, + 'direct' => 1, + 'indirect' => 1, + ], [ + 'unsilenced' => true, + 'self' => true, + 'legacy' => true, + 'other' => true, + 'direct' => false, + 'indirect' => true, + ]]; + + yield 'indirect & self threshold reached' => ['max[total]=99&max[direct]=0&max[self]=0', [ + 'unsilenced' => 0, + 'self' => 1, + 'legacy' => 1, + 'other' => 1, + 'direct' => 1, + 'indirect' => 1, + ], [ + 'unsilenced' => true, + 'self' => false, + 'legacy' => true, + 'other' => true, + 'direct' => false, + 'indirect' => true, + ]]; + + yield 'indirect & self threshold not reached' => ['max[total]=99&max[direct]=2&max[self]=2', [ + 'unsilenced' => 0, + 'self' => 1, + 'legacy' => 1, + 'other' => 1, + 'direct' => 1, + 'indirect' => 1, + ], [ + 'unsilenced' => true, + 'self' => true, + 'legacy' => true, + 'other' => true, + 'direct' => true, + 'indirect' => true, + ]]; + } + private function buildGroups($counts) { $groups = []; diff --git a/Tests/DeprecationErrorHandler/partially_quiet2.phpt b/Tests/DeprecationErrorHandler/partially_quiet2.phpt new file mode 100644 index 0000000..4d0d6c3 --- /dev/null +++ b/Tests/DeprecationErrorHandler/partially_quiet2.phpt @@ -0,0 +1,36 @@ +--TEST-- +Test DeprecationErrorHandler quiet on everything but self/direct deprecations +--FILE-- + +--EXPECTF-- +Unsilenced deprecation notices (3) + +Remaining direct deprecation notices (2) + + 1x: root deprecation + + 1x: silenced bar deprecation + 1x in FooTestCase::testNonLegacyBar + +Remaining indirect deprecation notices (1) + +Legacy deprecation notices (2)