Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix build #618

Merged
merged 2 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,21 @@
<code>Controller</code>
</UndefinedClass>
</file>
<file src="tests/BaseTest.php">
<ArgumentTypeCoercion>
<code><![CDATA[$value]]></code>
<code><![CDATA[$value]]></code>
</ArgumentTypeCoercion>
</file>
<file src="tests/CoreTest.php">
<InvalidArgument>
<code><![CDATA[ProfileAudit::class]]></code>
<code><![CDATA[UserAudit::class]]></code>
</InvalidArgument>
</file>
<file src="tests/Issue/Issue318Test.php">
<ArgumentTypeCoercion>
<code><![CDATA[$user::class]]></code>
</ArgumentTypeCoercion>
</file>
</files>
37 changes: 34 additions & 3 deletions src/AuditConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,19 @@ class AuditConfiguration
*/
private array $globalIgnoreColumns = [];

/** @phpstan-var literal-string */
private string $tablePrefix = '';

/** @phpstan-var literal-string */
private string $tableSuffix = '_audit';

/** @phpstan-var literal-string */
private string $revisionTableName = 'revisions';

/** @phpstan-var literal-string */
private string $revisionFieldName = 'rev';

/** @phpstan-var literal-string */
private string $revisionTypeFieldName = 'revtype';

private string $revisionIdFieldType = Types::INTEGER;
Expand Down Expand Up @@ -69,13 +74,19 @@ public static function forEntities(array $classes)
* @param ClassMetadataInfo<object> $metadata
*
* @return string
*
* @phpstan-return literal-string
*
* @psalm-suppress MoreSpecificReturnType,LessSpecificReturnStatement https://github.com/vimeo/psalm/issues/10910
*/
public function getTableName(ClassMetadataInfo $metadata)
{
/** @var literal-string $tableName */
$tableName = $metadata->getTableName();

if (null !== $metadata->getSchemaName() && '' !== $metadata->getSchemaName()) {
$tableName = $metadata->getSchemaName().'.'.$tableName;
/** @var literal-string|null $schemaName */
$schemaName = $metadata->getSchemaName();
if (null !== $schemaName && '' !== $schemaName) {
$tableName = $schemaName.'.'.$tableName;
}

return $this->getTablePrefix().$tableName.$this->getTableSuffix();
Expand All @@ -93,6 +104,8 @@ public function setDisabledForeignKeys(bool $disabled): void

/**
* @return string
*
* @phpstan-return literal-string
*/
public function getTablePrefix()
{
Expand All @@ -101,6 +114,8 @@ public function getTablePrefix()

/**
* @param string $prefix
*
* @phpstan-param literal-string $prefix
*/
public function setTablePrefix($prefix): void
{
Expand All @@ -109,6 +124,8 @@ public function setTablePrefix($prefix): void

/**
* @return string
*
* @phpstan-return literal-string
*/
public function getTableSuffix()
{
Expand All @@ -117,6 +134,8 @@ public function getTableSuffix()

/**
* @param string $suffix
*
* @phpstan-param literal-string $suffix
*/
public function setTableSuffix($suffix): void
{
Expand All @@ -125,6 +144,8 @@ public function setTableSuffix($suffix): void

/**
* @return string
*
* @phpstan-return literal-string
*/
public function getRevisionFieldName()
{
Expand All @@ -133,6 +154,8 @@ public function getRevisionFieldName()

/**
* @param string $revisionFieldName
*
* @phpstan-param literal-string $revisionFieldName
*/
public function setRevisionFieldName($revisionFieldName): void
{
Expand All @@ -141,6 +164,8 @@ public function setRevisionFieldName($revisionFieldName): void

/**
* @return string
*
* @phpstan-return literal-string
*/
public function getRevisionTypeFieldName()
{
Expand All @@ -149,6 +174,8 @@ public function getRevisionTypeFieldName()

/**
* @param string $revisionTypeFieldName
*
* @phpstan-param literal-string $revisionTypeFieldName
*/
public function setRevisionTypeFieldName($revisionTypeFieldName): void
{
Expand All @@ -157,6 +184,8 @@ public function setRevisionTypeFieldName($revisionTypeFieldName): void

/**
* @return string
*
* @phpstan-return literal-string
*/
public function getRevisionTableName()
{
Expand All @@ -165,6 +194,8 @@ public function getRevisionTableName()

/**
* @param string $revisionTableName
*
* @phpstan-param literal-string $revisionTableName
*/
public function setRevisionTableName($revisionTableName): void
{
Expand Down
24 changes: 15 additions & 9 deletions src/AuditReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -716,11 +716,14 @@ public function getEntityHistory($className, $id)
$id = [$classMetadata->identifier[0] => $id];
}

/** @phpstan-var array<literal-string> $whereId */
$whereId = [];
foreach ($classMetadata->identifier as $idField) {
if (isset($classMetadata->fieldMappings[$idField])) {
/** @phpstan-var literal-string $columnName */
$columnName = $classMetadata->fieldMappings[$idField]['columnName'];
} elseif (isset($classMetadata->associationMappings[$idField]['joinColumns'])) {
/** @phpstan-var literal-string $columnName */
$columnName = $classMetadata->associationMappings[$idField]['joinColumns'][0]['name'];
} else {
continue;
Expand All @@ -735,10 +738,14 @@ public function getEntityHistory($className, $id)

foreach ($classMetadata->fieldNames as $columnName => $field) {
$type = Type::getType($classMetadata->fieldMappings[$field]['type']);
$columnList[] = $type->convertToPHPValueSQL(
/** @phpstan-var literal-string $sqlExpr */
$sqlExpr = $type->convertToPHPValueSQL(
$this->quoteStrategy->getColumnName($field, $classMetadata, $this->platform),
$this->platform
).' AS '.$this->platform->quoteSingleIdentifier($field);
);
/** @phpstan-var literal-string $quotedField */
$quotedField = $this->platform->quoteSingleIdentifier($field);
$columnList[] = $sqlExpr.' AS '.$quotedField;
$columnMap[$field] = $this->getSQLResultCasing($this->platform, $columnName);
}

Expand All @@ -751,6 +758,7 @@ public function getEntityHistory($className, $id)
continue;
}

/** @phpstan-var literal-string $sourceCol */
foreach ($assoc['targetToSourceKeyColumns'] as $sourceCol) {
$columnList[] = $sourceCol;
$columnMap[$sourceCol] = $this->getSQLResultCasing($this->platform, $sourceCol);
Expand All @@ -759,13 +767,11 @@ public function getEntityHistory($className, $id)

$values = array_values($id);

$query = sprintf(
'SELECT %s FROM %s e WHERE %s ORDER BY e.%s DESC',
implode(', ', $columnList),
$tableName,
$whereSQL,
$this->config->getRevisionFieldName()
);
$query =
'SELECT '.implode(', ', $columnList)
.' FROM '.$tableName.' e'
.' WHERE '.$whereSQL
.' ORDER BY e.'.$this->config->getRevisionFieldName().' DESC';

$stmt = $this->em->getConnection()->executeQuery($query, $values);

Expand Down
Loading
Loading