From e4c9316253c9180dd9687ffc10a7a6a26f97291e Mon Sep 17 00:00:00 2001 From: Fran Moreno Date: Sun, 19 Nov 2023 17:18:28 +0100 Subject: [PATCH] Add compatibility with doctrine/orm 3 --- phpstan-baseline.neon | 6 ++++ psalm-baseline.xml | 9 +++++- src/Collection/AuditedCollection.php | 3 +- src/EventListener/LogRevisionsListener.php | 33 ++++++++++++++-------- 4 files changed, 38 insertions(+), 13 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 394c5050..652f5ea8 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -3,3 +3,9 @@ parameters: # The class "AuditController" is deprecated and extends a class that is only supported in "symfony/http-foundation" < 5. # NEXT_MAJOR: Remove these files - src/Controller/AuditController.php + ignoreErrors: + # For compatibility with doctrine/orm 2 and 3 + - '#.+ has invalid type Doctrine\\ORM\\Mapping\\AssociationMapping\.$#' + - '#^Property SimpleThings\\EntityAudit\\Collection\\AuditedCollection\:\:\$associationDefinition has unknown class Doctrine\\ORM\\Mapping\\AssociationMapping as its type\.$#' + - '#^Cannot access offset ''relationToSourceKey…'' on array\\|Doctrine\\ORM\\Mapping\\AssociationMapping\.$#' + - '#^Cannot access offset ''relationToTargetKey…'' on array\\|Doctrine\\ORM\\Mapping\\AssociationMapping\.$#' diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 5733a34c..efe38aa5 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,9 +1,16 @@ - + Controller + + + + |AssociationMapping]]> + |AssociationMapping]]> + + diff --git a/src/Collection/AuditedCollection.php b/src/Collection/AuditedCollection.php index 53bec428..578bc5e3 100644 --- a/src/Collection/AuditedCollection.php +++ b/src/Collection/AuditedCollection.php @@ -15,6 +15,7 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; +use Doctrine\ORM\Mapping\AssociationMapping; use Doctrine\ORM\Mapping\ClassMetadata; use SimpleThings\EntityAudit\AuditConfiguration; use SimpleThings\EntityAudit\AuditReader; @@ -70,7 +71,7 @@ public function __construct( protected AuditReader $auditReader, protected $class, protected ClassMetadata $metadata, - protected $associationDefinition, + protected array|AssociationMapping $associationDefinition, protected array $foreignKeys, protected $revision ) { diff --git a/src/EventListener/LogRevisionsListener.php b/src/EventListener/LogRevisionsListener.php index bd413c80..ea31414b 100644 --- a/src/EventListener/LogRevisionsListener.php +++ b/src/EventListener/LogRevisionsListener.php @@ -24,6 +24,7 @@ use Doctrine\ORM\Event\PostPersistEventArgs; use Doctrine\ORM\Event\PostUpdateEventArgs; use Doctrine\ORM\Events; +use Doctrine\ORM\Mapping\AssociationMapping; use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Persisters\Entity\EntityPersister; use Doctrine\ORM\UnitOfWork; @@ -451,12 +452,15 @@ private function getInsertRevisionSQL(EntityManagerInterface $em, ClassMetadata } /** - * @param ClassMetadata $class - * @param ClassMetadata $targetClass - * @param array $assoc + * @param ClassMetadata $class + * @param ClassMetadata $targetClass + * @param array|AssociationMapping $assoc */ - private function getInsertJoinTableRevisionSQL(ClassMetadata $class, ClassMetadata $targetClass, array $assoc): string - { + private function getInsertJoinTableRevisionSQL( + ClassMetadata $class, + ClassMetadata $targetClass, + array|AssociationMapping $assoc + ): string { $cacheKey = $class->name.'.'.$targetClass->name; if (!isset($this->insertJoinTableRevisionSQL[$cacheKey]) && isset($assoc['relationToSourceKeyColumns'], $assoc['relationToTargetKeyColumns'], $assoc['joinTable']['name'])) { @@ -608,13 +612,20 @@ private function saveRevisionEntityData(EntityManagerInterface $em, ClassMetadat } /** - * @param array $assoc - * @param array $entityData - * @param ClassMetadata $class - * @param ClassMetadata $targetClass + * @param array|AssociationMapping $assoc + * @param array $entityData + * @param ClassMetadata $class + * @param ClassMetadata $targetClass */ - private function recordRevisionForManyToManyEntity(object $relatedEntity, EntityManagerInterface $em, string $revType, array $entityData, array $assoc, ClassMetadata $class, ClassMetadata $targetClass): void - { + private function recordRevisionForManyToManyEntity( + object $relatedEntity, + EntityManagerInterface $em, + string $revType, + array $entityData, + array|AssociationMapping $assoc, + ClassMetadata $class, + ClassMetadata $targetClass + ): void { $conn = $em->getConnection(); $joinTableParams = [$this->getRevisionId($conn), $revType]; $joinTableTypes = [\PDO::PARAM_INT, \PDO::PARAM_STR];