Skip to content

Commit

Permalink
Adding union types to ContentExtension, FieldRenderingExtension, and …
Browse files Browse the repository at this point in the history
…RenderContentExtension
  • Loading branch information
MateuszKolankowski committed Feb 18, 2025
1 parent 23637ba commit 01ba35f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 108 deletions.
17 changes: 16 additions & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11026,7 +11026,7 @@ parameters:
path: src/lib/MVC/Symfony/Templating/Twig/Extension/ContentExtension.php

-
message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\Templating\\\\Twig\\\\Extension\\\\ContentExtension\\:\\:getContentType\\(\\) should return Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\ContentType\\\\ContentType\\|null but return statement is missing\\.$#"
message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\Templating\\\\Twig\\\\Extension\\\\ContentExtension\\:\\:getContentType\\(\\) never returns null so it can be removed from the return type\\.$#"
count: 1
path: src/lib/MVC/Symfony/Templating/Twig/Extension/ContentExtension.php

Expand All @@ -11035,11 +11035,26 @@ parameters:
count: 1
path: src/lib/MVC/Symfony/Templating/Twig/Extension/ContentExtension.php

-
message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\Templating\\\\Twig\\\\Extension\\\\ContentExtension\\:\\:getTranslatedContentName\\(\\) should return string but return statement is missing\\.$#"
count: 1
path: src/lib/MVC/Symfony/Templating/Twig/Extension/ContentExtension.php

-
message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\Templating\\\\Twig\\\\Extension\\\\ContentExtension\\:\\:getTranslatedField\\(\\) should return Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\Content\\\\Field but returns Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\Content\\\\Field\\|null\\.$#"
count: 1
path: src/lib/MVC/Symfony/Templating/Twig/Extension/ContentExtension.php

-
message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\Templating\\\\Twig\\\\Extension\\\\ContentExtension\\:\\:getTranslatedFieldDefinitionDescription\\(\\) should return string\\|null but return statement is missing\\.$#"
count: 1
path: src/lib/MVC/Symfony/Templating/Twig/Extension/ContentExtension.php

-
message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\Templating\\\\Twig\\\\Extension\\\\ContentExtension\\:\\:getTranslatedFieldDefinitionName\\(\\) should return string\\|null but return statement is missing\\.$#"
count: 1
path: src/lib/MVC/Symfony/Templating/Twig/Extension/ContentExtension.php

-
message: "#^Parameter \\#1 \\$content of method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\Templating\\\\Twig\\\\Extension\\\\ContentExtension\\:\\:getContentType\\(\\) expects Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\Content\\\\Content\\|Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\Content\\\\ContentInfo, Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\ValueObject given\\.$#"
count: 2
Expand Down
85 changes: 13 additions & 72 deletions src/lib/MVC/Symfony/Templating/Twig/Extension/ContentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo;
use Ibexa\Contracts\Core\Repository\Values\Content\Field;
use Ibexa\Contracts\Core\Repository\Values\ValueObject;
use Ibexa\Core\Base\Exceptions\InvalidArgumentType;
use Ibexa\Core\Helper\FieldHelper;
use Ibexa\Core\Helper\FieldsGroups\FieldsGroupsList;
use Ibexa\Core\Helper\TranslationHelper;
Expand Down Expand Up @@ -102,68 +101,58 @@ public function getFunctions(): array
}

/**
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Content|\Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo|\Ibexa\Contracts\Core\Repository\Values\Content\ContentAwareInterface $data Must be a valid Content, ContentInfo, or ContentAwareInterface object.
* @param string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR"). Null by default (takes current locale)
*
* @throws \Ibexa\Core\Base\Exceptions\InvalidArgumentType When $content is not a valid Content, ContentInfo, or ContentAwareInterface object.
*
* @return string
*/
public function getTranslatedContentName(object $data, $forcedLanguage = null)
public function getTranslatedContentName(Content|ContentInfo|ContentAwareInterface $data, $forcedLanguage = null)
{
$content = $this->resolveData($data);
if ($content instanceof Content) {
return $this->translationHelper->getTranslatedContentName($content, $forcedLanguage);
} elseif ($content instanceof ContentInfo) {
return $this->translationHelper->getTranslatedContentNameByContentInfo($content, $forcedLanguage);
}

throw new InvalidArgumentType(
'$data',
sprintf('%s or %s or %s', Content::class, ContentInfo::class, ContentAwareInterface::class),
$data
);
}

/**
* Returns the translated field, very similar to getTranslatedFieldValue but this returns the whole field.
* To be used with ibexa_image_alias for example, which requires the whole field.
*
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Content|\Ibexa\Contracts\Core\Repository\Values\Content\ContentAwareInterface $data
* @param string $fieldDefIdentifier Identifier for the field we want to get.
* @param string $forcedLanguage Locale we want the field in (e.g. "cro-HR"). Null by default (takes current locale).
*
* @return \Ibexa\Contracts\Core\Repository\Values\Content\Field
*/
public function getTranslatedField(object $data, $fieldDefIdentifier, $forcedLanguage = null)
public function getTranslatedField(Content|ContentAwareInterface $data, $fieldDefIdentifier, $forcedLanguage = null)
{
return $this->translationHelper->getTranslatedField($this->getContent($data), $fieldDefIdentifier, $forcedLanguage);
}

/**
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Content|\Ibexa\Contracts\Core\Repository\Values\Content\ContentAwareInterface $data
* @param string $fieldDefIdentifier Identifier for the field we want to get the value from.
* @param string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR"). Null by default (takes current locale).
*
* @return mixed A primitive type or a field type Value object depending on the field type.
*/
public function getTranslatedFieldValue(object $data, $fieldDefIdentifier, $forcedLanguage = null)
public function getTranslatedFieldValue(Content|ContentAwareInterface $data, $fieldDefIdentifier, $forcedLanguage = null)
{
return $this->translationHelper->getTranslatedField($this->getContent($data), $fieldDefIdentifier, $forcedLanguage)->value;
}

/**
* Gets name of a FieldDefinition name by loading ContentType based on Content/ContentInfo/ContentAwareInterface object.
*
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Content|\Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo|\Ibexa\Contracts\Core\Repository\Values\Content\ContentAwareInterface $data Must be Content, ContentInfo, or ContentAwareInterface object
* @param string $fieldDefIdentifier Identifier for the field we want to get the name from
* @param string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR"). Null by default (takes current locale)
*
* @throws \Ibexa\Core\Base\Exceptions\InvalidArgumentType When $content is not a valid Content, ContentInfo, or ContentAwareInterface object.
*
* @return string|null
*/
public function getTranslatedFieldDefinitionName(object $data, $fieldDefIdentifier, $forcedLanguage = null)
public function getTranslatedFieldDefinitionName(Content|ContentInfo|ContentAwareInterface $data, $fieldDefIdentifier, $forcedLanguage = null)
{
if ($contentType = $this->getContentType($this->resolveData($data))) {
return $this->translationHelper->getTranslatedFieldDefinitionProperty(
Expand All @@ -173,24 +162,17 @@ public function getTranslatedFieldDefinitionName(object $data, $fieldDefIdentifi
$forcedLanguage
);
}

throw new InvalidArgumentType(
'$data',
sprintf('%s or %s or %s', Content::class, ContentInfo::class, ContentAwareInterface::class),
$data
);
}

/**
* Gets name of a FieldDefinition description by loading ContentType based on Content/ContentInfo/ContentAwareInterface object.
*
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Content|\Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo|\Ibexa\Contracts\Core\Repository\Values\Content\ContentAwareInterface $data Must be Content, ContentInfo, or ContentAwareInterface object
* @param string $fieldDefIdentifier Identifier for the field we want to get the name from
* @param string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR"). Null by default (takes current locale)
*
* @return string|null
*/
public function getTranslatedFieldDefinitionDescription(object $data, $fieldDefIdentifier, $forcedLanguage = null)
public function getTranslatedFieldDefinitionDescription(Content|ContentInfo|ContentAwareInterface $data, $fieldDefIdentifier, $forcedLanguage = null)
{
if ($contentType = $this->getContentType($this->resolveData($data))) {
return $this->translationHelper->getTranslatedFieldDefinitionProperty(
Expand All @@ -200,18 +182,9 @@ public function getTranslatedFieldDefinitionDescription(object $data, $fieldDefI
$forcedLanguage
);
}

throw new InvalidArgumentType(
'$data',
sprintf('%s or %s or %s', Content::class, ContentInfo::class, ContentAwareInterface::class),
$data
);
}

/**
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Content|\Ibexa\Contracts\Core\Repository\Values\Content\ContentAwareInterface $data
*/
public function hasField(object $data, string $fieldDefIdentifier): bool
public function hasField(Content|ContentAwareInterface $data, string $fieldDefIdentifier): bool
{
$content = $this->getContent($data);

Expand All @@ -227,15 +200,14 @@ public function getFieldGroupName(string $identifier): ?string
* Checks if a given field is considered empty.
* This method accepts field as Objects or by identifiers.
*
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Content|\Ibexa\Contracts\Core\Repository\Values\Content\ContentAwareInterface $data
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Field|string $fieldDefIdentifier Field or Field Identifier to
* get the value from.
* @param string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR").
* Null by default (takes current locale).
*
* @return bool
*/
public function isFieldEmpty(object $data, $fieldDefIdentifier, $forcedLanguage = null)
public function isFieldEmpty(Content|ContentAwareInterface $data, $fieldDefIdentifier, $forcedLanguage = null)
{
if ($fieldDefIdentifier instanceof Field) {
$fieldDefIdentifier = $fieldDefIdentifier->fieldDefIdentifier;
Expand All @@ -247,25 +219,20 @@ public function isFieldEmpty(object $data, $fieldDefIdentifier, $forcedLanguage
/**
* Get ContentType by Content/ContentInfo.
*
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Content|\Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo $content
*
* @return \Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType|null
*/
private function getContentType(ValueObject $content)
private function getContentType(Content|ContentInfo $content)
{
if ($content instanceof Content) {
return $this->repository->getContentTypeService()->loadContentType(
$content->getVersionInfo()->getContentInfo()->contentTypeId
);
} elseif ($content instanceof ContentInfo) {
return $this->repository->getContentTypeService()->loadContentType($content->contentTypeId);
}

return $this->repository->getContentTypeService()->loadContentType($content->contentTypeId);
}

/**
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Content|\Ibexa\Contracts\Core\Repository\Values\Content\ContentAwareInterface $data
*/
public function getFirstFilledImageFieldIdentifier(object $data)
public function getFirstFilledImageFieldIdentifier(Content|ContentAwareInterface $data)
{
$content = $this->getContent($data);
foreach ($content->getFieldsByLanguage() as $field) {
Expand All @@ -287,47 +254,21 @@ public function getFirstFilledImageFieldIdentifier(object $data)
return null;
}

/**
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Content|\Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo|\Ibexa\Contracts\Core\Repository\Values\Content\ContentAwareInterface $data
*
* @throws \Ibexa\Core\Base\Exceptions\InvalidArgumentType
*/
private function resolveData(object $data): ValueObject
private function resolveData(Content|ContentInfo|ContentAwareInterface $data): ValueObject
{
if ($data instanceof Content || $data instanceof ContentInfo) {
return $data;
}

if ($data instanceof ContentAwareInterface) {
return $data->getContent();
}

throw new InvalidArgumentType(
'$content',
sprintf('%s or %s or %s', Content::class, ContentInfo::class, ContentAwareInterface::class),
$data,
);
}

/**
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Content|\Ibexa\Contracts\Core\Repository\Values\Content\ContentAwareInterface $data
*
* @throws \Ibexa\Core\Base\Exceptions\InvalidArgumentType
*/
private function getContent(object $data): Content
private function getContent(Content|ContentAwareInterface $data): Content
{
if ($data instanceof Content) {
return $data;
}

if ($data instanceof ContentAwareInterface) {
return $data->getContent();
}

throw new InvalidArgumentType(
'$content',
sprintf('%s or %s', Content::class, ContentAwareInterface::class),
$data,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Ibexa\Contracts\Core\Repository\Values\Content\Field;
use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition;
use Ibexa\Core\Base\Exceptions\InvalidArgumentException;
use Ibexa\Core\Base\Exceptions\InvalidArgumentType;
use Ibexa\Core\Helper\TranslationHelper;
use Ibexa\Core\MVC\Symfony\FieldType\View\ParameterProviderRegistryInterface;
use Ibexa\Core\MVC\Symfony\Templating\FieldBlockRendererInterface;
Expand Down Expand Up @@ -53,7 +52,7 @@ public function __construct(

public function getFunctions()
{
$renderFieldCallable = function (Environment $environment, $data, $fieldIdentifier, array $params = []) {
$renderFieldCallable = function (Environment $environment, Content|ContentAwareInterface $data, $fieldIdentifier, array $params = []) {
$this->fieldBlockRenderer->setTwig($environment);

return $this->renderField($this->getContent($data), $fieldIdentifier, $params);
Expand Down Expand Up @@ -195,24 +194,12 @@ private function getFieldTypeIdentifier(Content $content, Field $field)
return $this->fieldTypeIdentifiers[$key];
}

/**
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Content|\Ibexa\Contracts\Core\Repository\Values\Content\ContentAwareInterface $content
*
* @throws \Ibexa\Core\Base\Exceptions\InvalidArgumentType
*/
private function getContent(object $content): Content
private function getContent(Content|ContentAwareInterface $content): Content
{
if ($content instanceof Content) {
return $content;
}
if ($content instanceof ContentAwareInterface) {
return $content->getContent();
}

throw new InvalidArgumentType(
'$content',
sprintf('%s or %s', Content::class, ContentAwareInterface::class),
$content,
);
return $content->getContent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use Ibexa\Contracts\Core\Repository\Values\Content\Content;
use Ibexa\Contracts\Core\Repository\Values\Content\ContentAwareInterface;
use Ibexa\Core\Base\Exceptions\InvalidArgumentType;
use Ibexa\Core\MVC\Symfony\Event\ResolveRenderOptionsEvent;
use Ibexa\Core\MVC\Symfony\Templating\RenderContentStrategy;
use Ibexa\Core\MVC\Symfony\Templating\RenderOptions;
Expand Down Expand Up @@ -48,10 +47,7 @@ public function getFunctions(): array
];
}

/**
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Content|\Ibexa\Contracts\Core\Repository\Values\Content\ContentAwareInterface $data
*/
public function renderContent(object $data, array $options = []): string
public function renderContent(Content|ContentAwareInterface $data, array $options = []): string
{
$renderOptions = new RenderOptions($options);
$event = $this->eventDispatcher->dispatch(
Expand All @@ -61,25 +57,12 @@ public function renderContent(object $data, array $options = []): string
return $this->renderContentStrategy->render($this->getContent($data), $event->getRenderOptions());
}

/**
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Content|\Ibexa\Contracts\Core\Repository\Values\Content\ContentAwareInterface $data
*
* @throws \Ibexa\Core\Base\Exceptions\InvalidArgumentType
*/
private function getContent(object $data): Content
private function getContent(Content|ContentAwareInterface $data): Content
{
if ($data instanceof Content) {
return $data;
}

if ($data instanceof ContentAwareInterface) {
return $data->getContent();
}

throw new InvalidArgumentType(
'$content',
sprintf('%s or %s', Content::class, ContentAwareInterface::class),
$data,
);
}
}

0 comments on commit 01ba35f

Please sign in to comment.