Skip to content

Commit

Permalink
Update new services with prophecy and phpstan issues
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz committed Mar 20, 2023
1 parent 9d407e0 commit b58146b
Show file tree
Hide file tree
Showing 31 changed files with 144 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public function map(
}

/**
* @param WorkflowInterface&DimensionContentInterface $object
* @template T of DimensionContentInterface
*
* @param WorkflowInterface&T $object
* @param mixed[] $data
*/
private function setWorkflowData(WorkflowInterface $object, array $data): void
Expand All @@ -41,7 +43,9 @@ private function setWorkflowData(WorkflowInterface $object, array $data): void
}

/**
* @param WorkflowInterface&DimensionContentInterface $object
* @template T of DimensionContentInterface
*
* @param WorkflowInterface&T $object
* @param mixed[] $data
*/
private function setInitialPlaceToDraftDimension(WorkflowInterface $object, array $data): void
Expand All @@ -61,7 +65,9 @@ private function setInitialPlaceToDraftDimension(WorkflowInterface $object, arra
}

/**
* @param WorkflowInterface&DimensionContentInterface $object
* @template T of DimensionContentInterface
*
* @param WorkflowInterface&T $object
* @param mixed[] $data
*/
private function setPublishedToLiveDimension(WorkflowInterface $object, array $data): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function onUnpublish(TransitionEvent $transitionEvent): void
$unlocalizedLiveDimensionAttributes = \array_merge($liveDimensionAttributes, ['locale' => null]);

/** @var DimensionContentInterface $unlocalizedLiveDimensionContent */
$unlocalizedLiveDimensionContent = $dimensionContentCollection->getDimensionContent($unlocalizedLiveDimensionAttributes);
$unlocalizedLiveDimensionContent = $dimensionContentCollection->getDimensionContent($unlocalizedLiveDimensionAttributes); // @phpstan-ignore-line we can not define the generic of DimensionContentInterface here
$unlocalizedLiveDimensionContent->removeAvailableLocale($locale);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public function create(
}

$localizedDimensionContent = null;
/** @var string|null $locale */
$locale = $dimensionAttributes['locale'] ?? null;
if ($locale) {
$localizedDimensionContent = $dimensionContentCollection->getDimensionContent($dimensionAttributes);
Expand Down
20 changes: 6 additions & 14 deletions Content/Infrastructure/Doctrine/DimensionContentQueryEnhancer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;
use Sulu\Bundle\ContentBundle\Content\Application\ContentMetadataInspector\ContentMetadataInspectorInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ExcerptInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\TemplateInterface;
Expand All @@ -29,17 +28,6 @@
*/
class DimensionContentQueryEnhancer
{
/**
* @var ContentMetadataInspectorInterface
*/
private $contentMetadataInspector;

public function __construct(
ContentMetadataInspectorInterface $contentMetadataInspector
) {
$this->contentMetadataInspector = $contentMetadataInspector;
}

/**
* Withs represents additional selects which can be load to join and select specific sub entities.
* They are used by groups and fields.
Expand Down Expand Up @@ -76,7 +64,9 @@ public function __construct(
* TODO it should be possible to add custom filters for all contents here example when the
* excerpt tab and entity get extended with an additional field.
*
* @param class-string<DimensionContentInterface> $dimensionContentClassName
* @template T of DimensionContentInterface
*
* @param class-string<T> $dimensionContentClassName
* @param array{
* locale?: string|null,
* stage?: string|null,
Expand Down Expand Up @@ -238,7 +228,9 @@ private function addJoinFilter(
* TODO it should be possible to add custom select for all contents here example when the
* excerpt tab and entity get extended with additional relation.
*
* @param class-string<DimensionContentInterface> $dimensionContentClassName
* @template T of DimensionContentInterface
*
* @param class-string<T> $dimensionContentClassName
* @param mixed[] $dimensionAttributes
* @param array{
* content_admin?: bool,
Expand Down
2 changes: 1 addition & 1 deletion Content/Infrastructure/Doctrine/MetadataLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $event): void
* @param ClassMetadataInfo<object> $metadata
* @param class-string $class
*/
private function addManyToOne(// @phpstan-ignore-line ignore unused method
private function addManyToOne(
LoadClassMetadataEventArgs $event,
ClassMetadataInfo $metadata,
string $name,
Expand Down
7 changes: 4 additions & 3 deletions Content/Infrastructure/Doctrine/RouteRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ class RouteRemover implements EventSubscriber
private $routeRepository;

/**
* @var array<string, array<mixed>>
* @var array<string|int, array{resource_key: string, entityClass?: string}>
*/
private $routeMappings;

/**
* @param array<string, array<mixed>> $routeMappings
* @param array<string|int, array{resource_key: string, entityClass?: string}> $routeMappings
*/
public function __construct(
ContentMetadataInspectorInterface $contentMetadataInspector,
Expand Down Expand Up @@ -73,6 +73,7 @@ public function preRemove(LifecycleEventArgs $event): void
$entityClass = null;
foreach ($this->routeMappings as $key => $mapping) {
if ($resourceKey === $mapping['resource_key']) {
/** @var class-string $entityClass */
$entityClass = $mapping['entityClass'] ?? $key;
break;
}
Expand All @@ -82,7 +83,7 @@ public function preRemove(LifecycleEventArgs $event): void
return;
}

foreach ($this->routeRepository->findAllByEntity($entityClass, $object->getId()) as $route) {
foreach ($this->routeRepository->findAllByEntity($entityClass, (string) $object->getId()) as $route) {
$event->getEntityManager()->remove($route);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ class ContentViewBuilderFactory implements ContentViewBuilderFactoryInterface
private $securityChecker;

/**
* @var array<string, mixed[]>
* @var array<string, array{instanceOf: class-string}>
*/
private $settingsForms;

/**
* @param array<string, mixed[]> $settingsForms
* @param array<string, array{instanceOf: class-string}> $settingsForms
*/
public function __construct(
ViewBuilderFactoryInterface $viewBuilderFactory,
Expand Down
20 changes: 17 additions & 3 deletions Content/Infrastructure/Sulu/Link/ContentLinkProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,15 @@
use Sulu\Bundle\MarkupBundle\Markup\Link\LinkProviderInterface;
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;

/**
* @template B of DimensionContentInterface
* @template T of ContentRichEntityInterface<B>
*/
abstract class ContentLinkProvider implements LinkProviderInterface
{
/**
* @phpstan-use FindContentRichEntitiesTrait<T>
*/
use FindContentRichEntitiesTrait;
use ResolveContentDimensionUrlTrait;
use ResolveContentTrait;
Expand All @@ -46,10 +53,13 @@ abstract class ContentLinkProvider implements LinkProviderInterface
protected $entityManager;

/**
* @var string
* @var class-string<T>
*/
protected $contentRichEntityClass;

/**
* @param class-string<T> $contentRichEntityClass
*/
public function __construct(
ContentManagerInterface $contentManager,
StructureMetadataFactoryInterface $structureMetadataFactory,
Expand All @@ -73,7 +83,6 @@ public function preload(array $hrefs, $locale, $published = true): array
\array_values(
\array_filter(
\array_map(function(ContentRichEntityInterface $contentRichEntity) use ($locale, $published) {
/** @var DimensionContentInterface|null $resolvedDimensionContent */
$resolvedDimensionContent = $this->resolveContent($contentRichEntity, $locale, !$published);

if (!$resolvedDimensionContent) {
Expand All @@ -83,7 +92,7 @@ public function preload(array $hrefs, $locale, $published = true): array
$data = $this->contentManager->normalize($resolvedDimensionContent);

return new LinkItem(
$contentRichEntity->getId(),
(string) $contentRichEntity->getId(),
(string) $this->getTitle($resolvedDimensionContent, $data),
(string) $this->getUrl($resolvedDimensionContent, $data),
$published
Expand All @@ -94,6 +103,8 @@ public function preload(array $hrefs, $locale, $published = true): array
}

/**
* @param B $dimensionContent
*
* @param mixed[] $data
*/
protected function getTitle(DimensionContentInterface $dimensionContent, array $data): ?string
Expand All @@ -106,6 +117,9 @@ protected function getEntityIdField(): string
return 'id';
}

/**
* @return class-string<T>
*/
protected function getContentRichEntityClass(): string
{
return $this->contentRichEntityClass;
Expand Down
6 changes: 6 additions & 0 deletions Content/Infrastructure/Sulu/Teaser/ContentTeaserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
*/
abstract class ContentTeaserProvider implements TeaserProviderInterface
{
/**
* @phpstan-use FindContentRichEntitiesTrait<T>
*/
use FindContentRichEntitiesTrait;
use ResolveContentDimensionUrlTrait;
use ResolveContentTrait;
Expand Down Expand Up @@ -241,6 +244,9 @@ protected function getEntityIdField(): string
return 'id';
}

/**
* @return class-string<T>
*/
protected function getContentRichEntityClass(): string
{
return $this->contentRichEntityClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@
use Sulu\Bundle\ContentBundle\Content\Application\ContentWorkflow\ContentWorkflowInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface;

/**
* @template T of ContentRichEntityInterface
*
* @internal
*/
trait FindContentRichEntitiesTrait
{
/**
* @param string[]|int[] $ids
*
* @return ContentRichEntityInterface[]
* @return T[]
*/
protected function findEntitiesByIds(array $ids): array
{
Expand All @@ -31,6 +36,7 @@ protected function findEntitiesByIds(array $ids): array
$contentRichEntityClass = $this->getContentRichEntityClass();
$classMetadata = $entityManager->getClassMetadata($contentRichEntityClass);

/** @var T[] $entities */
$entities = $entityManager->createQueryBuilder()
->select(ContentWorkflowInterface::CONTENT_RICH_ENTITY_CONTEXT_KEY)
->from($contentRichEntityClass, ContentWorkflowInterface::CONTENT_RICH_ENTITY_CONTEXT_KEY)
Expand All @@ -56,6 +62,9 @@ function(ContentRichEntityInterface $a, ContentRichEntityInterface $b) use ($idP

abstract protected function getEntityIdField(): string;

/**
* @return class-string<T>
*/
abstract protected function getContentRichEntityClass(): string;

abstract protected function getEntityManager(): EntityManagerInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@
use Sulu\Bundle\ContentBundle\Content\Domain\Model\TemplateInterface;
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;

/**
* @internal
*/
trait ResolveContentDimensionUrlTrait
{
/**
* @template T of DimensionContentInterface
*
* @param T $dimensionContent
* @param mixed[] $data
*/
protected function getUrl(DimensionContentInterface $dimensionContent, array $data): ?string
Expand All @@ -41,6 +47,7 @@ protected function getUrl(DimensionContentInterface $dimensionContent, array $da

foreach ($metadata->getProperties() as $property) {
if ('route' === $property->getType()) {
/** @var string|null */
return $dimensionContent->getTemplateData()[$property->getName()] ?? null;
}
}
Expand Down
10 changes: 10 additions & 0 deletions Content/Infrastructure/Sulu/Traits/ResolveContentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,18 @@
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;

/**
* @internal
*/
trait ResolveContentTrait
{
/**
* @template B of DimensionContentInterface
*
* @param ContentRichEntityInterface<B> $contentRichEntity
*
* @return B|null
*/
protected function resolveContent(ContentRichEntityInterface $contentRichEntity, string $locale, bool $showDrafts = false): ?DimensionContentInterface
{
$stage = $showDrafts
Expand Down
4 changes: 2 additions & 2 deletions DependencyInjection/Compiler/SettingsFormPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ public function process(ContainerBuilder $container): void

$settingsForms[$key] = [
'instanceOf' => $instanceOf,
'priority' => $priority,
'priority' => (int) $priority,
];
}
}

\uasort($settingsForms, static function($a, $b) {
return $b['priority'] ?? 0 <=> $a['priority'] ?? 0;
return $b['priority'] <=> $a['priority'];
});

$container->setParameter('sulu_content.content_settings_forms', $settingsForms);
Expand Down
4 changes: 1 addition & 3 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@

<service id="Sulu\Bundle\ContentBundle\Content\Domain\Repository\DimensionContentRepositoryInterface" alias="sulu_content.dimension_content_repository"/>

<service id="sulu_content.dimension_content_query_enhancer" class="Sulu\Bundle\ContentBundle\Content\Infrastructure\Doctrine\DimensionContentQueryEnhancer">
<argument type="service" id="sulu_content.content_metadata_inspector"/>
</service>
<service id="sulu_content.dimension_content_query_enhancer" class="Sulu\Bundle\ContentBundle\Content\Infrastructure\Doctrine\DimensionContentQueryEnhancer"/>

<service id="Sulu\Bundle\ContentBundle\Content\Infrastructure\Doctrine\DimensionContentQueryEnhancer" alias="sulu_content.dimension_content_query_enhancer"/>

Expand Down
4 changes: 2 additions & 2 deletions Tests/Application/ExampleTestBundle/Entity/Example.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ class Example implements ContentRichEntityInterface
public const TEMPLATE_TYPE = 'example';

/**
* @var int|string
* @var int
*/
public $id;

/**
* @return int|string
* @return int
*/
public function getId()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
use Sulu\Bundle\ContentBundle\Content\Application\ContentManager\ContentManagerInterface;
use Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Link\ContentLinkProvider;
use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\Example;
use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\ExampleDimensionContent;
use Sulu\Bundle\MarkupBundle\Markup\Link\LinkConfiguration;
use Sulu\Bundle\MarkupBundle\Markup\Link\LinkConfigurationBuilder;
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;

/**
* @extends ContentLinkProvider<ExampleDimensionContent, Example>
*/
class ExampleLinkProvider extends ContentLinkProvider
{
public function __construct(
Expand Down
Loading

0 comments on commit b58146b

Please sign in to comment.