Skip to content

Commit

Permalink
Merge pull request #43 from ruudk/fix
Browse files Browse the repository at this point in the history
Fix Deprecation::ignoreDeprecations
  • Loading branch information
greg0ire authored Jun 2, 2023
2 parents bd8081c + 7f704ad commit 1be0069
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
38 changes: 25 additions & 13 deletions lib/Doctrine/Deprecations/Deprecation.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class Deprecation
private static $ignoredPackages = [];

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

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

/** @var bool */
Expand All @@ -79,13 +82,17 @@ public static function trigger(string $package, string $link, string $message, .
return;
}

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

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

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

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

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

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

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

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

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

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

public static function getUniqueTriggeredDeprecationsCount(): int
{
return array_reduce(self::$ignoredLinks, static function (int $carry, int $count) {
return array_reduce(self::$triggeredDeprecations, static function (int $carry, int $count) {
return $carry + $count;
}, 0);
}
Expand All @@ -270,7 +282,7 @@ public static function getUniqueTriggeredDeprecationsCount(): int
*/
public static function getTriggeredDeprecations(): array
{
return self::$ignoredLinks;
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 @@ -28,7 +28,7 @@ public function setUp(): void
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue([]);

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

Expand Down Expand Up @@ -203,6 +203,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 1be0069

Please sign in to comment.