Skip to content

Commit

Permalink
Fixed issues related to twig/twig 3.15 in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Steveb-p committed Nov 19, 2024
1 parent 9fda0ec commit 17486bc
Show file tree
Hide file tree
Showing 36 changed files with 715 additions and 217 deletions.
20 changes: 0 additions & 20 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -12945,11 +12945,6 @@ parameters:
count: 1
path: src/lib/MVC/Symfony/Templating/Twig/Extension/ContentExtension.php

-
message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\Templating\\\\Twig\\\\Extension\\\\ContentExtension\\:\\:getFunctions\\(\\) return type has no value type specified in iterable type array\\.$#"
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
Expand All @@ -12960,26 +12955,11 @@ parameters:
count: 2
path: src/lib/MVC/Symfony/Templating/Twig/Extension/ContentExtension.php

-
message: "#^Property Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\Templating\\\\Twig\\\\Extension\\\\ContentExtension\\:\\:\\$logger \\(Psr\\\\Log\\\\LoggerInterface\\) does not accept Psr\\\\Log\\\\LoggerInterface\\|null\\.$#"
count: 1
path: src/lib/MVC/Symfony/Templating/Twig/Extension/ContentExtension.php

-
message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\Templating\\\\Twig\\\\Extension\\\\CoreExtension\\:\\:getGlobals\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/lib/MVC/Symfony/Templating/Twig/Extension/CoreExtension.php

-
message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\Templating\\\\Twig\\\\Extension\\\\DataAttributesExtension\\:\\:getFilters\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/lib/MVC/Symfony/Templating/Twig/Extension/DataAttributesExtension.php

-
message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\Templating\\\\Twig\\\\Extension\\\\DataAttributesExtension\\:\\:serializeDataAttributes\\(\\) has parameter \\$dataAttributes with no value type specified in iterable type array\\.$#"
count: 1
path: src/lib/MVC/Symfony/Templating/Twig/Extension/DataAttributesExtension.php

-
message: "#^Parameter \\#1 \\$string of function htmlspecialchars expects string, string\\|false given\\.$#"
count: 1
Expand Down
44 changes: 12 additions & 32 deletions src/lib/MVC/Symfony/Templating/Twig/Extension/ContentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Ibexa\Core\Helper\FieldsGroups\FieldsGroupsList;
use Ibexa\Core\Helper\TranslationHelper;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

Expand All @@ -25,6 +26,8 @@
*/
class ContentExtension extends AbstractExtension
{
use DeprecationOptionsTrait;

/** @var \Ibexa\Contracts\Core\Repository\Repository */
protected $repository;

Expand All @@ -50,24 +53,19 @@ public function __construct(
$this->translationHelper = $translationHelper;
$this->fieldHelper = $fieldHelper;
$this->fieldsGroupsList = $fieldsGroupsList;
$this->logger = $logger;
$this->logger = $logger ?? new NullLogger();
}

/**
* Returns a list of functions to add to the existing list.
*
* @return array
*/
public function getFunctions()
public function getFunctions(): array
{
return [
new TwigFunction(
'ez_content_name',
[$this, 'getTranslatedContentName'],
[
'deprecated' => '4.0',
'alternative' => 'ibexa_content_name',
]
$this->getDeprecationOptions('ibexa_content_name'),
),
new TwigFunction(
'ibexa_content_name',
Expand All @@ -76,10 +74,7 @@ public function getFunctions()
new TwigFunction(
'ez_field_value',
[$this, 'getTranslatedFieldValue'],
[
'deprecated' => '4.0',
'alternative' => 'ibexa_field_value',
]
$this->getDeprecationOptions('ibexa_field_value'),
),
new TwigFunction(
'ibexa_field_value',
Expand All @@ -88,10 +83,7 @@ public function getFunctions()
new TwigFunction(
'ez_field',
[$this, 'getTranslatedField'],
[
'deprecated' => '4.0',
'alternative' => 'ibexa_field',
]
$this->getDeprecationOptions('ibexa_field'),
),
new TwigFunction(
'ibexa_field',
Expand All @@ -100,10 +92,7 @@ public function getFunctions()
new TwigFunction(
'ez_field_is_empty',
[$this, 'isFieldEmpty'],
[
'deprecated' => '4.0',
'alternative' => 'ibexa_field_is_empty',
]
$this->getDeprecationOptions('ibexa_field_is_empty'),
),
new TwigFunction(
'ibexa_has_field',
Expand All @@ -116,10 +105,7 @@ public function getFunctions()
new TwigFunction(
'ez_field_name',
[$this, 'getTranslatedFieldDefinitionName'],
[
'deprecated' => '4.0',
'alternative' => 'ibexa_field_name',
]
$this->getDeprecationOptions('ibexa_field_name'),
),
new TwigFunction(
'ibexa_field_name',
Expand All @@ -128,10 +114,7 @@ public function getFunctions()
new TwigFunction(
'ez_field_description',
[$this, 'getTranslatedFieldDefinitionDescription'],
[
'deprecated' => '4.0',
'alternative' => 'ibexa_field_description',
]
$this->getDeprecationOptions('ibexa_field_description'),
),
new TwigFunction(
'ibexa_field_description',
Expand All @@ -144,10 +127,7 @@ public function getFunctions()
new TwigFunction(
'ez_content_field_identifier_first_filled_image',
[$this, 'getFirstFilledImageFieldIdentifier'],
[
'deprecated' => '4.0',
'alternative' => 'ibexa_content_field_identifier_first_filled_image',
]
$this->getDeprecationOptions('ibexa_content_field_identifier_first_filled_image'),
),
new TwigFunction(
'ibexa_content_field_identifier_first_filled_image',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,23 @@
*/
class DataAttributesExtension extends AbstractExtension
{
use DeprecationOptionsTrait;

/**
* Returns a list of functions to add to the existing list.
*
* @return array
*/
public function getFilters(): array
{
return [
new TwigFilter(
'ez_data_attributes_serialize',
[$this, 'serializeDataAttributes'],
[
'is_safe' => ['html'],
'deprecated' => '4.0',
'alternative' => 'ibexa_data_attributes_serialize',
]
array_merge(
[
'is_safe' => ['html'],
],
$this->getDeprecationOptions('ibexa_data_attributes_serialize'),
),
),
new TwigFilter(
'ibexa_data_attributes_serialize',
Expand All @@ -45,7 +46,7 @@ public function getFilters(): array
* Processes an associative list of data attributes and returns them as HTML attributes list
* in the form of <code>data-<attribute_name>="<attribute_value>"</code>.
*
* @param array $dataAttributes
* @param array<string, mixed> $dataAttributes
*
* @return string
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace Ibexa\Core\MVC\Symfony\Templating\Twig\Extension;

use Twig\DeprecatedCallableInfo;

/**
* This trait provides ability to deprecate twig functions, maintaining compatibility with twig/twig prior to 3.15.
*/
trait DeprecationOptionsTrait
{
/**
* @phpstan-param non-empty-string $newFunction
*
* @phpstan-return array{
* deprecation_info: \Twig\DeprecatedCallableInfo,
* }|array{
* deprecated: non-empty-string,
* deprecating_package: non-empty-string,
* alternative: non-empty-string,
* }
*/
private function getDeprecationOptions(string $newFunction): array
{
if (class_exists(DeprecatedCallableInfo::class)) {
return [
'deprecation_info' => new DeprecatedCallableInfo('ibexa/core', '4.0', $newFunction),
];
}

// Compatibility with twig/twig prior to 3.15
return [
'deprecated' => '4.0',
'deprecating_package' => 'ibexa/core',
'alternative' => $newFunction,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/
class FieldRenderingExtension extends AbstractExtension
{
use DeprecationOptionsTrait;

/** @var \Ibexa\Core\MVC\Symfony\Templating\FieldBlockRendererInterface */
private $fieldBlockRenderer;

Expand Down Expand Up @@ -66,12 +68,13 @@ public function getFunctions()
new TwigFunction(
'ez_render_field',
$renderFieldCallable,
[
'is_safe' => ['html'],
'needs_environment' => true,
'deprecated' => '4.0',
'alternative' => 'ibexa_render_field',
]
array_merge(
[
'is_safe' => ['html'],
'needs_environment' => true,
],
$this->getDeprecationOptions('ibexa_render_field'),
),
),
new TwigFunction(
'ibexa_render_field',
Expand All @@ -81,12 +84,13 @@ public function getFunctions()
new TwigFunction(
'ez_render_field_definition_settings',
$renderFieldDefinitionSettingsCallable,
[
'is_safe' => ['html'],
'needs_environment' => true,
'deprecated' => '4.0',
'alternative' => 'ibexa_render_field_definition_settings',
]
array_merge(
[
'is_safe' => ['html'],
'needs_environment' => true,
],
$this->getDeprecationOptions('ibexa_render_field_definition_settings'),
),
),
new TwigFunction(
'ibexa_render_field_definition_settings',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/
class FileSizeExtension extends AbstractExtension
{
use DeprecationOptionsTrait;

/**
* @param \Symfony\Contracts\Translation\TranslatorInterface $translator
*/
Expand Down Expand Up @@ -77,10 +79,7 @@ public function getFilters()
new TwigFilter(
'ez_file_size',
[$this, 'sizeFilter'],
[
'deprecated' => '4.0',
'alternative' => 'ibexa_file_size',
]
$this->getDeprecationOptions('ibexa_file_size'),
),
new TwigFilter(
'ibexa_file_size',
Expand Down
26 changes: 15 additions & 11 deletions src/lib/MVC/Symfony/Templating/Twig/Extension/ImageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

class ImageExtension extends AbstractExtension
{
use DeprecationOptionsTrait;

/** @var \Ibexa\Contracts\Core\Variation\VariationHandler */
private $imageVariationService;

Expand All @@ -30,17 +32,18 @@ public function __construct(VariationHandler $imageVariationService, AssetMapper
$this->assetMapper = $assetMapper;
}

public function getFunctions()
public function getFunctions(): array
{
return [
new TwigFunction(
'ez_image_alias',
[$this, 'getImageVariation'],
[
'is_safe' => ['html'],
'deprecated' => '4.0',
'alternative' => 'ibexa_image_alias',
]
array_merge(
[
'is_safe' => ['html'],
],
$this->getDeprecationOptions('ibexa_image_alias'),
),
),
new TwigFunction(
'ibexa_image_alias',
Expand All @@ -50,11 +53,12 @@ public function getFunctions()
new TwigFunction(
'ez_content_field_identifier_image_asset',
[$this, 'getImageAssetContentFieldIdentifier'],
[
'is_safe' => ['html'],
'deprecated' => '4.0',
'alternative' => 'ibexa_content_field_identifier_image_asset',
]
array_merge(
[
'is_safe' => ['html'],
],
$this->getDeprecationOptions('ibexa_content_field_identifier_image_asset'),
),
),
new TwigFunction(
'ibexa_content_field_identifier_image_asset',
Expand Down
Loading

0 comments on commit 17486bc

Please sign in to comment.