lovata/oc-toolbox-plugin

View on GitHub
classes/helper/ParseXMLNode.php

Summary

Maintainability
A
4 hrs
Test Coverage
<?php namespace Lovata\Toolbox\Classes\Helper;
 
/**
* Class ParseXMLNode
* @package Lovata\Toolbox\Classes\Helper
* @author Andrey Kharanenka, a.khoronenko@lovata.com, LOVATA Group
*/
class ParseXMLNode
{
/** @var array */
protected $arImportData = [];
 
/** @var ImportXMLNode */
protected $obElementNode;
 
protected $arImportSettings = [];
 
protected $sPrefix;
 
protected $sNamespace;
 
Doc comment for parameter "$sPrefix" missing
Doc comment for parameter "$sNamespace" missing
/**
* @param ImportXMLNode $obNode
Expected 9 spaces after parameter type; 1 found
* @param array $arSettings
*/
public function __construct($obNode, $arSettings, $sPrefix, $sNamespace)
{
$this->obElementNode = $obNode;
$this->arImportSettings = $arSettings;
$this->sPrefix = $sPrefix;
$this->sNamespace = $sNamespace;
 
$this->parse();
}
 
/**
* @return ImportXMLNode
*/
public function getNode()
{
return $this->obElementNode;
}
 
/**
* Get node data
* @return array
*/
public function get()
{
return $this->arImportData;
}
 
Function `parse` has a Cognitive Complexity of 19 (exceeds 5 allowed). Consider refactoring.
Method `parse` has 35 lines of code (exceeds 25 allowed). Consider refactoring.
The method parse() has an NPath complexity of 362. The configured NPath complexity threshold is 200.
The method parse() has a Cyclomatic Complexity of 13. The configured cyclomatic complexity threshold is 10.
Refactor this function to reduce its Cognitive Complexity from 19 to the 15 allowed.
protected function parse()
{
if (empty($this->arImportSettings)) {
return;
}
 
foreach ($this->arImportSettings as $arFieldData) {
$sFieldName = array_get($arFieldData, 'field');
$sFieldPath = array_get($arFieldData, 'path_to_field');
if (empty($sFieldName) || empty($sFieldPath)) {
continue;
}
 
$sMethodName = 'parse'.studly_case($sFieldName).'Attribute';
if (method_exists(static::class, $sMethodName)) {
$sValue = $this->$sMethodName($sFieldPath, $this->sPrefix, $this->sNamespace);
} else {
$sValue = $this->obElementNode->getValueByPath($sFieldPath, $this->sPrefix, $this->sNamespace);
}
 
if ($sValue === null) {
continue;
}
 
$sCurrentValue = array_get($this->arImportData, $sFieldName);
if (!empty($sCurrentValue) && !is_array($sCurrentValue)) {
$sCurrentValue = [$sCurrentValue];
}
 
if (is_array($sCurrentValue) && is_array($sValue)) {
$sCurrentValue = array_merge($sCurrentValue, $sValue);
$sCurrentValue = array_filter($sCurrentValue);
$sCurrentValue = array_unique($sCurrentValue);
} elseif (is_array($sCurrentValue) && !is_array($sValue)) {
$sCurrentValue[] = $sValue;
$sCurrentValue = array_filter($sCurrentValue);
$sCurrentValue = array_unique($sCurrentValue);
} else {
$sCurrentValue = $sValue;
}
 
array_set($this->arImportData, $sFieldName, $sCurrentValue);
}
}
}