Skip to content

Commit

Permalink
Fix doctrine deprecation warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Oct 11, 2023
1 parent 41d1a0b commit 87dee24
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 21 deletions.
19 changes: 8 additions & 11 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd"
colors="true"
bootstrap="tests/bootstrap.php"
>
<php>
<ini name="display_errors" value="1" />
<ini name="error_reporting" value="-1" />
<server name="APP_ENV" value="test" force="true" />
<server name="SHELL_VERBOSITY" value="-1" />
<ini name="display_errors" value="1"/>
<ini name="error_reporting" value="-1"/>
<server name="APP_ENV" value="test" force="true"/>
<server name="SHELL_VERBOSITY" value="-1"/>
</php>

<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>

<coverage>
<coverage/>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>

</source>
</phpunit>
4 changes: 2 additions & 2 deletions src/Entity/PackageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ public function getTotal(): int
$sql,
[],
[],
new QueryCacheProfile(86400, 'total_packages', $this->getEntityManager()->getConfiguration()->getResultCacheImpl())
new QueryCacheProfile(86400, 'total_packages', $this->getEntityManager()->getConfiguration()->getResultCache())
);
$result = $stmt->fetchAllAssociative();
$stmt->free();
Expand All @@ -583,7 +583,7 @@ public function getCountByYearMonth(): array
$sql,
[],
[],
new QueryCacheProfile(3600, 'package_count_by_year_month', $this->getEntityManager()->getConfiguration()->getResultCacheImpl())
new QueryCacheProfile(3600, 'package_count_by_year_month', $this->getEntityManager()->getConfiguration()->getResultCache())
);
$result = $stmt->fetchAllAssociative();
$stmt->free();
Expand Down
4 changes: 2 additions & 2 deletions src/Entity/VersionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public function getTotal(): int
$sql,
[],
[],
new QueryCacheProfile(86400, 'total_package_versions', $this->getEntityManager()->getConfiguration()->getResultCacheImpl())
new QueryCacheProfile(86400, 'total_package_versions', $this->getEntityManager()->getConfiguration()->getResultCache())
);
$result = $stmt->fetchAllAssociative();
$stmt->free();
Expand All @@ -281,7 +281,7 @@ public function getCountByYearMonth(): array
$sql,
[],
[],
new QueryCacheProfile(3600, 'package_versions_count_by_year_month', $this->getEntityManager()->getConfiguration()->getResultCacheImpl())
new QueryCacheProfile(3600, 'package_versions_count_by_year_month', $this->getEntityManager()->getConfiguration()->getResultCache())
);
$result = $stmt->fetchAllAssociative();
$stmt->free();
Expand Down
28 changes: 22 additions & 6 deletions src/EventListener/PackageListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
use App\Entity\User;
use App\Util\DoctrineTrait;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\EntityManager;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bridge\Doctrine\Types\UlidType;
use Symfony\Bundle\SecurityBundle\Security;

#[AsEntityListener(event: 'postPersist', entity: Package::class)]
Expand All @@ -45,9 +47,7 @@ public function __construct(
*/
public function postPersist(Package $package, LifecycleEventArgs $event): void
{
$record = AuditRecord::packageCreated($package, $this->getUser());
$this->getEM()->persist($record);
$this->getEM()->flush();
$this->insert(AuditRecord::packageCreated($package, $this->getUser()));
}

/**
Expand All @@ -63,7 +63,7 @@ public function preRemove(Package $package, LifecycleEventArgs $event): void
public function preUpdate(Package $package, PreUpdateEventArgs $event): void
{
if ($event->hasChangedField('repository')) {
// buffering things to be flushed in postUpdate as flushing here results in an infinite loop
// buffering things to be inserted in postUpdate once we can confirm it is done
$this->buffered[] = AuditRecord::canonicalUrlChange($package, $this->getUser(), $event->getOldValue('repository'));
}
}
Expand All @@ -75,9 +75,8 @@ public function postUpdate(Package $package, LifecycleEventArgs $event): void
{
if ($this->buffered) {
foreach ($this->buffered as $record) {
$this->getEM()->persist($record);
$this->insert($record);
}
$this->getEM()->flush();
$this->buffered = [];
}
}
Expand All @@ -88,4 +87,21 @@ private function getUser(): User|null

return $user instanceof User ? $user : null;
}

private function insert(AuditRecord $record): void
{
$this->getEM()->getConnection()->insert('audit_log', [

Check failure on line 93 in src/EventListener/PackageListener.php

View workflow job for this annotation

GitHub Actions / PHPStan

Query error: Column "audit_log.attributes" expects value type string, got type array<string, mixed>

Check failure on line 93 in src/EventListener/PackageListener.php

View workflow job for this annotation

GitHub Actions / PHPStan

Query error: Column "audit_log.datetime" expects value type string, got type DateTimeImmutable

Check failure on line 93 in src/EventListener/PackageListener.php

View workflow job for this annotation

GitHub Actions / PHPStan

Query error: Column "audit_log.id" expects value type string, got type Symfony\Component\Uid\Ulid
'id' => $record->id,
'datetime' => $record->datetime,
'type' => $record->type->value,
'attributes' => $record->attributes,
'userId' => $record->userId,
'vendor' => $record->vendor,
'packageId' => $record->packageId,
], [
'id' => UlidType::NAME,
'datetime' => Types::DATETIME_IMMUTABLE,
'attributes' => Types::JSON
]);

This comment has been minimized.

Copy link
@Seldaek

Seldaek Oct 24, 2023

Author Member

@staabm any clue if the warnings above are fixable as I pass custom types here? Or should I rather just put this in the baseline?

This comment has been minimized.

Copy link
@staabm

staabm Oct 24, 2023

is this the same problem as the feature you requested in staabm/phpstan-dba#278 ?

This comment has been minimized.

Copy link
@Seldaek

Seldaek Oct 24, 2023

Author Member

I'm not sure, seems related in that phpstan-dba is not aware of custom types and it causes problems, but the problem manifests itself differently here. Anyway I'll add to baseline for now :)

}
}

0 comments on commit 87dee24

Please sign in to comment.