Skip to content

Commit

Permalink
IBX-8875: Dropped deprecated code (#185)
Browse files Browse the repository at this point in the history
* Dropped deprecated OnlineEditorCustomAttributesExtractor class
* Defined Xslt::$xsltProcessor property so it's not dynamic. Creation of dynamic property Ibexa\FieldTypeRichText\RichText\Converter\Xslt::$xsltProcessor is deprecated
* Improved quality of Xslt Converter implementation
* Dropped deprecated support for not configured custom tag. CustomTagsValidator will now return a validation error when a document contains not configured custom tag
* Improved CustomTagsValidator class code quality
* [Tests] Improved CustomTagsValidatorTest and RichTextTest code quality
* [PHPStan] Aligned baseline with the changes
  • Loading branch information
alongosz authored Sep 28, 2024
1 parent bf49c56 commit 1d7a8da
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 230 deletions.
35 changes: 0 additions & 35 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -890,11 +890,6 @@ parameters:
count: 1
path: src/lib/RichText/Converter/Render/Template.php

-
message: "#^Access to an undefined property Ibexa\\\\FieldTypeRichText\\\\RichText\\\\Converter\\\\Xslt\\:\\:\\$xsltProcessor\\.$#"
count: 1
path: src/lib/RichText/Converter/Xslt.php

-
message: "#^Cannot access property \\$firstChild on DOMElement\\|null\\.$#"
count: 1
Expand All @@ -905,31 +900,11 @@ parameters:
count: 1
path: src/lib/RichText/Converter/Xslt.php

-
message: "#^Method Ibexa\\\\FieldTypeRichText\\\\RichText\\\\Converter\\\\Xslt\\:\\:__construct\\(\\) has parameter \\$customStylesheets with no value type specified in iterable type array\\.$#"
count: 1
path: src/lib/RichText/Converter/Xslt.php

-
message: "#^Method Ibexa\\\\FieldTypeRichText\\\\RichText\\\\Converter\\\\Xslt\\:\\:convert\\(\\) should return DOMDocument but returns DOMDocument\\|false\\.$#"
count: 1
path: src/lib/RichText/Converter/Xslt.php

-
message: "#^Method Ibexa\\\\FieldTypeRichText\\\\RichText\\\\Converter\\\\Xslt\\:\\:getSortedCustomStylesheets\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/lib/RichText/Converter/Xslt.php

-
message: "#^Parameter \\#1 \\$source of method DOMDocument\\:\\:loadXML\\(\\) expects string, string\\|false given\\.$#"
count: 1
path: src/lib/RichText/Converter/Xslt.php

-
message: "#^Property Ibexa\\\\FieldTypeRichText\\\\RichText\\\\Converter\\\\Xslt\\:\\:\\$customStylesheets type has no value type specified in iterable type array\\.$#"
count: 1
path: src/lib/RichText/Converter/Xslt.php

-
message: "#^Cannot call method lookupNamespaceURI\\(\\) on DOMElement\\|null\\.$#"
count: 2
Expand Down Expand Up @@ -1245,11 +1220,6 @@ parameters:
count: 1
path: src/lib/RichText/XmlBase.php

-
message: "#^Parameter \\#2 \\$desc of method Ibexa\\\\FieldTypeRichText\\\\Translation\\\\Extractor\\\\OnlineEditorCustomAttributesExtractor\\:\\:createMessage\\(\\) expects string, int\\|string given\\.$#"
count: 1
path: src/lib/Translation/Extractor/OnlineEditorCustomAttributesExtractor.php

-
message: "#^Property Ibexa\\\\FieldTypeRichText\\\\Validator\\\\Constraints\\\\RichText\\:\\:\\$message has no type specified\\.$#"
count: 1
Expand Down Expand Up @@ -2935,11 +2905,6 @@ parameters:
count: 1
path: tests/lib/RichText/Validator/CustomTagsValidatorTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\FieldTypeRichText\\\\RichText\\\\Validator\\\\CustomTagsValidatorTest\\:\\:testValidateDocumentAcceptsLegacyTags\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/RichText/Validator/CustomTagsValidatorTest.php

-
message: "#^Call to method validateDocument\\(\\) on an unknown class Ibexa\\\\FieldTypeRichText\\\\RichText\\\\ValidatorInterface\\.$#"
count: 1
Expand Down
1 change: 0 additions & 1 deletion src/bundle/Resources/config/fieldtype_services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ services:

# Note: should typically be the last one as it produces embeddable fragment
Ibexa\FieldTypeRichText\RichText\Converter\Xslt:
class: Ibexa\FieldTypeRichText\RichText\Converter\Xslt
arguments: ['%ibexa.field_type.richtext.converter.output.xhtml5.fragment.resources%']
tags:
- {name: ibexa.field_type.richtext.converter.output.xhtml5, priority: 100}
Expand Down
7 changes: 0 additions & 7 deletions src/bundle/Resources/config/translation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ services:
autoconfigure: true
public: false

Ibexa\FieldTypeRichText\Translation\Extractor\OnlineEditorCustomAttributesExtractor:
deprecated: 'Since ibexa/fieldtype-richtext 4.6.7 the "%service_id%" service is deprecated, will be removed in 5.0.0'
arguments:
$siteAccessList: '%ibexa.site_access.list%'
tags:
- { name: jms_translation.extractor, alias: ez_online_editor_attributes }

Ibexa\FieldTypeRichText\Translation\Extractor\CustomTagExtractor:
arguments:
$customTags: '%ibexa.field_type.richtext.custom_tags%'
Expand Down
2 changes: 1 addition & 1 deletion src/lib/RichText/Converter/Html5.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
class Html5 extends XsltConverter
{
public function __construct($stylesheet, ConfigResolverInterface $configResolver)
public function __construct(string $stylesheet, ConfigResolverInterface $configResolver)
{
$customStylesheets = $configResolver->getParameter('fieldtypes.ezrichtext.output_custom_xsl');
$customStylesheets = $customStylesheets ?: [];
Expand Down
2 changes: 1 addition & 1 deletion src/lib/RichText/Converter/Html5Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
class Html5Edit extends XsltConverter
{
public function __construct($stylesheet, ConfigResolverInterface $configResolver)
public function __construct(string $stylesheet, ConfigResolverInterface $configResolver)
{
$customStylesheets = $configResolver->getParameter('fieldtypes.ezrichtext.edit_custom_xsl');
$customStylesheets = $customStylesheets ?: [];
Expand Down
2 changes: 1 addition & 1 deletion src/lib/RichText/Converter/Html5Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
class Html5Input extends XsltConverter
{
public function __construct($stylesheet, ConfigResolverInterface $configResolver)
public function __construct(string $stylesheet, ConfigResolverInterface $configResolver)
{
$customStylesheets = $configResolver->getParameter('fieldtypes.ezrichtext.input_custom_xsl');
$customStylesheets = $customStylesheets ?: [];
Expand Down
48 changes: 19 additions & 29 deletions src/lib/RichText/Converter/Xslt.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,39 @@ class Xslt extends XmlBase implements Converter
{
/**
* Path to stylesheet to use.
*
* @var string
*/
protected $stylesheet;
protected string $stylesheet;

/**
* Array of XSL stylesheets to add to the main one, grouped by priority.
*
* @var array
* @var array<int, array<string>>
*/
protected $customStylesheets = [];
protected array $customStylesheets = [];

private XSLTProcessor $xsltProcessor;

/**
* Constructor.
*
* @param string $stylesheet Stylesheet to use for conversion
* @param array $customStylesheets Array of XSL stylesheets. Each entry consists in a hash having "path" and "priority" keys.
* @param array<array{priority: int, path: string}> $customStylesheets Array of XSL stylesheets. Each entry consists in a hash having "path" and "priority" keys.
*/
public function __construct($stylesheet, array $customStylesheets = [])
public function __construct(string $stylesheet, array $customStylesheets = [])
{
$this->stylesheet = $stylesheet;

// Grouping stylesheets by priority.
foreach ($customStylesheets as $customStylesheet) {
$this->customStylesheets[$customStylesheet['priority']][] = $customStylesheet['path'];
$this->customStylesheets[(int)$customStylesheet['priority']][] = $customStylesheet['path'];
}
}

/**
* Returns the XSLTProcessor to use to transform internal XML to HTML5.
*
* @throws \RuntimeException
*
* @return \XSLTProcessor
* @throws \DOMException
*/
protected function getXSLTProcessor()
protected function getXSLTProcessor(): XSLTProcessor
{
if (isset($this->xsltProcessor)) {
return $this->xsltProcessor;
Expand Down Expand Up @@ -97,30 +94,23 @@ protected function getXSLTProcessor()
* The order is from the lowest priority to the highest since in case of a conflict,
* the last loaded XSL template always wins.
*
* @return array
* @return list<string>
*/
protected function getSortedCustomStylesheets()
protected function getSortedCustomStylesheets(): array
{
$sortedStylesheets = [];
ksort($this->customStylesheets);
foreach ($this->customStylesheets as $stylesheets) {
$sortedStylesheets = array_merge($sortedStylesheets, $stylesheets);
}

return $sortedStylesheets;
// flatten [priority => stylesheet[]] array to return a simple list
return array_merge(...$this->customStylesheets);
}

/**
* Performs conversion of the given $document using XSLT stylesheet.
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException if stylesheet is not found
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException if document does not transform
*
* @param \DOMDocument $document
*
* @return \DOMDocument
* @throws \DOMException
* @throws \Ibexa\Core\Base\Exceptions\InvalidArgumentException
*/
public function convert(DOMDocument $document)
public function convert(DOMDocument $xmlDoc): DOMDocument
{
if (!file_exists($this->stylesheet)) {
throw new InvalidArgumentException(
Expand All @@ -133,11 +123,11 @@ public function convert(DOMDocument $document)

$this->startRecordingErrors();

$document = $processor->transformToDoc($document);
$document = $processor->transformToDoc($xmlDoc);

$errors = $this->collectErrors();

if (!empty($errors)) {
if (!empty($errors) || $document === false) {
throw new InvalidArgumentException(
'$xmlDoc',
'Transformation of XML content failed: ' . implode("\n", $errors)
Expand Down
8 changes: 2 additions & 6 deletions src/lib/RichText/Validator/CustomTagsValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@ public function validateDocument(DOMDocument $xmlDocument): array
}

if (!isset($this->customTagsConfiguration[$tagName])) {
@trigger_error(
"Configuration for RichText Custom Tag '{$tagName}' not found. " .
'Custom Tags configuration is required since 7.1, its lack will result in validation error in 8.x',
E_USER_DEPRECATED
);
$errors[] = "Missing configuration for RichText CustomTag: '$tagName'";
continue;
}

Expand Down Expand Up @@ -92,7 +88,7 @@ public function validateDocument(DOMDocument $xmlDocument): array
continue;
}

if (!in_array($attributeName, $nonEmptyAttributes)) {
if (!in_array($attributeName, $nonEmptyAttributes, true)) {
$errors[] = "The attribute '{$attributeName}' of RichText Custom Tag '{$tagName}' cannot be empty";
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected function configureAndLoad(array $configurationValues = [])

$extensionAlias = $extension->getAlias();
// when loading extension, pass only relevant configuration
$extensionConfig = isset($configs[$extensionAlias]) ? $configs[$extensionAlias] : [];
$extensionConfig = $configs[$extensionAlias] ?? [];

$extension->load([$extensionConfig], $this->container);
}
Expand Down
Loading

0 comments on commit 1d7a8da

Please sign in to comment.