Skip to content

Commit

Permalink
Merge pull request #75 from samsonasik/apply-php80-syntax
Browse files Browse the repository at this point in the history
Apply PHP 8.0 Syntax and constructor promotion
  • Loading branch information
Ocramius committed Oct 10, 2022
2 parents 627e06f + 74725af commit 63b66bd
Show file tree
Hide file tree
Showing 29 changed files with 77 additions and 179 deletions.
39 changes: 13 additions & 26 deletions src/ArrayObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use function asort;
use function class_exists;
use function count;
use function get_class;
use function get_debug_type;
use function get_object_vars;
use function gettype;
use function in_array;
Expand All @@ -31,7 +31,7 @@
use function natsort;
use function serialize;
use function sprintf;
use function strpos;
use function str_starts_with;
use function uasort;
use function uksort;
use function unserialize;
Expand Down Expand Up @@ -85,10 +85,9 @@ public function __construct($input = [], $flags = self::STD_PROP_LIST, $iterator
/**
* Returns whether the requested key exists
*
* @param mixed $key
* @return bool
*/
public function __isset($key)
public function __isset(mixed $key)
{
if ($this->flag === self::ARRAY_AS_PROPS) {
return $this->offsetExists($key);
Expand All @@ -104,11 +103,9 @@ public function __isset($key)
/**
* Sets the value at the specified key to value
*
* @param mixed $key
* @param mixed $value
* @return void
*/
public function __set($key, $value)
public function __set(mixed $key, mixed $value)
{
if ($this->flag === self::ARRAY_AS_PROPS) {
$this->offsetSet($key, $value);
Expand All @@ -125,10 +122,9 @@ public function __set($key, $value)
/**
* Unsets the value at the specified key
*
* @param mixed $key
* @return void
*/
public function __unset($key)
public function __unset(mixed $key)
{
if ($this->flag === self::ARRAY_AS_PROPS) {
$this->offsetUnset($key);
Expand All @@ -145,10 +141,9 @@ public function __unset($key)
/**
* Returns the value at the specified key by reference
*
* @param mixed $key
* @return mixed
*/
public function &__get($key)
public function &__get(mixed $key)
{
if ($this->flag === self::ARRAY_AS_PROPS) {
$ret = &$this->offsetGet($key);
Expand All @@ -166,10 +161,9 @@ public function &__get($key)
/**
* Appends the value
*
* @param mixed $value
* @return void
*/
public function append($value)
public function append(mixed $value)
{
$this->storage[] = $value;
}
Expand Down Expand Up @@ -299,23 +293,21 @@ public function natsort()
/**
* Returns whether the requested key exists
*
* @param mixed $key
* @return bool
*/
#[ReturnTypeWillChange]
public function offsetExists($key)
public function offsetExists(mixed $key)
{
return isset($this->storage[$key]);
}

/**
* Returns the value at the specified key
*
* @param mixed $key
* @return mixed
*/
#[ReturnTypeWillChange]
public function &offsetGet($key)
public function &offsetGet(mixed $key)
{
$ret = null;
if (! $this->offsetExists($key)) {
Expand All @@ -329,24 +321,21 @@ public function &offsetGet($key)
/**
* Sets the value at the specified key to value
*
* @param mixed $key
* @param mixed $value
* @return void
*/
#[ReturnTypeWillChange]
public function offsetSet($key, $value)
public function offsetSet(mixed $key, mixed $value)
{
$this->storage[$key] = $value;
}

/**
* Unsets the value at the specified key
*
* @param mixed $key
* @return void
*/
#[ReturnTypeWillChange]
public function offsetUnset($key)
public function offsetUnset(mixed $key)
{
if ($this->offsetExists($key)) {
unset($this->storage[$key]);
Expand Down Expand Up @@ -398,7 +387,7 @@ public function setIteratorClass($class)
return;
}

if (strpos($class, '\\') === 0) {
if (str_starts_with($class, '\\')) {
$class = '\\' . $class;
if (class_exists($class)) {
$this->iteratorClass = $class;
Expand Down Expand Up @@ -488,9 +477,7 @@ public function __unserialize($data)
throw new UnexpectedValueException(sprintf(
'Cannot deserialize %s instance: invalid iteratorClass; expected string, received %s',
self::class,
is_object($data['iteratorClass'])
? get_class($data['iteratorClass'])
: gettype($data['iteratorClass'])
get_debug_type($data['iteratorClass'])
));
}
$this->setIteratorClass($data['iteratorClass']);
Expand Down
1 change: 0 additions & 1 deletion src/ArraySerializableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ interface ArraySerializableInterface
/**
* Exchange internal values from provided array
*
* @param array $array
* @return void
*/
public function exchangeArray(array $array);
Expand Down
19 changes: 6 additions & 13 deletions src/ArrayUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,10 @@ abstract class ArrayUtils
/**
* Test whether an array contains one or more string keys
*
* @param mixed $value
* @param bool $allowEmpty Should an empty array() return true
* @return bool
*/
public static function hasStringKeys($value, $allowEmpty = false)
public static function hasStringKeys(mixed $value, $allowEmpty = false)
{
if (! is_array($value)) {
return false;
Expand All @@ -66,11 +65,10 @@ public static function hasStringKeys($value, $allowEmpty = false)
/**
* Test whether an array contains one or more integer keys
*
* @param mixed $value
* @param bool $allowEmpty Should an empty array() return true
* @return bool
*/
public static function hasIntegerKeys($value, $allowEmpty = false)
public static function hasIntegerKeys(mixed $value, $allowEmpty = false)
{
if (! is_array($value)) {
return false;
Expand All @@ -93,11 +91,10 @@ public static function hasIntegerKeys($value, $allowEmpty = false)
* - a float: 2.2120, -78.150999
* - a string with float: '4000.99999', '-10.10'
*
* @param mixed $value
* @param bool $allowEmpty Should an empty array() return true
* @return bool
*/
public static function hasNumericKeys($value, $allowEmpty = false)
public static function hasNumericKeys(mixed $value, $allowEmpty = false)
{
if (! is_array($value)) {
return false;
Expand Down Expand Up @@ -126,11 +123,10 @@ public static function hasNumericKeys($value, $allowEmpty = false)
* );
* </code>
*
* @param mixed $value
* @param bool $allowEmpty Is an empty list a valid list?
* @return bool
*/
public static function isList($value, $allowEmpty = false)
public static function isList(mixed $value, $allowEmpty = false)
{
if (! is_array($value)) {
return false;
Expand Down Expand Up @@ -168,11 +164,10 @@ public static function isList($value, $allowEmpty = false)
* );
* </code>
*
* @param mixed $value
* @param bool $allowEmpty Is an empty array() a valid hash table?
* @return bool
*/
public static function isHashTable($value, $allowEmpty = false)
public static function isHashTable(mixed $value, $allowEmpty = false)
{
if (! is_array($value)) {
return false;
Expand All @@ -193,12 +188,11 @@ public static function isHashTable($value, $allowEmpty = false)
* non-strict check is implemented. if $strict = -1, the default in_array
* non-strict behaviour is used.
*
* @param mixed $needle
* @param array $haystack
* @param int|bool $strict
* @return bool
*/
public static function inArray($needle, array $haystack, $strict = false)
public static function inArray(mixed $needle, array $haystack, $strict = false)
{
if (! $strict) {
if (is_int($needle) || is_float($needle)) {
Expand Down Expand Up @@ -318,7 +312,6 @@ public static function merge(array $a, array $b, $preserveNumericKeys = false)
/**
* @deprecated Since 3.2.0; use the native array_filter methods
*
* @param array $data
* @param callable $callback
* @param null|int $flag
* @return array
Expand Down
9 changes: 1 addition & 8 deletions src/ArrayUtils/MergeReplaceKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,8 @@

final class MergeReplaceKey implements MergeReplaceKeyInterface
{
/** @var mixed */
protected $data;

/**
* @param mixed $data
*/
public function __construct($data)
public function __construct(protected mixed $data)
{
$this->data = $data;
}

/**
Expand Down
22 changes: 7 additions & 15 deletions src/FastPriorityQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,10 @@ public function __unserialize(array $data): void
/**
* Insert an element in the queue with a specified priority
*
* @param mixed $value
* @param int $priority
* @return void
*/
public function insert($value, $priority)
public function insert(mixed $value, $priority)
{
if (! is_int($priority)) {
throw new Exception\InvalidArgumentException('The priority must be an integer');
Expand Down Expand Up @@ -155,10 +154,9 @@ public function extract()
* the same item has been added multiple times, it will not remove other
* instances.
*
* @param mixed $datum
* @return bool False if the item was not found, true otherwise.
*/
public function remove($datum)
public function remove(mixed $datum)
{
$currentIndex = $this->index;
$currentSubIndex = $this->subIndex;
Expand Down Expand Up @@ -356,15 +354,10 @@ public function unserialize($data)
*/
public function setExtractFlags($flag)
{
switch ($flag) {
case self::EXTR_DATA:
case self::EXTR_PRIORITY:
case self::EXTR_BOTH:
$this->extractFlag = $flag;
break;
default:
throw new Exception\InvalidArgumentException("The extract flag specified is not valid");
}
$this->extractFlag = match ($flag) {
self::EXTR_DATA, self::EXTR_PRIORITY, self::EXTR_BOTH => $flag,
default => throw new Exception\InvalidArgumentException("The extract flag specified is not valid"),
};
}

/**
Expand All @@ -380,10 +373,9 @@ public function isEmpty()
/**
* Does the queue contain the given datum?
*
* @param mixed $datum
* @return bool
*/
public function contains($datum)
public function contains(mixed $datum)
{
foreach ($this->values as $values) {
if (in_array($datum, $values)) {
Expand Down
8 changes: 3 additions & 5 deletions src/Guard/ArrayOrTraversableGuardTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
use Laminas\Stdlib\Exception\InvalidArgumentException;
use Traversable;

use function get_class;
use function gettype;
use function get_debug_type;
use function is_array;
use function is_object;
use function sprintf;

/**
Expand All @@ -29,15 +27,15 @@ trait ArrayOrTraversableGuardTrait
* @throws Exception
*/
protected function guardForArrayOrTraversable(
$data,
mixed $data,
$dataName = 'Argument',
$exceptionClass = InvalidArgumentException::class
) {
if (! is_array($data) && ! $data instanceof Traversable) {
$message = sprintf(
"%s must be an array or Traversable, [%s] given",
$dataName,
is_object($data) ? get_class($data) : gettype($data)
get_debug_type($data)
);
throw new $exceptionClass($message);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Guard/EmptyGuardTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ trait EmptyGuardTrait
* @throws Exception
*/
protected function guardAgainstEmpty(
$data,
mixed $data,
$dataName = 'Argument',
$exceptionClass = InvalidArgumentException::class
) {
Expand Down
2 changes: 1 addition & 1 deletion src/Guard/NullGuardTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ trait NullGuardTrait
* @throws Exception
*/
protected function guardAgainstNull(
$data,
mixed $data,
$dataName = 'Argument',
$exceptionClass = InvalidArgumentException::class
) {
Expand Down
6 changes: 2 additions & 4 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
use Traversable;

use function array_key_exists;
use function get_class;
use function gettype;
use function get_debug_type;
use function is_array;
use function is_object;
use function is_scalar;
use function sprintf;

Expand Down Expand Up @@ -42,7 +40,7 @@ public function setMetadata($spec, $value = null)
if (! is_array($spec) && ! $spec instanceof Traversable) {
throw new Exception\InvalidArgumentException(sprintf(
'Expected a string, array, or Traversable argument in first position; received "%s"',
is_object($spec) ? get_class($spec) : gettype($spec)
get_debug_type($spec)
));
}
foreach ($spec as $key => $value) {
Expand Down
Loading

0 comments on commit 63b66bd

Please sign in to comment.