Skip to content

Commit

Permalink
Add a constructor to CacheKey (#10212)
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus authored Nov 11, 2022
1 parent 318af0a commit 953e42d
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 6 deletions.
5 changes: 5 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Upgrade to 2.14

## Deprecated constructing a `CacheKey` without `$hash`

The `Doctrine\ORM\Cache\CacheKey` class has an explicit constructor now with
an optional parameter `$hash`. That parameter will become mandatory in 3.0.

## Deprecated `AttributeDriver::$entityAnnotationClasses`

If you need to change the behavior of `AttributeDriver::isTransient()`,
Expand Down
16 changes: 16 additions & 0 deletions lib/Doctrine/ORM/Cache/CacheKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Doctrine\ORM\Cache;

use Doctrine\Deprecations\Deprecation;

/**
* Defines entity / collection / query key to be stored in the cache region.
* Allows multiple roles to be stored in the same cache region.
Expand All @@ -17,4 +19,18 @@ abstract class CacheKey
* @var string
*/
public $hash;

public function __construct(?string $hash = null)
{
if ($hash === null) {
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/pull/10212',
'Calling %s() without providing a value for the $hash parameter is deprecated.',
__METHOD__
);
} else {
$this->hash = $hash;
}
}
}
3 changes: 2 additions & 1 deletion lib/Doctrine/ORM/Cache/CollectionCacheKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function __construct($entityClass, $association, array $ownerIdentifier)
$this->ownerIdentifier = $ownerIdentifier;
$this->entityClass = (string) $entityClass;
$this->association = (string) $association;
$this->hash = str_replace('\\', '.', strtolower($entityClass)) . '_' . implode(' ', $ownerIdentifier) . '__' . $association;

parent::__construct(str_replace('\\', '.', strtolower($entityClass)) . '_' . implode(' ', $ownerIdentifier) . '__' . $association);
}
}
3 changes: 2 additions & 1 deletion lib/Doctrine/ORM/Cache/EntityCacheKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function __construct($entityClass, array $identifier)

$this->identifier = $identifier;
$this->entityClass = $entityClass;
$this->hash = str_replace('\\', '.', strtolower($entityClass) . '_' . implode(' ', $identifier));

parent::__construct(str_replace('\\', '.', strtolower($entityClass) . '_' . implode(' ', $identifier)));
}
}
3 changes: 2 additions & 1 deletion lib/Doctrine/ORM/Cache/QueryCacheKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ public function __construct(
int $cacheMode = Cache::MODE_NORMAL,
?TimestampCacheKey $timestampKey = null
) {
$this->hash = $cacheId;
$this->lifetime = $lifetime;
$this->cacheMode = $cacheMode;
$this->timestampKey = $timestampKey;

parent::__construct($cacheId);
}
}
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Cache/TimestampCacheKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ class TimestampCacheKey extends CacheKey
/** @param string $space Result cache id */
public function __construct($space)
{
$this->hash = (string) $space;
parent::__construct((string) $space);
}
}
6 changes: 6 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@
<file name="lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php"/>
</errorLevel>
</MissingParamType>
<PropertyNotSetInConstructor>
<errorLevel type="suppress">
<!-- Remove on 3.0.x -->
<referencedProperty name="Doctrine\ORM\Cache\CacheKey::$hash"/>
</errorLevel>
</PropertyNotSetInConstructor>
<RedundantCastGivenDocblockType>
<errorLevel type="suppress">
<!-- Can be removed once the "getMaxResults" methods of those classes have native parameter types -->
Expand Down
4 changes: 2 additions & 2 deletions tests/Doctrine/Tests/Mocks/CacheKeyMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
*/
class CacheKeyMock extends CacheKey
{
/** @param string $hash The string hash that represend this cache key */
/** @param string $hash The string hash that represents this cache key */
public function __construct(string $hash)
{
$this->hash = $hash;
parent::__construct($hash);
}
}
28 changes: 28 additions & 0 deletions tests/Doctrine/Tests/ORM/Cache/CacheKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@

namespace Doctrine\Tests\ORM\Cache;

use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
use Doctrine\ORM\Cache\CacheKey;
use Doctrine\ORM\Cache\CollectionCacheKey;
use Doctrine\ORM\Cache\EntityCacheKey;
use Doctrine\Tests\DoctrineTestCase;

/** @group DDC-2183 */
class CacheKeyTest extends DoctrineTestCase
{
use VerifyDeprecations;

public function testEntityCacheKeyIdentifierCollision(): void
{
$key1 = new EntityCacheKey('Foo', ['id' => 1]);
Expand Down Expand Up @@ -66,4 +70,28 @@ public function testCollectionCacheKeyAssociationCollision(): void

self::assertNotEquals($key1->hash, $key2->hash);
}

public function testConstructor(): void
{
$key = new class ('my-hash') extends CacheKey {
};

self::assertSame('my-hash', $key->hash);
}

public function testDeprecatedConstructor(): void
{
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/pull/10212');

$key = new class extends CacheKey {
public function __construct()
{
$this->hash = 'my-hash';

parent::__construct();
}
};

self::assertSame('my-hash', $key->hash);
}
}

0 comments on commit 953e42d

Please sign in to comment.