Skip to content

Commit

Permalink
Remove some deprecations accesing mapping as array
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu committed Jun 16, 2024
1 parent a23ac68 commit 3c05a33
Show file tree
Hide file tree
Showing 9 changed files with 251 additions and 90 deletions.
63 changes: 63 additions & 0 deletions .github/workflows/test-orm-3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# TEMPORARILY USED until AuditBundle is compatible with ORM 3

name: Test

on:
pull_request:

permissions:
contents: read

jobs:
test:
name: PHP ${{ matrix.php-version }} + ${{ matrix.dependencies }} + ${{ matrix.variant }} (ORM 3)

runs-on: ubuntu-latest

continue-on-error: ${{ matrix.allowed-to-fail }}

env:
SYMFONY_REQUIRE: ${{matrix.symfony-require}}

strategy:
matrix:
include:
- php-version: '8.3'
dependencies: highest
allowed-to-fail: false
symfony-require: 7.0.*
variant: symfony/symfony:"7.0.*"

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install PHP with extensions
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
coverage: pcov
tools: composer:v2, flex

- name: Add PHPUnit matcher
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- name: Use gedmo-extension main branch
run: composer require "gedmo/doctrine-extensions":"dev-main" --dev --no-update

- name: Install variant
if: matrix.variant != 'normal' && !startsWith(matrix.variant, 'symfony/symfony')
run: composer require ${{ matrix.variant }} --no-update

- name: Install Composer dependencies (${{ matrix.dependencies }})
uses: ramsey/composer-install@v2
with:
dependency-versions: ${{ matrix.dependencies }}

- name: Run Tests with coverage
run: make coverage

- name: Send coverage to Codecov
uses: codecov/codecov-action@v3
with:
files: build/logs/clover.xml
2 changes: 0 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,3 @@ parameters:
# 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\<string, mixed\>\|Doctrine\\ORM\\Mapping\\AssociationMapping\.$#'
- '#^Cannot access offset ''relationToTargetKey…'' on array\<string, mixed\>\|Doctrine\\ORM\\Mapping\\AssociationMapping\.$#'
53 changes: 24 additions & 29 deletions src/AuditReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@
use SimpleThings\EntityAudit\Exception\NotAuditedException;
use SimpleThings\EntityAudit\Metadata\MetadataFactory;
use SimpleThings\EntityAudit\Utils\ArrayDiff;
use SimpleThings\EntityAudit\Utils\ORMCompatibilityTrait;
use SimpleThings\EntityAudit\Utils\SQLResultCasing;

class AuditReader
{
use ORMCompatibilityTrait;
use SQLResultCasing;

private AbstractPlatform $platform;
Expand Down Expand Up @@ -213,7 +215,7 @@ public function find($className, $id, $revision, array $options = [])
$idKeys = array_keys($id);
$columnName = $idKeys[0];
} elseif (isset($classMetadata->fieldMappings[$idField])) {
$columnName = $classMetadata->fieldMappings[$idField]['columnName'];
$columnName = self::getMappingValue($classMetadata->fieldMappings[$idField], 'columnName');
} elseif (isset($classMetadata->associationMappings[$idField]['joinColumns'])) {
$columnName = $classMetadata->associationMappings[$idField]['joinColumns'][0]['name'];
} else {
Expand All @@ -237,7 +239,7 @@ public function find($className, $id, $revision, array $options = [])
? 're' // root entity
: 'e';

$type = Type::getType($classMetadata->fieldMappings[$field]['type']);
$type = Type::getType(self::getMappingValue($classMetadata->fieldMappings[$field], 'type'));
$columnList[] = sprintf(
'%s AS %s',
$type->convertToPHPValueSQL(
Expand Down Expand Up @@ -286,7 +288,7 @@ public function find($className, $id, $revision, array $options = [])
!$classMetadata->isInheritanceTypeNone()
&& null !== $classMetadata->discriminatorColumn
) {
$columnList[] = $classMetadata->discriminatorColumn['name'];
$columnList[] = self::getMappingValue($classMetadata->discriminatorColumn, 'name');
if ($classMetadata->isInheritanceTypeSingleTable()
&& null !== $classMetadata->discriminatorValue) {
// Support for single table inheritance sub-classes
Expand All @@ -298,7 +300,7 @@ public function find($className, $id, $revision, array $options = [])

$whereSQL .= sprintf(
' AND %s IN (%s)',
$classMetadata->discriminatorColumn['name'],
self::getMappingValue($classMetadata->discriminatorColumn, 'name'),
implode(', ', $queriedDiscrValues)
);
}
Expand Down Expand Up @@ -410,7 +412,7 @@ public function findEntitiesChangedAtRevision($revision)
$columnMap = [];

foreach ($classMetadata->fieldNames as $columnName => $field) {
$type = Type::getType($classMetadata->fieldMappings[$field]['type']);
$type = Type::getType(self::getMappingValue($classMetadata->fieldMappings[$field], 'type'));
$tableAlias = $classMetadata->isInheritanceTypeJoined()
&& $classMetadata->isInheritedField($field)
&& !$classMetadata->isIdentifier($field)
Expand Down Expand Up @@ -441,15 +443,15 @@ public function findEntitiesChangedAtRevision($revision)
$classMetadata->isInheritanceTypeSingleTable()
&& null !== $classMetadata->discriminatorColumn
) {
$columnList .= ', e.'.$classMetadata->discriminatorColumn['name'];
$whereSQL .= ' AND e.'.$classMetadata->discriminatorColumn['fieldName'].' = ?';
$columnList .= ', e.'.self::getMappingValue($classMetadata->discriminatorColumn, 'name');
$whereSQL .= ' AND e.'.self::getMappingValue($classMetadata->discriminatorColumn, 'fieldName').' = ?';
$params[] = $classMetadata->discriminatorValue;
} elseif (
$classMetadata->isInheritanceTypeJoined()
&& $classMetadata->rootEntityName !== $classMetadata->name
&& null !== $classMetadata->discriminatorColumn
) {
$columnList .= ', re.'.$classMetadata->discriminatorColumn['name'];
$columnList .= ', re.'.self::getMappingValue($classMetadata->discriminatorColumn, 'name');

$rootClass = $this->em->getClassMetadata($classMetadata->rootEntityName);
$rootTableName = $this->config->getTableName($rootClass);
Expand Down Expand Up @@ -543,7 +545,7 @@ public function findRevisions($className, $id)
if ('' !== $whereSQL) {
$whereSQL .= ' AND ';
}
$whereSQL .= 'e.'.$classMetadata->fieldMappings[$idField]['columnName'].' = ?';
$whereSQL .= 'e.'.self::getMappingValue($classMetadata->fieldMappings[$idField], 'columnName').' = ?';
} elseif (isset($classMetadata->associationMappings[$idField]['joinColumns'])) {
if ('' !== $whereSQL) {
$whereSQL .= ' AND ';
Expand Down Expand Up @@ -605,12 +607,12 @@ public function getCurrentRevision($className, $id)
if ('' !== $whereSQL) {
$whereSQL .= ' AND ';
}
$whereSQL .= 'e.'.$classMetadata->fieldMappings[$idField]['columnName'].' = ?';
$whereSQL .= 'e.'.self::getMappingValue($classMetadata->fieldMappings[$idField], 'columnName').' = ?';
} elseif (isset($classMetadata->associationMappings[$idField]['joinColumns'])) {
if ('' !== $whereSQL) {
$whereSQL .= ' AND ';
}
$whereSQL .= 'e.'.$classMetadata->associationMappings[$idField]['joinColumns'][0]['name'].' = ?';
$whereSQL .= 'e.'.self::getMappingValue($classMetadata->associationMappings[$idField]['joinColumns'][0], 'name').' = ?';
}
}

Expand Down Expand Up @@ -721,10 +723,10 @@ public function getEntityHistory($className, $id)
foreach ($classMetadata->identifier as $idField) {
if (isset($classMetadata->fieldMappings[$idField])) {
/** @phpstan-var literal-string $columnName */
$columnName = $classMetadata->fieldMappings[$idField]['columnName'];
$columnName = self::getMappingValue($classMetadata->fieldMappings[$idField], 'columnName');
} elseif (isset($classMetadata->associationMappings[$idField]['joinColumns'])) {
/** @phpstan-var literal-string $columnName */
$columnName = $classMetadata->associationMappings[$idField]['joinColumns'][0]['name'];
$columnName = self::getMappingValue($classMetadata->associationMappings[$idField]['joinColumns'][0], 'name');
} else {
continue;
}
Expand All @@ -737,7 +739,7 @@ public function getEntityHistory($className, $id)
$columnMap = [];

foreach ($classMetadata->fieldNames as $columnName => $field) {
$type = Type::getType($classMetadata->fieldMappings[$field]['type']);
$type = Type::getType(self::getMappingValue($classMetadata->fieldMappings[$field], 'type'));
/** @phpstan-var literal-string $sqlExpr */
$sqlExpr = $type->convertToPHPValueSQL(
$this->quoteStrategy->getColumnName($field, $classMetadata, $this->platform),
Expand Down Expand Up @@ -845,10 +847,7 @@ private function createEntity($className, array $columnMap, array $data, $revisi
!$classMetadata->isInheritanceTypeNone()
&& null !== $classMetadata->discriminatorColumn
) {
if (!isset($data[$classMetadata->discriminatorColumn['name']])) {
throw new \RuntimeException('Expecting discriminator value in data set.');
}
$discriminator = $data[$classMetadata->discriminatorColumn['name']];
$discriminator = $data[self::getMappingValue($classMetadata->discriminatorColumn, 'name')];
if (!isset($classMetadata->discriminatorMap[$discriminator])) {
throw new \RuntimeException("No mapping found for [{$discriminator}].");
}
Expand Down Expand Up @@ -884,7 +883,7 @@ private function createEntity($className, array $columnMap, array $data, $revisi

foreach ($data as $field => $value) {
if (isset($classMetadata->fieldMappings[$field])) {
$type = Type::getType($classMetadata->fieldMappings[$field]['type']);
$type = Type::getType(self::getMappingValue($classMetadata->fieldMappings[$field], 'type'));
$value = $type->convertToPHPValue($value, $this->platform);

$reflField = $classMetadata->reflFields[$field];
Expand Down Expand Up @@ -963,7 +962,7 @@ private function createEntity($className, array $columnMap, array $data, $revisi
$joinColumnValue = $data[$columnMap[$srcColumn]] ?? null;
if (null !== $joinColumnValue) {
$targetField = $targetClass->fieldNames[$targetColumn];
$joinColumnType = Type::getType($targetClass->fieldMappings[$targetField]['type']);
$joinColumnType = Type::getType(self::getMappingValue($targetClass->fieldMappings[$targetField], 'type'));
$joinColumnValue = $joinColumnType->convertToPHPValue(
$joinColumnValue,
$this->platform
Expand Down Expand Up @@ -1031,14 +1030,10 @@ private function createEntity($className, array $columnMap, array $data, $revisi
\assert(null !== $reflField);
$reflField->setValue($entity, $collection);
} elseif (0 !== ($assoc['type'] & ClassMetadata::MANY_TO_MANY)) {
if ($assoc['isOwningSide'] && isset(
$assoc['relationToSourceKeyColumns'],
$assoc['relationToTargetKeyColumns'],
$assoc['joinTable']['name']
)) {
if (self::isManyToManyOwningSideMapping($assoc)) {
$whereId = [$this->config->getRevisionFieldName().' = ?'];
$values = [$revision];
foreach ($assoc['relationToSourceKeyColumns'] as $sourceKeyJoinColumn => $sourceKeyColumn) {
foreach (self::getMappingValue($assoc, 'relationToSourceKeyColumns') as $sourceKeyJoinColumn => $sourceKeyColumn) {
$whereId[] = "{$sourceKeyJoinColumn} = ?";

$reflField = $classMetadata->reflFields['id'];
Expand All @@ -1053,10 +1048,10 @@ private function createEntity($className, array $columnMap, array $data, $revisi
$this->config->getRevisionTypeFieldName(),
];
$tableName = $this->config->getTablePrefix()
.$assoc['joinTable']['name']
.self::getJoinTableName($assoc)

Check failure on line 1051 in src/AuditReader.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #1 $mapping of static method SimpleThings\EntityAudit\AuditReader::getJoinTableName() expects array<string, mixed>|Doctrine\ORM\Mapping\ManyToManyOwningSideMapping, Doctrine\ORM\Mapping\ManyToManyInverseSideMapping|Doctrine\ORM\Mapping\ManyToManyOwningSideMapping|Doctrine\ORM\Mapping\ManyToOneAssociationMapping|Doctrine\ORM\Mapping\OneToManyAssociationMapping|Doctrine\ORM\Mapping\OneToOneInverseSideMapping|Doctrine\ORM\Mapping\OneToOneOwningSideMapping given.

Check failure on line 1051 in src/AuditReader.php

View workflow job for this annotation

GitHub Actions / Psalm

PossiblyInvalidArgument

src/AuditReader.php:1051:49: PossiblyInvalidArgument: Argument 1 of SimpleThings\EntityAudit\AuditReader::getJoinTableName expects Doctrine\ORM\Mapping\ManyToManyOwningSideMapping|array<string, mixed>, but possibly different type Doctrine\ORM\Mapping\ManyToManyInverseSideMapping|Doctrine\ORM\Mapping\ManyToManyOwningSideMapping|Doctrine\ORM\Mapping\ManyToOneAssociationMapping|Doctrine\ORM\Mapping\OneToManyAssociationMapping|Doctrine\ORM\Mapping\OneToOneInverseSideMapping|Doctrine\ORM\Mapping\OneToOneOwningSideMapping provided (see https://psalm.dev/092)
.$this->config->getTableSuffix();

foreach ($assoc['relationToTargetKeyColumns'] as $targetKeyJoinColumn => $targetKeyColumn) {
foreach (self::getMappingValue($assoc, 'relationToTargetKeyColumns') as $targetKeyJoinColumn => $targetKeyColumn) {
$columnList[] = $targetKeyJoinColumn;
}

Expand All @@ -1078,7 +1073,7 @@ private function createEntity($className, array $columnMap, array $data, $revisi
$id = [];

/** @phpstan-var string $targetKeyColumn */
foreach ($assoc['relationToTargetKeyColumns'] as $targetKeyJoinColumn => $targetKeyColumn) {
foreach (self::getMappingValue($assoc, 'relationToTargetKeyColumns') as $targetKeyJoinColumn => $targetKeyColumn) {
$joinKey = $row[$targetKeyJoinColumn];
$id[$targetKeyColumn] = $joinKey;
}
Expand Down
2 changes: 0 additions & 2 deletions src/Collection/AuditedCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ public function __construct(

/**
* @return void
*
* @phpstan-ignore-next-line https://github.com/phpstan/phpstan-doctrine/pull/560
*/
#[\ReturnTypeWillChange]
public function add(mixed $element)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class SimpleThingsEntityAuditExtension extends Extension
{
Expand Down
15 changes: 8 additions & 7 deletions src/EventListener/CreateSchemaListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@
use SimpleThings\EntityAudit\AuditConfiguration;
use SimpleThings\EntityAudit\AuditManager;
use SimpleThings\EntityAudit\Metadata\MetadataFactory;
use SimpleThings\EntityAudit\Utils\ORMCompatibilityTrait;

/**
* NEXT_MAJOR: do not implement EventSubscriber interface anymore.
*/
class CreateSchemaListener implements EventSubscriber
{
use ORMCompatibilityTrait;

private AuditConfiguration $config;

private MetadataFactory $metadataFactory;
Expand Down Expand Up @@ -108,13 +111,11 @@ public function postGenerateSchemaTable(GenerateSchemaTableEventArgs $eventArgs)
$revisionTable->addIndex([$this->config->getRevisionFieldName()], $revIndexName);

foreach ($cm->associationMappings as $associationMapping) {
if ($associationMapping['isOwningSide'] && isset($associationMapping['joinTable'])) {
if (isset($associationMapping['joinTable']['name'])) {
if ($schema->hasTable($associationMapping['joinTable']['name'])) {
$this->createRevisionJoinTableForJoinTable($schema, $associationMapping['joinTable']['name']);
} else {
$this->defferedJoinTablesToCreate[] = $associationMapping['joinTable']['name'];
}
if (self::isManyToManyOwningSideMapping($associationMapping)) {
if ($schema->hasTable(self::getJoinTableName($associationMapping))) {

Check failure on line 115 in src/EventListener/CreateSchemaListener.php

View workflow job for this annotation

GitHub Actions / Psalm

PossiblyInvalidArgument

src/EventListener/CreateSchemaListener.php:115:62: PossiblyInvalidArgument: Argument 1 of SimpleThings\EntityAudit\EventListener\CreateSchemaListener::getJoinTableName expects Doctrine\ORM\Mapping\ManyToManyOwningSideMapping|array<string, mixed>, but possibly different type Doctrine\ORM\Mapping\ManyToManyInverseSideMapping|Doctrine\ORM\Mapping\ManyToManyOwningSideMapping|Doctrine\ORM\Mapping\ManyToOneAssociationMapping|Doctrine\ORM\Mapping\OneToManyAssociationMapping|Doctrine\ORM\Mapping\OneToOneInverseSideMapping|Doctrine\ORM\Mapping\OneToOneOwningSideMapping provided (see https://psalm.dev/092)
$this->createRevisionJoinTableForJoinTable($schema, self::getJoinTableName($associationMapping));
} else {
$this->defferedJoinTablesToCreate[] = self::getJoinTableName($associationMapping);
}
}
}
Expand Down
Loading

0 comments on commit 3c05a33

Please sign in to comment.