From 509a9c60bd6b882c937eb34f1629fcd2f4bfb751 Mon Sep 17 00:00:00 2001 From: Simon R Jones Date: Sun, 28 Apr 2024 12:31:09 +0100 Subject: [PATCH] Use native union types --- .github/workflows/php.yml | 2 +- composer.json | 2 +- src/Collection.php | 4 +- src/CollectionInterface.php | 2 +- src/Helper/UnionTypes.php | 93 ------------------------- src/Http/Http.php | 5 +- src/Mapper/MapCollection.php | 6 +- src/Mapper/MappingStrategy.php | 6 +- src/Mapper/MappingStrategyInterface.php | 5 +- src/Mapper/WildcardMappingStrategy.php | 10 +-- src/Query/GraphQLQuery.php | 6 -- src/Traits/PaginationPropertyTrait.php | 10 +-- src/Transform/Data/CallableData.php | 4 +- src/Transform/Data/Concatenate.php | 5 +- src/Transform/Data/MapValues.php | 3 +- src/Transform/Data/RenameFields.php | 3 +- src/Transform/Value/BaseValue.php | 4 +- src/Transform/Value/CallableValue.php | 4 +- tests/Helper/UnionTypesTest.php | 87 ----------------------- tests/Http/HttpTest.php | 3 +- 20 files changed, 25 insertions(+), 239 deletions(-) delete mode 100644 src/Helper/UnionTypes.php delete mode 100644 tests/Helper/UnionTypesTest.php diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 36ebbbd..c01e2dd 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -15,7 +15,7 @@ jobs: strategy: matrix: - php-versions: ['8.0', '8.1', '8.2', '8.3'] + php-versions: ['8.1', '8.2', '8.3'] runs-on: ubuntu-latest diff --git a/composer.json b/composer.json index 9959b83..b7fd9c5 100755 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ } ], "require": { - "php": "^8.0|^8.1", + "php": "^8.1", "symfony/http-client": "^5.4|^6.0", "spatie/yaml-front-matter": "^2.0", "erusev/parsedown-extra": "^0.8.1", diff --git a/src/Collection.php b/src/Collection.php index fbdcd3f..f56e8a0 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -4,7 +4,6 @@ namespace Strata\Data; -use Strata\Data\Helper\UnionTypes; use Strata\Data\Pagination\Pagination; use Strata\Data\Traits\IterableTrait; @@ -35,9 +34,8 @@ public function setCollection(array $collection) * Add an item to the collection * @param array|object $item */ - public function add($item) + public function add(array|object $item) { - UnionTypes::assert('$item', $item, 'array', 'object'); $this->collection[] = $item; } diff --git a/src/CollectionInterface.php b/src/CollectionInterface.php index 8fd5875..a78119f 100644 --- a/src/CollectionInterface.php +++ b/src/CollectionInterface.php @@ -13,7 +13,7 @@ interface CollectionInterface extends \SeekableIterator, \Countable, \ArrayAcces { public function setCollection(array $collection); - public function add($item); + public function add(array|object $item); public function setPagination(Pagination $pagination); diff --git a/src/Helper/UnionTypes.php b/src/Helper/UnionTypes.php deleted file mode 100644 index 93086fc..0000000 --- a/src/Helper/UnionTypes.php +++ /dev/null @@ -1,93 +0,0 @@ -paginationData = $data; return $this; } diff --git a/src/Mapper/MappingStrategy.php b/src/Mapper/MappingStrategy.php index 6387efc..6890106 100644 --- a/src/Mapper/MappingStrategy.php +++ b/src/Mapper/MappingStrategy.php @@ -5,7 +5,6 @@ namespace Strata\Data\Mapper; use Strata\Data\Exception\MapperException; -use Strata\Data\Helper\UnionTypes; use Strata\Data\Transform\Data\CallableData; use Strata\Data\Transform\PropertyAccessorInterface; use Strata\Data\Transform\PropertyAccessorTrait; @@ -71,9 +70,8 @@ public function getPropertyPaths(): array * @param array|object $item Destination item to map data to * @return mixed */ - public function mapItem(array $data, $item) + public function mapItem(array $data, array|object $item) { - UnionTypes::assert('$item', $item, 'array', 'object'); $propertyAccessor = $this->getPropertyAccessor(); // Loop through property paths to map to new item (destination => source) @@ -111,7 +109,7 @@ public function mapItem(array $data, $item) } // Invalid source type - if (!UnionTypes::is($source, 'string', 'array')) { + if (!is_string($source) && !is_array($source)) { $type = gettype($source); if ($type === 'object') { $type = get_class($source); diff --git a/src/Mapper/MappingStrategyInterface.php b/src/Mapper/MappingStrategyInterface.php index 8b27944..9ae7759 100644 --- a/src/Mapper/MappingStrategyInterface.php +++ b/src/Mapper/MappingStrategyInterface.php @@ -6,7 +6,8 @@ use Symfony\Component\PropertyAccess\PropertyAccessor; -interface MappingStrategyInterface +interface +MappingStrategyInterface { public function setPropertyAccessor(PropertyAccessor $propertyAccessor); public function getPropertyAccessor(): PropertyAccessor; @@ -18,5 +19,5 @@ public function getPropertyAccessor(): PropertyAccessor; * @param array|object $item * @return mixed */ - public function mapItem(array $data, $item); + public function mapItem(array $data, array|object $item); } diff --git a/src/Mapper/WildcardMappingStrategy.php b/src/Mapper/WildcardMappingStrategy.php index 942f6cf..f40229d 100644 --- a/src/Mapper/WildcardMappingStrategy.php +++ b/src/Mapper/WildcardMappingStrategy.php @@ -4,8 +4,6 @@ namespace Strata\Data\Mapper; -use Strata\Data\Helper\UnionTypes; - class WildcardMappingStrategy extends MappingStrategy { private array $mapping = []; @@ -70,10 +68,8 @@ public function getMappingByRootElement(string $field): array * * @param array|string $field */ - public function addIgnore($field) + public function addIgnore(array|string $field) { - UnionTypes::assert('$field', $field, 'array', 'string'); - if (is_array($field)) { foreach ($field as $item) { $this->ignore[] = $this->normaliseFieldName($item); @@ -102,10 +98,8 @@ public function isRootElementInIgnore(string $field): bool * @param array|object $item * @return mixed */ - public function mapItem(array $data, $item) + public function mapItem(array $data, array|object $item) { - UnionTypes::assert('$item', $item, 'array', 'object'); - // Loop through all root data to build property path mapping $propertyPaths = []; foreach ($data as $field => $value) { diff --git a/src/Query/GraphQLQuery.php b/src/Query/GraphQLQuery.php index 702e356..3edf79e 100644 --- a/src/Query/GraphQLQuery.php +++ b/src/Query/GraphQLQuery.php @@ -50,12 +50,6 @@ public function getDataProvider(): GraphQL return $this->dataProvider; } - public function setDataProvider(GraphQL $dataProvider): QueryAbstract - { - parent::setDataProvider($dataProvider); - return $this; - } - /** * Return query name * diff --git a/src/Traits/PaginationPropertyTrait.php b/src/Traits/PaginationPropertyTrait.php index 648f696..c616fd4 100644 --- a/src/Traits/PaginationPropertyTrait.php +++ b/src/Traits/PaginationPropertyTrait.php @@ -4,7 +4,6 @@ namespace Strata\Data\Traits; -use Strata\Data\Helper\UnionTypes; use Strata\Data\Query\QueryInterface; /** @@ -21,9 +20,8 @@ trait PaginationPropertyTrait * @param string|int $totalResults Property path to retrieve data from, or actual value * @return $this Fluent interface */ - public function setTotalResults($totalResults) + public function setTotalResults(string|int $totalResults) { - UnionTypes::assert('$totalResults', $totalResults, 'string', 'int'); $this->totalResults = $totalResults; return $this; } @@ -42,9 +40,8 @@ public function getTotalResults() * @param string|int $resultsPerPage Property path to retrieve data from, or actual value * @return $this Fluent interface */ - public function setResultsPerPage($resultsPerPage) + public function setResultsPerPage(string|int $resultsPerPage) { - UnionTypes::assert('$resultsPerPage', $resultsPerPage, 'string', 'int'); $this->resultsPerPage = $resultsPerPage; return $this; } @@ -63,9 +60,8 @@ public function getResultsPerPage() * @param string|int $currentPage Property path to retrieve data from, or actual value * @return $this Fluent interface */ - public function setCurrentPage($currentPage) + public function setCurrentPage(string|int $currentPage) { - UnionTypes::assert('currentPage', $currentPage, 'string', 'int'); $this->currentPage = $currentPage; return $this; } diff --git a/src/Transform/Data/CallableData.php b/src/Transform/Data/CallableData.php index 3066174..a09efc9 100644 --- a/src/Transform/Data/CallableData.php +++ b/src/Transform/Data/CallableData.php @@ -4,8 +4,6 @@ namespace Strata\Data\Transform\Data; -use Strata\Data\Helper\UnionTypes; - class CallableData extends DataAbstract { private $callable; @@ -53,7 +51,7 @@ public function getCallable(): callable */ public function canTransform($data): bool { - return UnionTypes::is($data, 'array', 'object'); + return (is_array($data) || is_object($data)); } /** diff --git a/src/Transform/Data/Concatenate.php b/src/Transform/Data/Concatenate.php index 1ba63ad..2d0afe9 100644 --- a/src/Transform/Data/Concatenate.php +++ b/src/Transform/Data/Concatenate.php @@ -3,9 +3,6 @@ declare(strict_types=1); namespace Strata\Data\Transform\Data; - -use Strata\Data\Helper\UnionTypes; - class Concatenate extends DataAbstract { private array $propertyPaths; @@ -23,7 +20,7 @@ public function __construct(...$propertyPaths) */ public function canTransform($data): bool { - return UnionTypes::is($data, 'array', 'object'); + return (is_array($data) || is_object($data)); } /** diff --git a/src/Transform/Data/MapValues.php b/src/Transform/Data/MapValues.php index a5af010..eb02f8e 100644 --- a/src/Transform/Data/MapValues.php +++ b/src/Transform/Data/MapValues.php @@ -4,7 +4,6 @@ namespace Strata\Data\Transform\Data; -use Strata\Data\Helper\UnionTypes; use Strata\Data\Transform\NotTransformedInterface; use Strata\Data\Transform\NotTransformedTrait; @@ -77,7 +76,7 @@ public function setMapping(array $mapping) */ public function canTransform($data): bool { - return UnionTypes::is($data, 'array', 'object'); + return (is_array($data) || is_object($data)); } /** diff --git a/src/Transform/Data/RenameFields.php b/src/Transform/Data/RenameFields.php index 3fe2043..9ea55cb 100644 --- a/src/Transform/Data/RenameFields.php +++ b/src/Transform/Data/RenameFields.php @@ -4,7 +4,6 @@ namespace Strata\Data\Transform\Data; -use Strata\Data\Helper\UnionTypes; use Strata\Data\Transform\NotTransformedInterface; use Strata\Data\Transform\NotTransformedTrait; use Symfony\Component\PropertyAccess\PropertyPath; @@ -42,7 +41,7 @@ public function setPropertyPaths(array $propertyPaths) */ public function canTransform($data): bool { - return UnionTypes::is($data, 'array', 'object'); + return (is_array($data) || is_object($data)); } /** diff --git a/src/Transform/Value/BaseValue.php b/src/Transform/Value/BaseValue.php index 5e9e89a..66d91d8 100644 --- a/src/Transform/Value/BaseValue.php +++ b/src/Transform/Value/BaseValue.php @@ -4,7 +4,6 @@ namespace Strata\Data\Transform\Value; -use Strata\Data\Helper\UnionTypes; use Strata\Data\Transform\PropertyAccessorTrait; class BaseValue implements MapValueInterface @@ -17,9 +16,8 @@ class BaseValue implements MapValueInterface * BaseValue constructor. * @param string|array $propertyPath Property path to read data from */ - public function __construct($propertyPath) + public function __construct(string|array $propertyPath) { - UnionTypes::assert('$propertyPath', $propertyPath, 'string', 'array'); $this->propertyPath = $propertyPath; } diff --git a/src/Transform/Value/CallableValue.php b/src/Transform/Value/CallableValue.php index 0330b67..cc47647 100644 --- a/src/Transform/Value/CallableValue.php +++ b/src/Transform/Value/CallableValue.php @@ -4,8 +4,6 @@ namespace Strata\Data\Transform\Value; -use Strata\Data\Helper\UnionTypes; - class CallableValue extends BaseValue { private $callable; @@ -39,7 +37,7 @@ public function getCallable(): callable */ public function canTransform($data): bool { - return UnionTypes::is($data, 'array', 'object'); + return (is_array($data) || is_object($data)); } /** diff --git a/tests/Helper/UnionTypesTest.php b/tests/Helper/UnionTypesTest.php deleted file mode 100644 index 21f936d..0000000 --- a/tests/Helper/UnionTypesTest.php +++ /dev/null @@ -1,87 +0,0 @@ -assertFalse(UnionTypes::is(42, 'array', 'object')); - $this->assertFalse(UnionTypes::is(24.99, 'array', 'object')); - $this->assertFalse(UnionTypes::is('string', 'array', 'object')); - $this->assertFalse(UnionTypes::is('24', 'array', 'object')); - $this->assertTrue(UnionTypes::is([1, 2, 3], 'array', 'object')); - $this->assertTrue(UnionTypes::is(new \stdClass(), 'array', 'object')); - $this->assertFalse(UnionTypes::is(null, 'array', 'object')); - - $this->assertTrue(UnionTypes::is(new \DateTime(), 'DateTime', 'array')); - $this->assertFalse(UnionTypes::is(new \stdClass(), 'DateTime', 'array')); - } - - public function testStringOrInt() - { - $this->assertTrue(UnionTypes::is(42, 'string', 'int')); - $this->assertFalse(UnionTypes::is(24.99, 'string', 'int')); - $this->assertTrue(UnionTypes::is('string', 'string', 'int')); - $this->assertTrue(UnionTypes::is('24', 'string', 'int')); - $this->assertFalse(UnionTypes::is([1,2,3], 'string', 'int')); - $this->assertFalse(UnionTypes::is(new \stdClass(), 'string', 'int')); - $this->assertFalse(UnionTypes::is(null, 'string', 'int')); - } - - public function testStringOrObject() - { - $this->assertFalse(UnionTypes::is(42, 'string', 'object')); - $this->assertFalse(UnionTypes::is(24.99, 'string', 'object')); - $this->assertTrue(UnionTypes::is('string', 'string', 'object')); - $this->assertTrue(UnionTypes::is('24', 'string', 'object')); - $this->assertFalse(UnionTypes::is([1,2,3], 'string', 'object')); - $this->assertTrue(UnionTypes::is(new \stdClass(), 'string', 'object')); - $this->assertFalse(UnionTypes::is(null, 'string', 'object')); - - $this->assertTrue(UnionTypes::is(new \DateTime(), 'DateTime', 'string')); - $this->assertFalse(UnionTypes::is(new \stdClass(), 'DateTime', 'string')); - } - - public function testStringOrArray() - { - $this->assertFalse(UnionTypes::is(42, 'string', 'array')); - $this->assertFalse(UnionTypes::is(24.99, 'string', 'array')); - $this->assertTrue(UnionTypes::is('string', 'string', 'array')); - $this->assertTrue(UnionTypes::is('24', 'string', 'array')); - $this->assertTrue(UnionTypes::is([1,2,3], 'string', 'array')); - $this->assertFalse(UnionTypes::is(new \stdClass(), 'string', 'array')); - $this->assertFalse(UnionTypes::is(null, 'string', 'array')); - } - - public function testAssertStringArray() - { - $this->expectException('InvalidArgumentException'); - UnionTypes::assert('test', 42, 'string', 'array'); - } - - public function testAssertArrayInt() - { - $this->expectException('InvalidArgumentException'); - UnionTypes::assert('test', 'value', 'array', 'int'); - } - - public function testAssertObjectString() - { - $this->expectException('InvalidArgumentException'); - UnionTypes::assert('test', [1,2,3], 'object', 'string'); - } - - public function testInterface() - { - $this->assertFalse(UnionTypes::is([1,2,3], '\Countable')); - $this->assertTrue(UnionTypes::is(new \ArrayIterator([1,2,3]), '\Countable')); - $this->assertTrue(UnionTypes::is(new \ArrayIterator([1,2,3]), '\ArrayAccess')); - $this->assertTrue(UnionTypes::is(new \ArrayIterator([1,2,3]), '\ArrayIterator')); - } -} diff --git a/tests/Http/HttpTest.php b/tests/Http/HttpTest.php index 3aa60f8..d152b27 100644 --- a/tests/Http/HttpTest.php +++ b/tests/Http/HttpTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase; use Strata\Data\Cache\DataCache; +use Strata\Data\Exception\InvalidHttpMethodException; use Strata\Data\Helper\ContentHasher; use Strata\Data\Http\Http; use Strata\Data\Http\Rest; @@ -57,7 +58,7 @@ public function testValidMethod() public function testInvalidMethod() { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(\TypeError::class); Http::validMethod(99); }