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;
|
||||
}
|
||||
|
||||
|
||||
@@ -1274,7 +1274,7 @@ class Miscellaneous
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getProjectRootPath()
|
||||
public static function getProjectRootPath(): string
|
||||
{
|
||||
$projectRootPath = '';
|
||||
|
||||
@@ -1298,4 +1298,25 @@ class Miscellaneous
|
||||
|
||||
return $projectRootPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes marginal character (first or last) from given string
|
||||
*
|
||||
* @param string $string The string which should be shortened
|
||||
* @param bool $last (optional) If is set to true, last element is removed (default behaviour). Otherwise -
|
||||
* first.
|
||||
* @return null|string
|
||||
*/
|
||||
public static function removeMarginalCharacter(string $string, bool $last = true): ?string
|
||||
{
|
||||
if (empty($string)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($last) {
|
||||
return substr($string, 0, -1);
|
||||
}
|
||||
|
||||
return substr($string, 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,11 @@ use Meritoo\Common\Exception\Reflection\CannotResolveClassNameException;
|
||||
use Meritoo\Common\Exception\Reflection\MissingChildClassesException;
|
||||
use Meritoo\Common\Exception\Reflection\NotExistingPropertyException;
|
||||
use Meritoo\Common\Exception\Reflection\TooManyChildClassesException;
|
||||
use ReflectionClass;
|
||||
use ReflectionException;
|
||||
use ReflectionMethod;
|
||||
use ReflectionObject;
|
||||
use ReflectionProperty;
|
||||
|
||||
/**
|
||||
* Useful reflection methods
|
||||
@@ -32,18 +37,18 @@ class Reflection
|
||||
* Otherwise - all methods, with inherited methods too.
|
||||
* @return array
|
||||
*/
|
||||
public static function getMethods($class, $withoutInheritance = false)
|
||||
public static function getMethods($class, bool $withoutInheritance = false): array
|
||||
{
|
||||
$effect = [];
|
||||
|
||||
$reflection = new \ReflectionClass($class);
|
||||
$reflection = new ReflectionClass($class);
|
||||
$methods = $reflection->getMethods();
|
||||
|
||||
if (!empty($methods)) {
|
||||
$className = self::getClassName($class);
|
||||
|
||||
foreach ($methods as $method) {
|
||||
if ($method instanceof \ReflectionMethod) {
|
||||
if ($method instanceof ReflectionMethod) {
|
||||
if ($withoutInheritance && $className !== $method->class) {
|
||||
continue;
|
||||
}
|
||||
@@ -62,9 +67,9 @@ class Reflection
|
||||
* @param object|string $class The object or name of object's class
|
||||
* @return array
|
||||
*/
|
||||
public static function getConstants($class)
|
||||
public static function getConstants($class): array
|
||||
{
|
||||
$reflection = new \ReflectionClass($class);
|
||||
$reflection = new ReflectionClass($class);
|
||||
|
||||
return $reflection->getConstants();
|
||||
}
|
||||
@@ -76,7 +81,7 @@ class Reflection
|
||||
* @param object|string $class The object or name of object's class
|
||||
* @return null|int
|
||||
*/
|
||||
public static function getMaxNumberConstant($class)
|
||||
public static function getMaxNumberConstant($class): ?int
|
||||
{
|
||||
$constants = self::getConstants($class);
|
||||
|
||||
@@ -102,9 +107,9 @@ class Reflection
|
||||
* @param string $method Name of the method to find
|
||||
* @return bool
|
||||
*/
|
||||
public static function hasMethod($class, $method)
|
||||
public static function hasMethod($class, string $method): bool
|
||||
{
|
||||
$reflection = new \ReflectionClass($class);
|
||||
$reflection = new ReflectionClass($class);
|
||||
|
||||
return $reflection->hasMethod($method);
|
||||
}
|
||||
@@ -116,9 +121,9 @@ class Reflection
|
||||
* @param string $property Name of the property to find
|
||||
* @return bool
|
||||
*/
|
||||
public static function hasProperty($class, $property)
|
||||
public static function hasProperty($class, string $property): bool
|
||||
{
|
||||
$reflection = new \ReflectionClass($class);
|
||||
$reflection = new ReflectionClass($class);
|
||||
|
||||
return $reflection->hasProperty($property);
|
||||
}
|
||||
@@ -130,9 +135,9 @@ class Reflection
|
||||
* @param string $constant Name of the constant to find
|
||||
* @return bool
|
||||
*/
|
||||
public static function hasConstant($class, $constant)
|
||||
public static function hasConstant($class, string $constant): bool
|
||||
{
|
||||
$reflection = new \ReflectionClass($class);
|
||||
$reflection = new ReflectionClass($class);
|
||||
|
||||
return $reflection->hasConstant($constant);
|
||||
}
|
||||
@@ -144,9 +149,9 @@ class Reflection
|
||||
* @param string $constant Name of the constant that contains a value
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getConstantValue($class, $constant)
|
||||
public static function getConstantValue($class, string $constant)
|
||||
{
|
||||
$reflection = new \ReflectionClass($class);
|
||||
$reflection = new ReflectionClass($class);
|
||||
|
||||
if (self::hasConstant($class, $constant)) {
|
||||
return $reflection->getConstant($constant);
|
||||
@@ -165,7 +170,7 @@ class Reflection
|
||||
* property. Otherwise - not.
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getPropertyValue($source, $property, $force = false)
|
||||
public static function getPropertyValue($source, string $property, bool $force = false)
|
||||
{
|
||||
if (Regex::contains($property, '.')) {
|
||||
return self::getPropertyValueByPropertiesChain($source, $property, $force);
|
||||
@@ -209,7 +214,7 @@ class Reflection
|
||||
* object does not have property. Otherwise - not.
|
||||
* @return array
|
||||
*/
|
||||
public static function getPropertyValues($objects, $property, $force = false)
|
||||
public static function getPropertyValues($objects, string $property, bool $force = false): array
|
||||
{
|
||||
/*
|
||||
* No objects?
|
||||
@@ -245,7 +250,7 @@ class Reflection
|
||||
* not, full name of class is returned, with namespace.
|
||||
* @return null|string
|
||||
*/
|
||||
public static function getClassName($source, $withoutNamespace = false)
|
||||
public static function getClassName($source, bool $withoutNamespace = false): ?string
|
||||
{
|
||||
/*
|
||||
* First argument is not proper source of class?
|
||||
@@ -303,7 +308,7 @@ class Reflection
|
||||
* @param array|object|string $source An array of objects, namespaces, object or namespace
|
||||
* @return string
|
||||
*/
|
||||
public static function getClassNamespace($source)
|
||||
public static function getClassNamespace($source): string
|
||||
{
|
||||
$fullClassName = self::getClassName($source);
|
||||
|
||||
@@ -327,7 +332,7 @@ class Reflection
|
||||
* @param string $interface The interface that should be implemented
|
||||
* @return bool
|
||||
*/
|
||||
public static function isInterfaceImplemented($source, $interface)
|
||||
public static function isInterfaceImplemented($source, string $interface): bool
|
||||
{
|
||||
$className = self::getClassName($source);
|
||||
$interfaces = class_implements($className);
|
||||
@@ -342,7 +347,7 @@ class Reflection
|
||||
* @param array|object|string $parentClass The parent class. An array of objects, namespaces, object or namespace.
|
||||
* @return bool
|
||||
*/
|
||||
public static function isChildOfClass($childClass, $parentClass)
|
||||
public static function isChildOfClass($childClass, $parentClass): bool
|
||||
{
|
||||
$childClassName = self::getClassName($childClass);
|
||||
$parentClassName = self::getClassName($parentClass);
|
||||
@@ -364,18 +369,18 @@ class Reflection
|
||||
* constants. By default all properties are returned.
|
||||
* @param bool $includeParents (optional) If is set to true, properties of parent classes are
|
||||
* included (recursively). Otherwise - not.
|
||||
* @return \ReflectionProperty[]
|
||||
* @return ReflectionProperty[]
|
||||
*/
|
||||
public static function getProperties($source, $filter = null, $includeParents = false): array
|
||||
public static function getProperties($source, int $filter = null, bool $includeParents = false): array
|
||||
{
|
||||
$className = self::getClassName($source);
|
||||
$reflection = new \ReflectionClass($className);
|
||||
$reflection = new ReflectionClass($className);
|
||||
|
||||
if (null === $filter) {
|
||||
$filter = \ReflectionProperty::IS_PRIVATE
|
||||
+ \ReflectionProperty::IS_PROTECTED
|
||||
+ \ReflectionProperty::IS_PUBLIC
|
||||
+ \ReflectionProperty::IS_STATIC;
|
||||
$filter = ReflectionProperty::IS_PRIVATE
|
||||
+ ReflectionProperty::IS_PROTECTED
|
||||
+ ReflectionProperty::IS_PUBLIC
|
||||
+ ReflectionProperty::IS_STATIC;
|
||||
}
|
||||
|
||||
$properties = $reflection->getProperties($filter);
|
||||
@@ -397,12 +402,12 @@ class Reflection
|
||||
* Returns a parent class or false if there is no parent class
|
||||
*
|
||||
* @param array|object|string $source An array of objects, namespaces, object or namespace
|
||||
* @return bool|\ReflectionClass
|
||||
* @return false|ReflectionClass
|
||||
*/
|
||||
public static function getParentClass($source)
|
||||
{
|
||||
$className = self::getClassName($source);
|
||||
$reflection = new \ReflectionClass($className);
|
||||
$reflection = new ReflectionClass($className);
|
||||
|
||||
return $reflection->getParentClass();
|
||||
}
|
||||
@@ -416,7 +421,7 @@ class Reflection
|
||||
* @throws CannotResolveClassNameException
|
||||
* @return null|array
|
||||
*/
|
||||
public static function getChildClasses($class)
|
||||
public static function getChildClasses($class): ?array
|
||||
{
|
||||
$allClasses = get_declared_classes();
|
||||
|
||||
@@ -500,15 +505,15 @@ class Reflection
|
||||
* @param string $property Name of the property
|
||||
* @param int $filter (optional) Filter of properties. Uses \ReflectionProperty class constants.
|
||||
* By default all properties are allowed / processed.
|
||||
* @return null|\ReflectionProperty
|
||||
* @return null|ReflectionProperty
|
||||
*/
|
||||
public static function getProperty($class, $property, $filter = null)
|
||||
public static function getProperty($class, string $property, int $filter = null): ?ReflectionProperty
|
||||
{
|
||||
$className = self::getClassName($class);
|
||||
$properties = self::getProperties($className, $filter);
|
||||
|
||||
if (!empty($properties)) {
|
||||
/** @var \ReflectionProperty $reflectionProperty */
|
||||
/** @var ReflectionProperty $reflectionProperty */
|
||||
foreach ($properties as $reflectionProperty) {
|
||||
if ($reflectionProperty->getName() === $property) {
|
||||
return $reflectionProperty;
|
||||
@@ -529,7 +534,7 @@ class Reflection
|
||||
* @throws CannotResolveClassNameException
|
||||
* @return null|bool
|
||||
*/
|
||||
public static function usesTrait($class, $trait, $verifyParents = false)
|
||||
public static function usesTrait($class, $trait, bool $verifyParents = false): ?bool
|
||||
{
|
||||
$className = self::getClassName($class);
|
||||
$traitName = self::getClassName($trait);
|
||||
@@ -544,7 +549,7 @@ class Reflection
|
||||
throw new CannotResolveClassNameException($class, false);
|
||||
}
|
||||
|
||||
$reflection = new \ReflectionClass($className);
|
||||
$reflection = new ReflectionClass($className);
|
||||
$traitsNames = $reflection->getTraitNames();
|
||||
|
||||
$uses = in_array($traitName, $traitsNames, true);
|
||||
@@ -567,10 +572,10 @@ class Reflection
|
||||
* @param array|object|string $class An array of objects, namespaces, object or namespace
|
||||
* @return null|string
|
||||
*/
|
||||
public static function getParentClassName($class)
|
||||
public static function getParentClassName($class): ?string
|
||||
{
|
||||
$className = self::getClassName($class);
|
||||
$reflection = new \ReflectionClass($className);
|
||||
$reflection = new ReflectionClass($className);
|
||||
$parentClass = $reflection->getParentClass();
|
||||
|
||||
if (null === $parentClass || false === $parentClass) {
|
||||
@@ -588,7 +593,7 @@ class Reflection
|
||||
* @param mixed $value Value of the property
|
||||
* @throws NotExistingPropertyException
|
||||
*/
|
||||
public static function setPropertyValue($object, $property, $value)
|
||||
public static function setPropertyValue($object, string $property, $value): void
|
||||
{
|
||||
$reflectionProperty = self::getProperty($object, $property);
|
||||
|
||||
@@ -616,7 +621,7 @@ class Reflection
|
||||
* @param mixed $object Object that should contains given property
|
||||
* @param array $propertiesValues Key-value pairs, where key - name of the property, value - value of the property
|
||||
*/
|
||||
public static function setPropertiesValues($object, array $propertiesValues)
|
||||
public static function setPropertiesValues($object, array $propertiesValues): void
|
||||
{
|
||||
/*
|
||||
* No properties?
|
||||
@@ -637,7 +642,7 @@ class Reflection
|
||||
* @param string $class Class to verify
|
||||
* @return string
|
||||
*/
|
||||
private static function getRealClass($class): string
|
||||
private static function getRealClass(string $class): string
|
||||
{
|
||||
if (false === $pos = strrpos($class, '\\' . Proxy::MARKER . '\\')) {
|
||||
return $class;
|
||||
@@ -650,15 +655,15 @@ class Reflection
|
||||
* Returns value of given property using the property represented by reflection.
|
||||
* If value cannot be fetched, makes the property accessible temporarily.
|
||||
*
|
||||
* @param mixed $object Object that should contains given property
|
||||
* @param string $property Name of the property that contains a value
|
||||
* @param null|\ReflectionProperty $reflectionProperty (optional) Property represented by reflection
|
||||
* @param mixed $object Object that should contains given property
|
||||
* @param string $property Name of the property that contains a value
|
||||
* @param null|ReflectionProperty $reflectionProperty (optional) Property represented by reflection
|
||||
* @return mixed
|
||||
*/
|
||||
private static function getPropertyValueByReflectionProperty(
|
||||
$object,
|
||||
string $property,
|
||||
?\ReflectionProperty $reflectionProperty = null
|
||||
?ReflectionProperty $reflectionProperty = null
|
||||
) {
|
||||
$value = null;
|
||||
$valueFound = false;
|
||||
@@ -666,12 +671,12 @@ class Reflection
|
||||
|
||||
try {
|
||||
if (null === $reflectionProperty) {
|
||||
$reflectionProperty = new \ReflectionProperty($className, $property);
|
||||
$reflectionProperty = new ReflectionProperty($className, $property);
|
||||
}
|
||||
|
||||
$value = $reflectionProperty->getValue($object);
|
||||
$valueFound = true;
|
||||
} catch (\ReflectionException $exception) {
|
||||
} catch (ReflectionException $exception) {
|
||||
}
|
||||
|
||||
if (null !== $reflectionProperty) {
|
||||
@@ -705,7 +710,7 @@ class Reflection
|
||||
$value = null;
|
||||
$valueFound = false;
|
||||
|
||||
$reflectionObject = new \ReflectionObject($source);
|
||||
$reflectionObject = new ReflectionObject($source);
|
||||
$property = Inflector::classify($property);
|
||||
|
||||
$gettersPrefixes = [
|
||||
@@ -718,7 +723,7 @@ class Reflection
|
||||
$getter = sprintf('%s%s', $prefix, $property);
|
||||
|
||||
if ($reflectionObject->hasMethod($getter)) {
|
||||
$method = new \ReflectionMethod($source, $getter);
|
||||
$method = new ReflectionMethod($source, $getter);
|
||||
|
||||
/*
|
||||
* Getter is not accessible publicly?
|
||||
@@ -791,7 +796,7 @@ class Reflection
|
||||
* property. Otherwise - not.
|
||||
* @return mixed
|
||||
*/
|
||||
private static function getPropertyValueByPropertiesChain($source, $property, $force)
|
||||
private static function getPropertyValueByPropertiesChain($source, string $property, bool $force)
|
||||
{
|
||||
$exploded = explode('.', $property);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user