Skip to content

Commit

Permalink
Fix Deprecation::ignoreDeprecations
Browse files Browse the repository at this point in the history
Turns out that `Deprecation::ignoreDeprecations` does not work at all.
  • Loading branch information
ruudk committed Jun 2, 2023
1 parent 077a19a commit 7f704ad
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
40 changes: 26 additions & 14 deletions lib/Doctrine/Deprecations/Deprecation.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ class Deprecation
private static $ignoredPackages = [];

/** @var array<string,int> */
private static $ignoredLinksCount = [];
private static $triggeredDeprecations = [];

/** @var array<string,bool> */
private static $ignoredLinks = [];

/** @var bool */
private static $deduplication = true;
Expand All @@ -78,13 +81,17 @@ public static function trigger(string $package, string $link, string $message, .
return;
}

if (array_key_exists($link, self::$ignoredLinksCount)) {
self::$ignoredLinksCount[$link]++;
if (isset(self::$ignoredLinks[$link])) {
return;
}

if (array_key_exists($link, self::$triggeredDeprecations)) {
self::$triggeredDeprecations[$link]++;
} else {
self::$ignoredLinksCount[$link] = 1;
self::$triggeredDeprecations[$link] = 1;
}

if (self::$deduplication === true && self::$ignoredLinksCount[$link] > 1) {
if (self::$deduplication === true && self::$triggeredDeprecations[$link] > 1) {
return;
}

Expand Down Expand Up @@ -141,13 +148,17 @@ public static function triggerIfCalledFromOutside(string $package, string $link,
}
}

if (array_key_exists($link, self::$ignoredLinksCount)) {
self::$ignoredLinksCount[$link]++;
if (isset(self::$ignoredLinks[$link])) {
return;
}

if (array_key_exists($link, self::$triggeredDeprecations)) {
self::$triggeredDeprecations[$link]++;
} else {
self::$ignoredLinksCount[$link] = 1;
self::$triggeredDeprecations[$link] = 1;
}

if (self::$deduplication === true && self::$ignoredLinksCount[$link] > 1) {
if (self::$deduplication === true && self::$triggeredDeprecations[$link] > 1) {
return;
}

Expand Down Expand Up @@ -235,9 +246,10 @@ public static function disable(): void
self::$type = self::TYPE_NONE;
self::$logger = null;
self::$deduplication = true;
self::$ignoredLinks = [];

foreach (self::$ignoredLinksCount as $link => $count) {
self::$ignoredLinksCount[$link] = 0;
foreach (self::$triggeredDeprecations as $link => $count) {
self::$triggeredDeprecations[$link] = 0;
}
}

Expand All @@ -249,13 +261,13 @@ public static function ignorePackage(string $packageName): void
public static function ignoreDeprecations(string ...$links): void
{
foreach ($links as $link) {
self::$ignoredLinksCount[$link] = 0;
self::$ignoredLinks[$link] = true;
}
}

public static function getUniqueTriggeredDeprecationsCount(): int
{
return array_reduce(self::$ignoredLinksCount, static function (int $carry, int $count) {
return array_reduce(self::$triggeredDeprecations, static function (int $carry, int $count) {
return $carry + $count;
}, 0);
}
Expand All @@ -267,7 +279,7 @@ public static function getUniqueTriggeredDeprecationsCount(): int
*/
public static function getTriggeredDeprecations(): array
{
return self::$ignoredLinksCount;
return self::$triggeredDeprecations;
}

/**
Expand Down
19 changes: 18 additions & 1 deletion tests/Doctrine/Deprecations/DeprecationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function setUp(): void
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue([]);

$reflectionProperty = new ReflectionProperty(Deprecation::class, 'ignoredLinksCount');
$reflectionProperty = new ReflectionProperty(Deprecation::class, 'triggeredDeprecations');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue([]);

Expand Down Expand Up @@ -202,6 +202,23 @@ public function testDeprecationWithIgnoredPackage(): void
$this->assertEquals(['https://github.com/doctrine/orm/issue/1234' => 1], Deprecation::getTriggeredDeprecations());
}

public function testDeprecationWithIgnoredLink(): void
{
Deprecation::enableWithTriggerError();
Deprecation::ignoreDeprecations('https://github.com/doctrine/orm/issue/1234');

Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/issue/1234',
'this is deprecated %s %d',
'foo',
1234
);

$this->assertEquals(0, Deprecation::getUniqueTriggeredDeprecationsCount());
$this->assertEquals([], Deprecation::getTriggeredDeprecations());
}

public function testDeprecationIfCalledFromOutside(): void
{
Deprecation::enableWithTriggerError();
Expand Down

0 comments on commit 7f704ad

Please sign in to comment.