Unverified Commit f279d3b2 authored by Côme Chilliet's avatar Côme Chilliet
Browse files

:ambulance: fix(xml) Fix PHPStan warning in xml due to extract use

issue #6114
Showing with 71 additions and 59 deletions
+71 -59
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2003-2010 Cajus Pollmeier
Copyright (C) 2011-2016 FusionDirectory
Copyright (C) 2011-2020 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -62,107 +63,118 @@ class xml
return;
}
//Initializations
// Initializations
$xml_array = [];
$current = &$xml_array; //Refference
// Reference
$current = &$xml_array;
//Go through the tags.
$repeated_tag_index = [];//Multiple tags with same name will be turned into an array
// Go through the tags.
// Multiple tags with same name will be turned into an array
$repeated_tag_index = [];
foreach ($xml_values as $data) {
unset($attributes, $value);//Remove existing values, or there will be trouble
//This command will extract these variables into the foreach scope
// tag(string), type(string), level(int), attributes(array).
extract($data);//We could use the array by itself, but this cooler.
$result = [];
$attributes_data = [];
if (isset($value)) {
if (isset($data['value'])) {
if ($priority == 'tag') {
$result = $value;
$result = $data['value'];
} else {
//Put the value in a assoc array if we are in the 'Attribute' mode
$result['value'] = $value;
// Put the value in a assoc array if we are in the 'Attribute' mode
$result['value'] = $data['value'];
}
}
//Set the attributes too.
if (isset($attributes) and $get_attributes) {
foreach ($attributes as $attr => $val) {
if (isset($data['attributes']) and $get_attributes) {
foreach ($data['attributes'] as $attr => $val) {
if ($priority == 'tag') {
$attributes_data[$attr] = $val;
} else {
//Set all the attributes in a array called 'attr'
// Set all the attributes in a array called 'attr'
$result['attr'][$attr] = $val;
}
}
}
//See tag status and do the needed.
if ($type == "open") {//The starting of the tag '<tag>'
$parent[$level - 1] = &$current;
if (!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
$current[$tag] = $result;
// See tag status and do the needed.
if ($data['type'] == 'open') {
// The starting of the tag '<tag>'
$parent[$data['level'] - 1] = &$current;
if (!is_array($current) or (!in_array($data['tag'], array_keys($current)))) {
// Insert New tag
$current[$data['tag']] = $result;
if ($attributes_data) {
$current[$tag. '_attr'] = $attributes_data;
$current[$data['tag']. '_attr'] = $attributes_data;
}
$repeated_tag_index[$tag.'_'.$level] = 1;
$current = &$current[$tag];
} else { //There was another element with the same tag name
$repeated_tag_index[$data['tag'].'_'.$data['level']] = 1;
if (isset($current[$tag][0])) {//If there is a 0th element it is already an array
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
$repeated_tag_index[$tag.'_'.$level]++;
} else {//This section will make the value an array if multiple tags with the same name appear together
$current[$tag] = [$current[$tag],$result];//This will combine the existing item and the new item together to make an array
$repeated_tag_index[$tag.'_'.$level] = 2;
$current = &$current[$data['tag']];
} else {
// There was another element with the same tag name
if (isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
$current[$tag]['0_attr'] = $current[$tag.'_attr'];
unset($current[$tag.'_attr']);
if (isset($current[$data['tag']][0])) {
// If there is a 0th element it is already an array
$current[$data['tag']][$repeated_tag_index[$data['tag'].'_'.$data['level']]] = $result;
$repeated_tag_index[$data['tag'].'_'.$data['level']]++;
} else {
// This section will make the value an array if multiple tags with the same name appear together
// This will combine the existing item and the new item together to make an array
$current[$data['tag']] = [$current[$data['tag']],$result];
$repeated_tag_index[$data['tag'].'_'.$data['level']] = 2;
if (isset($current[$data['tag'].'_attr'])) {
// The attribute of the last(0th) tag must be moved as well
$current[$data['tag']]['0_attr'] = $current[$data['tag'].'_attr'];
unset($current[$data['tag'].'_attr']);
}
}
$last_item_index = $repeated_tag_index[$tag.'_'.$level] - 1;
$current = &$current[$tag][$last_item_index];
$last_item_index = $repeated_tag_index[$data['tag'].'_'.$data['level']] - 1;
$current = &$current[$data['tag']][$last_item_index];
}
} elseif ($type == "complete") { //Tags that ends in 1 line '<tag />'
//See if the key is already taken.
if (!isset($current[$tag])) { //New Key
$current[$tag] = $result;
$repeated_tag_index[$tag.'_'.$level] = 1;
} elseif ($data['type'] == "complete") {
// Tags that ends in 1 line '<tag />'
// See if the key is already taken.
if (!isset($current[$data['tag']])) {
// New Key
$current[$data['tag']] = $result;
$repeated_tag_index[$data['tag'].'_'.$data['level']] = 1;
if ($priority == 'tag' and $attributes_data) {
$current[$tag. '_attr'] = $attributes_data;
$current[$data['tag']. '_attr'] = $attributes_data;
}
} else { //If taken, put all things inside a list(array)
if (isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...
} else {
// If taken, put all things inside a list(array)
if (isset($current[$data['tag']][0]) and is_array($current[$data['tag']])) {
// If it is already an array...
// ...push the new element into that array.
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
$current[$data['tag']][$repeated_tag_index[$data['tag'].'_'.$data['level']]] = $result;
if ($priority == 'tag' and $get_attributes and $attributes_data) {
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
$current[$data['tag']][$repeated_tag_index[$data['tag'].'_'.$data['level']] . '_attr'] = $attributes_data;
}
$repeated_tag_index[$tag.'_'.$level]++;
$repeated_tag_index[$data['tag'].'_'.$data['level']]++;
} else { //If it is not an array...
$current[$tag] = [$current[$tag],$result]; //...Make it an array using using the existing value and the new value
$repeated_tag_index[$tag.'_'.$level] = 1;
//...Make it an array using using the existing value and the new value
$current[$data['tag']] = [$current[$data['tag']],$result];
$repeated_tag_index[$data['tag'].'_'.$data['level']] = 1;
if ($priority == 'tag' and $get_attributes) {
if (isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
$current[$tag]['0_attr'] = $current[$tag.'_attr'];
unset($current[$tag.'_attr']);
if (isset($current[$data['tag'].'_attr'])) {
// The attribute of the last(0th) tag must be moved as well
$current[$data['tag']]['0_attr'] = $current[$data['tag'].'_attr'];
unset($current[$data['tag'].'_attr']);
}
if ($attributes_data) {
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
$current[$data['tag']][$repeated_tag_index[$data['tag'].'_'.$data['level']] . '_attr'] = $attributes_data;
}
}
$repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken
// 0 and 1 index is already taken
$repeated_tag_index[$data['tag'].'_'.$data['level']]++;
}
}
} elseif ($type == 'close') { //End of tag '</tag>'
$current = &$parent[$level - 1];
} elseif ($data['type'] == 'close') {
// End of tag '</tag>'
$current = &$parent[$data['level'] - 1];
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment