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 <?php
/* /*
This code is part of FusionDirectory (http://www.fusiondirectory.org/) This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2003-2010 Cajus Pollmeier 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 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 it under the terms of the GNU General Public License as published by
...@@ -62,107 +63,118 @@ class xml ...@@ -62,107 +63,118 @@ class xml
return; return;
} }
//Initializations // Initializations
$xml_array = []; $xml_array = [];
$current = &$xml_array; //Refference // Reference
$current = &$xml_array;
//Go through the tags. // Go through the tags.
$repeated_tag_index = [];//Multiple tags with same name will be turned into an array // Multiple tags with same name will be turned into an array
$repeated_tag_index = [];
foreach ($xml_values as $data) { 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 = []; $result = [];
$attributes_data = []; $attributes_data = [];
if (isset($value)) { if (isset($data['value'])) {
if ($priority == 'tag') { if ($priority == 'tag') {
$result = $value; $result = $data['value'];
} else { } else {
//Put the value in a assoc array if we are in the 'Attribute' mode // Put the value in a assoc array if we are in the 'Attribute' mode
$result['value'] = $value; $result['value'] = $data['value'];
} }
} }
//Set the attributes too. //Set the attributes too.
if (isset($attributes) and $get_attributes) { if (isset($data['attributes']) and $get_attributes) {
foreach ($attributes as $attr => $val) { foreach ($data['attributes'] as $attr => $val) {
if ($priority == 'tag') { if ($priority == 'tag') {
$attributes_data[$attr] = $val; $attributes_data[$attr] = $val;
} else { } else {
//Set all the attributes in a array called 'attr' // Set all the attributes in a array called 'attr'
$result['attr'][$attr] = $val; $result['attr'][$attr] = $val;
} }
} }
} }
//See tag status and do the needed. // See tag status and do the needed.
if ($type == "open") {//The starting of the tag '<tag>' if ($data['type'] == 'open') {
$parent[$level - 1] = &$current; // The starting of the tag '<tag>'
if (!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag $parent[$data['level'] - 1] = &$current;
$current[$tag] = $result; if (!is_array($current) or (!in_array($data['tag'], array_keys($current)))) {
// Insert New tag
$current[$data['tag']] = $result;
if ($attributes_data) { if ($attributes_data) {
$current[$tag. '_attr'] = $attributes_data; $current[$data['tag']. '_attr'] = $attributes_data;
} }
$repeated_tag_index[$tag.'_'.$level] = 1; $repeated_tag_index[$data['tag'].'_'.$data['level']] = 1;
$current = &$current[$tag];
} else { //There was another element with the same tag name
if (isset($current[$tag][0])) {//If there is a 0th element it is already an array $current = &$current[$data['tag']];
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result; } else {
$repeated_tag_index[$tag.'_'.$level]++; // There was another element with the same tag name
} 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;
if (isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well if (isset($current[$data['tag']][0])) {
$current[$tag]['0_attr'] = $current[$tag.'_attr']; // If there is a 0th element it is already an array
unset($current[$tag.'_attr']); $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; $last_item_index = $repeated_tag_index[$data['tag'].'_'.$data['level']] - 1;
$current = &$current[$tag][$last_item_index]; $current = &$current[$data['tag']][$last_item_index];
} }
} elseif ($type == "complete") { //Tags that ends in 1 line '<tag />' } elseif ($data['type'] == "complete") {
//See if the key is already taken. // Tags that ends in 1 line '<tag />'
if (!isset($current[$tag])) { //New Key // See if the key is already taken.
$current[$tag] = $result; if (!isset($current[$data['tag']])) {
$repeated_tag_index[$tag.'_'.$level] = 1; // New Key
$current[$data['tag']] = $result;
$repeated_tag_index[$data['tag'].'_'.$data['level']] = 1;
if ($priority == 'tag' and $attributes_data) { 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) } else {
if (isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array... // 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. // ...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) { 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... } 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 //...Make it an array using using the existing value and the new value
$repeated_tag_index[$tag.'_'.$level] = 1; $current[$data['tag']] = [$current[$data['tag']],$result];
$repeated_tag_index[$data['tag'].'_'.$data['level']] = 1;
if ($priority == 'tag' and $get_attributes) { if ($priority == 'tag' and $get_attributes) {
if (isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well if (isset($current[$data['tag'].'_attr'])) {
$current[$tag]['0_attr'] = $current[$tag.'_attr']; // The attribute of the last(0th) tag must be moved as well
unset($current[$tag.'_attr']); $current[$data['tag']]['0_attr'] = $current[$data['tag'].'_attr'];
unset($current[$data['tag'].'_attr']);
} }
if ($attributes_data) { 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>' } elseif ($data['type'] == 'close') {
$current = &$parent[$level - 1]; // 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