Skip to content

Commit

Permalink
https://github.com/rotexsoft/versatile-collections/issues/12.
Browse files Browse the repository at this point in the history
Enabling strict typing and proper type hints for php 7.2+
  • Loading branch information
rotexdegba committed Nov 10, 2019
1 parent db2ae4f commit d16c6c8
Show file tree
Hide file tree
Showing 33 changed files with 212 additions and 154 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
New PHP 7.0 - 7.2 Features for Versatile Collections PHP 7.2+
===============================================================
- https://gist.github.com/rotexdegba/2e04245570810ff05f571ab95cc4c728 new stuff in PHP 7.X
- Bump PHPunit to version 8
- Use Scalar type definitions in method parameters and return signature and get rid of code formerly used to check types (and the exceptions thrown when types are wrong)
- Update unit test expecting those exceptions to no longer do so where appropriate they should expect \TypeError in those cases I guess
- Add declare strict types at the top of the different classes
- The spaceship operator is used for comparing two expressions. It returns -1, 0 or 1 when $a is respectively less than, equal to, or greater than $b.
- use where reasonable
- Use anonymous classes where reasonable
Expand Down
1 change: 1 addition & 0 deletions src/ArraysCollection.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace VersatileCollections;

/**
Expand Down
93 changes: 47 additions & 46 deletions src/CallablesCollection.php
Original file line number Diff line number Diff line change
@@ -1,46 +1,47 @@
<?php
namespace VersatileCollections;

/**
* Description of CallablesCollection
*
* Below is a list of acceptable value(s), that could be comma separated,
* for the @used-for tag in phpdoc blocks for public methods in this class:
*
* - accessing-or-extracting-keys-or-items
* - adding-items
* - adding-methods-at-runtime
* - checking-keys-presence
* - checking-items-presence
* - creating-new-collections
* - deleting-items
* - finding-or-searching-for-items
* - getting-collection-meta-data
* - iteration
* - mathematical-operations
* - modifying-keys
* - modifying-items
* - ordering-or-sorting-items
* - other-operations
*
* @author aadegbam
*/
class CallablesCollection implements \VersatileCollections\StrictlyTypedCollectionInterface {

use StrictlyTypedCollectionInterfaceImplementationTrait;

public function __construct(callable ...$callables) {

$this->versatile_collections_items = $callables;
}

public function checkType($item) {

return is_callable($item);
}

public function getType() {

return 'callable';
}
}
<?php
declare(strict_types=1);
namespace VersatileCollections;

/**
* Description of CallablesCollection
*
* Below is a list of acceptable value(s), that could be comma separated,
* for the @used-for tag in phpdoc blocks for public methods in this class:
*
* - accessing-or-extracting-keys-or-items
* - adding-items
* - adding-methods-at-runtime
* - checking-keys-presence
* - checking-items-presence
* - creating-new-collections
* - deleting-items
* - finding-or-searching-for-items
* - getting-collection-meta-data
* - iteration
* - mathematical-operations
* - modifying-keys
* - modifying-items
* - ordering-or-sorting-items
* - other-operations
*
* @author aadegbam
*/
class CallablesCollection implements \VersatileCollections\StrictlyTypedCollectionInterface {

use StrictlyTypedCollectionInterfaceImplementationTrait;

public function __construct(callable ...$callables) {

$this->versatile_collections_items = $callables;
}

public function checkType($item) {

return is_callable($item);
}

public function getType() {

return 'callable';
}
}
65 changes: 33 additions & 32 deletions src/CollectionInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace VersatileCollections;

/**
Expand Down Expand Up @@ -45,7 +46,7 @@ interface CollectionInterface extends \ArrayAccess, \Countable, \IteratorAggrega
* @title: Creates a new collection from an array of items. Items must be rightly typed if collection class is strictly typed.
*
*/
public static function makeNew(array $items=[], $preserve_keys=true);
public static function makeNew(array $items=[], bool $preserve_keys=true): \VersatileCollections\CollectionInterface;

/**
*
Expand All @@ -60,7 +61,7 @@ public static function makeNew(array $items=[], $preserve_keys=true);
* @title: Retrieves an item associated with a specified key in the collection.
*
*/
public function __get($key);
public function __get(string $key);

/**
*
Expand All @@ -75,7 +76,7 @@ public function __get($key);
* @title: Checks if an item with a specified key exists in the collection.
*
*/
public function __isset($key);
public function __isset(string $key);

/**
*
Expand All @@ -92,7 +93,7 @@ public function __isset($key);
* @title: Adds an item with a specified key to the collection.
*
*/
public function __set($key, $val);
public function __set(string $key, $val);

/**
*
Expand All @@ -107,13 +108,13 @@ public function __set($key, $val);
* @title: Removes an item associated with the specified key from the collection.
*
*/
public function __unset($key);
public function __unset(string $key);

/**
*
* ArrayAccess: does the requested key exist?
*
* @param string $key The requested key.
* @param mixed $key The requested key.
*
* @return bool
*
Expand All @@ -122,13 +123,13 @@ public function __unset($key);
* @title: Checks if an item with a specified key exists in the collection.
*
*/
public function offsetExists($key);
public function offsetExists($key): bool;

/**
*
* ArrayAccess: get a key's value.
*
* @param string $key The requested key.
* @param mixed $key The requested key.
*
* @return mixed
*
Expand All @@ -143,9 +144,9 @@ public function offsetGet($key);
*
* ArrayAccess: set a key's value.
*
* @param string $key The requested key.
* @param mixed $key The requested key.
*
* @param string $val The value to set it to.
* @param mixed $val The value to set it to.
*
* @return void
*
Expand All @@ -154,13 +155,13 @@ public function offsetGet($key);
* @title: Adds an item with a specified key to the collection.
*
*/
public function offsetSet($key, $val);
public function offsetSet($key, $val): void;

/**
*
* ArrayAccess: unset a key.
*
* @param string $key The requested key.
* @param mixed $key The requested key.
*
* @return void
*
Expand All @@ -169,7 +170,7 @@ public function offsetSet($key, $val);
* @title: Removes an item associated with the specified key from the collection.
*
*/
public function offsetUnset($key);
public function offsetUnset($key): void;

/**
*
Expand All @@ -180,7 +181,7 @@ public function offsetUnset($key);
* @title: Returns all items in the collection and their corresponding keys in an array.
*
*/
public function toArray();
public function toArray(): array;

/**
*
Expand All @@ -191,7 +192,7 @@ public function toArray();
* @title: Returns an Iterator object that can be used to iterate through the collection.
*
*/
public function getIterator();
public function getIterator(): \Iterator;

/**
*
Expand All @@ -202,7 +203,7 @@ public function getIterator();
* @title: Returns the number of items in the collection.
*
*/
public function count();
public function count(): int;

////////////////////////////////////////////////////////////////////////////
////////// OTHER COLLECTION METHODS ////////////////////////////////////////
Expand Down Expand Up @@ -242,7 +243,7 @@ public function lastItem();
* @title: Returns a new instance of **`\VersatileCollections\GenericCollection`** containing all the keys in the original collection.
*
*/
public function getKeys();
public function getKeys(): \VersatileCollections\GenericCollection;

/**
*
Expand All @@ -261,7 +262,7 @@ public function getKeys();
* @title: Sets the specified field in each array or object in the collection to a specified value.
*
*/
public function setValForEachItem($field_name, $field_val, $add_field_if_not_present=false);
public function setValForEachItem(string $field_name, $field_val, bool $add_field_if_not_present=false): \VersatileCollections\CollectionInterface;

/**
*
Expand Down Expand Up @@ -291,7 +292,7 @@ public function setValForEachItem($field_name, $field_val, $add_field_if_not_pre
* @title: Filters out items in the collection via a callback function and returns filtered items in a new collection.
*
*/
public function filterAll(callable $filterer, $copy_keys=false, $bind_callback_to_this=true, $remove_filtered_items=false);
public function filterAll(callable $filterer, bool $copy_keys=false, bool $bind_callback_to_this=true, bool $remove_filtered_items=false): \VersatileCollections\CollectionInterface;

/**
*
Expand Down Expand Up @@ -323,7 +324,7 @@ public function filterAll(callable $filterer, $copy_keys=false, $bind_callback_t
* @title: Filters out the first N items in the collection via a callback function and returns filtered items in a new collection.
*
*/
public function filterFirstN(callable $filterer, $max_number_of_filtered_items_to_return=null, $copy_keys=false, $bind_callback_to_this=true, $remove_filtered_items=false);
public function filterFirstN(callable $filterer, ?int $max_number_of_filtered_items_to_return=null, bool $copy_keys=false, bool $bind_callback_to_this=true, bool $remove_filtered_items=false): \VersatileCollections\CollectionInterface;

/**
*
Expand All @@ -345,7 +346,7 @@ public function filterFirstN(callable $filterer, $max_number_of_filtered_items_t
* @title: Transforms each item in the collection via a callback function.
*
*/
public function transform(callable $transformer, $bind_callback_to_this=true);
public function transform(callable $transformer, bool $bind_callback_to_this=true): \VersatileCollections\CollectionInterface;

/**
*
Expand Down Expand Up @@ -400,7 +401,7 @@ public function reduceWithKeyAccess(callable $reducer, $initial_value=NULL);
* @title: Reverses the order of items in the collection and returns the reversed items in a new collection.
*
*/
public function reverse();
public function reverse(): \VersatileCollections\CollectionInterface;

/**
*
Expand All @@ -413,7 +414,7 @@ public function reverse();
* @title: Reverses the order of items in the collection. Original collection is modified.
*
*/
public function reverseMe();
public function reverseMe(): \VersatileCollections\CollectionInterface;

/**
*
Expand All @@ -426,7 +427,7 @@ public function reverseMe();
* @title: Returns true if there are one or more items in the collection or false otherwise.
*
*/
public function isEmpty();
public function isEmpty(): bool;

/**
*
Expand Down Expand Up @@ -457,7 +458,7 @@ public function getIfExists($key, $default_value=null);
* @title: Checks if a collection contains a specified item (using strict comparison).
*
*/
public function containsItem($item);
public function containsItem($item): bool;

/**
*
Expand All @@ -474,7 +475,7 @@ public function containsItem($item);
* @title: Checks if a collection contains a specified item (using strict comparison) together with the specified key.
*
*/
public function containsItemWithKey($key, $item);
public function containsItemWithKey($key, $item): bool;

/**
*
Expand All @@ -490,7 +491,7 @@ public function containsItemWithKey($key, $item);
* @title: Checks if a collection contains all specified items (using strict comparison for each comparison).
*
*/
public function containsItems(array $items);
public function containsItems(array $items): bool;

/**
*
Expand All @@ -505,7 +506,7 @@ public function containsItems(array $items);
* @title: Checks if a collection contains a specified key.
*
*/
public function containsKey($key);
public function containsKey($key): bool;

/**
*
Expand All @@ -520,7 +521,7 @@ public function containsKey($key);
* @title: Checks if a collection contains all specified keys.
*
*/
public function containsKeys(array $keys);
public function containsKeys(array $keys): bool;

/**
*
Expand All @@ -536,7 +537,7 @@ public function containsKeys(array $keys);
* @title: Appends all items from a specified collection to the end of a collection. Note that appended items will be assigned numeric keys.
*
*/
public function appendCollection(CollectionInterface $other);
public function appendCollection(CollectionInterface $other): \VersatileCollections\CollectionInterface;

/**
*
Expand All @@ -551,7 +552,7 @@ public function appendCollection(CollectionInterface $other);
* @title: Appends a specified item to the end of a collection.
*
*/
public function appendItem($item);
public function appendItem($item): \VersatileCollections\CollectionInterface;

/**
*
Expand All @@ -567,7 +568,7 @@ public function appendItem($item);
* @title: Prepends all items from a specified collection to the front of a collection.
*
*/
public function prependCollection(CollectionInterface $other);
public function prependCollection(CollectionInterface $other): \VersatileCollections\CollectionInterface;

/**
*
Expand Down
Loading

0 comments on commit d16c6c8

Please sign in to comment.