mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-13 01:51:50 +01:00
Fix code pointed by Psalm
This commit is contained in:
@@ -335,7 +335,7 @@ class Arrays
|
||||
* The last row is not an array or it's an empty array?
|
||||
* Let's use the previous candidate
|
||||
*/
|
||||
if (!is_array($effect) || (is_array($effect) && empty($effect))) {
|
||||
if (!is_array($effect) || self::isEmptyArray($effect)) {
|
||||
$effect = $last;
|
||||
}
|
||||
}
|
||||
@@ -346,27 +346,29 @@ class Arrays
|
||||
/**
|
||||
* Replaces array keys that match given pattern with new key name
|
||||
*
|
||||
* @param array $dataArray The array
|
||||
* @param string $oldKeyPattern Old key pattern
|
||||
* @param string $newKey New key name
|
||||
* @return array
|
||||
* @param array $array Array which keys should be replaced
|
||||
* @param string $oldKeyPattern Regular expression of the old key
|
||||
* @param string $newKey Name of the new key
|
||||
* @return null|array
|
||||
*/
|
||||
public static function replaceArrayKeys($dataArray, $oldKeyPattern, $newKey)
|
||||
public static function replaceKeys(array $array, string $oldKeyPattern, string $newKey): ?array
|
||||
{
|
||||
if (empty($array)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$effect = [];
|
||||
|
||||
if (is_array($dataArray) && !empty($dataArray)) {
|
||||
foreach ($dataArray as $key => $value) {
|
||||
if (preg_match($oldKeyPattern, $key)) {
|
||||
$key = $newKey;
|
||||
}
|
||||
|
||||
if (is_array($value)) {
|
||||
$value = self::replaceArrayKeys($value, $oldKeyPattern, $newKey);
|
||||
}
|
||||
|
||||
$effect[$key] = $value;
|
||||
foreach ($array as $key => $value) {
|
||||
if (preg_match($oldKeyPattern, $key)) {
|
||||
$key = $newKey;
|
||||
}
|
||||
|
||||
if (is_array($value)) {
|
||||
$value = self::replaceKeys($value, $oldKeyPattern, $newKey);
|
||||
}
|
||||
|
||||
$effect[$key] = $value;
|
||||
}
|
||||
|
||||
return $effect;
|
||||
@@ -376,17 +378,18 @@ class Arrays
|
||||
* Generates JavaScript code for given PHP array
|
||||
*
|
||||
* @param array $array The array that should be generated to JavaScript
|
||||
* @param string $jsVariableName (optional) Name of the variable that will be in generated JavaScript code
|
||||
* @param string $jsVariableName (optional) Name of the variable that will be in generated JavaScript code.
|
||||
* Default: "autoGeneratedVariable".
|
||||
* @param bool $preserveIndexes (optional) If is set to true and $jsVariableName isn't empty, indexes also
|
||||
* will be added to the JavaScript code. Otherwise not.
|
||||
* will be added to the JavaScript code. Otherwise not (default behaviour).
|
||||
* @return null|string
|
||||
*/
|
||||
public static function array2JavaScript(array $array, $jsVariableName = '', $preserveIndexes = false)
|
||||
{
|
||||
/*
|
||||
* No elements?
|
||||
* Nothing to do
|
||||
*/
|
||||
public static function array2JavaScript(
|
||||
array $array,
|
||||
string $jsVariableName = '',
|
||||
bool $preserveIndexes = false
|
||||
): ?string {
|
||||
// No elements? Nothing to do
|
||||
if (empty($array)) {
|
||||
return null;
|
||||
}
|
||||
@@ -396,7 +399,11 @@ class Arrays
|
||||
|
||||
$arrayCount = count($array);
|
||||
$arrayPrepared = self::quoteStrings($array);
|
||||
$isMultiDimensional = self::isMultiDimensional($arrayPrepared);
|
||||
$isMultiDimensional = false;
|
||||
|
||||
if (null !== $arrayPrepared) {
|
||||
$isMultiDimensional = self::isMultiDimensional($arrayPrepared);
|
||||
}
|
||||
|
||||
/*
|
||||
* Name of the variable was not provided and it's a multi dimensional array?
|
||||
@@ -406,7 +413,7 @@ class Arrays
|
||||
$jsVariableName = 'autoGeneratedVariable';
|
||||
}
|
||||
|
||||
if (!empty($jsVariableName) && is_string($jsVariableName)) {
|
||||
if (!empty($jsVariableName)) {
|
||||
$result .= sprintf('var %s = ', $jsVariableName);
|
||||
}
|
||||
|
||||
@@ -417,50 +424,52 @@ class Arrays
|
||||
$result .= ');';
|
||||
}
|
||||
|
||||
foreach ($arrayPrepared as $index => $value) {
|
||||
++$counter;
|
||||
if (null !== $arrayPrepared) {
|
||||
foreach ($arrayPrepared as $index => $value) {
|
||||
++$counter;
|
||||
|
||||
if (is_array($value)) {
|
||||
$variable = $index;
|
||||
if (is_array($value)) {
|
||||
$variable = $index;
|
||||
|
||||
if (is_int($index)) {
|
||||
$variable = 'value_' . $variable;
|
||||
}
|
||||
|
||||
$value = self::array2JavaScript($value, $variable, $preserveIndexes);
|
||||
|
||||
if (null !== $value && '' !== $value) {
|
||||
/*
|
||||
* Add an empty line for the 1st iteration only. Required to avoid missing empty line after
|
||||
* declaration of variable:
|
||||
*
|
||||
* var autoGeneratedVariable = new Array(...);autoGeneratedVariable[0] = new Array(...);
|
||||
* autoGeneratedVariable[1] = new Array(...);
|
||||
*/
|
||||
if (1 === $counter) {
|
||||
$result .= "\n";
|
||||
if (is_int($index)) {
|
||||
$variable = 'value_' . $variable;
|
||||
}
|
||||
|
||||
$result .= $value . "\n";
|
||||
$result .= sprintf('%s[%s] = %s;', $jsVariableName, Miscellaneous::quoteValue($index), $variable);
|
||||
$value = self::array2JavaScript($value, $variable, $preserveIndexes);
|
||||
|
||||
if ($counter !== $arrayCount) {
|
||||
$result .= "\n";
|
||||
if (null !== $value && '' !== $value) {
|
||||
/*
|
||||
* Add an empty line for the 1st iteration only. Required to avoid missing empty line after
|
||||
* declaration of variable:
|
||||
*
|
||||
* var autoGeneratedVariable = new Array(...);autoGeneratedVariable[0] = new Array(...);
|
||||
* autoGeneratedVariable[1] = new Array(...);
|
||||
*/
|
||||
if (1 === $counter) {
|
||||
$result .= "\n";
|
||||
}
|
||||
|
||||
$result .= $value . "\n";
|
||||
$result .= sprintf('%s[%s] = %s;', $jsVariableName, Miscellaneous::quoteValue($index), $variable);
|
||||
|
||||
if ($counter !== $arrayCount) {
|
||||
$result .= "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
} elseif ($preserveIndexes) {
|
||||
if (!empty($jsVariableName)) {
|
||||
$index = Miscellaneous::quoteValue($index);
|
||||
$result .= sprintf("\n%s[%s] = %s;", $jsVariableName, $index, $value);
|
||||
}
|
||||
} else {
|
||||
$format = '%s';
|
||||
} elseif ($preserveIndexes) {
|
||||
if (!empty($jsVariableName)) {
|
||||
$index = Miscellaneous::quoteValue($index);
|
||||
$result .= sprintf("\n%s[%s] = %s;", $jsVariableName, $index, $value);
|
||||
}
|
||||
} else {
|
||||
$format = '%s';
|
||||
|
||||
if ($counter < $arrayCount) {
|
||||
$format .= ', ';
|
||||
}
|
||||
if ($counter < $arrayCount) {
|
||||
$format .= ', ';
|
||||
}
|
||||
|
||||
$result .= sprintf($format, $value);
|
||||
$result .= sprintf($format, $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -477,7 +486,7 @@ class Arrays
|
||||
* @param array $array The array to check for string values
|
||||
* @return null|array
|
||||
*/
|
||||
public static function quoteStrings(array $array)
|
||||
public static function quoteStrings(array $array): ?array
|
||||
{
|
||||
/*
|
||||
* No elements?
|
||||
@@ -492,10 +501,8 @@ class Arrays
|
||||
foreach ($array as $index => $value) {
|
||||
if (is_array($value)) {
|
||||
$value = self::quoteStrings($value);
|
||||
} elseif (is_string($value)) {
|
||||
if (!Regex::isQuoted($value)) {
|
||||
$value = '\'' . $value . '\'';
|
||||
}
|
||||
} elseif (is_string($value) && !Regex::isQuoted($value)) {
|
||||
$value = '\'' . $value . '\'';
|
||||
}
|
||||
|
||||
$result[$index] = $value;
|
||||
@@ -505,31 +512,28 @@ class Arrays
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes marginal element (first or last)
|
||||
* Removes marginal element (first or last) from given array
|
||||
*
|
||||
* @param array|string $item The item which should be shortened
|
||||
* @param bool $last (optional) If is set to true, last element is removed. Otherwise - first.
|
||||
* @return array|string
|
||||
* @param array $array The array which should be shortened
|
||||
* @param bool $last (optional) If is set to true, last element is removed (default behaviour). Otherwise - first.
|
||||
* @return null|array
|
||||
*/
|
||||
public static function removeMarginalElement($item, $last = true)
|
||||
public static function removeMarginalElement(array $array, bool $last = true): ?array
|
||||
{
|
||||
if (is_string($item)) {
|
||||
if ($last) {
|
||||
$item = substr($item, 0, -1);
|
||||
} else {
|
||||
$item = substr($item, 1);
|
||||
}
|
||||
} elseif (is_array($item)) {
|
||||
$key = self::getFirstKey($item);
|
||||
|
||||
if ($last) {
|
||||
$key = self::getLastKey($item);
|
||||
}
|
||||
|
||||
unset($item[$key]);
|
||||
// No elements? Nothing to do
|
||||
if (empty($array)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $item;
|
||||
$key = self::getFirstKey($array);
|
||||
|
||||
if ($last) {
|
||||
$key = self::getLastKey($array);
|
||||
}
|
||||
|
||||
unset($array[$key]);
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -590,7 +594,7 @@ class Arrays
|
||||
* @param bool $before (optional) If is set to true, all elements before given needle are removed. Otherwise - all
|
||||
* after needle.
|
||||
*/
|
||||
public static function removeElements(array &$array, $needle, $before = true)
|
||||
public static function removeElements(array &$array, $needle, $before = true): void
|
||||
{
|
||||
if (!empty($array)) {
|
||||
if (!$before) {
|
||||
@@ -604,7 +608,7 @@ class Arrays
|
||||
if ($isArray) {
|
||||
self::removeElements($value, $needle, $before);
|
||||
|
||||
if ($isArray && empty($value)) {
|
||||
if (empty($value)) {
|
||||
$remove = true;
|
||||
}
|
||||
} elseif ($value === $needle) {
|
||||
@@ -730,7 +734,7 @@ class Arrays
|
||||
* @param array $array The array to count
|
||||
* @return null|int
|
||||
*/
|
||||
public static function getNonArrayElementsCount(array $array)
|
||||
public static function getNonArrayElementsCount(array $array): ?int
|
||||
{
|
||||
/*
|
||||
* No elements?
|
||||
@@ -742,9 +746,9 @@ class Arrays
|
||||
|
||||
$count = 0;
|
||||
|
||||
foreach ($array as &$value) {
|
||||
foreach ($array as $value) {
|
||||
if (is_array($value)) {
|
||||
$count += self::getNonArrayElementsCount($value);
|
||||
$count += (int)self::getNonArrayElementsCount($value);
|
||||
|
||||
continue;
|
||||
}
|
||||
@@ -772,14 +776,14 @@ class Arrays
|
||||
* @param string $separator (optional) Separator used between name-value pairs in the string.
|
||||
* Default: "|".
|
||||
* @param string $valuesKeysSeparator (optional) Separator used between name and value in the string. Default: ":".
|
||||
* @return array
|
||||
* @return null|array
|
||||
*/
|
||||
public static function string2array($string, $separator = '|', $valuesKeysSeparator = ':')
|
||||
{
|
||||
/*
|
||||
* Empty string?
|
||||
* Nothing to do
|
||||
*/
|
||||
public static function string2array(
|
||||
string $string,
|
||||
string $separator = '|',
|
||||
string $valuesKeysSeparator = ':'
|
||||
): ?array {
|
||||
// Empty string? Nothing to do
|
||||
if (empty($string)) {
|
||||
return null;
|
||||
}
|
||||
@@ -844,12 +848,12 @@ class Arrays
|
||||
/**
|
||||
* Returns paths of the last elements
|
||||
*
|
||||
* @param array $array The array with elements
|
||||
* @param string $separator (optional) Separator used between elements. Default: ".".
|
||||
* @param string $parentPath (optional) Path of the parent element. Default: "".
|
||||
* @param array|string $stopIfMatchedBy (optional) Patterns of keys or paths that matched will stop the process
|
||||
* of path building and including children of those keys or paths (recursive
|
||||
* will not be used for keys in lower level of given array). Default: "".
|
||||
* @param array $array The array with elements
|
||||
* @param string $separator (optional) Separator used between elements. Default: ".".
|
||||
* @param string $parentPath (optional) Path of the parent element. Default: "".
|
||||
* @param array $stopIfMatchedBy (optional) Patterns of keys or paths when matched will stop the process of path
|
||||
* building and including children of those keys or paths (recursive will not be
|
||||
* used for keys in lower level of given array). Default: [].
|
||||
* @return null|array
|
||||
*
|
||||
* Examples - $stopIfMatchedBy argument:
|
||||
@@ -859,21 +863,18 @@ class Arrays
|
||||
* "\d+",
|
||||
* ];
|
||||
*/
|
||||
public static function getLastElementsPaths(array $array, $separator = '.', $parentPath = '', $stopIfMatchedBy = '')
|
||||
{
|
||||
/*
|
||||
* No elements?
|
||||
* Nothing to do
|
||||
*/
|
||||
public static function getLastElementsPaths(
|
||||
array $array,
|
||||
string $separator = '.',
|
||||
string $parentPath = '',
|
||||
array $stopIfMatchedBy = []
|
||||
): ?array {
|
||||
// No elements? Nothing to do
|
||||
if (empty($array)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!empty($stopIfMatchedBy)) {
|
||||
$stopIfMatchedBy = self::makeArray($stopIfMatchedBy);
|
||||
}
|
||||
|
||||
$paths = [];
|
||||
$result = [];
|
||||
|
||||
foreach ($array as $key => $value) {
|
||||
$path = $key;
|
||||
@@ -908,24 +909,27 @@ class Arrays
|
||||
|
||||
/*
|
||||
* The value is passed to the returned array if:
|
||||
* - the process is stopped, recursive is not used
|
||||
* or
|
||||
* - it's not an array
|
||||
* or
|
||||
* - the process is stopped, recursive is not used
|
||||
* - it's an array, but empty
|
||||
*/
|
||||
if (!$valueIsArray || ($valueIsArray && empty($value)) || $stopRecursion) {
|
||||
$paths[$path] = $value;
|
||||
if ($stopRecursion || !$valueIsArray || self::isEmptyArray($value)) {
|
||||
$result[$path] = $value;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// Let's iterate through the next level, using recursive
|
||||
if ($valueIsArray) {
|
||||
$recursivePaths = self::getLastElementsPaths($value, $separator, $path, $stopIfMatchedBy);
|
||||
$paths += $recursivePaths;
|
||||
$recursivePaths = self::getLastElementsPaths($value, $separator, $path, $stopIfMatchedBy);
|
||||
|
||||
if (null !== $recursivePaths) {
|
||||
$result += $recursivePaths;
|
||||
}
|
||||
}
|
||||
|
||||
return $paths;
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -934,7 +938,7 @@ class Arrays
|
||||
* @param mixed $variable Variable that should be an array
|
||||
* @return array
|
||||
*/
|
||||
public static function makeArray($variable)
|
||||
public static function makeArray($variable): array
|
||||
{
|
||||
if (is_array($variable)) {
|
||||
return $variable;
|
||||
@@ -952,7 +956,7 @@ class Arrays
|
||||
* first level only.
|
||||
* @return bool
|
||||
*/
|
||||
public static function areAllKeysMatchedByPattern(array $array, $pattern, $firstLevelOnly = false)
|
||||
public static function areAllKeysMatchedByPattern(array $array, string $pattern, bool $firstLevelOnly = false): bool
|
||||
{
|
||||
/*
|
||||
* No elements?
|
||||
@@ -1413,9 +1417,9 @@ class Arrays
|
||||
* (default behaviour).
|
||||
* @return array
|
||||
*/
|
||||
public static function arrayDiffRecursive(array $array1, array $array2, $valuesOnly = false)
|
||||
public static function arrayDiffRecursive(array $array1, array $array2, bool $valuesOnly = false): array
|
||||
{
|
||||
$effect = [];
|
||||
$result = [];
|
||||
|
||||
/*
|
||||
* Values should be compared only and both arrays are one-dimensional?
|
||||
@@ -1436,7 +1440,7 @@ class Arrays
|
||||
if ($array2HasKey && is_array($array2[$key])) {
|
||||
$difference = self::arrayDiffRecursive($value, $array2[$key], $valuesOnly);
|
||||
}
|
||||
} elseif (!$array2HasKey || ($array2HasKey && $value !== $array2[$key])) {
|
||||
} elseif (!$array2HasKey || $value !== $array2[$key]) {
|
||||
/*
|
||||
* We are here, because:
|
||||
* a) 2nd array hasn't key from 1st array
|
||||
@@ -1447,7 +1451,7 @@ class Arrays
|
||||
}
|
||||
|
||||
if (null !== $difference) {
|
||||
$effect[] = $difference;
|
||||
$result[] = $difference;
|
||||
}
|
||||
|
||||
// The key exists in 2nd array?
|
||||
@@ -1465,19 +1469,19 @@ class Arrays
|
||||
continue;
|
||||
}
|
||||
|
||||
$effect[$key] = $diff;
|
||||
$result[$key] = $diff;
|
||||
} elseif ($value !== $array2[$key]) {
|
||||
// Value is different than in 2nd array?
|
||||
// OKay, I've got difference
|
||||
$effect[$key] = $value;
|
||||
$result[$key] = $value;
|
||||
}
|
||||
} else {
|
||||
// OKay, I've got difference
|
||||
$effect[$key] = $value;
|
||||
$result[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $effect;
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1513,7 +1517,7 @@ class Arrays
|
||||
* @param int $incrementStep (optional) Value used for incrementation. The step of incrementation.
|
||||
* @return null|array
|
||||
*/
|
||||
public static function incrementIndexes(array $array, $startIndex = null, $incrementStep = 1)
|
||||
public static function incrementIndexes(array $array, ?int $startIndex = null, int $incrementStep = 1): ?array
|
||||
{
|
||||
/*
|
||||
* No elements?
|
||||
@@ -1557,7 +1561,7 @@ class Arrays
|
||||
*/
|
||||
if (!empty($valuesToIncrement)) {
|
||||
foreach ($valuesToIncrement as $oldIndex => $value) {
|
||||
$newIndex = $oldIndex + $incrementStep;
|
||||
$newIndex = (int)$oldIndex + $incrementStep;
|
||||
$array[$newIndex] = $value;
|
||||
}
|
||||
}
|
||||
@@ -1596,7 +1600,7 @@ class Arrays
|
||||
* @param array $array The array to verify
|
||||
* @return null|bool
|
||||
*/
|
||||
public static function isMultiDimensional(array $array)
|
||||
public static function isMultiDimensional(array $array): ?bool
|
||||
{
|
||||
/*
|
||||
* No elements?
|
||||
@@ -1615,7 +1619,7 @@ class Arrays
|
||||
* @param array $array The array to verify
|
||||
* @return int
|
||||
*/
|
||||
public static function getDimensionsCount(array $array)
|
||||
public static function getDimensionsCount(array $array): int
|
||||
{
|
||||
/*
|
||||
* No elements?
|
||||
@@ -1650,7 +1654,7 @@ class Arrays
|
||||
* @param array $values The values to filter
|
||||
* @return null|array
|
||||
*/
|
||||
public static function getNonEmptyValues(array $values)
|
||||
public static function getNonEmptyValues(array $values): ?array
|
||||
{
|
||||
/*
|
||||
* No values?
|
||||
@@ -1660,9 +1664,9 @@ class Arrays
|
||||
return null;
|
||||
}
|
||||
|
||||
return array_filter($values, function ($value) {
|
||||
return array_filter($values, static function ($value): bool {
|
||||
$nonEmptyScalar = is_scalar($value) && '' !== $value;
|
||||
$nonEmptyArray = is_array($value) && !empty($value);
|
||||
$nonEmptyArray = self::isNotEmptyArray($value);
|
||||
|
||||
return $nonEmptyScalar || $nonEmptyArray || is_object($value);
|
||||
});
|
||||
@@ -1675,7 +1679,7 @@ class Arrays
|
||||
* @param string $separator (optional) Separator used to implode the values. Default: ", ".
|
||||
* @return null|string
|
||||
*/
|
||||
public static function getNonEmptyValuesAsString(array $values, $separator = ', ')
|
||||
public static function getNonEmptyValuesAsString(array $values, string $separator = ', '): ?string
|
||||
{
|
||||
/*
|
||||
* No elements?
|
||||
@@ -1698,6 +1702,28 @@ class Arrays
|
||||
return implode($separator, $nonEmpty);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns information if given value is an empty array
|
||||
*
|
||||
* @param mixed $value The value to verify
|
||||
* @return bool
|
||||
*/
|
||||
public static function isEmptyArray($value): bool
|
||||
{
|
||||
return is_array($value) && empty($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns information if given value is non-empty array
|
||||
*
|
||||
* @param mixed $value The value to verify
|
||||
* @return bool
|
||||
*/
|
||||
public static function isNotEmptyArray($value): bool
|
||||
{
|
||||
return is_array($value) && !empty($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns neighbour (next or previous element) for given element
|
||||
*
|
||||
@@ -1706,7 +1732,7 @@ class Arrays
|
||||
* @param bool $next (optional) If is set to true, returns next neighbour. Otherwise - previous.
|
||||
* @return null|mixed
|
||||
*/
|
||||
private static function getNeighbour(array $array, $element, $next = true)
|
||||
private static function getNeighbour(array $array, $element, bool $next = true)
|
||||
{
|
||||
/*
|
||||
* No elements?
|
||||
@@ -1730,7 +1756,7 @@ class Arrays
|
||||
*
|
||||
* Nothing to do
|
||||
*/
|
||||
if ($noPrevious || $noNext || empty($array) || !in_array($element, $array, true)) {
|
||||
if ($noPrevious || $noNext || !in_array($element, $array, true)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user