Skip to content

Commit

Permalink
chore: phpstan fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed Jun 27, 2023
1 parent db8cec8 commit 48fb561
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 35 deletions.
11 changes: 6 additions & 5 deletions lib/RoadizCompatBundle/src/Controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,15 @@ protected function getPreviewResolver(): PreviewResolverInterface
}

/**
* @template T of object
* @param T $event
* @return T The passed $event MUST be returned
* @param object $event
* @param string|null $eventName
* @return object The passed $event MUST be returned
*/
protected function dispatchEvent($event)
protected function dispatchEvent(object $event, string $eventName = null): object
{
/** @var EventDispatcherInterface $eventDispatcher */ # php-stan hint
$eventDispatcher = $this->get(EventDispatcherInterface::class);
return $eventDispatcher->dispatch($event);
return $eventDispatcher->dispatch($event, $eventName);
}

protected function getSettingsBag(): Settings
Expand Down Expand Up @@ -254,6 +254,7 @@ public static function getCalledClass(): string
if (!str_starts_with($className, "\\")) {
$className = "\\" . $className;
}
// @phpstan-ignore-next-line
return $className;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace RZ\Roadiz\CoreBundle\Api\Controller;

use Doctrine\ORM\NonUniqueResultException;
use Doctrine\Persistence\ManagerRegistry;
use RZ\Roadiz\Core\AbstractEntities\TranslationInterface;
use RZ\Roadiz\CoreBundle\Preview\PreviewResolverInterface;
Expand All @@ -16,12 +17,15 @@ trait TranslationAwareControllerTrait
abstract protected function getManagerRegistry(): ManagerRegistry;
abstract protected function getPreviewResolver(): PreviewResolverInterface;

/**
* @throws NonUniqueResultException
*/
protected function getTranslation(Request $request): TranslationInterface
{
$locale = $request->query->get('_locale');
/** @var TranslationRepository $repository */
$repository = $this->getManagerRegistry()->getRepository(TranslationInterface::class);
if (null === $locale) {
if (!\is_string($locale) || $locale === '') {
return $repository->findDefault();
}

Expand Down
6 changes: 5 additions & 1 deletion lib/RoadizCoreBundle/src/Api/ListManager/SolrPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public function getItemsPerPage(): float
public function getIterator(): \Traversable
{
$this->handleOnce();
return new ArrayCollection($this->listManager->getEntities());
$entities = $this->listManager->getEntities();
if (\is_array($entities)) {
return new \ArrayIterator($entities);
}
return $entities->getIterator();
}
}
6 changes: 3 additions & 3 deletions lib/RoadizCoreBundle/src/Model/AttributeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ public function getOptions(TranslationInterface $translation): ?array
function (AttributeTranslationInterface $attributeTranslation) use ($translation) {
return $attributeTranslation->getTranslation() === $translation;
}
);
if ($attributeTranslation->count() > 0) {
return $attributeTranslation->first()->getOptions();
)->first();
if (false !== $attributeTranslation) {
return $attributeTranslation->getOptions();
}

return null;
Expand Down
6 changes: 5 additions & 1 deletion lib/RoadizCoreBundle/src/Node/NodeDuplicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@ public function duplicate(): Node
*/
private function doDuplicate(Node &$node): Node
{
$nodeSource = $node->getNodeSources()->first();
if (false === $nodeSource) {
throw new \RuntimeException('Node source is missing.');
}
$node->setNodeName(
$this->nodeNamePolicy->getSafeNodeName($node->getNodeSources()->first())
$this->nodeNamePolicy->getSafeNodeName($nodeSource)
);

/** @var Node $child */
Expand Down
2 changes: 1 addition & 1 deletion lib/RoadizCoreBundle/src/Node/NodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function createWithUrlAlias(
/** @var UrlAliasRepository $repository */
$repository = $this->managerRegistry->getRepository(UrlAlias::class);
if (false === $repository->exists($urlAlias)) {
$alias = new UrlAlias($node->getNodeSources()->first());
$alias = new UrlAlias($node->getNodeSources()->first() ?: null);
$alias->setAlias($urlAlias);
$this->managerRegistry->getManagerForClass(UrlAlias::class)->persist($alias);
}
Expand Down
5 changes: 3 additions & 2 deletions lib/RoadizCoreBundle/src/Node/UniqueNodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function generate(
$node->setPosition(0.5);
}

/** @var class-string<NodesSources> $sourceClass */ # phpstan hint
$sourceClass = NodeType::getGeneratedEntitiesNamespace() . "\\" . $nodeType->getSourceEntityClassName();

$source = new $sourceClass($node, $translation);
Expand Down Expand Up @@ -139,8 +140,8 @@ public function generateFromRequest(Request $request): NodesSources
/*
* If parent has only on translation, use parent translation instead of default one.
*/
if (null !== $parent && $parent->getNodeSources()->count() === 1) {
$translation = $parent->getNodeSources()->first()->getTranslation();
if (null !== $parent && false !== $parentNodeSource = $parent->getNodeSources()->first()) {
$translation = $parentNodeSource->getTranslation();
} else {
/** @var Translation $translation */
$translation = $this->managerRegistry
Expand Down
13 changes: 9 additions & 4 deletions lib/RoadizCoreBundle/src/Repository/EntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use RZ\Roadiz\CoreBundle\Doctrine\Event\QueryEvent;
use RZ\Roadiz\CoreBundle\Doctrine\ORM\SimpleQueryBuilder;
use RZ\Roadiz\CoreBundle\Entity\Tag;
use Symfony\Contracts\EventDispatcher\Event;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
Expand All @@ -34,7 +35,7 @@ abstract class EntityRepository extends ServiceEntityRepository

/**
* @param ManagerRegistry $registry
* @param string $entityClass
* @param class-string<TEntityClass> $entityClass
* @param EventDispatcherInterface $dispatcher
*/
public function __construct(
Expand Down Expand Up @@ -82,6 +83,7 @@ public function __construct(
*/
protected function dispatchQueryBuilderEvent(QueryBuilder $qb, string $entityClass): void
{
// @phpstan-ignore-next-line
$this->dispatcher->dispatch(new QueryBuilderSelectEvent($qb, $entityClass));
}

Expand All @@ -90,10 +92,11 @@ protected function dispatchQueryBuilderEvent(QueryBuilder $qb, string $entityCla
* @param string $property
* @param mixed $value
*
* @return QueryBuilderBuildEvent
* @return Event
*/
protected function dispatchQueryBuilderBuildEvent(QueryBuilder $qb, string $property, mixed $value): object
{
// @phpstan-ignore-next-line
return $this->dispatcher->dispatch(new QueryBuilderBuildEvent(
$qb,
$this->getEntityName(),
Expand All @@ -106,10 +109,11 @@ protected function dispatchQueryBuilderBuildEvent(QueryBuilder $qb, string $prop
/**
* @param Query $query
*
* @return QueryEvent
* @return Event
*/
protected function dispatchQueryEvent(Query $query): object
{
// @phpstan-ignore-next-line
return $this->dispatcher->dispatch(new QueryEvent(
$query,
$this->getEntityName()
Expand All @@ -121,10 +125,11 @@ protected function dispatchQueryEvent(Query $query): object
* @param string $property
* @param mixed $value
*
* @return QueryBuilderApplyEvent
* @return Event
*/
protected function dispatchQueryBuilderApplyEvent(QueryBuilder $qb, string $property, mixed $value): object
{
// @phpstan-ignore-next-line
return $this->dispatcher->dispatch(new QueryBuilderApplyEvent(
$qb,
$this->getEntityName(),
Expand Down
9 changes: 5 additions & 4 deletions lib/RoadizCoreBundle/src/Repository/NodeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@
use RZ\Roadiz\CoreBundle\Doctrine\ORM\SimpleQueryBuilder;
use RZ\Roadiz\CoreBundle\Entity\Node;
use RZ\Roadiz\CoreBundle\Entity\NodesSources;
use RZ\Roadiz\CoreBundle\Entity\NodeTypeField;
use RZ\Roadiz\CoreBundle\Entity\UrlAlias;
use RZ\Roadiz\CoreBundle\Preview\PreviewResolverInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\EventDispatcher\Event;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
Expand All @@ -45,10 +44,11 @@ public function __construct(
* @param string $property
* @param mixed $value
*
* @return object|QueryBuilderBuildEvent
* @return Event
*/
protected function dispatchQueryBuilderBuildEvent(QueryBuilder $qb, string $property, mixed $value): object
{
// @phpstan-ignore-next-line
return $this->dispatcher->dispatch(
new QueryBuilderBuildEvent($qb, Node::class, $property, $value, $this->getEntityName())
);
Expand All @@ -59,10 +59,11 @@ protected function dispatchQueryBuilderBuildEvent(QueryBuilder $qb, string $prop
* @param string $property
* @param mixed $value
*
* @return object|QueryBuilderApplyEvent
* @return Event
*/
protected function dispatchQueryBuilderApplyEvent(QueryBuilder $qb, string $property, mixed $value): object
{
// @phpstan-ignore-next-line
return $this->dispatcher->dispatch(
new QueryBuilderApplyEvent($qb, Node::class, $property, $value, $this->getEntityName())
);
Expand Down
16 changes: 10 additions & 6 deletions lib/RoadizCoreBundle/src/Repository/NodesSourcesRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@
use RZ\Roadiz\CoreBundle\Entity\Log;
use RZ\Roadiz\CoreBundle\Entity\Node;
use RZ\Roadiz\CoreBundle\Entity\NodesSources;
use RZ\Roadiz\CoreBundle\Entity\NodeTypeField;
use RZ\Roadiz\CoreBundle\Exception\SolrServerNotAvailableException;
use RZ\Roadiz\CoreBundle\Preview\PreviewResolverInterface;
use RZ\Roadiz\CoreBundle\SearchEngine\NodeSourceSearchHandlerInterface;
use RZ\Roadiz\CoreBundle\SearchEngine\SearchResultsInterface;
use RZ\Roadiz\CoreBundle\SearchEngine\SolrSearchResults;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\EventDispatcher\Event;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
* EntityRepository that implements search engine query with Solr.
*
* @template T of NodesSources
* @extends StatusAwareRepository<T>
* @template-extends StatusAwareRepository<T>
* @extends StatusAwareRepository<T|NodesSources>
* @template-extends StatusAwareRepository<T|NodesSources>
*/
class NodesSourcesRepository extends StatusAwareRepository
{
Expand Down Expand Up @@ -60,10 +60,11 @@ public function __construct(
* @param string $property
* @param mixed $value
*
* @return object|QueryBuilderNodesSourcesBuildEvent
* @return Event
*/
protected function dispatchQueryBuilderBuildEvent(QueryBuilder $qb, string $property, mixed $value): object
{
// @phpstan-ignore-next-line
return $this->dispatcher->dispatch(
new QueryBuilderNodesSourcesBuildEvent($qb, $property, $value, $this->getEntityName())
);
Expand All @@ -74,10 +75,11 @@ protected function dispatchQueryBuilderBuildEvent(QueryBuilder $qb, string $prop
* @param string $property
* @param mixed $value
*
* @return object|QueryBuilderNodesSourcesApplyEvent
* @return Event
*/
protected function dispatchQueryBuilderApplyEvent(QueryBuilder $qb, string $property, mixed $value): object
{
// @phpstan-ignore-next-line
return $this->dispatcher->dispatch(
new QueryBuilderNodesSourcesApplyEvent($qb, $property, $value, $this->getEntityName())
);
Expand All @@ -86,10 +88,11 @@ protected function dispatchQueryBuilderApplyEvent(QueryBuilder $qb, string $prop
/**
* @param Query $query
*
* @return object|QueryNodesSourcesEvent
* @return Event
*/
protected function dispatchQueryEvent(Query $query): object
{
// @phpstan-ignore-next-line
return $this->dispatcher->dispatch(
new QueryNodesSourcesEvent($query, $this->getEntityName())
);
Expand Down Expand Up @@ -184,6 +187,7 @@ protected function applyFilterByCriteria(array &$criteria, QueryBuilder $qb): vo
continue;
}

/** @var QueryBuilderNodesSourcesApplyEvent $event */
$event = $this->dispatchQueryBuilderApplyEvent($qb, $key, $value);
if (!$event->isPropagationStopped()) {
$simpleQB->bindValue($key, $value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ abstract class StatusAwareRepository extends EntityRepository

/**
* @param ManagerRegistry $registry
* @param string $entityClass
* @param class-string<TEntityClass> $entityClass
* @param PreviewResolverInterface $previewResolver
* @param EventDispatcherInterface $dispatcher
* @param Security $security
Expand Down
11 changes: 6 additions & 5 deletions lib/Rozier/src/Controllers/AbstractAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,24 +461,25 @@ protected function getPostDeleteResponse(PersistableInterface $item): Response
}

/**
* @template T of Event|iterable<Event>|array<Event>|null
* @param T $event
* @return T
* @template T of object|Event
* @param T|iterable<T>|array<int, T>|null $event
* @return T|iterable<T>|array<int, T>|null
*/
protected function dispatchSingleOrMultipleEvent(mixed $event): mixed
{
if (null === $event) {
return null;
}
if ($event instanceof Event) {
// @phpstan-ignore-next-line
return $this->dispatchEvent($event);
}
if (\is_iterable($event)) {
$events = [];
/** @var Event|null $singleEvent */
/** @var T|null $singleEvent */
foreach ($event as $singleEvent) {
$returningEvent = $this->dispatchSingleOrMultipleEvent($singleEvent);
if (null !== $returningEvent) {
if ($returningEvent instanceof Event) {
$events[] = $returningEvent;
}
}
Expand Down
4 changes: 3 additions & 1 deletion lib/Rozier/src/RozierApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ public function prepareBaseAssignation()
$this->assignation['head']['googleClientId'] = $this->getSettingsBag()->get('google_client_id', "");
$this->assignation['head']['themeName'] = static::$themeName;
$this->assignation['head']['ajaxToken'] = $tokenManager->getToken(static::AJAX_TOKEN_INTENTION);
$this->assignation['rozier_user_actions'] = $this->dispatchEvent(new UserActionsMenuEvent())->getActions();
/** @var UserActionsMenuEvent $userActionsMenuEvent */
$userActionsMenuEvent = $this->dispatchEvent(new UserActionsMenuEvent());
$this->assignation['rozier_user_actions'] = $userActionsMenuEvent->getActions();

$this->assignation['nodeStatuses'] = [
Node::getStatusLabel(Node::DRAFT) => Node::DRAFT,
Expand Down

0 comments on commit 48fb561

Please sign in to comment.