From 49671fb8a9c31ebc83a95f15298d957a8233e187 Mon Sep 17 00:00:00 2001 From: odan Date: Sun, 5 Jan 2020 19:48:43 +0100 Subject: [PATCH 01/39] Use is null and blank string as invalid values --- src/ArrayReader.php | 57 ++++++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/src/ArrayReader.php b/src/ArrayReader.php index 06d8dbe..afe5ca3 100644 --- a/src/ArrayReader.php +++ b/src/ArrayReader.php @@ -3,7 +3,6 @@ namespace Selective\ArrayReader; use Cake\Chronos\Chronos; -use Exception; use InvalidArgumentException; /** @@ -32,13 +31,15 @@ public function __construct(array $data = []) * @param string $key The key * @param int|null $default The default value * + * @throws InvalidArgumentException + * * @return int The value */ public function getInt(string $key, int $default = null): int { $value = $this->find($key, $default); - if ($value === null) { + if ($this->isNullOrBlank($value)) { throw new InvalidArgumentException(sprintf('No value found for key "%s"', $key)); } @@ -49,19 +50,19 @@ public function getInt(string $key, int $default = null): int * Get value as integer or null. * * @param string $key The key - * @param int $default The default value + * @param int|null $default The default value * * @return int|null The value */ public function findInt(string $key, int $default = null) { - $result = $this->find($key, $default); + $value = $this->find($key, $default); - if ($result === null) { + if ($this->isNullOrBlank($value)) { return null; } - return (int)$result; + return (int)$value; } /** @@ -70,6 +71,8 @@ public function findInt(string $key, int $default = null) * @param string $key The key * @param string|null $default The default value * + * @throws InvalidArgumentException + * * @return string The value */ public function getString(string $key, string $default = null): string @@ -108,13 +111,15 @@ public function findString(string $key, string $default = null) * @param string $key The key * @param array|null $default The default value * + * @throws InvalidArgumentException + * * @return array The value */ public function getArray(string $key, array $default = null): array { $value = $this->find($key, $default); - if ($value === null) { + if ($this->isNullOrBlank($value)) { throw new InvalidArgumentException(sprintf('No value found for key "%s"', $key)); } @@ -133,7 +138,7 @@ public function findArray(string $key, array $default = null) { $value = $this->find($key, $default); - if ($value === null) { + if ($this->isNullOrBlank($value)) { return null; } @@ -146,13 +151,15 @@ public function findArray(string $key, array $default = null) * @param string $key The key * @param float|null $default The default value * + * @throws InvalidArgumentException + * * @return float The value */ public function getFloat(string $key, float $default = null): float { $value = $this->find($key, $default); - if ($value === null) { + if ($this->isNullOrBlank($value)) { throw new InvalidArgumentException(sprintf('No value found for key "%s"', $key)); } @@ -171,7 +178,7 @@ public function findFloat(string $key, float $default = null) { $value = $this->find($key, $default); - if ($value === null) { + if ($this->isNullOrBlank($value)) { return null; } @@ -184,13 +191,15 @@ public function findFloat(string $key, float $default = null) * @param string $key The key * @param bool|null $default The default value * + * @throws InvalidArgumentException + * * @return bool The value */ public function getBool(string $key, bool $default = null): bool { $value = $this->find($key, $default); - if ($value === null) { + if ($this->isNullOrBlank($value)) { throw new InvalidArgumentException(sprintf('No value found for key "%s"', $key)); } @@ -209,7 +218,7 @@ public function findBool(string $key, bool $default = null) { $value = $this->find($key, $default); - if ($value === null) { + if ($this->isNullOrBlank($value)) { return null; } @@ -222,13 +231,15 @@ public function findBool(string $key, bool $default = null) * @param string $key The key * @param Chronos|null $default The default value * + * @throws InvalidArgumentException + * * @return Chronos The value */ public function getChronos(string $key, Chronos $default = null): Chronos { $value = $this->find($key, $default); - if ($value === null) { + if ($this->isNullOrBlank($value)) { throw new InvalidArgumentException(sprintf('No value found for key "%s"', $key)); } @@ -245,15 +256,17 @@ public function getChronos(string $key, Chronos $default = null): Chronos * @param string $key The key * @param Chronos $default The default value * - * @throws Exception Chronos date time parsing error - * * @return Chronos|null The value */ public function findChronos(string $key, Chronos $default = null) { $value = $this->find($key, $default); - if ($value === null || $value instanceof Chronos) { + if ($this->isNullOrBlank($value)) { + return null; + } + + if ($value instanceof Chronos) { return $value; } @@ -332,4 +345,16 @@ public function isEmpty(string $path): bool { return empty($this->find($path)); } + + /** + * Is null or blank. + * + * @param mixed $value The value + * + * @return bool The status + */ + private function isNullOrBlank($value): bool + { + return $value === null || $value === ''; + } } From 22f5d02831d0f43869685da56448a02ab083183a Mon Sep 17 00:00:00 2001 From: odan Date: Sun, 5 Jan 2020 23:02:09 +0100 Subject: [PATCH 02/39] Add similar library --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 6effcd4..423619d 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,7 @@ echo $arrayReader->getString('key1.key2.key3'); ## Similar libraries * https://github.com/codeliner/array-reader +* https://github.com/adbario/php-dot-notation ## License From 024981a5313495675722ada8b0512ee04213c890 Mon Sep 17 00:00:00 2001 From: odan Date: Thu, 30 Jan 2020 16:07:03 +0100 Subject: [PATCH 03/39] Add support for keys with dot --- src/ArrayReader.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/ArrayReader.php b/src/ArrayReader.php index afe5ca3..804b5c5 100644 --- a/src/ArrayReader.php +++ b/src/ArrayReader.php @@ -283,6 +283,14 @@ public function findChronos(string $key, Chronos $default = null) */ public function find(string $path, $default = null) { + if (array_key_exists($path, $this->data)) { + return $this->data[$path] ?? $default; + } + + if (strpos($path, '.') === false) { + return $default; + } + $pathKeys = explode('.', $path); $arrayCopyOrValue = $this->data; From 0403718d411ce95382596b54aa2ce27e817459d1 Mon Sep 17 00:00:00 2001 From: peter279k Date: Wed, 26 Feb 2020 01:14:14 +0800 Subject: [PATCH 04/39] Add PHP 7.4 test --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 45adfdd..a924f20 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,7 @@ php: - 7.1 - 7.2 - 7.3 + - 7.4 dist: xenial From e1ff52adcb5a71a45e6a5e2ce64efd0e5ced235d Mon Sep 17 00:00:00 2001 From: peter279k Date: Fri, 28 Feb 2020 15:33:12 +0800 Subject: [PATCH 05/39] Add findInt and getInt tests --- tests/ArrayReaderTest.php | 103 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/tests/ArrayReaderTest.php b/tests/ArrayReaderTest.php index 321f295..926fc7d 100644 --- a/tests/ArrayReaderTest.php +++ b/tests/ArrayReaderTest.php @@ -11,6 +11,109 @@ */ class ArrayReaderTest extends TestCase { + /** + * Test. + * + * @dataProvider providerGetInt + * + * @param mixed $data The data + * @param string $key The lookup key + * @param mixed $default The default value + * @param mixed $expected The expected value + * + * @return void + */ + public function testGetInt($data, string $key, $default, $expected) + { + $reader = new ArrayReader($data); + static::assertSame($expected, $reader->getInt($key, $default)); + } + + /** + * Test. + * + * @dataProvider providerGetIntError + * + * @param mixed $data The data + * @param string $key The lookup key + * @param mixed $default The default value + * + * @return void + */ + public function testGetIntError($data, string $key, $default) + { + $reader = new ArrayReader($data); + + $this->expectException(InvalidArgumentException::class); + + $reader->getInt($key, $default); + } + + /** + * Test. + * + * @dataProvider providerFindInt + * + * @param mixed $data The data + * @param string $key The lookup key + * @param mixed $default The default value + * @param mixed $expected The expected value + * + * @return void + */ + public function testFindInt($data, string $key, $default, $expected) + { + $reader = new ArrayReader($data); + static::assertSame($expected, $reader->findInt($key, $default)); + } + + /** + * Provider. + * + * @return array[] The test data + */ + public function providerGetInt(): array + { + return [ + [[0, 1, 2, 3], 0, null, 0], + [[0, 1, 2, 3], 1, null, 1], + [[0, 1, 2, 3], 2, null, 2], + [[0, 1, 2, 3], 3, null, 3], + ]; + } + + /** + * Provider. + * + * @return array[] The test data + */ + public function providerGetIntError(): array + { + return [ + [[0, 1, 2, 3], 4, null], + [[0, 1, 2, 3, null], 4, null], + [[0, 1, 2, null, 4], 3, null], + ]; + } + + /** + * Provider. + * + * @return array[] The test data + */ + public function providerFindInt(): array + { + return [ + [[0, 1, 2, 3], 0, null, 0], + [[0, 1, 2, 3], 1, null, 1], + [[0, 1, 2, 3], 2, null, 2], + [[0, 1, 2, 3], 3, null, 3], + [[0, 1, 2, 3], 4, null, null], + [[0, 1, 2, 3, null], 4, null, null], + [[0, 1, 2, null, 4], 3, null, null], + ]; + } + /** * Test. * From f7e1b0d7ab7ac78799ab4075c5415f56998137b2 Mon Sep 17 00:00:00 2001 From: peter279k Date: Fri, 28 Feb 2020 18:26:56 +0800 Subject: [PATCH 06/39] Add findArray and getArray tests --- tests/ArrayReaderTest.php | 102 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/tests/ArrayReaderTest.php b/tests/ArrayReaderTest.php index 321f295..ae48cd8 100644 --- a/tests/ArrayReaderTest.php +++ b/tests/ArrayReaderTest.php @@ -115,4 +115,106 @@ public function providerFindString(): array [['key' => ['key2' => 'value']], 'key.nope', 'default', 'default'], ]; } + + /** + * Test. + * + * @dataProvider providerGetArray + * + * @param mixed $data The data + * @param string $key The lookup key + * @param mixed $default The default value + * @param mixed $expected The expected value + * + * @return void + */ + public function testGetArray($data, string $key, $default, $expected) + { + $reader = new ArrayReader($data); + static::assertSame($expected, $reader->getArray($key, $default)); + } + + /** + * Provider. + * + * @return array[] The test data + */ + public function providerGetArray(): array + { + return [ + [[[1, 2, 3, 4], [2, 3, 4, 5, 6]], 0, null, [1, 2, 3, 4]], + [[[1, 2, 3, 4], [2, 3, 4, 5, 6]], 1, null, [2, 3, 4, 5, 6]], + [[['key' => 'value'], ['key2' => 'value2']], 0, null, ['key' => 'value']], + [[['key' => 'value'], ['key2' => 'value2']], 1, null, ['key2' => 'value2']], + ]; + } + + /** + * Test. + * + * @dataProvider providerGetArrayError + * + * @param mixed $data The data + * @param string $key The lookup key + * @param mixed $default The default value + * + * @return void + */ + public function testGetArrayError($data, string $key, $default) + { + $reader = new ArrayReader($data); + + $this->expectException(InvalidArgumentException::class); + + $reader->getArray($key, $default); + } + + /** + * Provider. + * + * @return array[] The test data + */ + public function providerGetArrayError(): array + { + return [ + [[[1, 2, 3, 4], [2, 3, 4, 5, 6]], 2, null], + [[['key' => 'value'], ['key2' => 'value2']], 2, null], + ]; + } + + /** + * Test. + * + * @dataProvider providerFindArray + * + * @param mixed $data The data + * @param string $key The lookup key + * @param mixed $default The default value + * @param mixed $expected The expected value + * + * @return void + */ + public function testFindArray($data, string $key, $default, $expected) + { + $reader = new ArrayReader($data); + static::assertSame($expected, $reader->findArray($key, $default)); + } + + /** + * Provider. + * + * @return array[] The test data + */ + public function providerFindArray(): array + { + return [ + [[[1, 2, 3, 4], [2, 3, 4, 5, 6]], 0, null, [1, 2, 3, 4]], + [[[1, 2, 3, 4], [2, 3, 4, 5, 6]], 1, null, [2, 3, 4, 5, 6]], + [[[1, 2, 3, 4], [2, 3, 4, 5, 6]], 2, [], []], + [[[1, 2, 3, 4], [2, 3, 4, 5, 6]], 2, null, null], + [[['key' => 'value'], ['key2' => 'value2']], 0, null, ['key' => 'value']], + [[['key' => 'value'], ['key2' => 'value2']], 1, null, ['key2' => 'value2']], + [[['key' => 'value'], ['key2' => 'value2']], 2, null, null], + ]; + } } From 0e65566a114d6f9af7b1c3489d35aee0880c07ec Mon Sep 17 00:00:00 2001 From: odan Date: Fri, 3 Apr 2020 21:56:04 +0200 Subject: [PATCH 07/39] Update phpstan --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 641abdb..d0409ef 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ }, "require-dev": { "overtrue/phplint": "^1.1", - "phpstan/phpstan-shim": "^0.9|^0.10|^0.11", + "phpstan/phpstan": "*", "phpunit/phpunit": "^6|^7", "squizlabs/php_codesniffer": "^3.4" }, From eb80d2717b4a59dac9fe40cd6e13c09b8ecc5952 Mon Sep 17 00:00:00 2001 From: odan Date: Fri, 3 Apr 2020 21:57:18 +0200 Subject: [PATCH 08/39] Update year --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 30d7b83..0235044 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2019 odan +Copyright (c) 2020 odan Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal From 51e6fc902233b83b820438909dee18a45c277d99 Mon Sep 17 00:00:00 2001 From: odan Date: Fri, 3 Apr 2020 22:56:11 +0200 Subject: [PATCH 09/39] Add github actions --- .github/workflows/php.yml | 41 +++++++++++++++++++++++++++++++++++++++ README.md | 4 ++-- phpunit.xml | 2 +- tests/boostrap.php | 3 --- 4 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/php.yml delete mode 100644 tests/boostrap.php diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml new file mode 100644 index 0000000..aa61e9e --- /dev/null +++ b/.github/workflows/php.yml @@ -0,0 +1,41 @@ +name: PHP + +on: [push] + +jobs: + run: + runs-on: ${{ matrix.operating-system }} + strategy: + matrix: + operating-system: [ubuntu-latest] + php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4'] + name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }} + + steps: + - name: Checkout + uses: actions/checkout@v1 + + - name: Setup PHP + uses: shivammathur/setup-php@v1 + with: + php-version: ${{ matrix.php-versions }} + extensions: mbstring, pdo, pdo_mysql, intl, zip + coverage: none + + - name: Check PHP Version + run: php -v + + - name: Check Composer Version + run: composer -V + + - name: Check PHP Extensions + run: php -m + + - name: Validate composer.json and composer.lock + run: composer validate + + - name: Install dependencies + run: composer install --prefer-dist --no-progress --no-suggest + + - name: Run test suite + run: composer check-all diff --git a/README.md b/README.md index 423619d..dad2b69 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,8 @@ A strictly typed array reader for PHP. [![Latest Version on Packagist](https://img.shields.io/github/release/selective-php/array-reader.svg?style=flat-square)](https://packagist.org/packages/selective/array-reader) -[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) -[![Build Status](https://img.shields.io/travis/selective-php/array-reader/master.svg?style=flat-square)](https://travis-ci.org/selective-php/array-reader) +[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE) +[![Build Status](https://github.com/selective-php/array-reader/workflows/PHP/badge.svg)](https://github.com/selective-php/array-reader/actions) [![Coverage Status](https://img.shields.io/scrutinizer/coverage/g/selective-php/array-reader.svg?style=flat-square)](https://scrutinizer-ci.com/g/selective-php/array-reader/code-structure) [![Quality Score](https://img.shields.io/scrutinizer/quality/g/selective-php/array-reader.svg?style=flat-square)](https://scrutinizer-ci.com/g/selective-php/array-reader/?branch=master) [![Total Downloads](https://img.shields.io/packagist/dt/selective/array-reader.svg?style=flat-square)](https://packagist.org/packages/selective/array-reader/stats) diff --git a/phpunit.xml b/phpunit.xml index e3d5d55..1babff2 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,5 +1,5 @@ - Date: Fri, 3 Apr 2020 23:04:36 +0200 Subject: [PATCH 10/39] Fix cs --- src/ArrayReader.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/ArrayReader.php b/src/ArrayReader.php index 804b5c5..0112a7f 100644 --- a/src/ArrayReader.php +++ b/src/ArrayReader.php @@ -11,14 +11,14 @@ final class ArrayReader { /** - * @var array The data + * @var array The data */ private $data; /** * Constructor. * - * @param array $data Data + * @param array $data Data */ public function __construct(array $data = []) { @@ -109,11 +109,11 @@ public function findString(string $key, string $default = null) * Get value as array. * * @param string $key The key - * @param array|null $default The default value + * @param array|null $default The default value * * @throws InvalidArgumentException * - * @return array The value + * @return array The value */ public function getArray(string $key, array $default = null): array { @@ -130,9 +130,9 @@ public function getArray(string $key, array $default = null): array * Get value as array or null. * * @param string $key The key - * @param array $default The default value + * @param array $default The default value * - * @return array|null The value + * @return array|null The value */ public function findArray(string $key, array $default = null) { @@ -308,7 +308,7 @@ public function find(string $path, $default = null) /** * Return all data as array. * - * @return array The data + * @return array The data */ public function all(): array { From 0e3c36a7e9041ba070326f070edf7930388d2e79 Mon Sep 17 00:00:00 2001 From: odan Date: Thu, 16 Apr 2020 18:48:01 +0200 Subject: [PATCH 11/39] Add createFromArray method --- src/ArrayReader.php | 14 +++++++++++++- tests/ArrayReaderTest.php | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/ArrayReader.php b/src/ArrayReader.php index 0112a7f..558e6ab 100644 --- a/src/ArrayReader.php +++ b/src/ArrayReader.php @@ -16,7 +16,7 @@ final class ArrayReader private $data; /** - * Constructor. + * The constructor. * * @param array $data Data */ @@ -25,6 +25,18 @@ public function __construct(array $data = []) $this->data = $data; } + /** + * Crate instance from array. + * + * @param array $data The data + * + * @return $this The new instance + */ + public static function createFromArray(array $data = []): self + { + return new static($data); + } + /** * Get value as integer. * diff --git a/tests/ArrayReaderTest.php b/tests/ArrayReaderTest.php index b78d5ee..cbde76f 100644 --- a/tests/ArrayReaderTest.php +++ b/tests/ArrayReaderTest.php @@ -11,6 +11,24 @@ */ class ArrayReaderTest extends TestCase { + /** + * Test. + * + * @dataProvider providerGetInt + * + * @param mixed $data The data + * @param string $key The lookup key + * @param mixed $default The default value + * @param mixed $expected The expected value + * + * @return void + */ + public function testCreateFromArray($data, string $key, $default, $expected) + { + $reader = ArrayReader::createFromArray($data); + static::assertSame($expected, $reader->getInt($key, $default)); + } + /** * Test. * From 8efaca6125e444ffe1de2e1cc90685f7c919d6fb Mon Sep 17 00:00:00 2001 From: Daniel Opitz Date: Tue, 28 Apr 2020 09:20:38 +0200 Subject: [PATCH 12/39] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index dad2b69..f51bda3 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ echo $arrayReader->getString('key1.key2.key3'); * https://github.com/codeliner/array-reader * https://github.com/adbario/php-dot-notation +* https://symfony.com/doc/current/components/property_access.html ## License From f26642e48c9bca66f50c83a520c98654c98627e0 Mon Sep 17 00:00:00 2001 From: odan Date: Tue, 28 Apr 2020 09:24:27 +0200 Subject: [PATCH 13/39] Add createFromArray method --- src/ArrayReader.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ArrayReader.php b/src/ArrayReader.php index 558e6ab..ba96e04 100644 --- a/src/ArrayReader.php +++ b/src/ArrayReader.php @@ -30,7 +30,7 @@ public function __construct(array $data = []) * * @param array $data The data * - * @return $this The new instance + * @return self The new instance */ public static function createFromArray(array $data = []): self { From 6ac875d226e768e63fb8bc271719c3861a020689 Mon Sep 17 00:00:00 2001 From: odan Date: Tue, 28 Apr 2020 09:26:37 +0200 Subject: [PATCH 14/39] Add createFromArray method --- src/ArrayReader.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ArrayReader.php b/src/ArrayReader.php index ba96e04..ee97370 100644 --- a/src/ArrayReader.php +++ b/src/ArrayReader.php @@ -28,7 +28,7 @@ public function __construct(array $data = []) /** * Crate instance from array. * - * @param array $data The data + * @param array $data The data * * @return self The new instance */ From ba4ff529fe478d3d3a3449d8289ff0e5647cd1c5 Mon Sep 17 00:00:00 2001 From: peter279k Date: Mon, 10 Aug 2020 23:02:11 +0800 Subject: [PATCH 15/39] Remove Travis CI build setting --- .github/workflows/php.yml | 2 +- .travis.yml | 22 ---------------------- 2 files changed, 1 insertion(+), 23 deletions(-) delete mode 100644 .travis.yml diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index aa61e9e..5050ba0 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -1,6 +1,6 @@ name: PHP -on: [push] +on: [push, pull_request] jobs: run: diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index a924f20..0000000 --- a/.travis.yml +++ /dev/null @@ -1,22 +0,0 @@ -language: php - -php: - - 7.0 - - 7.1 - - 7.2 - - 7.3 - - 7.4 - -dist: xenial - -cache: - directories: - - $HOME/.composer/cache - -before_script: - - composer self-update - - composer install --no-interaction --prefer-dist --no-progress - - cd $TRAVIS_BUILD_DIR - -script: - - composer check-all From 17803d6e4657e8483fc4fbc68b95836446ae6c42 Mon Sep 17 00:00:00 2001 From: odan Date: Mon, 28 Dec 2020 18:17:49 +0100 Subject: [PATCH 16/39] Add PHP 8 support --- .cs.php | 81 +++++++++++++----------- .github/workflows/{php.yml => build.yml} | 23 ++++--- .gitignore | 3 +- .scrutinizer.yml | 4 +- LICENSE | 2 +- README.md | 14 ++-- composer.json | 53 ++++++++-------- phpunit.xml | 23 +++---- 8 files changed, 109 insertions(+), 94 deletions(-) rename .github/workflows/{php.yml => build.yml} (54%) diff --git a/.cs.php b/.cs.php index e983838..7b9c18a 100644 --- a/.cs.php +++ b/.cs.php @@ -1,42 +1,47 @@ setUsingCache(false) ->setRiskyAllowed(true) - //->setCacheFile(__DIR__ . '/.php_cs.cache') - ->setRules([ - '@PSR1' => true, - '@PSR2' => true, - '@Symfony' => true, - 'psr4' => true, - // custom rules - 'align_multiline_comment' => ['comment_type' => 'phpdocs_only'], // psr-5 - 'phpdoc_to_comment' => false, - 'array_indentation' => true, - 'array_syntax' => ['syntax' => 'short'], - 'cast_spaces' => ['space' => 'none'], - 'concat_space' => ['spacing' => 'one'], - 'compact_nullable_typehint' => true, - 'declare_equal_normalize' => ['space' => 'single'], - 'increment_style' => ['style' => 'post'], - 'list_syntax' => ['syntax' => 'long'], - 'no_short_echo_tag' => true, - 'phpdoc_add_missing_param_annotation' => ['only_untyped' => false], - 'phpdoc_align' => false, - 'phpdoc_no_empty_return' => false, - 'phpdoc_order' => true, // psr-5 - 'phpdoc_no_useless_inheritdoc' => false, - 'protected_to_private' => false, - 'yoda_style' => false, - 'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'], - 'ordered_imports' => [ - 'sort_algorithm' => 'alpha', - 'imports_order' => ['class', 'const', 'function'] - ], - ]) - ->setFinder(PhpCsFixer\Finder::create() - ->in(__DIR__ . '/src') - ->in(__DIR__ . '/tests') - ->name('*.php') - ->ignoreDotFiles(true) - ->ignoreVCS(true)); + ->setRules( + [ + '@PSR1' => true, + '@PSR2' => true, + '@Symfony' => true, + 'psr4' => true, + // custom rules + 'align_multiline_comment' => ['comment_type' => 'phpdocs_only'], // psr-5 + 'phpdoc_to_comment' => false, + 'no_superfluous_phpdoc_tags' => false, + 'array_indentation' => true, + 'array_syntax' => ['syntax' => 'short'], + 'cast_spaces' => ['space' => 'none'], + 'concat_space' => ['spacing' => 'one'], + 'compact_nullable_typehint' => true, + 'declare_equal_normalize' => ['space' => 'single'], + 'increment_style' => ['style' => 'post'], + 'list_syntax' => ['syntax' => 'short'], + 'no_short_echo_tag' => true, + 'phpdoc_add_missing_param_annotation' => ['only_untyped' => false], + 'phpdoc_align' => false, + 'phpdoc_no_empty_return' => false, + 'phpdoc_order' => true, // psr-5 + 'phpdoc_no_useless_inheritdoc' => false, + 'protected_to_private' => false, + 'yoda_style' => false, + 'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'], + 'ordered_imports' => [ + 'sort_algorithm' => 'alpha', + 'imports_order' => ['class', 'const', 'function'] + ], + 'single_line_throw' => false, + ] + ) + ->setFinder( + PhpCsFixer\Finder::create() + ->in(__DIR__ . '/src') + ->in(__DIR__ . '/tests') + ->name('*.php') + ->ignoreDotFiles(true) + ->ignoreVCS(true) + ); diff --git a/.github/workflows/php.yml b/.github/workflows/build.yml similarity index 54% rename from .github/workflows/php.yml rename to .github/workflows/build.yml index 5050ba0..4758e33 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/build.yml @@ -1,6 +1,6 @@ -name: PHP +name: build -on: [push, pull_request] +on: [ push, pull_request ] jobs: run: @@ -8,7 +8,7 @@ jobs: strategy: matrix: operating-system: [ubuntu-latest] - php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4'] + php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4', '8.0'] name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }} steps: @@ -16,10 +16,10 @@ jobs: uses: actions/checkout@v1 - name: Setup PHP - uses: shivammathur/setup-php@v1 + uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php-versions }} - extensions: mbstring, pdo, pdo_mysql, intl, zip + extensions: mbstring, intl, zip coverage: none - name: Check PHP Version @@ -34,8 +34,15 @@ jobs: - name: Validate composer.json and composer.lock run: composer validate - - name: Install dependencies - run: composer install --prefer-dist --no-progress --no-suggest + - name: Install dependencies for PHP 7 + if: matrix.php-versions < '8.0' + run: composer update --prefer-dist --no-progress + + - name: Install dependencies for PHP 8 + if: matrix.php-versions >= '8.0' + run: composer update --prefer-dist --no-progress --ignore-platform-req=php - name: Run test suite - run: composer check-all + run: composer check + env: + PHP_CS_FIXER_IGNORE_ENV: 1 diff --git a/.gitignore b/.gitignore index a27d9ba..22f5e7f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ composer.lock nbproject/ vendor/ -build/ \ No newline at end of file +build/ +.phpunit.result.cache \ No newline at end of file diff --git a/.scrutinizer.yml b/.scrutinizer.yml index d3ff45f..fce7d02 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -12,7 +12,7 @@ tools: build: environment: - php: 7.2 + php: 7.4 mysql: false node: false postgresql: false @@ -34,7 +34,7 @@ build: tests: before: - - command: composer test-coverage + command: composer test:coverage coverage: file: 'build/logs/clover.xml' format: 'clover' diff --git a/LICENSE b/LICENSE index 0235044..fa19b85 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2020 odan +Copyright (c) 2021 odan Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index f51bda3..fc4a9af 100644 --- a/README.md +++ b/README.md @@ -2,17 +2,17 @@ A strictly typed array reader for PHP. -[![Latest Version on Packagist](https://img.shields.io/github/release/selective-php/array-reader.svg?style=flat-square)](https://packagist.org/packages/selective/array-reader) -[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE) -[![Build Status](https://github.com/selective-php/array-reader/workflows/PHP/badge.svg)](https://github.com/selective-php/array-reader/actions) -[![Coverage Status](https://img.shields.io/scrutinizer/coverage/g/selective-php/array-reader.svg?style=flat-square)](https://scrutinizer-ci.com/g/selective-php/array-reader/code-structure) -[![Quality Score](https://img.shields.io/scrutinizer/quality/g/selective-php/array-reader.svg?style=flat-square)](https://scrutinizer-ci.com/g/selective-php/array-reader/?branch=master) -[![Total Downloads](https://img.shields.io/packagist/dt/selective/array-reader.svg?style=flat-square)](https://packagist.org/packages/selective/array-reader/stats) +[![Latest Version on Packagist](https://img.shields.io/github/release/selective-php/array-reader.svg)](https://packagist.org/packages/selective/array-reader) +[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE) +[![Build Status](https://github.com/selective-php/array-reader/workflows/build/badge.svg)](https://github.com/selective-php/array-reader/actions) +[![Coverage Status](https://img.shields.io/scrutinizer/coverage/g/selective-php/array-reader.svg)](https://scrutinizer-ci.com/g/selective-php/array-reader/code-structure) +[![Quality Score](https://img.shields.io/scrutinizer/quality/g/selective-php/array-reader.svg)](https://scrutinizer-ci.com/g/selective-php/array-reader/?branch=master) +[![Total Downloads](https://img.shields.io/packagist/dt/selective/array-reader.svg)](https://packagist.org/packages/selective/array-reader/stats) ## Requirements -* PHP 7.0+ +* PHP 7.0+ or 8.0+ ## Installation diff --git a/composer.json b/composer.json index d0409ef..447ea87 100644 --- a/composer.json +++ b/composer.json @@ -13,43 +13,44 @@ "homepage": "https://github.com/selective-php/array-reader", "license": "MIT", "require": { - "php": "^7.0", - "cakephp/chronos": "^1.2|^2" + "php": "^7.0 || ^8.0", + "cakephp/chronos": "^1.2 || ^2" }, "require-dev": { - "overtrue/phplint": "^1.1", - "phpstan/phpstan": "*", - "phpunit/phpunit": "^6|^7", - "squizlabs/php_codesniffer": "^3.4" + "friendsofphp/php-cs-fixer": "^2.16", + "overtrue/phplint": "^2.3", + "phpstan/phpstan": "0.*", + "phpunit/phpunit": "^9", + "squizlabs/php_codesniffer": "^3.5" }, - "scripts": { - "test": "phpunit --configuration phpunit.xml", - "test-coverage": "phpunit --configuration phpunit.xml --coverage-clover build/logs/clover.xml --coverage-html build/coverage", - "check-style": "phpcs --standard=phpcs.xml", - "fix-style": "phpcbf --standard=phpcs.xml", - "phpstan": "phpstan analyse src tests --level=max -c phpstan.neon --no-progress", - "lint": "phplint ./ --exclude=vendor --no-interaction --no-cache", - "install-cs": "php -r \"@mkdir('build'); copy('https://cs.symfony.com/download/php-cs-fixer-v2.phar', 'build/php-cs-fixer-v2.phar');\"", - "fix-cs": "php build/php-cs-fixer-v2.phar fix --config=.cs.php", - "check-cs": "php build/php-cs-fixer-v2.phar fix --dry-run --format=txt --verbose --diff --diff-format=udiff --config=.cs.php", - "check-all": [ - "@lint", - "@check-style", - "@phpstan", - "@test-coverage" - ] + "config": { + "sort-packages": true }, "autoload": { "psr-4": { - "Selective\\ArrayReader\\": "src" + "Selective\\ArrayReader\\": "src/" } }, "autoload-dev": { "psr-4": { - "Selective\\ArrayReader\\Test\\": "tests" + "Selective\\ArrayReader\\Test\\": "tests/" } }, - "config": { - "sort-packages": true + "scripts": { + "check": [ + "@lint", + "@cs:check", + "@sniffer:check", + "@phpstan", + "@test:coverage" + ], + "cs:check": "php-cs-fixer fix --dry-run --format=txt --verbose --diff --diff-format=udiff --config=.cs.php", + "cs:fix": "php-cs-fixer fix --config=.cs.php", + "lint": "phplint ./ --exclude=vendor --no-interaction --no-cache", + "phpstan": "phpstan analyse src tests --level=max -c phpstan.neon --no-progress --ansi", + "sniffer:check": "phpcs --standard=phpcs.xml", + "sniffer:fix": "phpcbf --standard=phpcs.xml", + "test": "phpunit --configuration phpunit.xml --do-not-cache-result --colors=always", + "test:coverage": "phpunit --configuration phpunit.xml --do-not-cache-result --colors=always --coverage-clover build/logs/clover.xml --coverage-html build/coverage" } } diff --git a/phpunit.xml b/phpunit.xml index 1babff2..f2e36c3 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,21 +1,22 @@ - + xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"> + + + src + + + vendor + build + + tests - - - src - - vendor - build - - - From 1d1c98a68619464e2ccd3f12f07d37dbda076099 Mon Sep 17 00:00:00 2001 From: odan Date: Mon, 28 Dec 2020 18:19:35 +0100 Subject: [PATCH 17/39] Add PHP 8 support --- .github/workflows/build.yml | 2 +- README.md | 2 +- composer.json | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 4758e33..7878383 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -8,7 +8,7 @@ jobs: strategy: matrix: operating-system: [ubuntu-latest] - php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4', '8.0'] + php-versions: ['7.3', '7.4', '8.0'] name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }} steps: diff --git a/README.md b/README.md index fc4a9af..ed29cce 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ A strictly typed array reader for PHP. ## Requirements -* PHP 7.0+ or 8.0+ +* PHP 7.3+ or 8.0+ ## Installation diff --git a/composer.json b/composer.json index 447ea87..f7332c9 100644 --- a/composer.json +++ b/composer.json @@ -13,8 +13,8 @@ "homepage": "https://github.com/selective-php/array-reader", "license": "MIT", "require": { - "php": "^7.0 || ^8.0", - "cakephp/chronos": "^1.2 || ^2" + "php": "^7.3 || ^8.0", + "cakephp/chronos": "^2" }, "require-dev": { "friendsofphp/php-cs-fixer": "^2.16", From e316995cc82dd4a76a756bfb3f9f0334a9c5ecf1 Mon Sep 17 00:00:00 2001 From: odan Date: Mon, 28 Dec 2020 18:25:58 +0100 Subject: [PATCH 18/39] Add PHP 8 support --- tests/ArrayReaderTest.php | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/tests/ArrayReaderTest.php b/tests/ArrayReaderTest.php index cbde76f..9d04b59 100644 --- a/tests/ArrayReaderTest.php +++ b/tests/ArrayReaderTest.php @@ -17,13 +17,13 @@ class ArrayReaderTest extends TestCase * @dataProvider providerGetInt * * @param mixed $data The data - * @param string $key The lookup key + * @param string|int $key The lookup key * @param mixed $default The default value * @param mixed $expected The expected value * * @return void */ - public function testCreateFromArray($data, string $key, $default, $expected) + public function testCreateFromArray($data, $key, $default, $expected): void { $reader = ArrayReader::createFromArray($data); static::assertSame($expected, $reader->getInt($key, $default)); @@ -35,13 +35,13 @@ public function testCreateFromArray($data, string $key, $default, $expected) * @dataProvider providerGetInt * * @param mixed $data The data - * @param string $key The lookup key + * @param string|key $key The lookup key * @param mixed $default The default value * @param mixed $expected The expected value * * @return void */ - public function testGetInt($data, string $key, $default, $expected) + public function testGetInt($data, $key, $default, $expected): void { $reader = new ArrayReader($data); static::assertSame($expected, $reader->getInt($key, $default)); @@ -53,12 +53,12 @@ public function testGetInt($data, string $key, $default, $expected) * @dataProvider providerGetIntError * * @param mixed $data The data - * @param string $key The lookup key + * @param string|int $key The lookup key * @param mixed $default The default value * * @return void */ - public function testGetIntError($data, string $key, $default) + public function testGetIntError($data, $key, $default): void { $reader = new ArrayReader($data); @@ -73,13 +73,13 @@ public function testGetIntError($data, string $key, $default) * @dataProvider providerFindInt * * @param mixed $data The data - * @param string $key The lookup key + * @param string|int $key The lookup key * @param mixed $default The default value * @param mixed $expected The expected value * * @return void */ - public function testFindInt($data, string $key, $default, $expected) + public function testFindInt($data, $key, $default, $expected): void { $reader = new ArrayReader($data); static::assertSame($expected, $reader->findInt($key, $default)); @@ -144,7 +144,7 @@ public function providerFindInt(): array * * @return void */ - public function testGetString($data, string $key, $default, $expected) + public function testGetString($data, string $key, $default, $expected): void { $reader = new ArrayReader($data); static::assertSame($expected, $reader->findString($key, $default)); @@ -177,7 +177,7 @@ public function providerGetString(): array * * @return void */ - public function testGetStringError($data, string $key) + public function testGetStringError($data, string $key): void { $this->expectException(InvalidArgumentException::class); @@ -214,7 +214,7 @@ public function providerGetStringError(): array * * @return void */ - public function testFindString($data, string $key, $default, $expected) + public function testFindString($data, string $key, $default, $expected): void { $reader = new ArrayReader($data); static::assertSame($expected, $reader->findString($key, $default)); @@ -243,13 +243,13 @@ public function providerFindString(): array * @dataProvider providerGetArray * * @param mixed $data The data - * @param string $key The lookup key + * @param string|int $key The lookup key * @param mixed $default The default value * @param mixed $expected The expected value * * @return void */ - public function testGetArray($data, string $key, $default, $expected) + public function testGetArray($data, $key, $default, $expected): void { $reader = new ArrayReader($data); static::assertSame($expected, $reader->getArray($key, $default)); @@ -276,12 +276,12 @@ public function providerGetArray(): array * @dataProvider providerGetArrayError * * @param mixed $data The data - * @param string $key The lookup key + * @param string|int $key The lookup key * @param mixed $default The default value * * @return void */ - public function testGetArrayError($data, string $key, $default) + public function testGetArrayError($data, $key, $default): void { $reader = new ArrayReader($data); @@ -309,13 +309,13 @@ public function providerGetArrayError(): array * @dataProvider providerFindArray * * @param mixed $data The data - * @param string $key The lookup key + * @param string|int $key The lookup key * @param mixed $default The default value * @param mixed $expected The expected value * * @return void */ - public function testFindArray($data, string $key, $default, $expected) + public function testFindArray($data, $key, $default, $expected): void { $reader = new ArrayReader($data); static::assertSame($expected, $reader->findArray($key, $default)); From 845dd7c9ddd3ac320ad966d1ecc02dc4b3b9f7d1 Mon Sep 17 00:00:00 2001 From: odan Date: Mon, 28 Dec 2020 18:34:26 +0100 Subject: [PATCH 19/39] Add PHP 8 support --- composer.json | 2 +- tests/ArrayReaderTest.php | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/composer.json b/composer.json index f7332c9..6b17650 100644 --- a/composer.json +++ b/composer.json @@ -47,7 +47,7 @@ "cs:check": "php-cs-fixer fix --dry-run --format=txt --verbose --diff --diff-format=udiff --config=.cs.php", "cs:fix": "php-cs-fixer fix --config=.cs.php", "lint": "phplint ./ --exclude=vendor --no-interaction --no-cache", - "phpstan": "phpstan analyse src tests --level=max -c phpstan.neon --no-progress --ansi", + "phpstan": "phpstan analyse src --level=max -c phpstan.neon --no-progress --ansi", "sniffer:check": "phpcs --standard=phpcs.xml", "sniffer:fix": "phpcbf --standard=phpcs.xml", "test": "phpunit --configuration phpunit.xml --do-not-cache-result --colors=always", diff --git a/tests/ArrayReaderTest.php b/tests/ArrayReaderTest.php index 9d04b59..bc71ce5 100644 --- a/tests/ArrayReaderTest.php +++ b/tests/ArrayReaderTest.php @@ -26,7 +26,7 @@ class ArrayReaderTest extends TestCase public function testCreateFromArray($data, $key, $default, $expected): void { $reader = ArrayReader::createFromArray($data); - static::assertSame($expected, $reader->getInt($key, $default)); + $this->assertSame($expected, $reader->getInt($key, $default)); } /** @@ -44,7 +44,7 @@ public function testCreateFromArray($data, $key, $default, $expected): void public function testGetInt($data, $key, $default, $expected): void { $reader = new ArrayReader($data); - static::assertSame($expected, $reader->getInt($key, $default)); + $this->assertSame($expected, $reader->getInt($key, $default)); } /** @@ -82,7 +82,7 @@ public function testGetIntError($data, $key, $default): void public function testFindInt($data, $key, $default, $expected): void { $reader = new ArrayReader($data); - static::assertSame($expected, $reader->findInt($key, $default)); + $this->assertSame($expected, $reader->findInt($key, $default)); } /** @@ -147,8 +147,8 @@ public function providerFindInt(): array public function testGetString($data, string $key, $default, $expected): void { $reader = new ArrayReader($data); - static::assertSame($expected, $reader->findString($key, $default)); - static::assertSame($expected, $reader->getString($key, $default)); + $this->assertSame($expected, $reader->findString($key, $default)); + $this->assertSame($expected, $reader->getString($key, $default)); } /** @@ -184,7 +184,7 @@ public function testGetStringError($data, string $key): void $reader = new ArrayReader($data); $reader->getString($key); - static::assertTrue(true); + $this->assertTrue(true); } /** @@ -217,7 +217,7 @@ public function providerGetStringError(): array public function testFindString($data, string $key, $default, $expected): void { $reader = new ArrayReader($data); - static::assertSame($expected, $reader->findString($key, $default)); + $this->assertSame($expected, $reader->findString($key, $default)); } /** @@ -252,7 +252,7 @@ public function providerFindString(): array public function testGetArray($data, $key, $default, $expected): void { $reader = new ArrayReader($data); - static::assertSame($expected, $reader->getArray($key, $default)); + $this->assertSame($expected, $reader->getArray($key, $default)); } /** @@ -318,7 +318,7 @@ public function providerGetArrayError(): array public function testFindArray($data, $key, $default, $expected): void { $reader = new ArrayReader($data); - static::assertSame($expected, $reader->findArray($key, $default)); + $this->assertSame($expected, $reader->findArray($key, $default)); } /** From 2be4376acf448aef10931a586db1ae7e3250cc5d Mon Sep 17 00:00:00 2001 From: Daniel Opitz Date: Fri, 22 Jan 2021 12:56:46 +0100 Subject: [PATCH 20/39] Update readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ed29cce..80a33e9 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ echo $arrayReader->getString('key1.key2.key3'); ## Similar libraries +* https://github.com/michaelpetri/typed-input * https://github.com/codeliner/array-reader * https://github.com/adbario/php-dot-notation * https://symfony.com/doc/current/components/property_access.html From 02285cec66a44202b52104935689985f5f034767 Mon Sep 17 00:00:00 2001 From: Daniel Opitz Date: Wed, 3 Mar 2021 09:38:28 +0100 Subject: [PATCH 21/39] Update README.md --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index 80a33e9..f851b2e 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,23 @@ $arrayReader = new ArrayReader([ echo $arrayReader->getString('key1.key2.key3'); ``` +## Better Code Quality + +Converting complex data with simple PHP works by using a lot of type casting and `if` conditions etc. +This leads to very high cyclomatic complexity and nesting depth, and thus poor "code rating". + +**Before**: Conditions: 10, Paths: 512, CRAP Score: 10 +
+ Click to expand! + +
+ +**After**: Conditions: 1, Paths: 1, CRAP Score: 1 +
+ Click to expand! + +
+ ## Similar libraries * https://github.com/michaelpetri/typed-input From 02f6a079a2e3f2c073c32c3d29c449ce94143d3f Mon Sep 17 00:00:00 2001 From: peter279k Date: Wed, 8 Dec 2021 17:23:33 +0800 Subject: [PATCH 22/39] Upgrade PHPStan and PHP-CS-Fixer --- .cs.php | 4 ++-- composer.json | 6 +++--- phpstan.neon | 8 ++++++++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/.cs.php b/.cs.php index 7b9c18a..e4ab7fc 100644 --- a/.cs.php +++ b/.cs.php @@ -8,7 +8,7 @@ '@PSR1' => true, '@PSR2' => true, '@Symfony' => true, - 'psr4' => true, + 'psr_autoloading' => true, // custom rules 'align_multiline_comment' => ['comment_type' => 'phpdocs_only'], // psr-5 'phpdoc_to_comment' => false, @@ -21,7 +21,7 @@ 'declare_equal_normalize' => ['space' => 'single'], 'increment_style' => ['style' => 'post'], 'list_syntax' => ['syntax' => 'short'], - 'no_short_echo_tag' => true, + 'echo_tag_syntax' => ['format' => 'long'], 'phpdoc_add_missing_param_annotation' => ['only_untyped' => false], 'phpdoc_align' => false, 'phpdoc_no_empty_return' => false, diff --git a/composer.json b/composer.json index 6b17650..28b0d4c 100644 --- a/composer.json +++ b/composer.json @@ -17,9 +17,9 @@ "cakephp/chronos": "^2" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^2.16", + "friendsofphp/php-cs-fixer": "^3", "overtrue/phplint": "^2.3", - "phpstan/phpstan": "0.*", + "phpstan/phpstan": "^1", "phpunit/phpunit": "^9", "squizlabs/php_codesniffer": "^3.5" }, @@ -44,7 +44,7 @@ "@phpstan", "@test:coverage" ], - "cs:check": "php-cs-fixer fix --dry-run --format=txt --verbose --diff --diff-format=udiff --config=.cs.php", + "cs:check": "php-cs-fixer fix --dry-run --format=txt --verbose --diff --config=.cs.php", "cs:fix": "php-cs-fixer fix --config=.cs.php", "lint": "phplint ./ --exclude=vendor --no-interaction --no-cache", "phpstan": "phpstan analyse src --level=max -c phpstan.neon --no-progress --ansi", diff --git a/phpstan.neon b/phpstan.neon index e69de29..9854c88 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -0,0 +1,8 @@ +parameters: + reportUnmatchedIgnoredErrors: false + ignoreErrors: + - '#Cannot cast mixed to int.#' + - '#Cannot cast mixed to string.#' + - '#Cannot cast mixed to float.#' + - '#Parameter \#1 \$time of class Cake\\Chronos\\Chronos constructor expects DateTimeInterface\|int\|string\|null, mixed given.#' + - '#Cannot access offset string on mixed.#' From 9c5de04f4b3feee7a7b4e88e329224723c608353 Mon Sep 17 00:00:00 2001 From: Daniel Opitz Date: Sun, 27 Nov 2022 14:50:24 +0100 Subject: [PATCH 23/39] Require PHP 8 --- .cs.php | 7 +++++-- .editorconfig | 3 +++ .github/workflows/build.yml | 18 +++++----------- .scrutinizer.yml | 20 ++++++++++-------- LICENSE | 2 +- README.md | 2 +- composer.json | 41 +++++++++++++++++-------------------- phpcs.xml | 18 ---------------- 8 files changed, 45 insertions(+), 66 deletions(-) create mode 100644 .editorconfig diff --git a/.cs.php b/.cs.php index e4ab7fc..9653bee 100644 --- a/.cs.php +++ b/.cs.php @@ -1,6 +1,8 @@ setUsingCache(false) ->setRiskyAllowed(true) ->setRules( @@ -18,7 +20,6 @@ 'cast_spaces' => ['space' => 'none'], 'concat_space' => ['spacing' => 'one'], 'compact_nullable_typehint' => true, - 'declare_equal_normalize' => ['space' => 'single'], 'increment_style' => ['style' => 'post'], 'list_syntax' => ['syntax' => 'short'], 'echo_tag_syntax' => ['format' => 'long'], @@ -35,6 +36,8 @@ 'imports_order' => ['class', 'const', 'function'] ], 'single_line_throw' => false, + 'fully_qualified_strict_types' => true, + 'global_namespace_import' => false, ] ) ->setFinder( diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..ae90760 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,3 @@ +[composer.json] +indent_style = space +indent_size = 4 diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7878383..8ca1e0f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -7,8 +7,8 @@ jobs: runs-on: ${{ matrix.operating-system }} strategy: matrix: - operating-system: [ubuntu-latest] - php-versions: ['7.3', '7.4', '8.0'] + operating-system: [ ubuntu-latest ] + php-versions: [ '8.0', '8.1' ] name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }} steps: @@ -19,7 +19,6 @@ jobs: uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php-versions }} - extensions: mbstring, intl, zip coverage: none - name: Check PHP Version @@ -34,15 +33,8 @@ jobs: - name: Validate composer.json and composer.lock run: composer validate - - name: Install dependencies for PHP 7 - if: matrix.php-versions < '8.0' - run: composer update --prefer-dist --no-progress - - - name: Install dependencies for PHP 8 - if: matrix.php-versions >= '8.0' - run: composer update --prefer-dist --no-progress --ignore-platform-req=php + - name: Install dependencies + run: composer install --prefer-dist --no-progress --no-suggest - name: Run test suite - run: composer check - env: - PHP_CS_FIXER_IGNORE_ENV: 1 + run: composer test:all diff --git a/.scrutinizer.yml b/.scrutinizer.yml index fce7d02..5df7262 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -1,6 +1,6 @@ filter: - paths: ["src/*"] - excluded_paths: ["vendor/*", "tests/*"] + paths: [ "src/*" ] + excluded_paths: [ "vendor/*", "tests/*" ] checks: php: @@ -12,7 +12,10 @@ tools: build: environment: - php: 7.4 + php: + version: 8.1.2 + ini: + xdebug.mode: coverage mysql: false node: false postgresql: false @@ -30,11 +33,10 @@ build: dependencies: before: - composer self-update - - composer update --no-interaction --prefer-dist --no-progress + - composer install --no-interaction --prefer-dist --no-progress tests: before: - - - command: composer test:coverage - coverage: - file: 'build/logs/clover.xml' - format: 'clover' + - command: composer test:coverage + coverage: + file: 'build/logs/clover.xml' + format: 'clover' diff --git a/LICENSE b/LICENSE index fa19b85..bf7a9a3 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2021 odan +Copyright (c) 2023 odan Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index f851b2e..f3aef47 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ A strictly typed array reader for PHP. ## Requirements -* PHP 7.3+ or 8.0+ +* PHP 8.0+ ## Installation diff --git a/composer.json b/composer.json index 28b0d4c..a133155 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,8 @@ { "name": "selective/array-reader", - "type": "library", "description": "A strictly typed array reader", + "license": "MIT", + "type": "library", "keywords": [ "array", "reader", @@ -11,20 +12,15 @@ "strong" ], "homepage": "https://github.com/selective-php/array-reader", - "license": "MIT", "require": { - "php": "^7.3 || ^8.0", - "cakephp/chronos": "^2" + "php": "^8.0", + "cakephp/chronos": "^2 || ^3" }, "require-dev": { "friendsofphp/php-cs-fixer": "^3", - "overtrue/phplint": "^2.3", "phpstan/phpstan": "^1", - "phpunit/phpunit": "^9", - "squizlabs/php_codesniffer": "^3.5" - }, - "config": { - "sort-packages": true + "phpunit/phpunit": "^9 || ^10", + "squizlabs/php_codesniffer": "^3" }, "autoload": { "psr-4": { @@ -36,21 +32,22 @@ "Selective\\ArrayReader\\Test\\": "tests/" } }, + "config": { + "sort-packages": true + }, "scripts": { - "check": [ - "@lint", - "@cs:check", - "@sniffer:check", - "@phpstan", - "@test:coverage" - ], - "cs:check": "php-cs-fixer fix --dry-run --format=txt --verbose --diff --config=.cs.php", - "cs:fix": "php-cs-fixer fix --config=.cs.php", - "lint": "phplint ./ --exclude=vendor --no-interaction --no-cache", - "phpstan": "phpstan analyse src --level=max -c phpstan.neon --no-progress --ansi", + "cs:check": "php-cs-fixer fix --dry-run --format=txt --verbose --diff --config=.cs.php --ansi", + "cs:fix": "php-cs-fixer fix --config=.cs.php --ansi", "sniffer:check": "phpcs --standard=phpcs.xml", "sniffer:fix": "phpcbf --standard=phpcs.xml", + "stan": "phpstan analyse -c phpstan.neon --no-progress --ansi --xdebug", "test": "phpunit --configuration phpunit.xml --do-not-cache-result --colors=always", - "test:coverage": "phpunit --configuration phpunit.xml --do-not-cache-result --colors=always --coverage-clover build/logs/clover.xml --coverage-html build/coverage" + "test:all": [ + "@cs:check", + "@sniffer:check", + "@stan", + "@test" + ], + "test:coverage": "php -d xdebug.mode=coverage -r \"require 'vendor/bin/phpunit';\" -- --configuration phpunit.xml --do-not-cache-result --colors=always --coverage-clover build/logs/clover.xml --coverage-html build/coverage" } } diff --git a/phpcs.xml b/phpcs.xml index c825571..e218be3 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -12,24 +12,6 @@ - - - warning - */tests/ - - - warning - - - warning - */config/ - - - warning - - - warning - warning From 2698c327f2f8aa46247e9c43b7615697824b11d1 Mon Sep 17 00:00:00 2001 From: Daniel Opitz Date: Sun, 27 Nov 2022 15:02:42 +0100 Subject: [PATCH 24/39] Fix phpstan settings --- phpstan.neon | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/phpstan.neon b/phpstan.neon index 9854c88..461b9cf 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,4 +1,8 @@ parameters: + level: 8 + paths: + - src + - tests reportUnmatchedIgnoredErrors: false ignoreErrors: - '#Cannot cast mixed to int.#' From 74de5edd697b41feb5c6492183c2299f4f605fb7 Mon Sep 17 00:00:00 2001 From: Daniel Opitz Date: Sun, 27 Nov 2022 15:15:21 +0100 Subject: [PATCH 25/39] Add parameter and return types --- phpstan.neon | 9 +++------ src/ArrayReader.php | 44 ++++++++++++++++++++++---------------------- 2 files changed, 25 insertions(+), 28 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 461b9cf..d3e46c9 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -2,11 +2,8 @@ parameters: level: 8 paths: - src - - tests reportUnmatchedIgnoredErrors: false ignoreErrors: - - '#Cannot cast mixed to int.#' - - '#Cannot cast mixed to string.#' - - '#Cannot cast mixed to float.#' - - '#Parameter \#1 \$time of class Cake\\Chronos\\Chronos constructor expects DateTimeInterface\|int\|string\|null, mixed given.#' - - '#Cannot access offset string on mixed.#' + - '#Property (.*?) type has no value type specified in iterable type array#' + - '#Method (.*?) has parameter (.*?) with no value type specified in iterable type array#' + - '#Method (.*?) return type has no value type specified in iterable type array#' \ No newline at end of file diff --git a/src/ArrayReader.php b/src/ArrayReader.php index ee97370..eb214c8 100644 --- a/src/ArrayReader.php +++ b/src/ArrayReader.php @@ -11,14 +11,14 @@ final class ArrayReader { /** - * @var array The data + * @var array */ - private $data; + private array $data; /** * The constructor. * - * @param array $data Data + * @param array $data Data */ public function __construct(array $data = []) { @@ -28,7 +28,7 @@ public function __construct(array $data = []) /** * Crate instance from array. * - * @param array $data The data + * @param array $data The data * * @return self The new instance */ @@ -66,7 +66,7 @@ public function getInt(string $key, int $default = null): int * * @return int|null The value */ - public function findInt(string $key, int $default = null) + public function findInt(string $key, int $default = null): ?int { $value = $this->find($key, $default); @@ -102,11 +102,11 @@ public function getString(string $key, string $default = null): string * Get value as string or null. * * @param string $key The key - * @param string $default The default value + * @param string|null $default The default value * * @return string|null The value */ - public function findString(string $key, string $default = null) + public function findString(string $key, string $default = null): ?string { $value = $this->find($key, $default); @@ -121,11 +121,11 @@ public function findString(string $key, string $default = null) * Get value as array. * * @param string $key The key - * @param array|null $default The default value + * @param array|null $default The default value * * @throws InvalidArgumentException * - * @return array The value + * @return array The value */ public function getArray(string $key, array $default = null): array { @@ -142,11 +142,11 @@ public function getArray(string $key, array $default = null): array * Get value as array or null. * * @param string $key The key - * @param array $default The default value + * @param array|null $default The default value * - * @return array|null The value + * @return array|null The value */ - public function findArray(string $key, array $default = null) + public function findArray(string $key, array $default = null): ?array { $value = $this->find($key, $default); @@ -182,11 +182,11 @@ public function getFloat(string $key, float $default = null): float * Get value as float or null. * * @param string $key The key - * @param float $default The default value + * @param float|null $default The default value * * @return float|null The value */ - public function findFloat(string $key, float $default = null) + public function findFloat(string $key, float $default = null): ?float { $value = $this->find($key, $default); @@ -226,7 +226,7 @@ public function getBool(string $key, bool $default = null): bool * * @return bool|null The value */ - public function findBool(string $key, bool $default = null) + public function findBool(string $key, bool $default = null): ?bool { $value = $this->find($key, $default); @@ -266,11 +266,11 @@ public function getChronos(string $key, Chronos $default = null): Chronos * Get value as Chronos or null. * * @param string $key The key - * @param Chronos $default The default value + * @param Chronos|null $default The default value * * @return Chronos|null The value */ - public function findChronos(string $key, Chronos $default = null) + public function findChronos(string $key, Chronos $default = null): ?Chronos { $value = $this->find($key, $default); @@ -293,13 +293,13 @@ public function findChronos(string $key, Chronos $default = null) * * @return mixed|null The value */ - public function find(string $path, $default = null) + public function find(string $path, mixed $default = null): mixed { if (array_key_exists($path, $this->data)) { return $this->data[$path] ?? $default; } - if (strpos($path, '.') === false) { + if (!str_contains($path, '.')) { return $default; } @@ -320,7 +320,7 @@ public function find(string $path, $default = null) /** * Return all data as array. * - * @return array The data + * @return array The data */ public function all(): array { @@ -328,7 +328,7 @@ public function all(): array } /** - * Test whether or not a given path exists in $data. + * Test whether a given path exists in $data. * This method uses the same path syntax as Hash::extract(). * * Checking for paths that could target more than one element will @@ -373,7 +373,7 @@ public function isEmpty(string $path): bool * * @return bool The status */ - private function isNullOrBlank($value): bool + private function isNullOrBlank(mixed $value): bool { return $value === null || $value === ''; } From 2497c885dde8ec77666e8adb0fc1e16bddc9154a Mon Sep 17 00:00:00 2001 From: peter279k Date: Mon, 28 Nov 2022 17:04:10 +0800 Subject: [PATCH 26/39] Add getFloat, findFloat and getFloatError tests --- tests/ArrayReaderTest.php | 103 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/tests/ArrayReaderTest.php b/tests/ArrayReaderTest.php index bc71ce5..4b80955 100644 --- a/tests/ArrayReaderTest.php +++ b/tests/ArrayReaderTest.php @@ -85,6 +85,62 @@ public function testFindInt($data, $key, $default, $expected): void $this->assertSame($expected, $reader->findInt($key, $default)); } + /** + * Test. + * + * @dataProvider providerGetFloat + * + * @param mixed $data The data + * @param string|key $key The lookup key + * @param mixed $default The default value + * @param mixed $expected The expected value + * + * @return void + */ + public function testGetFloat($data, $key, $default, $expected): void + { + $reader = new ArrayReader($data); + $this->assertSame($expected, $reader->getFloat($key, $default)); + } + + /** + * Test. + * + * @dataProvider providerGetFloatError + * + * @param mixed $data The data + * @param string|int $key The lookup key + * @param mixed $default The default value + * + * @return void + */ + public function testGetFloatError($data, $key, $default): void + { + $reader = new ArrayReader($data); + + $this->expectException(InvalidArgumentException::class); + + $reader->getFloat($key, $default); + } + + /** + * Test. + * + * @dataProvider providerFindFloat + * + * @param mixed $data The data + * @param string|int $key The lookup key + * @param mixed $default The default value + * @param mixed $expected The expected value + * + * @return void + */ + public function testFindFloat($data, $key, $default, $expected): void + { + $reader = new ArrayReader($data); + $this->assertSame($expected, $reader->findFloat($key, $default)); + } + /** * Provider. * @@ -132,6 +188,53 @@ public function providerFindInt(): array ]; } + /** + * Provider. + * + * @return array[] The test data + */ + public function providerGetFloat(): array + { + return [ + [[0.1, 1.2, 2.3, 3.4], 0, null, 0.1], + [[0.1, 1.2, 2.3, 3.4], 1, null, 1.2], + [[0.1, 1.2, 2.3, 3.4], 2, null, 2.3], + [[0.1, 1.2, 2.3, 3.4], 3, null, 3.4], + ]; + } + + /** + * Provider. + * + * @return array[] The test data + */ + public function providerGetFloatError(): array + { + return [ + [[0.1, 1.2, 2.3, 3.4], 4, null], + [[0.1, 1.2, 2.3, 3.4, null], 4, null], + [[0.1, 1.2, 2.3, null, 4.4], 3, null], + ]; + } + + /** + * Provider. + * + * @return array[] The test data + */ + public function providerFindFloat(): array + { + return [ + [[0.1, 1.2, 2.3, 3.4], 0, null, 0.1], + [[0.1, 1.2, 2.3, 3.4], 1, null, 1.2], + [[0.1, 1.2, 2.3, 3.4], 2, null, 2.3], + [[0.1, 1.2, 2.3, 3.4], 3, null, 3.4], + [[0.1, 1.2, 2.3, 3.4], 4, null, null], + [[0.1, 1.2, 2.3, 3.4, null], 4, null, null], + [[0.1, 1.2, 2.3, null, 4.4], 3, null, null], + ]; + } + /** * Test. * From 3dcf6ba496311e5ebb4a9a585fb5310855fe6aa3 Mon Sep 17 00:00:00 2001 From: Peter Date: Tue, 5 Sep 2023 14:20:41 +0800 Subject: [PATCH 27/39] Add PHP 8.2 test in GitHub action --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 8ca1e0f..6490dfd 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -8,7 +8,7 @@ jobs: strategy: matrix: operating-system: [ ubuntu-latest ] - php-versions: [ '8.0', '8.1' ] + php-versions: [ '8.0', '8.1', '8.2' ] name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }} steps: From d96018cf647a0ac773c38a20c51aa62651020cfd Mon Sep 17 00:00:00 2001 From: Daniel Opitz Date: Sat, 9 Sep 2023 14:31:20 +0200 Subject: [PATCH 28/39] Require PHP 8.1 --- .cs.php | 22 ++++++++++-- .editorconfig | 21 ++++++++++- .github/workflows/build.yml | 71 +++++++++++++++++++------------------ .gitignore | 1 + .scrutinizer.yml | 8 ++--- README.md | 2 +- composer.json | 16 ++++++--- phpcs.xml | 3 +- phpstan.neon | 8 ++--- phpunit.xml | 20 ++++++----- src/ArrayReader.php | 16 ++++----- tests/ArrayReaderTest.php | 24 ++++++------- 12 files changed, 127 insertions(+), 85 deletions(-) diff --git a/.cs.php b/.cs.php index 9653bee..2bba5e9 100644 --- a/.cs.php +++ b/.cs.php @@ -20,6 +20,13 @@ 'cast_spaces' => ['space' => 'none'], 'concat_space' => ['spacing' => 'one'], 'compact_nullable_typehint' => true, + 'declare_equal_normalize' => ['space' => 'single'], + 'general_phpdoc_annotation_remove' => [ + 'annotations' => [ + 'author', + 'package', + ], + ], 'increment_style' => ['style' => 'post'], 'list_syntax' => ['syntax' => 'short'], 'echo_tag_syntax' => ['format' => 'long'], @@ -33,11 +40,22 @@ 'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'], 'ordered_imports' => [ 'sort_algorithm' => 'alpha', - 'imports_order' => ['class', 'const', 'function'] + 'imports_order' => ['class', 'const', 'function'], ], 'single_line_throw' => false, + 'declare_strict_types' => false, + 'blank_line_between_import_groups' => true, 'fully_qualified_strict_types' => true, - 'global_namespace_import' => false, + 'no_null_property_initialization' => false, + 'operator_linebreak' => [ + 'only_booleans' => true, + 'position' => 'beginning', + ], + 'global_namespace_import' => [ + 'import_classes' => true, + 'import_constants' => null, + 'import_functions' => null + ] ] ) ->setFinder( diff --git a/.editorconfig b/.editorconfig index ae90760..0a6abee 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,3 +1,22 @@ -[composer.json] +root = true + +[*] +charset = utf-8 indent_style = space +end_of_line = lf + +[composer.json] +indent_size = 4 + +[*.js] +indent_size = 4 + +[*.neon] +indent_size = 4 +indent_style = tab + +[*.xml] +indent_size = 4 + +[*.yml] indent_size = 4 diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 6490dfd..a4bf3d5 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -3,38 +3,39 @@ name: build on: [ push, pull_request ] jobs: - run: - runs-on: ${{ matrix.operating-system }} - strategy: - matrix: - operating-system: [ ubuntu-latest ] - php-versions: [ '8.0', '8.1', '8.2' ] - name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }} - - steps: - - name: Checkout - uses: actions/checkout@v1 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-versions }} - coverage: none - - - name: Check PHP Version - run: php -v - - - name: Check Composer Version - run: composer -V - - - name: Check PHP Extensions - run: php -m - - - name: Validate composer.json and composer.lock - run: composer validate - - - name: Install dependencies - run: composer install --prefer-dist --no-progress --no-suggest - - - name: Run test suite - run: composer test:all + run: + runs-on: ${{ matrix.operating-system }} + strategy: + matrix: + operating-system: [ ubuntu-latest ] + php-versions: [ '8.1', '8.2' ] + name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }} + + steps: + - name: Checkout + uses: actions/checkout@v1 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + extensions: mbstring, intl, zip + coverage: none + + - name: Check PHP Version + run: php -v + + - name: Check Composer Version + run: composer -V + + - name: Check PHP Extensions + run: php -m + + - name: Validate composer.json and composer.lock + run: composer validate + + - name: Install dependencies + run: composer install --prefer-dist --no-progress --no-suggest + + - name: Run test suite + run: composer test:all diff --git a/.gitignore b/.gitignore index 22f5e7f..a4689b6 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ composer.lock nbproject/ vendor/ build/ +.phpunit.cache/ .phpunit.result.cache \ No newline at end of file diff --git a/.scrutinizer.yml b/.scrutinizer.yml index 5df7262..5daa7a3 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -36,7 +36,7 @@ build: - composer install --no-interaction --prefer-dist --no-progress tests: before: - - command: composer test:coverage - coverage: - file: 'build/logs/clover.xml' - format: 'clover' + - command: composer test:coverage + coverage: + file: 'build/logs/clover.xml' + format: 'clover' diff --git a/README.md b/README.md index f3aef47..f6f45b8 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ A strictly typed array reader for PHP. ## Requirements -* PHP 8.0+ +* PHP 8.1+ ## Installation diff --git a/composer.json b/composer.json index a133155..6ff2890 100644 --- a/composer.json +++ b/composer.json @@ -13,13 +13,13 @@ ], "homepage": "https://github.com/selective-php/array-reader", "require": { - "php": "^8.0", + "php": "^8.1", "cakephp/chronos": "^2 || ^3" }, "require-dev": { "friendsofphp/php-cs-fixer": "^3", "phpstan/phpstan": "^1", - "phpunit/phpunit": "^9 || ^10", + "phpunit/phpunit": "^10", "squizlabs/php_codesniffer": "^3" }, "autoload": { @@ -36,11 +36,17 @@ "sort-packages": true }, "scripts": { - "cs:check": "php-cs-fixer fix --dry-run --format=txt --verbose --diff --config=.cs.php --ansi", - "cs:fix": "php-cs-fixer fix --config=.cs.php --ansi", + "cs:check": [ + "@putenv PHP_CS_FIXER_IGNORE_ENV=1", + "php-cs-fixer fix --dry-run --format=txt --verbose --diff --config=.cs.php --ansi" + ], + "cs:fix": [ + "@putenv PHP_CS_FIXER_IGNORE_ENV=1", + "php-cs-fixer fix --config=.cs.php --ansi --verbose" + ], "sniffer:check": "phpcs --standard=phpcs.xml", "sniffer:fix": "phpcbf --standard=phpcs.xml", - "stan": "phpstan analyse -c phpstan.neon --no-progress --ansi --xdebug", + "stan": "phpstan analyse -c phpstan.neon --no-progress --ansi", "test": "phpunit --configuration phpunit.xml --do-not-cache-result --colors=always", "test:all": [ "@cs:check", diff --git a/phpcs.xml b/phpcs.xml index e218be3..f2f42d9 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -9,8 +9,7 @@ ./src ./tests - - + warning diff --git a/phpstan.neon b/phpstan.neon index d3e46c9..5045b69 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,9 +1,5 @@ parameters: level: 8 - paths: - - src reportUnmatchedIgnoredErrors: false - ignoreErrors: - - '#Property (.*?) type has no value type specified in iterable type array#' - - '#Method (.*?) has parameter (.*?) with no value type specified in iterable type array#' - - '#Method (.*?) return type has no value type specified in iterable type array#' \ No newline at end of file + paths: + - src \ No newline at end of file diff --git a/phpunit.xml b/phpunit.xml index f2e36c3..2927473 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -3,9 +3,16 @@ bootstrap="vendor/autoload.php" colors="true" backupGlobals="false" - backupStaticAttributes="false" - xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"> - + xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd" + cacheDirectory=".phpunit.cache" + backupStaticProperties="false"> + + + + tests + + + src @@ -13,10 +20,5 @@ vendor build - - - - tests - - +
diff --git a/src/ArrayReader.php b/src/ArrayReader.php index eb214c8..f536e18 100644 --- a/src/ArrayReader.php +++ b/src/ArrayReader.php @@ -11,14 +11,14 @@ final class ArrayReader { /** - * @var array + * @var array */ private array $data; /** * The constructor. * - * @param array $data Data + * @param array $data Data */ public function __construct(array $data = []) { @@ -28,7 +28,7 @@ public function __construct(array $data = []) /** * Crate instance from array. * - * @param array $data The data + * @param array $data The data * * @return self The new instance */ @@ -121,11 +121,11 @@ public function findString(string $key, string $default = null): ?string * Get value as array. * * @param string $key The key - * @param array|null $default The default value + * @param array|null $default The default value * * @throws InvalidArgumentException * - * @return array The value + * @return array The value */ public function getArray(string $key, array $default = null): array { @@ -142,9 +142,9 @@ public function getArray(string $key, array $default = null): array * Get value as array or null. * * @param string $key The key - * @param array|null $default The default value + * @param array|null $default The default value * - * @return array|null The value + * @return array|null The value */ public function findArray(string $key, array $default = null): ?array { @@ -320,7 +320,7 @@ public function find(string $path, mixed $default = null): mixed /** * Return all data as array. * - * @return array The data + * @return array The data */ public function all(): array { diff --git a/tests/ArrayReaderTest.php b/tests/ArrayReaderTest.php index 4b80955..d0e7453 100644 --- a/tests/ArrayReaderTest.php +++ b/tests/ArrayReaderTest.php @@ -146,7 +146,7 @@ public function testFindFloat($data, $key, $default, $expected): void * * @return array[] The test data */ - public function providerGetInt(): array + public static function providerGetInt(): array { return [ [[0, 1, 2, 3], 0, null, 0], @@ -161,7 +161,7 @@ public function providerGetInt(): array * * @return array[] The test data */ - public function providerGetIntError(): array + public static function providerGetIntError(): array { return [ [[0, 1, 2, 3], 4, null], @@ -175,7 +175,7 @@ public function providerGetIntError(): array * * @return array[] The test data */ - public function providerFindInt(): array + public static function providerFindInt(): array { return [ [[0, 1, 2, 3], 0, null, 0], @@ -193,7 +193,7 @@ public function providerFindInt(): array * * @return array[] The test data */ - public function providerGetFloat(): array + public static function providerGetFloat(): array { return [ [[0.1, 1.2, 2.3, 3.4], 0, null, 0.1], @@ -208,7 +208,7 @@ public function providerGetFloat(): array * * @return array[] The test data */ - public function providerGetFloatError(): array + public static function providerGetFloatError(): array { return [ [[0.1, 1.2, 2.3, 3.4], 4, null], @@ -222,7 +222,7 @@ public function providerGetFloatError(): array * * @return array[] The test data */ - public function providerFindFloat(): array + public static function providerFindFloat(): array { return [ [[0.1, 1.2, 2.3, 3.4], 0, null, 0.1], @@ -259,7 +259,7 @@ public function testGetString($data, string $key, $default, $expected): void * * @return array[] The test data */ - public function providerGetString(): array + public static function providerGetString(): array { return [ [['key' => 'value'], 'key', null, 'value'], @@ -295,7 +295,7 @@ public function testGetStringError($data, string $key): void * * @return array[] The test data */ - public function providerGetStringError(): array + public static function providerGetStringError(): array { return [ [['key' => 'value'], 'nope'], @@ -328,7 +328,7 @@ public function testFindString($data, string $key, $default, $expected): void * * @return array[] The test data */ - public function providerFindString(): array + public static function providerFindString(): array { return [ [['key' => 'value'], 'key', null, 'value'], @@ -363,7 +363,7 @@ public function testGetArray($data, $key, $default, $expected): void * * @return array[] The test data */ - public function providerGetArray(): array + public static function providerGetArray(): array { return [ [[[1, 2, 3, 4], [2, 3, 4, 5, 6]], 0, null, [1, 2, 3, 4]], @@ -398,7 +398,7 @@ public function testGetArrayError($data, $key, $default): void * * @return array[] The test data */ - public function providerGetArrayError(): array + public static function providerGetArrayError(): array { return [ [[[1, 2, 3, 4], [2, 3, 4, 5, 6]], 2, null], @@ -429,7 +429,7 @@ public function testFindArray($data, $key, $default, $expected): void * * @return array[] The test data */ - public function providerFindArray(): array + public static function providerFindArray(): array { return [ [[[1, 2, 3, 4], [2, 3, 4, 5, 6]], 0, null, [1, 2, 3, 4]], From 3233c5409da33009514d276c4ecf74d151c9a7d4 Mon Sep 17 00:00:00 2001 From: Daniel Opitz Date: Sat, 9 Sep 2023 14:35:34 +0200 Subject: [PATCH 29/39] Require PHP 8.1 --- .scrutinizer.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index 5daa7a3..3514e2e 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -33,7 +33,6 @@ build: dependencies: before: - composer self-update - - composer install --no-interaction --prefer-dist --no-progress tests: before: - command: composer test:coverage From 23ecfc858280a80f89af8873f9d50f0652153f94 Mon Sep 17 00:00:00 2001 From: heinrichschiller Date: Wed, 28 May 2025 13:31:04 +0200 Subject: [PATCH 30/39] Fix nullable types for PHP 8.4 --- src/ArrayReader.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/ArrayReader.php b/src/ArrayReader.php index f536e18..aa039c5 100644 --- a/src/ArrayReader.php +++ b/src/ArrayReader.php @@ -47,7 +47,7 @@ public static function createFromArray(array $data = []): self * * @return int The value */ - public function getInt(string $key, int $default = null): int + public function getInt(string $key, ?int $default = null): int { $value = $this->find($key, $default); @@ -66,7 +66,7 @@ public function getInt(string $key, int $default = null): int * * @return int|null The value */ - public function findInt(string $key, int $default = null): ?int + public function findInt(string $key, ?int $default = null): ?int { $value = $this->find($key, $default); @@ -87,7 +87,7 @@ public function findInt(string $key, int $default = null): ?int * * @return string The value */ - public function getString(string $key, string $default = null): string + public function getString(string $key, ?string $default = null): string { $value = $this->find($key, $default); @@ -106,7 +106,7 @@ public function getString(string $key, string $default = null): string * * @return string|null The value */ - public function findString(string $key, string $default = null): ?string + public function findString(string $key, ?string $default = null): ?string { $value = $this->find($key, $default); @@ -127,7 +127,7 @@ public function findString(string $key, string $default = null): ?string * * @return array The value */ - public function getArray(string $key, array $default = null): array + public function getArray(string $key, ?array $default = null): array { $value = $this->find($key, $default); @@ -146,7 +146,7 @@ public function getArray(string $key, array $default = null): array * * @return array|null The value */ - public function findArray(string $key, array $default = null): ?array + public function findArray(string $key, ?array $default = null): ?array { $value = $this->find($key, $default); @@ -167,7 +167,7 @@ public function findArray(string $key, array $default = null): ?array * * @return float The value */ - public function getFloat(string $key, float $default = null): float + public function getFloat(string $key, ?float $default = null): float { $value = $this->find($key, $default); @@ -186,7 +186,7 @@ public function getFloat(string $key, float $default = null): float * * @return float|null The value */ - public function findFloat(string $key, float $default = null): ?float + public function findFloat(string $key, ?float $default = null): ?float { $value = $this->find($key, $default); @@ -207,7 +207,7 @@ public function findFloat(string $key, float $default = null): ?float * * @return bool The value */ - public function getBool(string $key, bool $default = null): bool + public function getBool(string $key, ?bool $default = null): bool { $value = $this->find($key, $default); @@ -226,7 +226,7 @@ public function getBool(string $key, bool $default = null): bool * * @return bool|null The value */ - public function findBool(string $key, bool $default = null): ?bool + public function findBool(string $key, ?bool $default = null): ?bool { $value = $this->find($key, $default); @@ -247,7 +247,7 @@ public function findBool(string $key, bool $default = null): ?bool * * @return Chronos The value */ - public function getChronos(string $key, Chronos $default = null): Chronos + public function getChronos(string $key, ?Chronos $default = null): Chronos { $value = $this->find($key, $default); @@ -270,7 +270,7 @@ public function getChronos(string $key, Chronos $default = null): Chronos * * @return Chronos|null The value */ - public function findChronos(string $key, Chronos $default = null): ?Chronos + public function findChronos(string $key, ?Chronos $default = null): ?Chronos { $value = $this->find($key, $default); From 9bfa075451ea61672fffc405a47e5fdb20802304 Mon Sep 17 00:00:00 2001 From: Daniel Opitz Date: Wed, 28 May 2025 18:05:06 +0200 Subject: [PATCH 31/39] Add support for PHP 8.4 --- .github/workflows/build.yml | 83 +++++++++++++++++++++---------------- README.md | 2 +- composer.json | 11 +++-- 3 files changed, 55 insertions(+), 41 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a4bf3d5..64bbad7 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -3,39 +3,50 @@ name: build on: [ push, pull_request ] jobs: - run: - runs-on: ${{ matrix.operating-system }} - strategy: - matrix: - operating-system: [ ubuntu-latest ] - php-versions: [ '8.1', '8.2' ] - name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }} - - steps: - - name: Checkout - uses: actions/checkout@v1 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-versions }} - extensions: mbstring, intl, zip - coverage: none - - - name: Check PHP Version - run: php -v - - - name: Check Composer Version - run: composer -V - - - name: Check PHP Extensions - run: php -m - - - name: Validate composer.json and composer.lock - run: composer validate - - - name: Install dependencies - run: composer install --prefer-dist --no-progress --no-suggest - - - name: Run test suite - run: composer test:all + run: + runs-on: ${{ matrix.operating-system }} + strategy: + matrix: + operating-system: [ ubuntu-latest ] + php-versions: [ '8.1', '8.2', '8.3', '8.4' ] + name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }} + + steps: + - name: Checkout + uses: actions/checkout@v1 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + extensions: mbstring, intl, zip + coverage: none + + - name: Check PHP Version + run: php -v + + - name: Check Composer Version + run: composer -V + + - name: Check PHP Extensions + run: php -m + + - name: Validate composer.json and composer.lock + run: composer validate + + - name: Install dependencies + run: composer install --prefer-dist --no-progress --no-suggest + + - name: Run PHP CodeSniffer + run: composer sniffer:check + + - name: Run PHPStan + run: composer stan + + - name: Run tests + if: ${{ matrix.php-versions != '8.4' }} + run: composer test + + - name: Run tests with coverage + if: ${{ matrix.php-versions == '8.4' }} + run: composer test:coverage diff --git a/README.md b/README.md index f6f45b8..f0f3d5d 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ A strictly typed array reader for PHP. ## Requirements -* PHP 8.1+ +* PHP 8.1 - 8.4 ## Installation diff --git a/composer.json b/composer.json index 6ff2890..ff7ebe1 100644 --- a/composer.json +++ b/composer.json @@ -13,12 +13,12 @@ ], "homepage": "https://github.com/selective-php/array-reader", "require": { - "php": "^8.1", + "php": "8.1.* || 8.2.* || 8.3.* || 8.4.*", "cakephp/chronos": "^2 || ^3" }, "require-dev": { "friendsofphp/php-cs-fixer": "^3", - "phpstan/phpstan": "^1", + "phpstan/phpstan": "^1 || ^2", "phpunit/phpunit": "^10", "squizlabs/php_codesniffer": "^3" }, @@ -47,13 +47,16 @@ "sniffer:check": "phpcs --standard=phpcs.xml", "sniffer:fix": "phpcbf --standard=phpcs.xml", "stan": "phpstan analyse -c phpstan.neon --no-progress --ansi", - "test": "phpunit --configuration phpunit.xml --do-not-cache-result --colors=always", + "test": "phpunit --configuration phpunit.xml --do-not-cache-result --colors=always --display-warnings --display-deprecations --no-coverage", "test:all": [ "@cs:check", "@sniffer:check", "@stan", "@test" ], - "test:coverage": "php -d xdebug.mode=coverage -r \"require 'vendor/bin/phpunit';\" -- --configuration phpunit.xml --do-not-cache-result --colors=always --coverage-clover build/logs/clover.xml --coverage-html build/coverage" + "test:coverage": [ + "@putenv XDEBUG_MODE=coverage", + "phpunit --configuration phpunit.xml --do-not-cache-result --colors=always --display-warnings --display-deprecations --coverage-clover build/coverage/clover.xml --coverage-html build/coverage --coverage-text" + ] } } From ad9b6bc23c62778f6d11fff2392516fd0292dd57 Mon Sep 17 00:00:00 2001 From: Daniel Opitz Date: Wed, 28 May 2025 18:05:14 +0200 Subject: [PATCH 32/39] Update year --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index bf7a9a3..bfcf021 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2023 odan +Copyright (c) 2025 odan Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal From e20d09621e5b664aed6d7ed0edf89291ac4e6b16 Mon Sep 17 00:00:00 2001 From: Daniel Opitz Date: Wed, 28 May 2025 18:05:27 +0200 Subject: [PATCH 33/39] Remove --- .scrutinizer.yml | 41 ----------------------------------------- 1 file changed, 41 deletions(-) delete mode 100644 .scrutinizer.yml diff --git a/.scrutinizer.yml b/.scrutinizer.yml deleted file mode 100644 index 3514e2e..0000000 --- a/.scrutinizer.yml +++ /dev/null @@ -1,41 +0,0 @@ -filter: - paths: [ "src/*" ] - excluded_paths: [ "vendor/*", "tests/*" ] - -checks: - php: - code_rating: true - duplication: true - -tools: - external_code_coverage: false - -build: - environment: - php: - version: 8.1.2 - ini: - xdebug.mode: coverage - mysql: false - node: false - postgresql: false - mongodb: false - elasticsearch: false - redis: false - memcached: false - neo4j: false - rabbitmq: false - nodes: - analysis: - tests: - override: - - php-scrutinizer-run - dependencies: - before: - - composer self-update - tests: - before: - - command: composer test:coverage - coverage: - file: 'build/logs/clover.xml' - format: 'clover' From 9843f79392f773d6b046c0fb6ab8f5543e8a7076 Mon Sep 17 00:00:00 2001 From: Daniel Opitz Date: Wed, 28 May 2025 18:05:38 +0200 Subject: [PATCH 34/39] Update cs rules --- .cs.php | 22 ++++++++++++++++++---- .editorconfig | 2 +- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/.cs.php b/.cs.php index 2bba5e9..c0e1811 100644 --- a/.cs.php +++ b/.cs.php @@ -9,9 +9,8 @@ [ '@PSR1' => true, '@PSR2' => true, - '@Symfony' => true, - 'psr_autoloading' => true, // custom rules + 'psr_autoloading' => true, 'align_multiline_comment' => ['comment_type' => 'phpdocs_only'], // psr-5 'phpdoc_to_comment' => false, 'no_superfluous_phpdoc_tags' => false, @@ -19,7 +18,7 @@ 'array_syntax' => ['syntax' => 'short'], 'cast_spaces' => ['space' => 'none'], 'concat_space' => ['spacing' => 'one'], - 'compact_nullable_typehint' => true, + 'compact_nullable_type_declaration' => true, 'declare_equal_normalize' => ['space' => 'single'], 'general_phpdoc_annotation_remove' => [ 'annotations' => [ @@ -36,7 +35,11 @@ 'phpdoc_order' => true, // psr-5 'phpdoc_no_useless_inheritdoc' => false, 'protected_to_private' => false, - 'yoda_style' => false, + 'yoda_style' => [ + 'equal' => false, + 'identical' => false, + 'less_and_greater' => false + ], 'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'], 'ordered_imports' => [ 'sort_algorithm' => 'alpha', @@ -47,6 +50,7 @@ 'blank_line_between_import_groups' => true, 'fully_qualified_strict_types' => true, 'no_null_property_initialization' => false, + 'nullable_type_declaration_for_default_null_value' => false, 'operator_linebreak' => [ 'only_booleans' => true, 'position' => 'beginning', @@ -55,6 +59,16 @@ 'import_classes' => true, 'import_constants' => null, 'import_functions' => null + ], + 'class_definition' => [ + 'space_before_parenthesis' => true, + ], + 'trailing_comma_in_multiline' => [ + 'after_heredoc' => true, + 'elements' => ['array_destructuring', 'arrays', 'match'] + ], + 'function_declaration' => [ + 'closure_fn_spacing' => 'none', ] ] ) diff --git a/.editorconfig b/.editorconfig index 0a6abee..a628c91 100644 --- a/.editorconfig +++ b/.editorconfig @@ -19,4 +19,4 @@ indent_style = tab indent_size = 4 [*.yml] -indent_size = 4 +indent_size = 2 From fc4dc888210b2044a59f9e8336d8b3636e037622 Mon Sep 17 00:00:00 2001 From: Daniel Opitz Date: Wed, 28 May 2025 18:06:14 +0200 Subject: [PATCH 35/39] Update readme --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index f0f3d5d..cc39510 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,8 @@ A strictly typed array reader for PHP. [![Latest Version on Packagist](https://img.shields.io/github/release/selective-php/array-reader.svg)](https://packagist.org/packages/selective/array-reader) [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE) [![Build Status](https://github.com/selective-php/array-reader/workflows/build/badge.svg)](https://github.com/selective-php/array-reader/actions) -[![Coverage Status](https://img.shields.io/scrutinizer/coverage/g/selective-php/array-reader.svg)](https://scrutinizer-ci.com/g/selective-php/array-reader/code-structure) -[![Quality Score](https://img.shields.io/scrutinizer/quality/g/selective-php/array-reader.svg)](https://scrutinizer-ci.com/g/selective-php/array-reader/?branch=master) [![Total Downloads](https://img.shields.io/packagist/dt/selective/array-reader.svg)](https://packagist.org/packages/selective/array-reader/stats) - ## Requirements * PHP 8.1 - 8.4 From 96dd87a73ebb817b1915034df1ed3e2244189297 Mon Sep 17 00:00:00 2001 From: Daniel Opitz Date: Wed, 28 May 2025 18:07:25 +0200 Subject: [PATCH 36/39] Update build --- .github/workflows/build.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 64bbad7..b88998f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -20,7 +20,6 @@ jobs: with: php-version: ${{ matrix.php-versions }} extensions: mbstring, intl, zip - coverage: none - name: Check PHP Version run: php -v From 4e74565cc222688b23c2d4d46d8db314cfb42377 Mon Sep 17 00:00:00 2001 From: Heinrich Schiller Date: Sat, 7 Mar 2026 11:41:21 +0100 Subject: [PATCH 37/39] Add support for PHP 8.5 --- .github/workflows/build.yml | 2 +- LICENSE | 2 +- README.md | 2 +- composer.json | 10 ++++------ phpunit.xml | 2 +- tests/ArrayReaderTest.php | 40 +++++++++++++------------------------ 6 files changed, 22 insertions(+), 36 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b88998f..429673a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -8,7 +8,7 @@ jobs: strategy: matrix: operating-system: [ ubuntu-latest ] - php-versions: [ '8.1', '8.2', '8.3', '8.4' ] + php-versions: [ '8.2', '8.3', '8.4', '8.5' ] name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }} steps: diff --git a/LICENSE b/LICENSE index bfcf021..2013f40 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2025 odan +Copyright (c) 2026 odan Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index cc39510..019ac65 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ A strictly typed array reader for PHP. ## Requirements -* PHP 8.1 - 8.4 +* PHP 8.2 - 8.5 ## Installation diff --git a/composer.json b/composer.json index ff7ebe1..e4917db 100644 --- a/composer.json +++ b/composer.json @@ -13,13 +13,13 @@ ], "homepage": "https://github.com/selective-php/array-reader", "require": { - "php": "8.1.* || 8.2.* || 8.3.* || 8.4.*", + "php": "~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0", "cakephp/chronos": "^2 || ^3" }, "require-dev": { "friendsofphp/php-cs-fixer": "^3", "phpstan/phpstan": "^1 || ^2", - "phpunit/phpunit": "^10", + "phpunit/phpunit": "^11", "squizlabs/php_codesniffer": "^3" }, "autoload": { @@ -37,12 +37,10 @@ }, "scripts": { "cs:check": [ - "@putenv PHP_CS_FIXER_IGNORE_ENV=1", - "php-cs-fixer fix --dry-run --format=txt --verbose --diff --config=.cs.php --ansi" + "php-cs-fixer fix --dry-run --format=txt --verbose --diff --config=.cs.php --ansi --allow-unsupported-php-version=yes" ], "cs:fix": [ - "@putenv PHP_CS_FIXER_IGNORE_ENV=1", - "php-cs-fixer fix --config=.cs.php --ansi --verbose" + "php-cs-fixer fix --config=.cs.php --ansi --verbose --allow-unsupported-php-version=yes" ], "sniffer:check": "phpcs --standard=phpcs.xml", "sniffer:fix": "phpcbf --standard=phpcs.xml", diff --git a/phpunit.xml b/phpunit.xml index 2927473..7b7ad74 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -3,7 +3,7 @@ bootstrap="vendor/autoload.php" colors="true" backupGlobals="false" - xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd" + xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false"> diff --git a/tests/ArrayReaderTest.php b/tests/ArrayReaderTest.php index d0e7453..f92df94 100644 --- a/tests/ArrayReaderTest.php +++ b/tests/ArrayReaderTest.php @@ -3,6 +3,7 @@ namespace Selective\ArrayReader\Test; use InvalidArgumentException; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Selective\ArrayReader\ArrayReader; @@ -14,8 +15,6 @@ class ArrayReaderTest extends TestCase /** * Test. * - * @dataProvider providerGetInt - * * @param mixed $data The data * @param string|int $key The lookup key * @param mixed $default The default value @@ -23,6 +22,7 @@ class ArrayReaderTest extends TestCase * * @return void */ + #[DataProvider('providerGetInt')] public function testCreateFromArray($data, $key, $default, $expected): void { $reader = ArrayReader::createFromArray($data); @@ -32,8 +32,6 @@ public function testCreateFromArray($data, $key, $default, $expected): void /** * Test. * - * @dataProvider providerGetInt - * * @param mixed $data The data * @param string|key $key The lookup key * @param mixed $default The default value @@ -41,6 +39,7 @@ public function testCreateFromArray($data, $key, $default, $expected): void * * @return void */ + #[DataProvider('providerGetInt')] public function testGetInt($data, $key, $default, $expected): void { $reader = new ArrayReader($data); @@ -50,14 +49,13 @@ public function testGetInt($data, $key, $default, $expected): void /** * Test. * - * @dataProvider providerGetIntError - * * @param mixed $data The data * @param string|int $key The lookup key * @param mixed $default The default value * * @return void */ + #[DataProvider('providerGetIntError')] public function testGetIntError($data, $key, $default): void { $reader = new ArrayReader($data); @@ -70,8 +68,6 @@ public function testGetIntError($data, $key, $default): void /** * Test. * - * @dataProvider providerFindInt - * * @param mixed $data The data * @param string|int $key The lookup key * @param mixed $default The default value @@ -79,6 +75,7 @@ public function testGetIntError($data, $key, $default): void * * @return void */ + #[DataProvider('providerFindInt')] public function testFindInt($data, $key, $default, $expected): void { $reader = new ArrayReader($data); @@ -88,8 +85,6 @@ public function testFindInt($data, $key, $default, $expected): void /** * Test. * - * @dataProvider providerGetFloat - * * @param mixed $data The data * @param string|key $key The lookup key * @param mixed $default The default value @@ -97,6 +92,7 @@ public function testFindInt($data, $key, $default, $expected): void * * @return void */ + #[DataProvider('providerGetFloat')] public function testGetFloat($data, $key, $default, $expected): void { $reader = new ArrayReader($data); @@ -106,14 +102,13 @@ public function testGetFloat($data, $key, $default, $expected): void /** * Test. * - * @dataProvider providerGetFloatError - * * @param mixed $data The data * @param string|int $key The lookup key * @param mixed $default The default value * * @return void */ + #[DataProvider('providerGetFloatError')] public function testGetFloatError($data, $key, $default): void { $reader = new ArrayReader($data); @@ -126,8 +121,6 @@ public function testGetFloatError($data, $key, $default): void /** * Test. * - * @dataProvider providerFindFloat - * * @param mixed $data The data * @param string|int $key The lookup key * @param mixed $default The default value @@ -135,6 +128,7 @@ public function testGetFloatError($data, $key, $default): void * * @return void */ + #[DataProvider('providerFindFloat')] public function testFindFloat($data, $key, $default, $expected): void { $reader = new ArrayReader($data); @@ -238,8 +232,6 @@ public static function providerFindFloat(): array /** * Test. * - * @dataProvider providerGetString - * * @param mixed $data The data * @param string $key The lookup key * @param mixed $default The default value @@ -247,6 +239,7 @@ public static function providerFindFloat(): array * * @return void */ + #[DataProvider('providerGetString')] public function testGetString($data, string $key, $default, $expected): void { $reader = new ArrayReader($data); @@ -273,13 +266,12 @@ public static function providerGetString(): array /** * Test. * - * @dataProvider providerGetStringError - * * @param mixed $data The data * @param string $key The lookup key * * @return void */ + #[DataProvider('providerGetStringError')] public function testGetStringError($data, string $key): void { $this->expectException(InvalidArgumentException::class); @@ -308,8 +300,6 @@ public static function providerGetStringError(): array /** * Test. * - * @dataProvider providerFindString - * * @param mixed $data The data * @param string $key The lookup key * @param mixed $default The default value @@ -317,6 +307,7 @@ public static function providerGetStringError(): array * * @return void */ + #[DataProvider('providerFindString')] public function testFindString($data, string $key, $default, $expected): void { $reader = new ArrayReader($data); @@ -343,8 +334,6 @@ public static function providerFindString(): array /** * Test. * - * @dataProvider providerGetArray - * * @param mixed $data The data * @param string|int $key The lookup key * @param mixed $default The default value @@ -352,6 +341,7 @@ public static function providerFindString(): array * * @return void */ + #[DataProvider('providerGetArray')] public function testGetArray($data, $key, $default, $expected): void { $reader = new ArrayReader($data); @@ -376,14 +366,13 @@ public static function providerGetArray(): array /** * Test. * - * @dataProvider providerGetArrayError - * * @param mixed $data The data * @param string|int $key The lookup key * @param mixed $default The default value * * @return void */ + #[DataProvider('providerGetArrayError')] public function testGetArrayError($data, $key, $default): void { $reader = new ArrayReader($data); @@ -409,8 +398,6 @@ public static function providerGetArrayError(): array /** * Test. * - * @dataProvider providerFindArray - * * @param mixed $data The data * @param string|int $key The lookup key * @param mixed $default The default value @@ -418,6 +405,7 @@ public static function providerGetArrayError(): array * * @return void */ + #[DataProvider('providerFindArray')] public function testFindArray($data, $key, $default, $expected): void { $reader = new ArrayReader($data); From 492ca20aeb5500caed9f81ca1ed51106472d3cd8 Mon Sep 17 00:00:00 2001 From: Daniel Opitz Date: Sun, 25 Oct 2026 17:08:34 +0100 Subject: [PATCH 38/39] Make chronos optional (#12) --- composer.json | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index e4917db..1b9045f 100644 --- a/composer.json +++ b/composer.json @@ -13,15 +13,18 @@ ], "homepage": "https://github.com/selective-php/array-reader", "require": { - "php": "~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0", - "cakephp/chronos": "^2 || ^3" + "php": "~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0" }, "require-dev": { + "cakephp/chronos": "^2 || ^3", "friendsofphp/php-cs-fixer": "^3", "phpstan/phpstan": "^1 || ^2", "phpunit/phpunit": "^11", "squizlabs/php_codesniffer": "^3" }, + "suggest": { + "cakephp/chronos": "Required to use ArrayReader::getChronos() and ArrayReader::findChronos()" + }, "autoload": { "psr-4": { "Selective\\ArrayReader\\": "src/" From d4fc104dc5d0b55fff48bc518d73cf7f5cb2cb9b Mon Sep 17 00:00:00 2001 From: Daniel Opitz Date: Sun, 25 Oct 2026 18:39:27 +0100 Subject: [PATCH 39/39] Add getDateTime and findDateTime --- .github/workflows/build.yml | 2 +- README.md | 2 +- composer.json | 2 +- src/ArrayReader.php | 73 +++++++++++++++++++++ tests/ArrayReaderTest.php | 123 ++++++++++++++++++++++++++++++++++++ 5 files changed, 199 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 429673a..36bb68e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -8,7 +8,7 @@ jobs: strategy: matrix: operating-system: [ ubuntu-latest ] - php-versions: [ '8.2', '8.3', '8.4', '8.5' ] + php-versions: [ '8.4', '8.5' ] name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }} steps: diff --git a/README.md b/README.md index 019ac65..b147ba5 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ A strictly typed array reader for PHP. ## Requirements -* PHP 8.2 - 8.5 +* PHP 8.4 - 8.5 ## Installation diff --git a/composer.json b/composer.json index 1b9045f..6e5eab3 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ ], "homepage": "https://github.com/selective-php/array-reader", "require": { - "php": "~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0" + "php": "~8.4.0 || ~8.5.0" }, "require-dev": { "cakephp/chronos": "^2 || ^3", diff --git a/src/ArrayReader.php b/src/ArrayReader.php index aa039c5..93de210 100644 --- a/src/ArrayReader.php +++ b/src/ArrayReader.php @@ -3,6 +3,8 @@ namespace Selective\ArrayReader; use Cake\Chronos\Chronos; +use DateTimeImmutable; +use DateTimeInterface; use InvalidArgumentException; /** @@ -237,6 +239,46 @@ public function findBool(string $key, ?bool $default = null): ?bool return (bool)$value; } + /** + * Get value as DateTimeImmutable. + * + * @param string $key The key + * @param DateTimeImmutable|null $default The default value + * + * @throws InvalidArgumentException + * + * @return DateTimeImmutable The value + */ + public function getDateTime(string $key, ?DateTimeImmutable $default = null): DateTimeImmutable + { + $value = $this->find($key, $default); + + if ($this->isNullOrBlank($value)) { + throw new InvalidArgumentException(sprintf('No value found for key "%s"', $key)); + } + + return $this->toDateTimeImmutable($key, $value); + } + + /** + * Get value as DateTimeImmutable or null. + * + * @param string $key The key + * @param DateTimeImmutable|null $default The default value + * + * @return DateTimeImmutable|null The value + */ + public function findDateTime(string $key, ?DateTimeImmutable $default = null): ?DateTimeImmutable + { + $value = $this->find($key, $default); + + if ($this->isNullOrBlank($value)) { + return null; + } + + return $this->toDateTimeImmutable($key, $value); + } + /** * Get value as Chronos. * @@ -377,4 +419,35 @@ private function isNullOrBlank(mixed $value): bool { return $value === null || $value === ''; } + + /** + * Cast a value to DateTimeImmutable. + * + * @param string $key The key + * @param mixed $value The value + * + * @throws InvalidArgumentException + * + * @return DateTimeImmutable The date + */ + private function toDateTimeImmutable(string $key, mixed $value): DateTimeImmutable + { + if ($value instanceof DateTimeImmutable) { + return $value; + } + + if ($value instanceof DateTimeInterface) { + return DateTimeImmutable::createFromInterface($value); + } + + if (is_int($value) || (is_float($value) && is_finite($value))) { + return DateTimeImmutable::createFromTimestamp($value); + } + + if (!is_string($value)) { + throw new InvalidArgumentException(sprintf('Invalid date value for key "%s"', $key)); + } + + return new DateTimeImmutable($value); + } } diff --git a/tests/ArrayReaderTest.php b/tests/ArrayReaderTest.php index f92df94..32bdc28 100644 --- a/tests/ArrayReaderTest.php +++ b/tests/ArrayReaderTest.php @@ -2,6 +2,9 @@ namespace Selective\ArrayReader\Test; +use Cake\Chronos\Chronos; +use DateTime; +use DateTimeImmutable; use InvalidArgumentException; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; @@ -429,4 +432,124 @@ public static function providerFindArray(): array [[['key' => 'value'], ['key2' => 'value2']], 2, null, null], ]; } + + /** + * Test. + * + * @param mixed $data The data + * @param string $key The lookup key + * @param mixed $default The default value + * @param mixed $expected The expected value + * + * @return void + */ + #[DataProvider('providerGetDateTime')] + public function testGetDateTime($data, string $key, $default, $expected): void + { + $reader = new ArrayReader($data); + + $this->assertEquals($expected, $reader->findDateTime($key, $default)); + $this->assertEquals($expected, $reader->getDateTime($key, $default)); + } + + /** + * Provider. + * + * @return array[] The test data + */ + public static function providerGetDateTime(): array + { + $date = new DateTimeImmutable('2020-01-02T03:04:05+00:00'); + $fraction = new DateTimeImmutable('2020-01-02T03:04:05.500000+00:00'); + $chronos = new Chronos('2020-01-02T03:04:05+00:00'); + + return [ + [['created' => '2020-01-02T03:04:05+00:00'], 'created', null, $date], + [['created' => $date], 'created', null, $date], + [['created' => new DateTime('2020-01-02T03:04:05+00:00')], 'created', null, $date], + [['created' => 1577934245], 'created', null, $date], + [['created' => 1577934245.5], 'created', null, $fraction], + [['user' => ['created' => '2020-01-02T03:04:05+00:00']], 'user.created', null, $date], + [['created' => $chronos], 'created', null, $chronos], + [[], 'missing', $date, $date], + [['created' => null], 'created', $date, $date], + ]; + } + + /** + * Test. + * + * @param mixed $data The data + * @param string $key The lookup key + * @param mixed $default The default value + * + * @return void + */ + #[DataProvider('providerGetDateTimeError')] + public function testGetDateTimeError($data, string $key, $default): void + { + $reader = new ArrayReader($data); + + $this->expectException(InvalidArgumentException::class); + + $reader->getDateTime($key, $default); + } + + /** + * Provider. + * + * @return array[] The test data + */ + public static function providerGetDateTimeError(): array + { + return [ + [['created' => null], 'created', null], + [['created' => ''], 'created', null], + [[], 'missing', null], + [['created' => ['not-a-date']], 'created', null], + [['created' => INF], 'created', null], + [['created' => NAN], 'created', null], + [['user' => ['created' => null]], 'user.created', null], + ]; + } + + /** + * Test. + * + * @param mixed $data The data + * @param string $key The lookup key + * @param mixed $default The default value + * @param mixed $expected The expected value + * + * @return void + */ + #[DataProvider('providerFindDateTime')] + public function testFindDateTime($data, string $key, $default, $expected): void + { + $reader = new ArrayReader($data); + + $this->assertEquals($expected, $reader->findDateTime($key, $default)); + } + + /** + * Provider. + * + * @return array[] The test data + */ + public static function providerFindDateTime(): array + { + $date = new DateTimeImmutable('2020-01-02T03:04:05+00:00'); + + return [ + [['created' => '2020-01-02T03:04:05+00:00'], 'created', null, $date], + [['created' => null], 'created', null, null], + [['created' => ''], 'created', null, null], + [['created' => ''], 'created', $date, null], + [[], 'missing', null, null], + [[], 'missing', $date, $date], + [['created' => null], 'created', $date, $date], + [['user' => ['created' => '2020-01-02T03:04:05+00:00']], 'user.created', null, $date], + [['user' => ['created' => null]], 'user.created', null, null], + ]; + } }