Minor refactoring

This commit is contained in:
Meritoo
2019-02-21 21:27:50 +01:00
parent 8c3c85608a
commit 651c4f2259
11 changed files with 126 additions and 130 deletions

View File

@@ -48,12 +48,10 @@ class Arrays
if (is_array($value)) {
$effect .= self::values2string($value, $arrayColumnKey, $separator);
} else {
if (empty($arrayColumnKey)) {
$effect .= $value;
} elseif ($key === $arrayColumnKey) {
$effect .= $array[$arrayColumnKey];
}
} elseif (empty($arrayColumnKey)) {
$effect .= $value;
} elseif ($key === $arrayColumnKey) {
$effect .= $array[$arrayColumnKey];
}
}
}
@@ -111,8 +109,8 @@ class Arrays
*/
if (is_array($row) && !empty($row)) {
foreach ($row as &$value) {
$value = html_entity_decode($value);
foreach ($row as $key => $value) {
$row[$key] = html_entity_decode($value);
}
$rows[] = implode($separator, $row);
@@ -363,7 +361,7 @@ class Arrays
* Name of the variable was not provided and it's a multi dimensional array?
* Let's create the name, because variable is required for later usage (related to multi dimensional array)
*/
if (self::isMultiDimensional($array) && empty($jsVariableName)) {
if (empty($jsVariableName) && $isMultiDimensional) {
$jsVariableName = 'autoGeneratedVariable';
}
@@ -384,13 +382,13 @@ class Arrays
if (is_array($value)) {
$variable = $index;
if (is_integer($index)) {
if (is_int($index)) {
$variable = 'value_' . $variable;
}
$value = self::array2JavaScript($value, $variable, $preserveIndexes);
if (!empty($value)) {
if (null !== $value && '' !== $value) {
/*
* Add an empty line for the 1st iteration only. Required to avoid missing empty line after
* declaration of variable:
@@ -409,21 +407,19 @@ class Arrays
$effect .= "\n";
}
}
} else {
if ($preserveIndexes) {
if (!empty($jsVariableName)) {
$index = Miscellaneous::quoteValue($index);
$effect .= sprintf("\n%s[%s] = %s;", $jsVariableName, $index, $value);
}
} else {
$format = '%s';
if ($counter < $arrayCount) {
$format .= ', ';
}
$effect .= sprintf($format, $value);
} elseif ($preserveIndexes) {
if (!empty($jsVariableName)) {
$index = Miscellaneous::quoteValue($index);
$effect .= sprintf("\n%s[%s] = %s;", $jsVariableName, $index, $value);
}
} else {
$format = '%s';
if ($counter < $arrayCount) {
$format .= ', ';
}
$effect .= sprintf($format, $value);
}
}
@@ -474,7 +470,7 @@ class Arrays
{
if (is_string($item)) {
if ($last) {
$item = substr($item, 0, strlen($item) - 1);
$item = substr($item, 0, -1);
} else {
$item = substr($item, 1);
}
@@ -517,7 +513,7 @@ class Arrays
* No elements or the element does not exist?
* Nothing to do
*/
if (empty($array) || !in_array($item, $array)) {
if (empty($array) || !in_array($item, $array, true)) {
return false;
}
@@ -556,19 +552,18 @@ class Arrays
foreach ($array as $key => &$value) {
$remove = false;
$isArray = is_array($value);
if (is_array($value)) {
if ($isArray) {
self::removeElements($value, $needle, $before);
if (is_array($value) && empty($value)) {
if ($isArray && empty($value)) {
$remove = true;
}
} elseif ($value === $needle) {
break;
} else {
if ($value === $needle) {
break;
} else {
$remove = true;
}
$remove = true;
}
if ($remove) {
@@ -749,7 +744,7 @@ class Arrays
foreach ($exploded as $item) {
$exploded2 = explode($valuesKeysSeparator, $item);
if (2 == count($exploded2)) {
if (2 === count($exploded2)) {
$key = trim($exploded2[0]);
$value = trim($exploded2[1]);
@@ -781,19 +776,17 @@ class Arrays
if ($firstKey) {
$effect = $exists;
$firstKey = false;
} elseif ($explicit) {
$effect = $effect && $exists;
if (!$effect) {
break;
}
} else {
if ($explicit) {
$effect = $effect && $exists;
$effect = $effect || $exists;
if (!$effect) {
break;
}
} else {
$effect = $effect || $exists;
if ($effect) {
break;
}
if ($effect) {
break;
}
}
}
@@ -839,6 +832,7 @@ class Arrays
foreach ($array as $key => $value) {
$path = $key;
$stopRecursion = false;
$valueIsArray = is_array($value);
/*
* If the path of parent element is delivered,
@@ -871,7 +865,7 @@ class Arrays
* or
* - the process is stopped, recursive is not used
*/
if (!is_array($value) || (is_array($value) && empty($value)) || $stopRecursion) {
if (!$valueIsArray || ($valueIsArray && empty($value)) || $stopRecursion) {
$paths[$path] = $value;
continue;
}
@@ -879,7 +873,7 @@ class Arrays
/*
* Let's iterate through the next level, using recursive
*/
if (is_array($value)) {
if ($valueIsArray) {
$recursivePaths = self::getLastElementsPaths($value, $separator, $path, $stopIfMatchedBy);
$paths += $recursivePaths;
}
@@ -1100,7 +1094,8 @@ class Arrays
$recursiveValues = self::getAllValuesOfKey($value, $key);
if (!empty($recursiveValues)) {
$values = array_merge($values, $recursiveValues);
$merged = array_merge($values, $recursiveValues);
$values = $merged;
}
}
}
@@ -1282,7 +1277,7 @@ class Arrays
}
if (Regex::endsWith($element, $separator)) {
$element = substr($element, 0, strlen($element) - 1);
$element = substr($element, 0, -1);
}
}
@@ -1354,7 +1349,7 @@ class Arrays
* Values should be compared only and both arrays are one-dimensional?
* Let's find difference by using simple function
*/
if ($valuesOnly && 1 == self::getDimensionsCount($array1) && 1 == self::getDimensionsCount($array2)) {
if ($valuesOnly && 1 === self::getDimensionsCount($array1) && 1 === self::getDimensionsCount($array2)) {
return array_diff($array1, $array2);
}
@@ -1371,58 +1366,55 @@ class Arrays
if ($array2HasKey && is_array($array2[$key])) {
$difference = self::arrayDiffRecursive($value, $array2[$key], $valuesOnly);
}
} else {
} elseif (!$array2HasKey || ($array2HasKey && $value !== $array2[$key])) {
/*
* 2nd array hasn't key from 1st array?
* We are here, because:
* a) 2nd array hasn't key from 1st array
* OR
* Key exists in both, 1st and 2nd array, but values are different?
* b) key exists in both, 1st and 2nd array, but values are different
*/
if (!$array2HasKey || ($array2HasKey && $value != $array2[$key])) {
$difference = $value;
}
$difference = $value;
}
if (null !== $difference) {
$effect[] = $difference;
}
} else {
/*
* The key exists in 2nd array?
*/
if ($array2HasKey) {
/*
* The value it's an array (it's a nested array)?
*/
if (is_array($value)) {
$diff = [];
* The key exists in 2nd array?
*/
} elseif ($array2HasKey) {
/*
* The value it's an array (it's a nested array)?
*/
if (is_array($value)) {
$diff = [];
if (is_array($array2[$key])) {
/*
* Let's verify the nested array
*/
$diff = self::arrayDiffRecursive($value, $array2[$key], $valuesOnly);
}
if (empty($diff)) {
continue;
}
$effect[$key] = $diff;
} else {
if (is_array($array2[$key])) {
/*
* Value is different than in 2nd array?
* OKay, I've got difference
* Let's verify the nested array
*/
if ($value != $array2[$key]) {
$effect[$key] = $value;
}
$diff = self::arrayDiffRecursive($value, $array2[$key], $valuesOnly);
}
} else {
if (empty($diff)) {
continue;
}
$effect[$key] = $diff;
/*
* Value is different than in 2nd array?
* OKay, I've got difference
*/
} elseif ($value !== $array2[$key]) {
$effect[$key] = $value;
}
} else {
/*
* OKay, I've got difference
*/
$effect[$key] = $value;
}
}
@@ -1595,17 +1587,17 @@ class Arrays
$noPrevious = !$next && self::isFirstElement($array, $element);
/*
* No elements?
* OR
* Given element does not exist in given array?
* Previous neighbour should be returned and given element is first?
* OR
* Next neighbour should be returned and given element is last?
* OR
* Previous neighbour should be returned and given element is first?
* No elements?
* OR
* Given element does not exist in given array?
*
* Nothing to do
*/
if (empty($array) || !in_array($element, $array) || $noNext || $noPrevious) {
if ($noPrevious || $noNext || empty($array) || !in_array($element, $array, true)) {
return null;
}