Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Code cleanup of Shopware\Components\Plugin\XmlReader namespace #2689

Draft
wants to merge 1 commit into
base: 5.7
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
'no_alias_functions' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'no_superfluous_phpdoc_tags' => true,
'no_superfluous_phpdoc_tags' => ['allow_mixed' => true],
'nullable_type_declaration_for_default_null_value' => true,
'operator_linebreak' => ['only_booleans' => true],
'ordered_class_elements' => true,
Expand Down
305 changes: 0 additions & 305 deletions .phpstan-baseline.neon

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public function getTypes(array $activePlugins, array $pluginDirectories, array $
return $result;
}

/**
* @return list<array{file: string, type: string}>
*/
private function getConfigPaths(array $activePlugins, array $pluginDirectories): array
{
$configs = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
*/
protected $xsdFile = __DIR__ . '/../../Resources/contenttypes.xsd';

/**
* @param array{file: string, type: string} $xmlFile
*
* @return array<array<string, mixed>>
*/
public function readType(array $xmlFile): array
{
$data = $this->read($xmlFile['file']);
Expand All @@ -53,6 +58,7 @@

protected function parseFile(DOMDocument $xml): array
{
/** @var DOMNodeList<DOMElement>|false $nodeList */
$nodeList = (new DOMXPath($xml))->query('//types/type');
if (!$nodeList instanceof DOMNodeList) {
return [];
Expand All @@ -61,14 +67,18 @@
return self::parseList($nodeList);
}

/**
* @param DOMNodeList<DOMElement> $list
*
* @return array<array<string, mixed>>
*/
private static function parseList(DOMNodeList $list): array
{
if ($list->length === 0) {
return [];
}
$items = [];

/** @var DOMElement $item */
foreach ($list as $item) {
$item = self::parseItem($item);
$items[$item['typeName']] = $item;
Expand All @@ -78,6 +88,9 @@
return $items;
}

/**
* @return array<string, mixed>
*/
private static function parseItem(DOMElement $element): array
{
$item = [];
Expand Down Expand Up @@ -148,6 +161,9 @@
return $item;
}

/**
* @return array<string, mixed>
*/
private static function parseField(DOMElement $element): array
{
$item = [];
Expand All @@ -172,17 +188,17 @@
$item['custom'] = self::parseCustom($element);
$item['options'] = self::parseCustom($element, 'options');

$store = $element->getElementsByTagName('store');

if ($store->length) {
/** @var DOMElement $storeElement */
$storeElement = $store->item(0);
$item['store'] = self::parseComboboStoreList($storeElement);
$storeItem = $element->getElementsByTagName('store')->item(0);
if ($storeItem) {
$item['store'] = self::parseComboStoreList($storeItem);
}

return $item;
}

/**
* @return array<string, mixed>
*/
private static function parseFieldset(DOMElement $element): array
{
$fieldSet = [];
Expand All @@ -202,39 +218,48 @@
return $fieldSet;
}

/**
* @return array<string, mixed>
*/
private static function parseCustom(DOMElement $element, string $fieldName = 'custom'): array
{
$elements = $element->getElementsByTagName($fieldName);
if (!$elements->length) {
return [];
}

return self::cleanArray(self::xmlToArray($elements->item(0)));
$elementsItem = $elements->item(0);
if (!$elementsItem instanceof DOMNode) {
return [];
}

return self::cleanArray(self::xmlToArray($elementsItem));
}

/**
* @see https://stackoverflow.com/questions/14553547/what-is-the-best-php-dom-2-array-function
*
* @return array<array-key, mixed>
*/
private static function xmlToArray(DOMNode $root)
private static function xmlToArray(DOMNode $root): array
{
$result = [];

if ($root->hasAttributes()) {
foreach ($root->attributes as $attr) {
$attributes = $root->attributes;
if ($attributes) {
foreach ($attributes as $attr) {
$result['@attributes'][$attr->name] = $attr->value;
}
}

if ($root->hasChildNodes()) {
$children = $root->childNodes;
if ($children->length == 1) {
if ($children->length === 1) {
$child = $children->item(0);
if ($child->nodeType == XML_TEXT_NODE) {
if ($child && $child->nodeType === XML_TEXT_NODE) {
$result['_value'] = $child->nodeValue;

return \count($result) == 1
? $result['_value']
: $result;
return \count($result) === 1 ? $result['_value'] : $result;

Check failure on line 262 in engine/Shopware/Bundle/ContentTypeBundle/Services/XmlReader/ContentTypesReader.php

View workflow job for this annotation

GitHub Actions / PHPStan and CSFixer / php

Method Shopware\Bundle\ContentTypeBundle\Services\XmlReader\ContentTypesReader::xmlToArray() should return array but returns array<string, array|string|null>|string|null.
}
}
$groups = [];
Expand All @@ -254,6 +279,11 @@
return $result;
}

/**
* @param array<string, mixed> $haystack
*
* @return array<string, mixed>
*/
private static function cleanArray(array $haystack): array
{
foreach ($haystack as $key => $value) {
Expand All @@ -269,22 +299,24 @@
return $haystack;
}

private static function parseComboboStoreList(DOMElement $element): array
/**
* @return list<array{id: string, name: string}>
*/
private static function parseComboStoreList(DOMElement $element): array
{
$storeOptions = $element->getElementsByTagName('option');
if ($storeOptions->length === 0) {
return [];
}
$options = [];
/** @var DOMElement $storeOption */
foreach ($storeOptions as $storeOption) {
$value = '';
$label = '';
if ($optionValue = $storeOption->getElementsByTagName('value')->item(0)) {
$value = $optionValue->nodeValue;
$value = (string) $optionValue->nodeValue;
}
if ($labelNode = $storeOption->getElementsByTagName('label')->item(0)) {
$label = $labelNode->nodeValue;
$label = (string) $labelNode->nodeValue;
}
$options[] = [
'id' => $value,
Expand Down
38 changes: 16 additions & 22 deletions engine/Shopware/Components/Plugin/RequirementValidator.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

declare(strict_types=1);
/**
* Shopware 5
* Copyright (c) shopware AG
Expand Down Expand Up @@ -32,20 +34,11 @@

class RequirementValidator
{
/**
* @var ModelManager
*/
private $em;
private ModelManager $em;

/**
* @var XmlPluginReader
*/
private $infoReader;
private XmlPluginReader $infoReader;

/**
* @var Enlight_Components_Snippet_Namespace
*/
private $namespace;
private Enlight_Components_Snippet_Namespace $namespace;

public function __construct(ModelManager $em, XmlPluginReader $infoReader, SnippetManager $snippetManager)
{
Expand All @@ -59,6 +52,8 @@ public function __construct(ModelManager $em, XmlPluginReader $infoReader, Snipp
* @param string $shopwareVersion current shopware version
*
* @throws Exception
*
* @return void
*/
public function validate($pluginXmlFile, $shopwareVersion)
{
Expand All @@ -77,14 +72,7 @@ public function validate($pluginXmlFile, $shopwareVersion)
}
}

/**
* @param string $version
* @param string $required
* @param string $operator
*
* @return bool
*/
private function assertVersion($version, $required, $operator)
private function assertVersion(string $version, string $required, string $operator): bool
{
if ($version === '___VERSION___') {
return true;
Expand All @@ -93,9 +81,12 @@ private function assertVersion($version, $required, $operator)
return version_compare($version, $required, $operator);
}

/**
* @param array{minVersion?: string, maxVersion?: string, blacklist?: list<string>} $compatibility
*/
private function assertShopwareVersion(array $compatibility, string $shopwareVersion): void
{
if (isset($compatibility['blacklist']) && \in_array($shopwareVersion, $compatibility['blacklist'])) {
if (isset($compatibility['blacklist']) && \in_array($shopwareVersion, $compatibility['blacklist'], true)) {
throw new Exception(sprintf($this->namespace->get('shopware_version_blacklisted'), $shopwareVersion));
}

Expand All @@ -114,6 +105,9 @@ private function assertShopwareVersion(array $compatibility, string $shopwareVer
}
}

/**
* @param array<array{pluginName: string, minVersion?: string, maxVersion?: string, blacklist?: list<string>}> $requiredPlugins
*/
private function assertRequiredPlugins(array $requiredPlugins): void
{
$pluginRepository = $this->em->getRepository(Plugin::class);
Expand All @@ -135,7 +129,7 @@ private function assertRequiredPlugins(array $requiredPlugins): void
throw new Exception(sprintf($this->namespace->get('required_plugin_not_active'), $requiredPlugin['pluginName']));
}

if (isset($requiredPlugin['blacklist']) && \in_array($plugin->getVersion(), $requiredPlugin['blacklist'])) {
if (isset($requiredPlugin['blacklist']) && \in_array($plugin->getVersion(), $requiredPlugin['blacklist'], true)) {
throw new Exception(sprintf($this->namespace->get('required_plugin_blacklisted'), $plugin->getName(), $plugin->getVersion()));
}

Expand Down
2 changes: 2 additions & 0 deletions engine/Shopware/Components/Plugin/XmlPluginInfoReader.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

declare(strict_types=1);
/**
* Shopware 5
* Copyright (c) shopware AG
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

declare(strict_types=1);
/**
* Shopware 5
* Copyright (c) shopware AG
Expand Down Expand Up @@ -27,6 +29,9 @@

class StoreExtjsValueParser implements StoreValueParserInterface
{
/**
* @return string
*/
public function parse(DOMElement $element)
{
return $element->nodeValue ?: '';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

declare(strict_types=1);
/**
* Shopware 5
* Copyright (c) shopware AG
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

declare(strict_types=1);
/**
* Shopware 5
* Copyright (c) shopware AG
Expand Down Expand Up @@ -30,7 +32,7 @@ interface StoreValueParserInterface
/**
* Parses store options.
*
* @return string|array
* @return mixed
*/
public function parse(DOMElement $element);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

declare(strict_types=1);
/**
* Shopware 5
* Copyright (c) shopware AG
Expand Down Expand Up @@ -28,6 +30,9 @@

class StoreXmlValueParser implements StoreValueParserInterface
{
/**
* @return list<array{0: string, 1: array<string, string>|null}>
*/
public function parse(DOMElement $element)
{
$storeOptions = $element->getElementsByTagName('option');
Expand All @@ -42,7 +47,7 @@ public function parse(DOMElement $element)
foreach ($storeOptions as $storeOption) {
$value = '';
if ($optionValue = $storeOption->getElementsByTagName('value')->item(0)) {
$value = $optionValue->nodeValue;
$value = (string) $optionValue->nodeValue;
}

$label = XmlReaderBase::parseTranslatableNodeList(
Expand Down
Loading
Loading