Skip to content

Commit

Permalink
Make AuditReader non-generic
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu committed May 19, 2022
1 parent 8da0ac0 commit ff60726
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 17 deletions.
3 changes: 3 additions & 0 deletions src/Action/CompareAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public function __construct(Environment $twig, AuditReader $auditReader)
$this->auditReader = $auditReader;
}

/**
* @phpstan-param class-string $className
*/
public function __invoke(Request $request, string $className, string $id, ?int $oldRev = null, ?int $newRev = null): Response
{
if (null === $oldRev) {
Expand Down
3 changes: 3 additions & 0 deletions src/Action/ViewDetailAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public function __construct(Environment $twig, AuditReader $auditReader)
$this->auditReader = $auditReader;
}

/**
* @phpstan-param class-string $className
*/
public function __invoke(string $className, string $id, int $rev): Response
{
$entity = $this->auditReader->find($className, $id, $rev);
Expand Down
3 changes: 3 additions & 0 deletions src/Action/ViewEntityAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public function __construct(Environment $twig, AuditReader $auditReader)
$this->auditReader = $auditReader;
}

/**
* @phpstan-param class-string $className
*/
public function __invoke(string $className, string $id): Response
{
$revisions = $this->auditReader->findRevisions($className, $id);
Expand Down
33 changes: 20 additions & 13 deletions src/AuditReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@
use SimpleThings\EntityAudit\Metadata\MetadataFactory;
use SimpleThings\EntityAudit\Utils\ArrayDiff;

/**
* @phpstan-template T of object
*/
class AuditReader
{
use SQLResultCasing;
Expand Down Expand Up @@ -69,7 +66,7 @@ class AuditReader
*
* @var array<string, array<string, array<int|string, object>>>
*
* @phpstan-var array<class-string, array<string, array<int|string, T>>>
* @phpstan-var array<class-string, array<string, array<int|string, object>>>
*/
private $entityCache;

Expand Down Expand Up @@ -206,6 +203,8 @@ public function clearEntityCache(): void
* returns last revision INCLUDING "DEL" revision. If you want to throw exception instead, set
* $threatDeletionAsException to true.
*
* @template T of object
*
* @param string $className
* @param int|string|array<string, int|string> $id
* @param int|string $revision
Expand Down Expand Up @@ -517,7 +516,7 @@ public function findRevision($revision)
*
* @return Revision[]
*
* @phpstan-param class-string<T> $className
* @phpstan-param class-string $className
*/
public function findRevisions($className, $id)
{
Expand Down Expand Up @@ -575,7 +574,7 @@ public function findRevisions($className, $id)
*
* @return int|string|null
*
* @phpstan-param class-string<T> $className
* @phpstan-param class-string $className
*/
public function getCurrentRevision($className, $id)
{
Expand Down Expand Up @@ -637,7 +636,7 @@ public function getCurrentRevision($className, $id)
*
* @return array<string, array<string, mixed>>
*
* @phpstan-param class-string<T> $className
* @phpstan-param class-string $className
* @phpstan-return array<string, array{old: mixed, new: mixed, same: mixed}>
*/
public function diff($className, $id, $oldRevision, $newRevision)
Expand All @@ -661,8 +660,7 @@ public function diff($className, $id, $oldRevision, $newRevision)
*
* @return array<string, mixed>
*
* @phpstan-param class-string<T> $className
* @phpstan-param T $entity
* @phpstan-param class-string $className
*/
public function getEntityValues($className, $entity)
{
Expand All @@ -678,6 +676,8 @@ public function getEntityValues($className, $entity)
}

/**
* @template T of object
*
* @param string $className
* @param int|string|array<string, int|string> $id
*
Expand Down Expand Up @@ -775,6 +775,8 @@ protected function getEntityPersister($className)
/**
* Simplified and stolen code from UnitOfWork::createEntity.
*
* @template T of object
*
* @param string $className
* @param array<string, string> $columnMap
* @param array<string, mixed> $data
Expand Down Expand Up @@ -805,9 +807,11 @@ private function createEntity($className, array $columnMap, array $data, $revisi

$key = implode(':', $keyParts);

if (isset($this->entityCache[$className], $this->entityCache[$className][$key], $this->entityCache[$className][$key][$revision])
) {
return $this->entityCache[$className][$key][$revision];
if (isset($this->entityCache[$className][$key][$revision])) {
/** @phpstan-var T $cachedEntity */
$cachedEntity = $this->entityCache[$className][$key][$revision];

return $cachedEntity;
}

if (!$classMetadata->isInheritanceTypeNone()) {
Expand All @@ -830,7 +834,10 @@ private function createEntity($className, array $columnMap, array $data, $revisi
$pk[$classMetadata->getColumnName($field)] = $data[$field];
}

return $this->find($classMetadata->discriminatorMap[$discriminator], $pk, $revision);
/** @phpstan-var class-string<T> $classNameDiscriminator */
$classNameDiscriminator = $classMetadata->discriminatorMap[$discriminator];

return $this->find($classNameDiscriminator, $pk, $revision);
}
} else {
/** @phpstan-var T $entity */
Expand Down
4 changes: 2 additions & 2 deletions src/Collection/AuditedCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class AuditedCollection implements Collection
/**
* Related audit reader instance.
*
* @var AuditReader<T>
* @var AuditReader
*/
protected $auditReader;

Expand Down Expand Up @@ -105,7 +105,7 @@ class AuditedCollection implements Collection
* @param array<string, mixed> $foreignKeys
* @param string|int $revision
*
* @phpstan-param AuditReader<T> $auditReader
* @phpstan-param AuditReader $auditReader
* @phpstan-param class-string<T> $class
* @phpstan-param ClassMetadataInfo<T> $classMeta
*/
Expand Down
6 changes: 4 additions & 2 deletions tests/RelationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -790,8 +790,10 @@ public function testOneToOneEdgeCase(): void

$auditedBase = $reader->find(\get_class($base), $base->getId(), 1);

static::assertSame('foobar', $auditedBase->getReferencedEntity()->getFoobarField());
static::assertSame('referenced', $auditedBase->getReferencedEntity()->getReferencedField());
$referencedEntity = $auditedBase->getReferencedEntity();
static::assertInstanceOf(RelationFoobarEntity::class, $referencedEntity);
static::assertSame('foobar', $referencedEntity->getFoobarField());
static::assertSame('referenced', $referencedEntity->getReferencedField());
}

/**
Expand Down

0 comments on commit ff60726

Please sign in to comment.