Skip to content

Commit 174c5df

Browse files
committed
v3.0.0 tests
1 parent fcafcb0 commit 174c5df

31 files changed

+455
-741
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
strategy:
1414
fail-fast: true
1515
matrix:
16-
php: [7.4, 8.0]
16+
php: [8.0, 8.1]
1717

1818
name: PHP ${{ matrix.php }}
1919

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ composer.lock
33
clover.xml
44
infection-log.txt
55
vendor/
6+
.php-cs-fixer.cache
67
.phpunit.result.cache

.php-cs-fixer.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
$config = new PhpCsFixer\Config();
4+
return $config
5+
->setRiskyAllowed(true)
6+
->setRules([
7+
'@PSR12' => true,
8+
'no_whitespace_in_blank_line' => true,
9+
'return_type_declaration' => true,
10+
'native_function_invocation' => ['include' => ['@all']],
11+
])
12+
->setFinder(PhpCsFixer\Finder::create()
13+
->exclude('vendor')
14+
->in(__DIR__)
15+
);

.scrutinizer.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ tools:
1414

1515
build:
1616
environment:
17-
php: 8.0
17+
php: 8.1
1818
tests:
1919
override:
2020
-

CHANGELOG.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ 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) - 2020-XX-XX
9+
## [v3.0.0](https://github.com/linna/typed-array/compare/v2.0.1...v3.0.0) - 2022-05-11
1010

1111
### Added
1212
- php 8.0 support
13-
- `ArrayArrayObject` class
14-
- `BoolArrayObject` class
15-
- `CallableArrayObject` class
16-
- `ClassArrayObject` class
17-
- `FloatArrayObject` class
18-
- `IntArrayObject` class
19-
- `ObjectArrayObject` class
20-
- `StringArrayObject` class
13+
- `ArrayOfArrays` class
14+
- `ArrayOfBooleans` class
15+
- `ArrayOfCallable` class
16+
- `ArrayOfClasses` class
17+
- `ArrayOfFloats` class
18+
- `ArrayOfIntegers` class
19+
- `ArrayOfObjects` class
20+
- `ArrayOfStrings` class
2121

2222
### Changed
2323
- namespace from `Linna` to `Linna\TypedArrayObject`

README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,23 @@ composer require linna/typed-array
3535

3636
| Name | Native Type Handled | Description |
3737
|-----------------------|---------------------|------------------------------------------------------|
38-
| `ArrayArrayObject` | array | |
39-
| `BoolArrayObject` | bool | |
40-
| `CallableArrayObject` | callable | |
41-
| `ClassArrayObject` | any existing class | passed as first argument in constructor as `::class` |
42-
| `FloatArrayObject` | float | |
43-
| `IntArrayObject` | int | |
44-
| `ObjectArrayObject` | object | |
45-
| `StringArrayObject` | string | |
38+
| `ArrayOfArrays` | array | |
39+
| `ArrayOfBooleans` | bool | |
40+
| `ArrayOfCallable` | callable | |
41+
| `ArrayOfClasses` | any existing class | passed as first argument in constructor as `::class` |
42+
| `ArrayOfFloats` | float | |
43+
| `ArrayOfIntegers` | int | |
44+
| `ArrayOfObjects` | object | |
45+
| `ArrayOfStrings` | string | |
4646

4747

4848
## Usage
4949
```php
50-
use Linna\TypedArrayObject\IntArrayObject;
51-
use Linna\TypedArrayObject\ClassArrayObject;
50+
use Linna\TypedArrayObject\ArrayOfIntegers;
51+
use Linna\TypedArrayObject\ArrayOfClasses;
5252

5353
//correct, only int passed to constructor.
54-
$intArray = new IntArrayObject([1, 2, 3, 4]);
54+
$intArray = new ArrayOfIntegers([1, 2, 3, 4]);
5555

5656
//correct, int assigned
5757
$intArray[] = 5;
@@ -64,10 +64,10 @@ $intArray->append(5);
6464
$intArray->append('a');
6565

6666
//throw InvalidArgumentException, mixed array passed to constructor.
67-
$otherIntArray = new IntArrayObject([1, 'a', 3, 4]);
67+
$otherIntArray = new ArrayOfIntegers([1, 'a', 3, 4]);
6868

6969
//correct, only Foo class instances passed to constructor.
70-
$fooArray = new ClassArrayObject(Foo::class, [
70+
$fooArray = new ArrayOfClasses(Foo::class, [
7171
new Foo(),
7272
new Foo()
7373
]);
@@ -83,7 +83,7 @@ $fooArray->append(new Foo());
8383
$fooArray->append(new Bar());
8484

8585
//throw InvalidArgumentException, mixed array of instances passed to constructor.
86-
$otherFooArray = new ClassArrayObject(Foo::class, [
86+
$otherFooArray = new ArrayOfClasses(Foo::class, [
8787
new Foo(),
8888
new Bar()
8989
]);
@@ -92,7 +92,7 @@ $otherFooArray = new ClassArrayObject(Foo::class, [
9292
> **Note:** Allowed types are: *array*, *bool*, *callable*, *float*, *int*, *object*, *string* and all existing classes.
9393
9494
## Performance consideration for v3.0
95-
Compared to previous versions of the library, this version is a bit faster because every types has it own class.
95+
Compared to previous versions of the library, this version is a bit faster because every types has it own class. Do milliseconds really matters?
9696

9797
![Array Speed Test](array-speed-test-v3.png)
9898

array-speed-test-v3.png

3.52 KB
Loading

composer.json

Lines changed: 7 additions & 2 deletions
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": ">=7.4"
18+
"php": ">=8.0"
1919
},
2020
"require-dev": {
2121
"infection/infection": "^0.21",
@@ -29,7 +29,12 @@
2929
},
3030
"autoload-dev": {
3131
"psr-4": {
32-
"Linna\\Tests\\": "tests/"
32+
"Linna\\": "tests/Linna"
33+
}
34+
},
35+
"config": {
36+
"allow-plugins": {
37+
"infection/extension-installer": true
3338
}
3439
}
3540
}

src/Linna/TypedArrayObject/StringArrayObject.php renamed to src/Linna/TypedArrayObject/AbstractArray.php

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,33 @@
1717
/**
1818
* Provide a way to create an array of string typed elements with php.
1919
*/
20-
class StringArrayObject extends ArrayObject
20+
class AbstractArray extends ArrayObject
2121
{
22-
public const EXC_MESSAGE = 'Elements passed must be of the type <string>.';
22+
protected string $exceptionMessage;
23+
protected string $func;
2324

2425
/**
2526
* Class Contructor.
2627
*
27-
* @param array $input
28-
* @param int $flags
29-
* @param string $iterator_class
28+
* @param string $func function to check the array values
29+
* @param array<mixed> $input array of values
30+
* @param int $flags see array object on php site
31+
* @param string $iterator_class see array object on php site
3032
*
3133
* @throws InvalidArgumentException If elements in the optional array parameter
3234
* aren't of the configured type.
3335
*/
34-
public function __construct(array $input = [], int $flags = 0, string $iterator_class = "ArrayIterator")
36+
public function __construct(string $func, array $input = [], int $flags = 0, string $iterator_class = "ArrayIterator")
3537
{
36-
//get input array len
37-
$count = \count($input);
3838
//check for invalid values inside provided array
39-
//for is simple than previosu solution
40-
for ($i = 0; $i < $count; $i++) {
41-
if (\is_string($input[$i])) {
42-
continue;
43-
}
44-
throw new InvalidArgumentException(self::EXC_MESSAGE);
39+
//array_map returns an array of trues or false
40+
//product of all trues return 1, only one false make result 0
41+
if (!\array_product(\array_map($func, $input))) {
42+
throw new InvalidArgumentException($this->exceptionMessage);
4543
}
4644

45+
$this->func = $func;
46+
4747
//call parent constructor
4848
parent::__construct($input, $flags, $iterator_class);
4949
}
@@ -62,13 +62,11 @@ public function __construct(array $input = [], int $flags = 0, string $iterator_
6262
*/
6363
public function offsetSet($index, $newval): void
6464
{
65-
if (\is_string($newval)) {
66-
parent::offsetSet($index, $newval);
67-
68-
return;
65+
if (!($this->func)($newval)) {
66+
throw new InvalidArgumentException($this->exceptionMessage);
6967
}
7068

71-
throw new InvalidArgumentException(self::EXC_MESSAGE);
69+
parent::offsetSet($index, $newval);
7270
}
7371

7472
/**
@@ -81,12 +79,10 @@ public function offsetSet($index, $newval): void
8179
*/
8280
public function append($value): void
8381
{
84-
if (\is_string($value)) {
85-
parent::append($value);
86-
87-
return;
82+
if (!($this->func)($value)) {
83+
throw new InvalidArgumentException($this->exceptionMessage);
8884
}
8985

90-
throw new InvalidArgumentException(self::EXC_MESSAGE);
86+
parent::append($value);
9187
}
9288
}

src/Linna/TypedArrayObject/ArrayArrayObject.php

Lines changed: 0 additions & 92 deletions
This file was deleted.

0 commit comments

Comments
 (0)