Skip to content

Commit 7708a10

Browse files
authored
Merge pull request #18 from linna/b3.0.0
B3.0.0 tests
2 parents 86b4751 + 174c5df commit 7708a10

34 files changed

+621
-1325
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: 7.1
17+
php: 8.1
1818
tests:
1919
override:
2020
-

.travis.yml

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

CHANGELOG.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,39 @@ 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-
## [v2.0.1](https://github.com/linna/typed-array/compare/v2.0.0...v2.0.1) - 2019-03-XX
9+
## [v3.0.0](https://github.com/linna/typed-array/compare/v2.0.1...v3.0.0) - 2022-05-11
10+
11+
### Added
12+
- php 8.0 support
13+
- `ArrayOfArrays` class
14+
- `ArrayOfBooleans` class
15+
- `ArrayOfCallable` class
16+
- `ArrayOfClasses` class
17+
- `ArrayOfFloats` class
18+
- `ArrayOfIntegers` class
19+
- `ArrayOfObjects` class
20+
- `ArrayOfStrings` class
21+
22+
### Changed
23+
- namespace from `Linna` to `Linna\TypedArrayObject`
24+
- type chek is performed als on method `ArrayObject->append()`
25+
26+
### removed
27+
- `Linna\TypedArray` class
28+
29+
## [v2.0.1](https://github.com/linna/typed-array/compare/v2.0.0...v2.0.1) - 2020-03-23
1030

1131
### Added
1232
- php 7.3 support
33+
- php 7.4 support
1334

1435
### Changed
1536
- `.scrutinizer.yml` updated for use phpunit from `vendor` directory
1637
- Merge pull request #14 from peter279k/enhance_stuffs with code enhancements
1738

39+
### Removed
40+
- Travis CI usage for test build.
41+
1842
## [v2.0.0](https://github.com/linna/typed-array/compare/v1.0.5...v2.0.0) - 2018-08-13
1943

2044
### Changed

README.md

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<div align="center">
1414

15-
[![Build Status](https://travis-ci.org/linna/typed-array.svg?branch=master)](https://travis-ci.org/linna/typed-array)
15+
[![Tests](https://github.com/linna/typed-array/actions/workflows/tests.yml/badge.svg)](https://github.com/linna/typed-array/actions/workflows/tests.yml)
1616
[![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)
1717
[![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)
1818
[![StyleCI](https://styleci.io/repos/93407083/shield?branch=master&style=flat)](https://styleci.io/repos/93407083)
@@ -23,58 +23,86 @@
2323
This package provide typed arrays for php as extension of native [ArrayObject](http://php.net/manual/en/class.arrayobject.php).
2424

2525
## Requirements
26-
This package require php 7.1
26+
This package require php 8.0
2727

2828
## Installation
2929
With composer:
3030
```
3131
composer require linna/typed-array
3232
```
3333

34+
## Classes
35+
36+
| Name | Native Type Handled | Description |
37+
|-----------------------|---------------------|------------------------------------------------------|
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 | |
46+
47+
3448
## Usage
3549
```php
36-
use Linna\TypedArray;
50+
use Linna\TypedArrayObject\ArrayOfIntegers;
51+
use Linna\TypedArrayObject\ArrayOfClasses;
3752

3853
//correct, only int passed to constructor.
39-
$intArray = new TypedArray('int', [1, 2, 3, 4]);
54+
$intArray = new ArrayOfIntegers([1, 2, 3, 4]);
4055

4156
//correct, int assigned
4257
$intArray[] = 5;
43-
44-
//throw InvalidArgumentException, string assigned.
58+
//throw InvalidArgumentException, string assigned, int expected.
4559
$intArray[] = 'a';
4660

61+
//correct, int used
62+
$intArray->append(5);
63+
//throw InvalidArgumentException, string used, int expected.
64+
$intArray->append('a');
65+
4766
//throw InvalidArgumentException, mixed array passed to constructor.
48-
$otherIntArray = new TypedArray('int', [1, 'a', 3, 4]);
67+
$otherIntArray = new ArrayOfIntegers([1, 'a', 3, 4]);
4968

5069
//correct, only Foo class instances passed to constructor.
51-
$fooArray = new TypedArray(Foo::class, [
70+
$fooArray = new ArrayOfClasses(Foo::class, [
5271
new Foo(),
5372
new Foo()
5473
]);
5574

5675
//correct, Foo() instance assigned.
5776
$fooArray[] = new Foo();
58-
5977
//throw InvalidArgumentException, Bar() instance assigned.
6078
$fooArray[] = new Bar();
6179

80+
//correct, Foo() instance used.
81+
$fooArray->append(new Foo());
82+
//throw InvalidArgumentException, Bar() instance used, Foo() instance expected.
83+
$fooArray->append(new Bar());
84+
6285
//throw InvalidArgumentException, mixed array of instances passed to constructor.
63-
$otherFooArray = new TypedArray(Foo::class, [
86+
$otherFooArray = new ArrayOfClasses(Foo::class, [
6487
new Foo(),
6588
new Bar()
6689
]);
6790
```
6891

6992
> **Note:** Allowed types are: *array*, *bool*, *callable*, *float*, *int*, *object*, *string* and all existing classes.
7093
94+
## 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. Do milliseconds really matters?
96+
97+
![Array Speed Test](array-speed-test-v3.png)
98+
7199
## Performance consideration for v2.0
72100
Compared to first version of the library, this version is a bit slower because after merging `TypedObjectArray` with `TypedArray`,
73101
there are more code that be executed when new instance is created and on assign operations.
74102

75103
![Array Speed Test](array-speed-test-v2.png)
76104

77-
## Performance consideration
105+
## Performance consideration for v1.0
78106
Compared to the parent class [ArrayObject](http://php.net/manual/en/class.arrayobject.php) typed arrays are slower on writing
79107
approximately from 6x to 8x. The slowness is due to not native `__construct()` and not native `offsetSet()`.
80108
Other operations do not have a speed difference with the native ArrayObject.

array-speed-test-v3.png

42.9 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: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +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-
//to avoid foreach, compare sizes of array
37-
//before and after apply a filter :)
38-
if (\count($input) > \count(\array_filter($input, 'is_string'))) {
39-
throw new InvalidArgumentException(self::EXC_MESSAGE);
38+
//check for invalid values inside provided array
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);
4043
}
4144

45+
$this->func = $func;
46+
4247
//call parent constructor
4348
parent::__construct($input, $flags, $iterator_class);
4449
}
@@ -57,13 +62,11 @@ public function __construct(array $input = [], int $flags = 0, string $iterator_
5762
*/
5863
public function offsetSet($index, $newval): void
5964
{
60-
if (\is_string($newval)) {
61-
parent::offsetSet($index, $newval);
62-
63-
return;
65+
if (!($this->func)($newval)) {
66+
throw new InvalidArgumentException($this->exceptionMessage);
6467
}
6568

66-
throw new InvalidArgumentException(self::EXC_MESSAGE);
69+
parent::offsetSet($index, $newval);
6770
}
6871

6972
/**
@@ -76,12 +79,10 @@ public function offsetSet($index, $newval): void
7679
*/
7780
public function append($value): void
7881
{
79-
if (\is_string($value)) {
80-
parent::append($value);
81-
82-
return;
82+
if (!($this->func)($value)) {
83+
throw new InvalidArgumentException($this->exceptionMessage);
8384
}
8485

85-
throw new InvalidArgumentException(self::EXC_MESSAGE);
86+
parent::append($value);
8687
}
8788
}

src/Linna/TypedArrayObject/ArrayArrayObject.php

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

0 commit comments

Comments
 (0)