Skip to content

Commit

Permalink
Fix phpstan (#2301)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet authored Apr 11, 2022
1 parent 49bec1d commit 5005e17
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
28 changes: 28 additions & 0 deletions src/Listener/BaseMediaEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@

use Doctrine\Common\EventSubscriber;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use Doctrine\Persistence\ObjectManager;
use Sonata\MediaBundle\Model\MediaInterface;
use Sonata\MediaBundle\Provider\MediaProviderInterface;
use Sonata\MediaBundle\Provider\Pool;

/**
* @phpstan-template T of ObjectManager
*/
abstract class BaseMediaEventSubscriber implements EventSubscriber
{
protected Pool $pool;
Expand All @@ -28,6 +32,9 @@ public function __construct(Pool $pool)
$this->pool = $pool;
}

/**
* @param LifecycleEventArgs<T> $args
*/
final public function postUpdate(LifecycleEventArgs $args): void
{
$media = $this->getMedia($args);
Expand All @@ -39,6 +46,9 @@ final public function postUpdate(LifecycleEventArgs $args): void
$this->getProvider($media)->postUpdate($media);
}

/**
* @param LifecycleEventArgs<T> $args
*/
final public function postRemove(LifecycleEventArgs $args): void
{
$media = $this->getMedia($args);
Expand All @@ -50,6 +60,9 @@ final public function postRemove(LifecycleEventArgs $args): void
$this->getProvider($media)->postRemove($media);
}

/**
* @param LifecycleEventArgs<T> $args
*/
final public function postPersist(LifecycleEventArgs $args): void
{
$media = $this->getMedia($args);
Expand All @@ -61,6 +74,9 @@ final public function postPersist(LifecycleEventArgs $args): void
$this->getProvider($media)->postPersist($media);
}

/**
* @param LifecycleEventArgs<T> $args
*/
final public function preUpdate(LifecycleEventArgs $args): void
{
$media = $this->getMedia($args);
Expand All @@ -77,6 +93,9 @@ final public function preUpdate(LifecycleEventArgs $args): void
$this->recomputeSingleEntityChangeSet($args);
}

/**
* @param LifecycleEventArgs<T> $args
*/
final public function preRemove(LifecycleEventArgs $args): void
{
$media = $this->getMedia($args);
Expand All @@ -88,6 +107,9 @@ final public function preRemove(LifecycleEventArgs $args): void
$this->getProvider($media)->preRemove($media);
}

/**
* @param LifecycleEventArgs<T> $args
*/
final public function prePersist(LifecycleEventArgs $args): void
{
$media = $this->getMedia($args);
Expand All @@ -102,8 +124,14 @@ final public function prePersist(LifecycleEventArgs $args): void
$provider->prePersist($media);
}

/**
* @param LifecycleEventArgs<T> $args
*/
abstract protected function recomputeSingleEntityChangeSet(LifecycleEventArgs $args): void;

/**
* @param LifecycleEventArgs<T> $args
*/
abstract protected function getMedia(LifecycleEventArgs $args): ?MediaInterface;

final protected function getProvider(MediaInterface $media): MediaProviderInterface
Expand Down
9 changes: 5 additions & 4 deletions src/Listener/ODM/MediaEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@

namespace Sonata\MediaBundle\Listener\ODM;

use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs as ODMLifecycleEventArgs;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Events;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use Sonata\MediaBundle\Listener\BaseMediaEventSubscriber;
use Sonata\MediaBundle\Model\MediaInterface;

/**
* @phpstan-extends BaseMediaEventSubscriber<DocumentManager>
*/
final class MediaEventSubscriber extends BaseMediaEventSubscriber
{
public function getSubscribedEvents(): array
Expand All @@ -35,9 +38,7 @@ public function getSubscribedEvents(): array

protected function recomputeSingleEntityChangeSet(LifecycleEventArgs $args): void
{
\assert($args instanceof ODMLifecycleEventArgs);

$em = $args->getDocumentManager();
$em = $args->getObjectManager();

$em->getUnitOfWork()->recomputeSingleDocumentChangeSet(
$em->getClassMetadata(\get_class($args->getObject())),
Expand Down
9 changes: 5 additions & 4 deletions src/Listener/ORM/MediaEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Sonata\MediaBundle\Listener\ORM;

use Doctrine\ORM\Event\LifecycleEventArgs as ORMLifecycleEventArgs;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Events;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use Sonata\ClassificationBundle\Model\CategoryInterface;
Expand All @@ -22,6 +22,9 @@
use Sonata\MediaBundle\Model\MediaInterface;
use Sonata\MediaBundle\Provider\Pool;

/**
* @phpstan-extends BaseMediaEventSubscriber<EntityManagerInterface>
*/
final class MediaEventSubscriber extends BaseMediaEventSubscriber
{
private ?CategoryManagerInterface $categoryManager = null;
Expand Down Expand Up @@ -58,9 +61,7 @@ public function onClear(): void

protected function recomputeSingleEntityChangeSet(LifecycleEventArgs $args): void
{
\assert($args instanceof ORMLifecycleEventArgs);

$em = $args->getEntityManager();
$em = $args->getObjectManager();

$em->getUnitOfWork()->recomputeSingleEntityChangeSet(
$em->getClassMetadata(\get_class($args->getObject())),
Expand Down
6 changes: 5 additions & 1 deletion tests/Listener/ORM/MediaEventSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@

namespace Sonata\MediaBundle\Tests\Listener\ORM;

use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Events;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sonata\ClassificationBundle\Model\CategoryInterface;
use Sonata\ClassificationBundle\Model\CategoryManagerInterface;
Expand Down Expand Up @@ -60,6 +62,7 @@ public function testRefetchCategoriesAfterClear(): void
$media1->setProviderName('provider');
$media1->setContext('context');

/** @var LifecycleEventArgs<EntityManagerInterface>&MockObject $args1 */
$args1 = $this->createMock(LifecycleEventArgs::class);
$args1->method('getObject')->willReturn($media1);

Expand All @@ -71,6 +74,7 @@ public function testRefetchCategoriesAfterClear(): void
$media2->setProviderName('provider');
$media2->setContext('context');

/** @var LifecycleEventArgs<EntityManagerInterface>&MockObject $args2 */
$args2 = $this->createMock(LifecycleEventArgs::class);
$args2->method('getObject')->willReturn($media2);

Expand Down

0 comments on commit 5005e17

Please sign in to comment.