Skip to content

Commit

Permalink
Merge pull request #42 from nreynis/feature/strong-typing
Browse files Browse the repository at this point in the history
Refactor to support strong typing.
  • Loading branch information
florianeckerstorfer committed Sep 15, 2019
2 parents 22cba1f + 0ab5c8c commit 7736894
Show file tree
Hide file tree
Showing 75 changed files with 413 additions and 267 deletions.
10 changes: 5 additions & 5 deletions src/AbstractChain.php
Expand Up @@ -23,7 +23,7 @@ abstract class AbstractChain implements ArrayAccess, IteratorAggregate, JsonSeri
/**
* @return ArrayIterator
*/
public function getIterator()
public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->array);
}
Expand All @@ -33,7 +33,7 @@ public function getIterator()
*
* @return bool
*/
public function offsetExists($offset)
public function offsetExists($offset): bool
{
return isset($this->array[$offset]);
}
Expand All @@ -52,23 +52,23 @@ public function offsetGet($offset)
* @param mixed $offset
* @param mixed $value
*/
public function offsetSet($offset, $value)
public function offsetSet($offset, $value): void
{
$this->array[$offset] = $value;
}

/**
* @param mixed $offset
*/
public function offsetUnset($offset)
public function offsetUnset($offset): void
{
unset($this->array[$offset]);
}

/**
* @return array
*/
public function jsonSerialize()
public function jsonSerialize(): array
{
return $this->array;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Chain.php
Expand Up @@ -104,7 +104,7 @@ public function __construct(array $array = [])
*
* @return Chain
*/
public static function create(array $array = [])
public static function create(array $array = []): Chain
{
return new static($array);
}
Expand All @@ -117,7 +117,7 @@ public static function create(array $array = [])
*
* @return Chain
*/
public static function createFromString($delimiter, $string, array $options = [])
public static function createFromString(string $delimiter, string $string, array $options = []): Chain
{
$options = array_merge(['regexp' => false], $options);
$chain = new static();
Expand Down
2 changes: 1 addition & 1 deletion src/Link/ChangeKeyCase.php
Expand Up @@ -20,7 +20,7 @@ trait ChangeKeyCase
*
* @return Chain
*/
public function changeKeyCase($case = CASE_LOWER)
public function changeKeyCase(int $case = CASE_LOWER): Chain
{
$this->array = array_change_key_case($this->array, $case);

Expand Down
2 changes: 1 addition & 1 deletion src/Link/Combine.php
Expand Up @@ -20,7 +20,7 @@ trait Combine
*
* @return Chain
*/
public function combine($keys, $values)
public function combine($keys, $values): Chain
{
$this->array = array_combine(
$keys instanceof Chain ? $keys->array : $keys,
Expand Down
2 changes: 1 addition & 1 deletion src/Link/Count.php
Expand Up @@ -15,7 +15,7 @@ trait Count
*
* @return int Returns the number of elements in the array.
*/
public function count()
public function count(): int
{
return count($this->array);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Link/CountValues.php
Expand Up @@ -17,7 +17,7 @@ trait CountValues
*
* @return array An associative array of values from the array as keys and their count value.
*/
public function countValues()
public function countValues(): array
{
return array_count_values($this->array);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Link/Diff.php
Expand Up @@ -22,7 +22,7 @@ trait Diff
*
* @return Chain
*/
public function diff($array2)
public function diff($array2): Chain
{
$this->array = array_diff(
$this->array,
Expand Down
2 changes: 1 addition & 1 deletion src/Link/Fill.php
Expand Up @@ -25,7 +25,7 @@ trait Fill
*
* @return Chain
*/
public static function fill($startIndex, $num, $value = null)
public static function fill(int $startIndex, int $num, $value = null): Chain
{
return new self(array_fill($startIndex, $num, $value));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Link/Filter.php
Expand Up @@ -22,7 +22,7 @@ trait Filter
*
* @return Chain
*/
public function filter(callable $callback)
public function filter(callable $callback): Chain
{
$this->array = array_filter($this->array, $callback, ARRAY_FILTER_USE_BOTH);

Expand Down
4 changes: 3 additions & 1 deletion src/Link/FlatMap.php
Expand Up @@ -12,9 +12,11 @@
trait FlatMap
{
/**
* @param callable $callback
*
* @return Chain
*/
public function flatMap(callable $callback)
public function flatMap(callable $callback): Chain
{
$flattened = [];
foreach ($this->array as $index => $element) {
Expand Down
4 changes: 3 additions & 1 deletion src/Link/Flip.php
Expand Up @@ -2,6 +2,8 @@

namespace Cocur\Chain\Link;

use Cocur\Chain\Chain;

/**
* Flip.
*
Expand All @@ -13,7 +15,7 @@ trait Flip
/**
* @return Chain
*/
public function flip()
public function flip(): Chain
{
$this->array = array_flip($this->array);

Expand Down
2 changes: 1 addition & 1 deletion src/Link/Intersect.php
Expand Up @@ -17,7 +17,7 @@ trait Intersect
*
* @return Chain
*/
public function intersect($array2)
public function intersect($array2): Chain
{
$this->array = array_intersect($this->array, $array2 instanceof Chain ? $array2->array : $array2);

Expand Down
2 changes: 1 addition & 1 deletion src/Link/IntersectAssoc.php
Expand Up @@ -17,7 +17,7 @@ trait IntersectAssoc
*
* @return Chain
*/
public function intersectAssoc($array)
public function intersectAssoc($array): Chain
{
$this->array = array_intersect_assoc($this->array, $array instanceof Chain ? $array->array : $array);

Expand Down
2 changes: 1 addition & 1 deletion src/Link/IntersectKey.php
Expand Up @@ -17,7 +17,7 @@ trait IntersectKey
*
* @return Chain
*/
public function intersectKey($array)
public function intersectKey($array): Chain
{
$this->array = array_intersect_key($this->array, $array instanceof Chain ? $array->array : $array);

Expand Down
2 changes: 1 addition & 1 deletion src/Link/Join.php
Expand Up @@ -18,7 +18,7 @@ trait Join
* @return string Returns a string containing a string representation of all the array elements in the same order,
* with the glue string between each element.
*/
public function join($glue = '')
public function join(string $glue = ''): string
{
return implode($glue, $this->array);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Link/KeyExists.php
Expand Up @@ -20,7 +20,7 @@ trait KeyExists
*
* @return bool `true` if key or index exists in array, `false` otherwise.
*/
public function keyExists($key)
public function keyExists($key): bool
{
return array_key_exists($key, $this->array);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Link/Keys.php
Expand Up @@ -15,7 +15,7 @@ trait Keys
/**
* @return Chain
*/
public function keys()
public function keys(): Chain
{
$this->array = array_keys($this->array);

Expand Down
2 changes: 1 addition & 1 deletion src/Link/Map.php
Expand Up @@ -17,7 +17,7 @@ trait Map
*
* @return Chain
*/
public function map(callable $callback)
public function map(callable $callback): Chain
{
foreach ($this->array as $index => $element) {
$this->array[$index] = $callback($element, $index);
Expand Down
2 changes: 1 addition & 1 deletion src/Link/Merge.php
Expand Up @@ -22,7 +22,7 @@ trait Merge
*
* @return Chain
*/
public function merge($array, array $options = [])
public function merge($array, array $options = []): Chain
{
$options = array_merge(['recursive' => false], $options);

Expand Down
4 changes: 3 additions & 1 deletion src/Link/Pad.php
Expand Up @@ -2,6 +2,8 @@

namespace Cocur\Chain\Link;

use Cocur\Chain\Chain;

/**
* Pad.
*
Expand All @@ -16,7 +18,7 @@ trait Pad
*
* @return Chain
*/
public function pad($size, $value)
public function pad(int $size, $value): Chain
{
$this->array = array_pad($this->array, $size, $value);

Expand Down
2 changes: 1 addition & 1 deletion src/Link/Product.php
Expand Up @@ -11,7 +11,7 @@
trait Product
{
/**
* @return number
* @return int|float
*/
public function product()
{
Expand Down
4 changes: 3 additions & 1 deletion src/Link/Push.php
Expand Up @@ -2,6 +2,8 @@

namespace Cocur\Chain\Link;

use Cocur\Chain\Chain;

/**
* Push.
*
Expand All @@ -15,7 +17,7 @@ trait Push
*
* @return Chain
*/
public function push($element)
public function push($element): Chain
{
array_push($this->array, $element);

Expand Down
2 changes: 1 addition & 1 deletion src/Link/Rand.php
Expand Up @@ -15,7 +15,7 @@ trait Rand
*
* @return mixed
*/
public function rand($num = 1)
public function rand(int $num = 1)
{
return array_rand($this->array, $num);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Link/Replace.php
Expand Up @@ -17,7 +17,7 @@ trait Replace
*
* @return Chain
*/
public function replace($array)
public function replace($array): Chain
{
$this->array = array_replace($this->array, $array instanceof Chain ? $chain->array : $array);

Expand Down
2 changes: 1 addition & 1 deletion src/Link/Reverse.php
Expand Up @@ -17,7 +17,7 @@ trait Reverse
*
* @return Chain
*/
public function reverse($preserveKeys = false)
public function reverse(bool $preserveKeys = false): Chain
{
$this->array = array_reverse($this->array, $preserveKeys);

Expand Down
2 changes: 1 addition & 1 deletion src/Link/Search.php
Expand Up @@ -16,7 +16,7 @@ trait Search
*
* @return mixed
*/
public function search($needle, $strict = false)
public function search($needle, bool $strict = false)
{
return array_search($needle, $this->array, $strict);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Link/Shuffle.php
Expand Up @@ -2,6 +2,8 @@

namespace Cocur\Chain\Link;

use Cocur\Chain\Chain;

/**
* Shuffle.
*
Expand All @@ -13,7 +15,7 @@ trait Shuffle
/**
* @return Chain
*/
public function shuffle()
public function shuffle(): Chain
{
shuffle($this->array);

Expand Down
4 changes: 3 additions & 1 deletion src/Link/Slice.php
Expand Up @@ -2,6 +2,8 @@

namespace Cocur\Chain\Link;

use Cocur\Chain\Chain;

/**
* Slice.
*
Expand All @@ -17,7 +19,7 @@ trait Slice
*
* @return Chain
*/
public function slice($offset, $length = null, $preserveKeys = false)
public function slice(int $offset, ?int $length = null, bool $preserveKeys = false): Chain
{
$this->array = array_slice($this->array, $offset, $length, $preserveKeys);

Expand Down
6 changes: 3 additions & 3 deletions src/Link/Sort.php
Expand Up @@ -20,7 +20,7 @@ trait Sort
*
* @return Chain
*/
public function sort($sortBy = SORT_REGULAR, array $options = [])
public function sort($sortBy = SORT_REGULAR, array $options = []): Chain
{
if (!$sortBy) {
$sortBy = SORT_REGULAR;
Expand All @@ -38,7 +38,7 @@ public function sort($sortBy = SORT_REGULAR, array $options = [])
* @param callable $callback
* @param array $options
*/
private function sortWithCallback(callable $callback, array $options = [])
private function sortWithCallback(callable $callback, array $options = []): void
{
if (isset($options['assoc']) && $options['assoc']) {
uasort($this->array, $callback);
Expand All @@ -51,7 +51,7 @@ private function sortWithCallback(callable $callback, array $options = [])
* @param int $sortFlags
* @param array $options
*/
private function sortWithFlags($sortFlags = SORT_REGULAR, array $options = [])
private function sortWithFlags(int $sortFlags = SORT_REGULAR, array $options = []): void
{
if (!empty($options['assoc']) && !empty($options['reverse'])) {
arsort($this->array, $sortFlags);
Expand Down
4 changes: 2 additions & 2 deletions src/Link/SortKeys.php
Expand Up @@ -20,7 +20,7 @@ trait SortKeys
*
* @return Chain
*/
public function sortKeys($sortBy = SORT_REGULAR, array $options = [])
public function sortKeys($sortBy = SORT_REGULAR, array $options = []): Chain
{
if ($sortBy && is_callable($sortBy)) {
uksort($this->array, $sortBy);
Expand All @@ -35,7 +35,7 @@ public function sortKeys($sortBy = SORT_REGULAR, array $options = [])
* @param int $sortFlags
* @param array $options
*/
private function sortKeysWithFlags($sortFlags = SORT_REGULAR, array $options = [])
private function sortKeysWithFlags(int $sortFlags = SORT_REGULAR, array $options = []): void
{
if (!empty($options['reverse'])) {
krsort($this->array, $sortFlags);
Expand Down

0 comments on commit 7736894

Please sign in to comment.