diff --git a/src/CollectionInterfaceImplementationTrait.php b/src/CollectionInterfaceImplementationTrait.php index 1f13718..e864609 100644 --- a/src/CollectionInterfaceImplementationTrait.php +++ b/src/CollectionInterfaceImplementationTrait.php @@ -1717,7 +1717,6 @@ public function unionMeWith(array $items): CollectionInterface * @see \VersatileCollections\CollectionInterface::column() * * @psalm-suppress MoreSpecificReturnType - * @psalm-suppress RedundantCondition */ public function column(int|string $column_key, int|string|null $index_key=null): GenericCollection { @@ -1776,48 +1775,37 @@ public function column(int|string $column_key, int|string|null $index_key=null): . " Collection Items: ". var_to_string($this->versatile_collections_items); throw new RuntimeException($msg); - } else if( $index_key === null ) { - - $column_2_return[] = $item[$column_key]; + } else { + // At this point we know that $item has a key or property named after the value in $column_key + // At this point we know that $item has a key or property named after the value in $index_key, if $index_key !== null - } else if( - $index_key !== null - && - ( - ( \is_array($item) && \array_key_exists($index_key, $item) ) - || - ( $item instanceof ArrayAccess && isset($item[$index_key]) ) - ) - ) { - if( - !\is_string($item[$index_key]) - && !\is_int($item[$index_key]) - ){ - $function = __FUNCTION__; - $class = $this::class; - $item_type = Utils::gettype($item[$index_key]); + if( $index_key === null ) { - $msg = "Error [{$class}::{$function}(...)]:" - . " \$collection['{$coll_key}']['{$index_key}'] of type `$item_type`" - . " has a non-string and non-int value of `". var_to_string($item[$index_key])."`" - . " which cannot be used as a key in the collection to be returned by this method." .PHP_EOL - . " Collection Items: ". var_to_string($this->versatile_collections_items).PHP_EOL .PHP_EOL; - throw new RuntimeException($msg); - } + $column_2_return[] = $item[$column_key]; - $column_2_return[$item[$index_key]] = $item[$column_key]; + } else { + // For sure $index_key !== null + // and we already know that $item has a key or property + // named after the value in $index_key since $index_key !== null - } else { + if( + !\is_string($item[$index_key]) + && !\is_int($item[$index_key]) + ) { + $function = __FUNCTION__; + $class = $this::class; + $item_type = Utils::gettype($item[$index_key]); - $function = __FUNCTION__; - $class = $this::class; - $item_type = Utils::gettype($item); + $msg = "Error [{$class}::{$function}(...)]:" + . " \$collection['{$coll_key}']['{$index_key}'] of type `$item_type`" + . " has a non-string and non-int value of `". var_to_string($item[$index_key])."`" + . " which cannot be used as a key in the collection to be returned by this method." .PHP_EOL + . " Collection Items: ". var_to_string($this->versatile_collections_items).PHP_EOL .PHP_EOL; + throw new RuntimeException($msg); + } - $msg = "Error [{$class}::{$function}(...)]:" - . " Error occured while accessing an item of type `$item_type` with the specified index key `$index_key`" - . " and specified column key `$column_key` with this key `$coll_key` in the collection." . PHP_EOL - . " Collection Items: ". var_to_string($this->versatile_collections_items).PHP_EOL .PHP_EOL; - throw new RuntimeException($msg); + $column_2_return[$item[$index_key]] = $item[$column_key]; + } } } else { // \is_object($item) === true && !($item instanceof ArrayAccess) diff --git a/src/SpecificObjectsCollection.php b/src/SpecificObjectsCollection.php index c363ef1..b7d8bcb 100644 --- a/src/SpecificObjectsCollection.php +++ b/src/SpecificObjectsCollection.php @@ -34,18 +34,7 @@ final class SpecificObjectsCollection extends ObjectsCollection */ protected function __construct(object ...$objects) { - if( \is_null($this->class_name) ) { - - // we don't have a specific class, allow all objects - $this->versatile_collections_items = $objects; - - } else { - - // we have a specific class, allow only instances of that class - // use the strictly typed constructor instead to enforce the - // strict typing - static::strictlyTypedCollectionTrait__construct(...$objects); - } + parent::__construct(...$objects); } /** @@ -69,11 +58,6 @@ protected function __construct(object ...$objects) */ public static function makeNewForSpecifiedClassName(?string $class_name=null, iterable $items =[], bool $preserve_keys=true): StrictlyTypedCollectionInterface { - if( $class_name === null ) { - - return static::makeNew($items, $preserve_keys); // collection that stores any type of object - } - // Class was specified, create collection for only instances of the specified class $new_collection = static::makeNew(); // make an empty collection first $new_collection->class_name = $class_name; diff --git a/src/helper-functions.php b/src/helper-functions.php index 0d85be1..1af29c1 100644 --- a/src/helper-functions.php +++ b/src/helper-functions.php @@ -2,15 +2,13 @@ declare(strict_types=1); namespace VersatileCollections { - use Error; - use Exception; + use Throwable; use InvalidArgumentException; use LengthException; use ReflectionClass; use ReflectionException; use RuntimeException; use stdClass; - use TypeError; /** * A robust way of retrieving the value of a specified property in @@ -137,7 +135,6 @@ function random_array_key(array $array): string|int|null throw new LengthException($msg); } - $error_occurred = false; $keys = \array_keys($array); $random_key = null; @@ -148,31 +145,7 @@ function random_array_key(array $array): string|int|null $random_index = \random_int( $min, $max ); $random_key = $keys[$random_index]; - } catch ( TypeError) { - - // random_int: If invalid parameters are given, a TypeError will be thrown. - // This is okay, so long as `Error` is caught before `Exception`. - // Probably will never occur since $min and $max above will always be ints. - $error_occurred = true; - - } catch ( Error) { - - // random_int: If max is less than min, an Error will be thrown. - // This is required, if you do not need to do anything just rethrow. - // Probably will never occur since $min and $max above will always have $min < $max. - $error_occurred = true; - - } catch ( Exception) { - - // random_int: If an appropriate source of randomness cannot be found, an Exception will be thrown. - // This is optional and maybe omitted if you do not want to handle errors - // during generation. - // Hard to consistently test this since it's an internal - // random number generator specific logic error. - $error_occurred = true; - } - - if( $error_occurred ) { + } catch ( Throwable) { // fallback to array_rand since an error / exception occurred // while trying to use random_int diff --git a/tests/GenericCollectionTest.php b/tests/GenericCollectionTest.php index b9856a1..3b3c518 100644 --- a/tests/GenericCollectionTest.php +++ b/tests/GenericCollectionTest.php @@ -2708,6 +2708,12 @@ public function testThatSortDescByKeyWorksAsExpected() { public function testThatSortByMultipleFieldsWorksAsExpected() { + $collection = new \VersatileCollections\GenericCollection(); + $sort_param = new \VersatileCollections\MultiSortParameters('volume', SORT_ASC, SORT_NUMERIC); + $sort_param2 = new \VersatileCollections\MultiSortParameters('edition', SORT_DESC, SORT_NUMERIC); + $sorted_collection_asc_desc = $collection->sortByMultipleFields($sort_param, $sort_param2); + self::assertEquals([], $collection->toArray()); + //////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////// // Collection of Arrays @@ -3247,6 +3253,13 @@ public function testThatSortMeDescByKeyWorksAsExpected() { public function testThatSortMeByMultipleFieldsWorksAsExpected() { + $collection = new \VersatileCollections\GenericCollection(); + $sort_param = new \VersatileCollections\MultiSortParameters('volume', SORT_ASC, SORT_NUMERIC); + $sort_param2 = new \VersatileCollections\MultiSortParameters('edition', SORT_DESC, SORT_NUMERIC); + $sorted_collection_asc_desc = $collection->sortMeByMultipleFields($sort_param, $sort_param2); + self::assertEquals([], $sorted_collection_asc_desc->toArray()); + + $data = []; $data[0] = [ 'volume' => 67, 'edition' => 2 ]; $data[1] = [ 'volume' => 86, 'edition' => 2 ]; diff --git a/tests/SpecificObjectsCollectionTest.php b/tests/SpecificObjectsCollectionTest.php index 29a6ac2..a039bff 100644 --- a/tests/SpecificObjectsCollectionTest.php +++ b/tests/SpecificObjectsCollectionTest.php @@ -272,7 +272,7 @@ public function testThatCheckTypeWorksAsExpected() { $this->assertTrue($collection->checkType($item6)); } - public function testThatGetTypeWorksAsExpected() { + public function testThatGetTypesWorksAsExpected() { // Create a collection that stores only instances of \ArrayObject $collection = \VersatileCollections\SpecificObjectsCollection::makeNewForSpecifiedClassName(