Skip to content

Commit

Permalink
Add support for PHP 8.3 and 8.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Lentner committed Jan 8, 2025
1 parent 20583a8 commit 25af541
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [ "8.0", "8.1", "8.2" ]
php: [ "8.0", "8.1", "8.2", "8.3", "8.4" ]

runs-on: ubuntu-latest
name: PHP@${{ matrix.php }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ vendor/
coverage/
composer.lock
.phpunit.result.cache
.phpunit.cache
coverage.xml
junit.xml
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [5.0.1] - 2025-01-09
### Added
- Support for PHP 8.3. and PHP 8.4.

### Changed
- Code improvements to avoid type errors on runtime
- Implicitly nullable parameters to nullable (PHP 8.4 deprecation)

## [5.0.0] - 2023-03-01
### Added
- Support for PHP 8.2.
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
}
},
"require": {
"php": "8.0.* | 8.1.* | 8.2.*",
"php": "8.0.* | 8.1.* | 8.2.* | 8.3.* | 8.4.*",
"php-di/phpdoc-reader": "^2.1"
},
"require-dev": {
"laravel/pint": "^1.5 | ^1.6",
"pestphp/pest": "^1.22",
"phpstan/phpstan": "^1.10"
"pestphp/pest": "^1.22 | ^2.0 | ^3.0",
"phpstan/phpstan": "^1.10 | ^2.0"
},
"scripts": {
"analyse": "phpstan analyse --memory-limit 512M",
Expand Down
4 changes: 3 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ parameters:
paths:
- src
level: max
checkMissingIterableValueType: false
checkExplicitMixed: false
ignoreErrors:
- identifier: missingType.iterableValue
26 changes: 11 additions & 15 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
colors="true"
verbose="true"
>
<testsuites>
<testsuite name="Json Decoder Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd" colors="true" cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="Json Decoder Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
11 changes: 8 additions & 3 deletions src/Bindings/DateTimeBinding.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public function __construct(
public function validate(array $jsonData): bool
{
if ($this->jsonField && array_key_exists($this->jsonField, $jsonData) && ! empty($jsonData[$this->jsonField])) {
if (! is_string($jsonData[$this->jsonField])) {
return false;
}
return DateTime::createFromFormat($this->dateTimeFormat, $jsonData[$this->jsonField]) !== false;
}

Expand All @@ -31,10 +34,12 @@ public function validate(array $jsonData): bool
public function bind(JsonDecoder $jsonDecoder, Property $property, array $jsonData = []): void
{
if ($this->jsonField && array_key_exists($this->jsonField, $jsonData)) {
$dateTimeObject = DateTime::createFromFormat($this->dateTimeFormat, $jsonData[$this->jsonField]);
if (is_string($jsonData[$this->jsonField])) {
$dateTimeObject = DateTime::createFromFormat($this->dateTimeFormat, $jsonData[$this->jsonField]);

if ($dateTimeObject !== false) {
$property->set($dateTimeObject);
if ($dateTimeObject !== false) {
$property->set($dateTimeObject);
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/Bindings/FieldBinding.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public function bind(JsonDecoder $jsonDecoder, Property $property, array $jsonDa
{
if ($this->jsonField && array_key_exists($this->jsonField, $jsonData) && $this->type) {
$data = $jsonData[$this->jsonField];
$property->set($jsonDecoder->decodeArray($data, $this->type));
if (is_null($data) || is_array($data)) {
$property->set($jsonDecoder->decodeArray($data, $this->type));
}
}
}
}
6 changes: 3 additions & 3 deletions src/JsonDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function scanAndRegister(string $class): void
* @throws JsonValueException
* @throws ReflectionException
*/
public function decode(string $json, string $classType, string $root = null): mixed
public function decode(string $json, string $classType, ?string $root = null): mixed
{
return $this->decodeArray($this->parseJson($json, $root), $classType);
}
Expand All @@ -73,13 +73,13 @@ public function decode(string $json, string $classType, string $root = null): mi
* @throws JsonValueException
* @throws ReflectionException
*/
public function decodeMultiple(string $json, string $classType, string $root = null): array
public function decodeMultiple(string $json, string $classType, ?string $root = null): array
{
$data = $this->parseJson($json, $root);

return array_map(
function ($element) use ($classType) {
return $this->decodeArray($element, $classType);
return is_array($element) ? $this->decodeArray($element, $classType) : $this->decodeArray(null, $classType);
},
$data
);
Expand Down
6 changes: 4 additions & 2 deletions src/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ private function typesMatch(?ReflectionType $reflectionType, mixed $value): bool

if ($reflectionType instanceof ReflectionUnionType) {
foreach ($reflectionType->getTypes() as $type) {
if ($type->getName() === $valueType) {
return true;
if ($type instanceof ReflectionNamedType) {
if ($type->getName() === $valueType) {
return true;
}
}
}
}
Expand Down
14 changes: 4 additions & 10 deletions tests/PropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,11 @@
expect($sample->{$propertyName})->toEqual('value');
}
})->with([
'public' => [
'name' => 'publicProperty',
],
'protected' => [
'name' => 'protectedProperty',
],
'private' => [
'name' => 'privateProperty',
],
'public' => ['publicProperty'],
'protected' => ['protectedProperty'],
'private' => ['privateProperty'],
'new' => [
'name' => 'newProperty',
'newProperty',
'methodAccess' => false,
],
]);
Expand Down

0 comments on commit 25af541

Please sign in to comment.