Skip to content

Commit

Permalink
Fix #99: Finalize classes ArrayHelper and ArraySorter
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Sep 7, 2021
1 parent 35904e5 commit 09b6a17
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 48 deletions.
1 change: 0 additions & 1 deletion .styleci.yml
Expand Up @@ -25,7 +25,6 @@ enabled:
- combine_nested_dirname
- declare_strict_types
- dir_constant
- final_static_access
- fully_qualified_strict_types
- function_to_constant
- hash_to_slash_comment
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
@@ -1,8 +1,9 @@
# Yii Arrays Change Log

## 1.0.2 under development
## 2.0.0 under development

- New #91: Add method `ArrayHelper::group()` that groups the array according to a specified key (sagittaracc)
- Chg #99: Finalize classes `ArrayHelper` and `ArraySorter` (vjik)

## 1.0.1 February 10, 2021

Expand Down
74 changes: 37 additions & 37 deletions src/ArrayHelper.php
Expand Up @@ -25,7 +25,7 @@
* @psalm-type ArrayKey = float|int|string|list<float|int|string>
* @psalm-type ArrayPath = float|int|string|list<float|int|string|list<float|int|string>>
*/
class ArrayHelper
final class ArrayHelper
{
/**
* Converts an object or an array of objects into an array.
Expand Down Expand Up @@ -77,7 +77,7 @@ public static function toArray($object, array $properties = [], bool $recursive
/** @var mixed $value */
foreach ($object as $key => $value) {
if (is_array($value) || is_object($value)) {
$object[$key] = static::toArray($value, $properties);
$object[$key] = self::toArray($value, $properties);
}
}
}
Expand All @@ -100,11 +100,11 @@ public static function toArray($object, array $properties = [], bool $recursive
$result[$name] = $object->$name;
} else {
/** @var mixed */
$result[$key] = static::getValue($object, $name);
$result[$key] = self::getValue($object, $name);
}
}

return $recursive ? static::toArray($result, $properties) : $result;
return $recursive ? self::toArray($result, $properties) : $result;
}
}
if ($object instanceof ArrayableInterface) {
Expand All @@ -121,7 +121,7 @@ public static function toArray($object, array $properties = [], bool $recursive
}
}

return $recursive ? static::toArray($result, $properties) : $result;
return $recursive ? self::toArray($result, $properties) : $result;
}

return [$object];
Expand Down Expand Up @@ -219,12 +219,12 @@ public static function getValue($array, $key, $default = null)
$lastKey = array_pop($key);
foreach ($key as $keyPart) {
/** @var mixed */
$array = static::getRootValue($array, $keyPart, $default);
$array = self::getRootValue($array, $keyPart, $default);
}
return static::getRootValue($array, $lastKey, $default);
return self::getRootValue($array, $lastKey, $default);
}

return static::getRootValue($array, $key, $default);
return self::getRootValue($array, $key, $default);
}

/**
Expand All @@ -238,7 +238,7 @@ public static function getValue($array, $key, $default = null)
private static function getRootValue($array, $key, $default)
{
if (is_array($array)) {
$key = static::normalizeArrayKey($key);
$key = self::normalizeArrayKey($key);
return array_key_exists($key, $array) ? $array[$key] : $default;
}

Expand Down Expand Up @@ -292,9 +292,9 @@ private static function getRootValue($array, $key, $default)
*/
public static function getValueByPath($array, $path, $default = null, string $delimiter = '.')
{
return static::getValue(
return self::getValue(
$array,
$path instanceof Closure ? $path : static::parsePath($path, $delimiter),
$path instanceof Closure ? $path : self::parsePath($path, $delimiter),
$default
);
}
Expand Down Expand Up @@ -347,7 +347,7 @@ public static function setValue(array &$array, $key, $value): void
$keys = is_array($key) ? $key : [$key];

while (count($keys) > 1) {
$k = static::normalizeArrayKey(array_shift($keys));
$k = self::normalizeArrayKey(array_shift($keys));
if (!isset($array[$k])) {
$array[$k] = [];
}
Expand All @@ -358,7 +358,7 @@ public static function setValue(array &$array, $key, $value): void
}

/** @var mixed */
$array[static::normalizeArrayKey(array_shift($keys))] = $value;
$array[self::normalizeArrayKey(array_shift($keys))] = $value;
}

/**
Expand Down Expand Up @@ -419,7 +419,7 @@ public static function setValue(array &$array, $key, $value): void
*/
public static function setValueByPath(array &$array, $path, $value, string $delimiter = '.'): void
{
static::setValue($array, $path === null ? null : static::parsePath($path, $delimiter), $value);
self::setValue($array, $path === null ? null : self::parsePath($path, $delimiter), $value);
}

/**
Expand All @@ -444,7 +444,7 @@ private static function parsePath($path, string $delimiter)
foreach ($path as $key) {
if (is_string($key) || is_array($key)) {
/** @var list<float|int|string> $parsedPath */
$parsedPath = static::parsePath($key, $delimiter);
$parsedPath = self::parsePath($key, $delimiter);
$newPath = array_merge($newPath, $parsedPath);
} else {
$newPath[] = $key;
Expand Down Expand Up @@ -484,14 +484,14 @@ public static function remove(array &$array, $key, $default = null)
$keys = is_array($key) ? $key : [$key];

while (count($keys) > 1) {
$key = static::normalizeArrayKey(array_shift($keys));
$key = self::normalizeArrayKey(array_shift($keys));
if (!isset($array[$key]) || !is_array($array[$key])) {
return $default;
}
$array = &$array[$key];
}

$key = static::normalizeArrayKey(array_shift($keys));
$key = self::normalizeArrayKey(array_shift($keys));
if (array_key_exists($key, $array)) {
/** @var mixed */
$value = $array[$key];
Expand Down Expand Up @@ -531,7 +531,7 @@ public static function remove(array &$array, $key, $default = null)
*/
public static function removeByPath(array &$array, $path, $default = null, string $delimiter = '.')
{
return static::remove($array, static::parsePath($path, $delimiter), $default);
return self::remove($array, self::parsePath($path, $delimiter), $default);
}

/**
Expand Down Expand Up @@ -688,8 +688,8 @@ public static function index(array $array, $key, $groups = []): array
$lastArray = &$result;

foreach ($groups as $group) {
$value = static::normalizeArrayKey(
static::getValue($element, $group)
$value = self::normalizeArrayKey(
self::getValue($element, $group)
);
if (!array_key_exists($value, $lastArray)) {
$lastArray[$value] = [];
Expand All @@ -703,9 +703,9 @@ public static function index(array $array, $key, $groups = []): array
}
} else {
/** @var mixed */
$value = static::getValue($element, $key);
$value = self::getValue($element, $key);
if ($value !== null) {
$lastArray[static::normalizeArrayKey($value)] = $element;
$lastArray[self::normalizeArrayKey($value)] = $element;
}
}
unset($lastArray);
Expand All @@ -728,7 +728,7 @@ public static function index(array $array, $key, $groups = []): array
*/
public static function group(array $array, $groups): array
{
return static::index($array, null, $groups);
return self::index($array, null, $groups);
}

/**
Expand Down Expand Up @@ -764,12 +764,12 @@ public static function getColumn(array $array, $name, bool $keepKeys = true): ar
if ($keepKeys) {
foreach ($array as $k => $element) {
/** @var mixed */
$result[$k] = static::getValue($element, $name);
$result[$k] = self::getValue($element, $name);
}
} else {
foreach ($array as $element) {
/** @var mixed */
$result[] = static::getValue($element, $name);
$result[] = self::getValue($element, $name);
}
}

Expand Down Expand Up @@ -826,9 +826,9 @@ public static function map(array $array, $from, $to, $group = null): array
if ($from instanceof Closure || $to instanceof Closure) {
$result = [];
foreach ($array as $element) {
$key = (string)static::getValue($element, $from);
$key = (string)self::getValue($element, $from);
/** @var mixed */
$result[$key] = static::getValue($element, $to);
$result[$key] = self::getValue($element, $to);
}

return $result;
Expand All @@ -839,10 +839,10 @@ public static function map(array $array, $from, $to, $group = null): array

$result = [];
foreach ($array as $element) {
$groupKey = (string)static::getValue($element, $group);
$key = (string)static::getValue($element, $from);
$groupKey = (string)self::getValue($element, $group);
$key = (string)self::getValue($element, $from);
/** @var mixed */
$result[$groupKey][$key] = static::getValue($element, $to);
$result[$groupKey][$key] = self::getValue($element, $to);
}

return $result;
Expand All @@ -865,12 +865,12 @@ public static function keyExists(array $array, $key, bool $caseSensitive = true)
{
if (is_array($key)) {
if (count($key) === 1) {
return static::rootKeyExists($array, end($key), $caseSensitive);
return self::rootKeyExists($array, end($key), $caseSensitive);
}

foreach (self::getExistsKeys($array, array_shift($key), $caseSensitive) as $existKey) {
/** @var mixed */
$array = static::getRootValue($array, $existKey, null);
$array = self::getRootValue($array, $existKey, null);
if (is_array($array) && self::keyExists($array, $key, $caseSensitive)) {
return true;
}
Expand All @@ -879,7 +879,7 @@ public static function keyExists(array $array, $key, bool $caseSensitive = true)
return false;
}

return static::rootKeyExists($array, $key, $caseSensitive);
return self::rootKeyExists($array, $key, $caseSensitive);
}

/**
Expand Down Expand Up @@ -951,7 +951,7 @@ public static function pathExists(
bool $caseSensitive = true,
string $delimiter = '.'
): bool {
return static::keyExists($array, static::parsePath($path, $delimiter), $caseSensitive);
return self::keyExists($array, self::parsePath($path, $delimiter), $caseSensitive);
}

/**
Expand Down Expand Up @@ -982,7 +982,7 @@ public static function htmlEncode(array $data, bool $valuesOnly = true, string $
if (is_string($value)) {
$d[$key] = htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, $encoding, true);
} elseif (is_array($value)) {
$d[$key] = static::htmlEncode($value, $valuesOnly, $encoding);
$d[$key] = self::htmlEncode($value, $valuesOnly, $encoding);
} else {
/** @var mixed */
$d[$key] = $value;
Expand Down Expand Up @@ -1019,7 +1019,7 @@ public static function htmlDecode(array $data, bool $valuesOnly = true): array
if (is_string($value)) {
$decoded[$key] = htmlspecialchars_decode($value, ENT_QUOTES);
} elseif (is_array($value)) {
$decoded[$key] = static::htmlDecode($value);
$decoded[$key] = self::htmlDecode($value);
} else {
/** @var mixed */
$decoded[$key] = $value;
Expand Down Expand Up @@ -1152,7 +1152,7 @@ public static function isSubset(iterable $needles, iterable $haystack, bool $str
{
/** @psalm-var mixed $needle */
foreach ($needles as $needle) {
if (!static::isIn($needle, $haystack, $strict)) {
if (!self::isIn($needle, $haystack, $strict)) {
return false;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/ArraySorter.php
Expand Up @@ -9,7 +9,7 @@

use function is_array;

class ArraySorter
final class ArraySorter
{
/**
* Sorts an array of objects or arrays (with the same structure) by one or several keys.
Expand Down Expand Up @@ -53,7 +53,7 @@ class ArraySorter
*/
public static function multisort(array &$array, $key, $direction = SORT_ASC, $sortFlag = SORT_REGULAR): void
{
$keys = static::getKeys($array, $key);
$keys = self::getKeys($array, $key);
if (empty($keys)) {
return;
}
Expand All @@ -71,7 +71,7 @@ public static function multisort(array &$array, $key, $direction = SORT_ASC, $so
throw new InvalidArgumentException('The length of $sortFlag parameter must be the same as that of $keys.');
}

$_args = static::getArguments($array, $keys, $direction, $sortFlag);
$_args = self::getArguments($array, $keys, $direction, $sortFlag);

$_args[] = &$array;
array_multisort(...$_args);
Expand Down
2 changes: 1 addition & 1 deletion tests/ArrayHelper/RemoveTest.php
Expand Up @@ -7,7 +7,7 @@
use PHPUnit\Framework\TestCase;
use Yiisoft\Arrays\ArrayHelper;

class RemoveTest extends TestCase
final class RemoveTest extends TestCase
{
public function removeData(): array
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Objects/ArrayAccessObject.php
Expand Up @@ -9,7 +9,7 @@
use IteratorAggregate;
use Yiisoft\Arrays\ArrayAccessTrait;

class ArrayAccessObject implements IteratorAggregate, ArrayAccess, Countable
final class ArrayAccessObject implements IteratorAggregate, ArrayAccess, Countable
{
use ArrayAccessTrait;

Expand Down
2 changes: 1 addition & 1 deletion tests/Objects/HardArrayableObject.php
Expand Up @@ -7,7 +7,7 @@
use Yiisoft\Arrays\ArrayableInterface;
use Yiisoft\Arrays\ArrayableTrait;

class HardArrayableObject implements ArrayableInterface
final class HardArrayableObject implements ArrayableInterface
{
use ArrayableTrait;

Expand Down
2 changes: 1 addition & 1 deletion tests/Objects/NestedStaticObject.php
Expand Up @@ -4,7 +4,7 @@

namespace Yiisoft\Arrays\Tests\Objects;

class NestedStaticObject
final class NestedStaticObject
{
public static int $b = 2;
}
2 changes: 1 addition & 1 deletion tests/Objects/SimpleArrayableObject.php
Expand Up @@ -7,7 +7,7 @@
use Yiisoft\Arrays\ArrayableInterface;
use Yiisoft\Arrays\ArrayableTrait;

class SimpleArrayableObject implements ArrayableInterface
final class SimpleArrayableObject implements ArrayableInterface
{
use ArrayableTrait;

Expand Down
2 changes: 1 addition & 1 deletion tests/Objects/StaticObject.php
Expand Up @@ -4,7 +4,7 @@

namespace Yiisoft\Arrays\Tests\Objects;

class StaticObject
final class StaticObject
{
public static int $a = 1;
public NestedStaticObject $nested;
Expand Down

0 comments on commit 09b6a17

Please sign in to comment.