Skip to content

Commit

Permalink
Fix PHPStan issues
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz committed Jun 6, 2024
1 parent d6496df commit 6814430
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 313 deletions.
2 changes: 2 additions & 0 deletions GoneSubscriber/GoneDocumentSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

/**
* This gone subscriber listens for removed pages.
*
* @internal this is a internal listener which should not be used directly
*/
class GoneDocumentSubscriber implements EventSubscriberInterface
{
Expand Down
31 changes: 14 additions & 17 deletions GoneSubscriber/GoneEntitySubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,31 @@
use Doctrine\ORM\Events;
use Sulu\Bundle\RedirectBundle\Entity\RedirectRoute;
use Sulu\Bundle\RedirectBundle\Exception\RedirectRouteNotUniqueException;
use Sulu\Bundle\RedirectBundle\Manager\RedirectRouteManager;
use Sulu\Bundle\RedirectBundle\Manager\RedirectRouteManagerInterface;
use Sulu\Bundle\RouteBundle\Model\RouteInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;

/**
* This gone subscriber listens for removed route entities.
*
* @internal this is a internal listener which should not be used directly
*/
class GoneEntitySubscriber implements EventSubscriber, ContainerAwareInterface
{
use ContainerAwareTrait;

/**
* {@inheritdoc}
* @var RedirectRouteManagerInterface
*/
private $redirectRouteManager;

public function __construct(
RedirectRouteManagerInterface $redirectRouteManager,
) {
$this->redirectRouteManager = $redirectRouteManager;
}

public function getSubscribedEvents()
{
return [
Expand All @@ -42,8 +52,7 @@ public function preRemove(LifecycleEventArgs $event): void
{
$route = $event->getObject();

$routeManager = $this->getRedirectRouteManager();
if (!$route instanceof RouteInterface || null === $routeManager) {
if (!$route instanceof RouteInterface) {
return;
}

Expand All @@ -53,21 +62,9 @@ public function preRemove(LifecycleEventArgs $event): void
$redirectRoute->setSource($route->getPath());

try {
$routeManager->save($redirectRoute);
$this->redirectRouteManager->save($redirectRoute);
} catch (RedirectRouteNotUniqueException $exception) {
// do nothing when there already exists a redirect route
}
}

/**
* @return RedirectRouteManager|null
*/
private function getRedirectRouteManager()
{
if (null === $this->container) {
return null;
}

return $this->container->get('sulu_redirect.redirect_route_manager');
}
}
2 changes: 2 additions & 0 deletions Import/Converter/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Sulu\Bundle\RedirectBundle\Import\Converter;

use Ramsey\Uuid\Uuid;
use Sulu\Bundle\RedirectBundle\Model\RedirectRouteInterface;
use Sulu\Bundle\RedirectBundle\Model\RedirectRouteRepositoryInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;

Expand Down Expand Up @@ -49,6 +50,7 @@ public function convert(array $item)

$entity = $this->repository->findBySource($item[self::SOURCE]);
if (!$entity) {
/** @var RedirectRouteInterface $entity */
$entity = $this->repository->createNew();
$entity->setId(Uuid::uuid4()->toString());
}
Expand Down
4 changes: 1 addition & 3 deletions Resources/config/gone_subscriber.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@

<service id="sulu_redirect.subscriber.entity"
class="Sulu\Bundle\RedirectBundle\GoneSubscriber\GoneEntitySubscriber">
<call method="setContainer">
<argument type="service" id="service_container"/>
</call>
<argument type="service" id="sulu_redirect.redirect_route_manager"/>

<tag name="doctrine.event_subscriber"/>
</service>
Expand Down
19 changes: 10 additions & 9 deletions Tests/Unit/GoneSubscriber/GoneDocumentSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Doctrine\ORM\EntityManager;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Sulu\Bundle\DocumentManagerBundle\Bridge\DocumentInspector;
use Sulu\Bundle\PageBundle\Document\BasePageDocument;
use Sulu\Bundle\RedirectBundle\GoneSubscriber\GoneDocumentSubscriber;
Expand All @@ -35,47 +36,47 @@ class GoneDocumentSubscriberTest extends TestCase
private $goneDocumentSubscriber;

/**
* @var EntityManager
* @var ObjectProphecy<EntityManager>
*/
private $entityManager;

/**
* @var RedirectRouteManager
* @var ObjectProphecy<RedirectRouteManager>
*/
private $redirectRouteManager;

/**
* @var DocumentInspector
* @var ObjectProphecy<DocumentInspector>
*/
private $documentInspector;

/**
* @var WebspaceManager
* @var ObjectProphecy<WebspaceManager>
*/
private $webspaceManager;

/**
* @var ResourceLocatorStrategyPool
* @var ObjectProphecy<ResourceLocatorStrategyPool>
*/
private $resourceLocatorStrategyPool;

/**
* @var RemoveEvent
* @var ObjectProphecy<RemoveEvent>
*/
private $removeEvent;

/**
* @var BasePageDocument
* @var ObjectProphecy<BasePageDocument>
*/
private $document;

/**
* @var ResourceLocatorStrategyInterface
* @var ObjectProphecy<ResourceLocatorStrategyInterface>
*/
private $resourceLocatorStrategy;

/**
* @var Webspace
* @var ObjectProphecy<Webspace>
*/
private $webspace;

Expand Down
18 changes: 5 additions & 13 deletions Tests/Unit/GoneSubscriber/GoneEntitySubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Doctrine\ORM\Event\LifecycleEventArgs;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Sulu\Bundle\RedirectBundle\Entity\RedirectRoute;
use Sulu\Bundle\RedirectBundle\GoneSubscriber\GoneEntitySubscriber;
use Sulu\Bundle\RedirectBundle\Manager\RedirectRouteManager;
Expand All @@ -28,25 +29,20 @@ class GoneEntitySubscriberTest extends TestCase
private $goneEntitySubscriber;

/**
* @var LifecycleEventArgs
* @var ObjectProphecy<LifecycleEventArgs>
*/
private $event;

/**
* @var RouteInterface
* @var ObjectProphecy<RouteInterface>
*/
private $object;

/**
* @var RedirectRouteManager
* @var ObjectProphecy<RedirectRouteManager>
*/
private $redirectRouteManager;

/**
* @var ContainerInterface
*/
private $container;

protected function setUp(): void
{
$this->object = $this->prophesize(RouteInterface::class);
Expand All @@ -62,11 +58,7 @@ protected function setUp(): void
return true;
}))->shouldBeCalledTimes(1);

$this->container = $this->prophesize(ContainerInterface::class);
$this->container->get('sulu_redirect.redirect_route_manager')->willReturn($this->redirectRouteManager->reveal());

$this->goneEntitySubscriber = new GoneEntitySubscriber();
$this->goneEntitySubscriber->setContainer($this->container->reveal());
$this->goneEntitySubscriber = new GoneEntitySubscriber($this->redirectRouteManager->reveal());
}

public function testPreRemoveWithWrongObject()
Expand Down
7 changes: 4 additions & 3 deletions Tests/Unit/Import/FileImportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Sulu\Bundle\RedirectBundle\Import\Converter\Converter;
use Sulu\Bundle\RedirectBundle\Import\Converter\ConverterInterface;
use Sulu\Bundle\RedirectBundle\Import\Converter\ConverterNotFoundException;
Expand All @@ -28,17 +29,17 @@
class FileImportTest extends TestCase
{
/**
* @var ReaderInterface
* @var ObjectProphecy<ReaderInterface>
*/
private $reader;

/**
* @var ConverterInterface
* @var ObjectProphecy<ConverterInterface>
*/
private $converter;

/**
* @var WriterInterface
* @var ObjectProphecy<WriterInterface>
*/
private $writer;

Expand Down
5 changes: 3 additions & 2 deletions Tests/Unit/Import/Writer/WriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Sulu\Bundle\RedirectBundle\Exception\RedirectRouteNotUniqueException;
use Sulu\Bundle\RedirectBundle\Import\Writer\DuplicatedSourceException;
use Sulu\Bundle\RedirectBundle\Import\Writer\TargetIsEmptyException;
Expand All @@ -24,12 +25,12 @@
class WriterTest extends TestCase
{
/**
* @var RedirectRouteManagerInterface
* @var ObjectProphecy<RedirectRouteManagerInterface>
*/
private $redirectRouteManager;

/**
* @var EntityManagerInterface
* @var ObjectProphecy<EntityManagerInterface>
*/
private $entityManager;

Expand Down
3 changes: 2 additions & 1 deletion Tests/Unit/Manager/RedirectRouteManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Sulu\Bundle\RedirectBundle\Exception\RedirectRouteNotUniqueException;
use Sulu\Bundle\RedirectBundle\Manager\RedirectRouteManager;
use Sulu\Bundle\RedirectBundle\Manager\RedirectRouteManagerInterface;
Expand All @@ -22,7 +23,7 @@
class RedirectRouteManagerTest extends TestCase
{
/**
* @var RedirectRouteRepositoryInterface
* @var ObjectProphecy<RedirectRouteRepositoryInterface>
*/
private $repository;

Expand Down
Loading

0 comments on commit 6814430

Please sign in to comment.