Skip to content

Commit 7bc077b

Browse files
committed
code refactor
1 parent c66e87e commit 7bc077b

12 files changed

+46
-60
lines changed

CHANGELOG.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ All notable changes to this project will be documented in this file.
66
The format is based on [Keep a Changelog](http://keepachangelog.com/)
77
and this project adheres to [Semantic Versioning](http://semver.org/).
88

9-
## [v3.0.0](https://github.com/linna/typed-array/compare/v2.0.1...v3.0.0) - 2022-05-11
9+
## [v3.0.0](https://github.com/linna/typed-array/compare/v2.0.1...v3.0.0) - 2022-05-13
1010

1111
### Added
12-
- php 8.0 support
12+
- php 8.1 support
1313
- `Linna\TypedArrayObject\ArrayOfArrays` class
1414
- `Linna\TypedArrayObject\ArrayOfBooleans` class
1515
- `Linna\TypedArrayObject\ArrayOfCallable` class

README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
<div align="center">
1414

1515
[![Tests](https://github.com/linna/typed-array/actions/workflows/tests.yml/badge.svg)](https://github.com/linna/typed-array/actions/workflows/tests.yml)
16-
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/linna/typed-array/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/linna/typed-array/?branch=master)
17-
[![Code Coverage](https://scrutinizer-ci.com/g/linna/typed-array/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/linna/typed-array/?branch=master)
18-
[![StyleCI](https://styleci.io/repos/93407083/shield?branch=master&style=flat)](https://styleci.io/repos/93407083)
16+
[![PDS Skeleton](https://img.shields.io/badge/pds-skeleton-blue.svg?style=flat)](https://github.com/php-pds/skeleton)
17+
[![PHP 8.1](https://img.shields.io/badge/PHP-8.1-8892BF.svg)](http://php.net)
1918

2019
</div>
2120

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"issues": "https://github.com/linna/typed-array/issues"
1616
},
1717
"require": {
18-
"php": ">=8.0"
18+
"php": ">=8.1"
1919
},
2020
"require-dev": {
2121
"infection/infection": "^0.21",

src/Linna/TypedArrayObject/AbstractArray.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Linna\TypedArrayObject;
1313

1414
use ArrayObject;
15+
use Closure;
1516
use InvalidArgumentException;
1617

1718
/**
@@ -20,20 +21,20 @@
2021
class AbstractArray extends ArrayObject
2122
{
2223
protected string $exceptionMessage;
23-
protected string $func;
24+
protected Closure $func;
2425

2526
/**
2627
* Class Contructor.
2728
*
28-
* @param string $func function to check the array values
29+
* @param Closure $func function to check the array values
2930
* @param array<mixed> $input array of values
3031
* @param int $flags see array object on php site
3132
* @param string $iterator_class see array object on php site
3233
*
3334
* @throws InvalidArgumentException If elements in the optional array parameter
3435
* aren't of the configured type.
3536
*/
36-
public function __construct(string $func, array $input = [], int $flags = 0, string $iterator_class = "ArrayIterator")
37+
public function __construct(Closure $func, array $input = [], int $flags = 0, string $iterator_class = "ArrayIterator")
3738
{
3839
//check for invalid values inside provided array
3940
//array_map returns an array of trues or false

src/Linna/TypedArrayObject/ArrayOfArrays.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class ArrayOfArrays extends AbstractArray
3636
*/
3737
public function __construct(array $input = [], int $flags = 0, string $iterator_class = "ArrayIterator")
3838
{
39-
parent::__construct('is_array', $input, $flags, $iterator_class);
39+
// first argument is the php8.1 method to pass function reference as closure.
40+
// check https://www.php.net/manual/en/functions.first_class_callable_syntax.php
41+
parent::__construct(is_array(...), $input, $flags, $iterator_class);
4042
}
4143
}

src/Linna/TypedArrayObject/ArrayOfBooleans.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class ArrayOfBooleans extends AbstractArray
3636
*/
3737
public function __construct(array $input = [], int $flags = 0, string $iterator_class = "ArrayIterator")
3838
{
39-
parent::__construct('is_bool', $input, $flags, $iterator_class);
39+
// first argument is the php8.1 method to pass function reference as closure.
40+
// check https://www.php.net/manual/en/functions.first_class_callable_syntax.php
41+
parent::__construct(is_bool(...), $input, $flags, $iterator_class);
4042
}
4143
}

src/Linna/TypedArrayObject/ArrayOfCallable.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class ArrayOfCallable extends AbstractArray
3636
*/
3737
public function __construct(array $input = [], int $flags = 0, string $iterator_class = "ArrayIterator")
3838
{
39-
parent::__construct('is_callable', $input, $flags, $iterator_class);
39+
// first argument is the php8.1 method to pass function reference as closure.
40+
// check https://www.php.net/manual/en/functions.first_class_callable_syntax.php
41+
parent::__construct(is_callable(...), $input, $flags, $iterator_class);
4042
}
4143
}

src/Linna/TypedArrayObject/ArrayOfClasses.php

+16-44
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,22 @@
1111

1212
namespace Linna\TypedArrayObject;
1313

14-
use ArrayObject;
1514
use InvalidArgumentException;
1615

1716
/**
1817
* Provide a way for create an array of typed elements with php.
1918
*/
20-
class ArrayOfClasses extends ArrayObject
19+
class ArrayOfClasses extends AbstractArray
2120
{
2221
/**
2322
* @var string Current type for array
2423
*/
2524
protected string $class;
25+
26+
/**
27+
* It overrides parent message
28+
* @var string Exception message
29+
*/
2630
protected string $exceptionMessage;
2731

2832

@@ -40,56 +44,24 @@ public function __construct(string $class, array $input = [], int $flags = 0, st
4044
{
4145
$this->class = $class;
4246
$this->exceptionMessage = "Elements passed must be of the type <{$class}>.";
43-
44-
if (!\class_exists($class)) {
45-
throw new InvalidArgumentException("Type <{$this->class}> provided isn't a class.");
46-
}
47-
48-
if (!\array_product(\array_map(function ($x) use ($class) {
49-
return $x instanceof $class;
50-
}, $input))) {
51-
throw new InvalidArgumentException($this->exceptionMessage);
52-
}
53-
54-
parent::__construct($input, $flags, $iterator_class);
55-
}
56-
57-
/**
58-
* Array style value assignment.
59-
*
60-
* @ignore
61-
*
62-
* @param mixed $index
63-
* @param object $newval
64-
*
65-
* @throws InvalidArgumentException If value passed with $newval are not of the expected type
66-
*
67-
* @return void
68-
*/
69-
public function offsetSet($index, $newval): void
70-
{
71-
if (!($newval instanceof $this->class)) {
72-
throw new InvalidArgumentException($this->exceptionMessage);
73-
}
74-
75-
parent::offsetSet($index, $newval);
47+
// first argument is the php8.1 method to pass function reference as closure.
48+
// check https://www.php.net/manual/en/functions.first_class_callable_syntax.php
49+
parent::__construct($this->is_class(...), $input, $flags, $iterator_class);
7650
}
7751

7852
/**
79-
* Append a value at the end of the array.
80-
*
81-
* @param object $value
53+
* Check if argument is instance of specific class.
8254
*
83-
* @return void
55+
* @param mixed $value
8456
*
85-
* @throws InvalidArgumentException If value passed with $value are not of the expected type
57+
* @return bool
8658
*/
87-
public function append($value): void
59+
protected function is_class(mixed $value): bool
8860
{
89-
if (!($value instanceof $this->class)) {
90-
throw new InvalidArgumentException($this->exceptionMessage);
61+
if ($value instanceof $this->class) {
62+
return true;
9163
}
9264

93-
parent::append($value);
65+
return false;
9466
}
9567
}

src/Linna/TypedArrayObject/ArrayOfFloats.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class ArrayOfFloats extends AbstractArray
3636
*/
3737
public function __construct(array $input = [], int $flags = 0, string $iterator_class = "ArrayIterator")
3838
{
39-
parent::__construct('is_float', $input, $flags, $iterator_class);
39+
// first argument is the php8.1 method to pass function reference as closure.
40+
// check https://www.php.net/manual/en/functions.first_class_callable_syntax.php
41+
parent::__construct(is_float(...), $input, $flags, $iterator_class);
4042
}
4143
}

src/Linna/TypedArrayObject/ArrayOfIntegers.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class ArrayOfIntegers extends AbstractArray
3636
*/
3737
public function __construct(array $input = [], int $flags = 0, string $iterator_class = "ArrayIterator")
3838
{
39-
parent::__construct('is_int', $input, $flags, $iterator_class);
39+
// first argument is the php8.1 method to pass function reference as closure.
40+
// check https://www.php.net/manual/en/functions.first_class_callable_syntax.php
41+
parent::__construct(is_int(...), $input, $flags, $iterator_class);
4042
}
4143
}

src/Linna/TypedArrayObject/ArrayOfObjects.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class ArrayOfObjects extends AbstractArray
3636
*/
3737
public function __construct(array $input = [], int $flags = 0, string $iterator_class = "ArrayIterator")
3838
{
39-
parent::__construct('is_object', $input, $flags, $iterator_class);
39+
// first argument is the php8.1 method to pass function reference as closure.
40+
// check https://www.php.net/manual/en/functions.first_class_callable_syntax.php
41+
parent::__construct(is_object(...), $input, $flags, $iterator_class);
4042
}
4143
}

src/Linna/TypedArrayObject/ArrayOfStrings.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class ArrayOfStrings extends AbstractArray
3636
*/
3737
public function __construct(array $input = [], int $flags = 0, string $iterator_class = "ArrayIterator")
3838
{
39-
parent::__construct('is_string', $input, $flags, $iterator_class);
39+
// first argument is the php8.1 method to pass function reference as closure.
40+
// check https://www.php.net/manual/en/functions.first_class_callable_syntax.php
41+
parent::__construct(is_string(...), $input, $flags, $iterator_class);
4042
}
4143
}

0 commit comments

Comments
 (0)