Skip to content

Commit

Permalink
PHP 8.1 minimum version prep
Browse files Browse the repository at this point in the history
  • Loading branch information
rotimi committed Mar 14, 2024
1 parent 56681a5 commit 49c6e95
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 84 deletions.
62 changes: 25 additions & 37 deletions src/CollectionInterfaceImplementationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 1 addition & 17 deletions src/SpecificObjectsCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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;
Expand Down
31 changes: 2 additions & 29 deletions src/helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions tests/GenericCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ];
Expand Down
2 changes: 1 addition & 1 deletion tests/SpecificObjectsCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 49c6e95

Please sign in to comment.