Skip to content

Commit

Permalink
Improve typehint
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Apr 4, 2021
1 parent fd702d1 commit a800c2a
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 40 deletions.
81 changes: 56 additions & 25 deletions src/AuditReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Doctrine\ORM\Mapping\QuoteStrategy;
use Doctrine\ORM\ORMException;
use Doctrine\ORM\PersistentCollection;
use Doctrine\ORM\Persisters\Entity\EntityPersister;
use Doctrine\ORM\Query;
use SimpleThings\EntityAudit\Collection\AuditedCollection;
use SimpleThings\EntityAudit\Exception\DeletedException;
Expand Down Expand Up @@ -199,9 +200,9 @@ public function clearEntityCache(): void
* returns last revision INCLUDING "DEL" revision. If you want to throw exception instead, set
* $threatDeletionAsException to true.
*
* @param string $className
* @param mixed $id
* @param int $revision
* @param string $className
* @param int|string|array $id
* @param int|string $revision
*
* @throws DeletedException
* @throws NoRevisionFoundException
Expand All @@ -211,6 +212,10 @@ public function clearEntityCache(): void
* @throws \RuntimeException
*
* @return object
*
* @phpstan-template T of object
* @phpstan-param class-string<T> $className
* @phpstan-return T|null
*/
public function find($className, $id, $revision, array $options = [])
{
Expand Down Expand Up @@ -363,6 +368,10 @@ public function findRevisionHistory($limit = 20, $offset = 0)
*
* @deprecated this function name is misspelled.
* Suggest using findEntitiesChangedAtRevision instead.
*
* @param string|int $revision
*
* @return ChangedEntity[]
*/
public function findEntitesChangedAtRevision($revision)
{
Expand All @@ -372,7 +381,7 @@ public function findEntitesChangedAtRevision($revision)
/**
* Return a list of ChangedEntity instances created at the given revision.
*
* @param int $revision
* @param string|int $revision
*
* @throws DeletedException
* @throws NoRevisionFoundException
Expand Down Expand Up @@ -470,17 +479,17 @@ public function findEntitiesChangedAtRevision($revision)
/**
* Return the revision object for a particular revision.
*
* @param int $rev
* @param string|int $revision
*
* @throws InvalidRevisionException
* @throws Exception
*
* @return Revision
*/
public function findRevision($rev)
public function findRevision($revision)
{
$query = 'SELECT * FROM '.$this->config->getRevisionTableName().' r WHERE r.id = ?';
$revisionsData = $this->em->getConnection()->fetchAllAssociative($query, [$rev]);
$revisionsData = $this->em->getConnection()->fetchAllAssociative($query, [$revision]);

if (1 === \count($revisionsData)) {
return new Revision(
Expand All @@ -489,19 +498,21 @@ public function findRevision($rev)
$revisionsData[0]['username']
);
}
throw new InvalidRevisionException($rev);
throw new InvalidRevisionException($revision);
}

/**
* Find all revisions that were made of entity class with given id.
*
* @param string $className
* @param mixed $id
* @param string $className
* @param int|string|array $id
*
* @throws NotAuditedException
* @throws Exception
*
* @return Revision[]
*
* @phpstan-param class-string $className
*/
public function findRevisions($className, $id)
{
Expand Down Expand Up @@ -551,13 +562,15 @@ public function findRevisions($className, $id)
/**
* Gets the current revision of the entity with given ID.
*
* @param string $className
* @param mixed $id
* @param string $className
* @param int|string|array $id
*
* @throws NotAuditedException
* @throws Exception
*
* @return int
* @return int|string
*
* @phpstan-param class-string $className
*/
public function getCurrentRevision($className, $id)
{
Expand Down Expand Up @@ -598,10 +611,10 @@ public function getCurrentRevision($className, $id)
* Get an array with the differences of between two specific revisions of
* an object with a given id.
*
* @param string $className
* @param int $id
* @param int $oldRevision
* @param int $newRevision
* @param string $className
* @param int|string|array $id
* @param int|string $oldRevision
* @param int|string $newRevision
*
* @throws DeletedException
* @throws NoRevisionFoundException
Expand All @@ -610,7 +623,10 @@ public function getCurrentRevision($className, $id)
* @throws ORMException
* @throws \RuntimeException
*
* @return array
* @return array<string, array<string, mixed>>
*
* @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 @@ -632,6 +648,8 @@ public function diff($className, $id, $oldRevision, $newRevision)
* @param object $entity
*
* @return array
*
* @phpstan-param class-string $className
*/
public function getEntityValues($className, $entity)
{
Expand All @@ -648,16 +666,18 @@ public function getEntityValues($className, $entity)
}

/**
* @param string $className
* @param int $id
* @param string $className
* @param int|string|array $id
*
* @throws DeletedException
* @throws NoRevisionFoundException
* @throws NotAuditedException
* @throws Exception
* @throws ORMException
*
* @return array
* @return object[]
*
* @phpstan-param class-string $className
*/
public function getEntityHistory($className, $id)
{
Expand Down Expand Up @@ -725,18 +745,25 @@ public function getEntityHistory($className, $id)
return $result;
}

protected function getEntityPersister($entity)
/**
* @param string $className
*
* @return EntityPersister
*
* @phpstan-param class-string $className
*/
protected function getEntityPersister($className)
{
$uow = $this->em->getUnitOfWork();

return $uow->getEntityPersister($entity);
return $uow->getEntityPersister($className);
}

/**
* Simplified and stolen code from UnitOfWork::createEntity.
*
* @param string $className
* @param $revision
* @param string $className
* @param int|string $revision
*
* @throws DeletedException
* @throws NoRevisionFoundException
Expand All @@ -746,6 +773,10 @@ protected function getEntityPersister($entity)
* @throws \RuntimeException
*
* @return object
*
* @phpstan-template T of object
* @phpstan-param class-string<T> $className
* @phpstan-return T
*/
private function createEntity($className, array $columnMap, array $data, $revision)
{
Expand Down
17 changes: 16 additions & 1 deletion src/ChangedEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,27 @@

class ChangedEntity
{
/**
* @var string
*/
private $className;

/**
* @var array
*/
private $id;

/**
* @var string
*/
private $revType;

/**
* @var object
*/
private $entity;

public function __construct($className, array $id, $revType, $entity)
public function __construct(string $className, array $id, string $revType, object $entity)
{
$this->className = $className;
$this->id = $id;
Expand Down
11 changes: 10 additions & 1 deletion src/Exception/AuditException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,22 @@

abstract class AuditException extends \Exception
{
/**
* @var string|null
*/
protected $className;

/**
* @var array|null
*/
protected $id;

/**
* @var int|string|null
*/
protected $revision;

public function __construct($className, $id, $revision)
public function __construct(?string $className, ?array $id, $revision)
{
$this->className = $className;
$this->id = $id;
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/AuditedCollectionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class AuditedCollectionException extends AuditException
{
public function __construct($message)
public function __construct(string $message)
{
\Exception::__construct($message);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Exception/DeletedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@

class DeletedException extends AuditException
{
public function __construct($className, $id, $revision)
public function __construct(string $className, array $id, $revision)
{
parent::__construct($className, $id, $revision);

$this->message = sprintf(
'Class "%s" entity id "%s" has been removed at revision %s',
$className,
Expand Down
6 changes: 2 additions & 4 deletions src/Exception/InvalidRevisionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ class InvalidRevisionException extends AuditException
public function __construct($revision)
{
parent::__construct(null, null, $revision);
$this->message = sprintf(
'No revision "%s" exists.',
$revision
);

$this->message = sprintf('No revision "%s" exists.', $revision);
}
}
3 changes: 2 additions & 1 deletion src/Exception/NoRevisionFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@

class NoRevisionFoundException extends AuditException
{
public function __construct($className, $id, $revision)
public function __construct(string $className, array $id, $revision)
{
parent::__construct($className, $id, $revision);

$this->message = sprintf(
'No revision of class "%s" (%s) was found at revision %s or before. The entity did not exist at the specified revision yet.',
$className,
Expand Down
8 changes: 3 additions & 5 deletions src/Exception/NotAuditedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@

class NotAuditedException extends AuditException
{
public function __construct($className)
public function __construct(string $className)
{
parent::__construct($className, null, null);
$this->message = sprintf(
'Class "%s" is not audited.',
$className
);

$this->message = sprintf('Class "%s" is not audited.', $className);
}
}
13 changes: 12 additions & 1 deletion src/Revision.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,22 @@
*/
class Revision
{
/**
* @var int|string
*/
private $rev;

/**
* @var \DateTime
*/
private $timestamp;

/**
* @var string
*/
private $username;

public function __construct($rev, $timestamp, $username)
public function __construct($rev, \DateTime $timestamp, string $username)
{
$this->rev = $rev;
$this->timestamp = $timestamp;
Expand Down
8 changes: 8 additions & 0 deletions src/Utils/ArrayDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
*/
class ArrayDiff
{
/**
* @param array $oldData
* @param array $newData
*
* @return array<string, array<string, mixed>>
*
* @phpstan-return array<string, array{old: mixed, new: mixed, same: mixed}>
*/
public function diff($oldData, $newData)
{
$diff = [];
Expand Down

0 comments on commit a800c2a

Please sign in to comment.