Skip to content

Commit

Permalink
feat: add php 8 support (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
phanan authored Mar 9, 2021
1 parent efe5c4c commit 8088be7
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 29 deletions.
27 changes: 17 additions & 10 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,30 @@ on: [push]

jobs:
run:
name: PHP ${{ matrix.php-version }} (${{ matrix.experimental && 'experimental' || 'full support' }})
runs-on: ubuntu-latest

continue-on-error: ${{ matrix.experimental }}
strategy:
matrix:
php-version: [7.4, 8.0]
experimental: [false]
include:
- php-version: 8.1
experimental: true
fail-fast: false
steps:
- name: Check out
uses: actions/checkout@v2
with:
fetch-depth: 2

- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.4

php-version: ${{ matrix.php-version }}
tools: composer:v2
- name: Install dependencies
run: composer install --no-progress --prefer-dist --optimize-autoloader

- name: Run checks and tests
run: |
composer cs
composer test
uses: ramsey/composer-install@v1
with:
composer-options: --prefer-dist
- name: Run tests
run: composer test
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ With this approach, we have a clear idea of what fields to expect as user creati

## Requirements and Installation

This package requires PHP^7.4. You can install it via Composer:
You can install eve/dto via Composer:

```bash
composer require eve/dto
```

This package requires PHP>7.4.

## Usage

### Basic Usage
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Simplistic, flexible Data Transfer Object library",
"type": "library",
"require": {
"php": "^7.4",
"php": "^7.4|~8",
"phpdocumentor/reflection-docblock": "^5.2",
"phpdocumentor/type-resolver": "^1.4"
},
Expand Down
27 changes: 16 additions & 11 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 24 additions & 5 deletions src/TypeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use phpDocumentor\Reflection\TypeResolver;
use phpDocumentor\Reflection\Types\Context;
use ReflectionProperty;
use ReflectionType;

class TypeValidator
{
Expand All @@ -33,7 +34,12 @@ public function __construct(
$this->context = $context;
$this->property = $property;

$this->allowedTypes = array_unique(array_merge($this->getNativeTypes(), $this->getDocBlockTypes()));
$this->allowedTypes = array_unique(
array_merge(
$this->getNativeTypes($this->property->getType()),
$this->getDocBlockTypes()
)
);
}

public function validate($value): void
Expand Down Expand Up @@ -67,12 +73,25 @@ public function validate($value): void
}

/** @return array<string> */
private function getNativeTypes(): array
private function getNativeTypes(?ReflectionType $type): array
{
$nativeType = $this->property->getType();
if (!$type) {
return [];
}

if (method_exists($type, 'getName')) {
return $type->allowsNull() ? ['NULL', $type->getName()] : [$type->getName()];
}

// @see https://www.php.net/manual/en/reflectionuniontype.gettypes.php
if (method_exists($type, 'getTypes')) {
$typeNames = [];

foreach ($type->getTypes() as $subType) {
$typeNames = array_merge($typeNames, $this->getNativeTypes($subType));
}

if ($nativeType) {
return $nativeType->allowsNull() ? ['NULL', $nativeType->getName()] : [$nativeType->getName()];
return $typeNames;
}

return [];
Expand Down
10 changes: 10 additions & 0 deletions tests/Fixtures/SampleData8.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Tests\Fixtures;

use Eve\DTO\DataTransferObject;

class SampleData8 extends DataTransferObject
{
public string | int $compound_property;
}
26 changes: 26 additions & 0 deletions tests/Unit/DataTransferObject8Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Tests\Unit;

use Eve\DTO\DataTransferObjectException;
use PHPUnit\Framework\TestCase;
use Tests\Fixtures\SampleData8;

class DataTransferObject8Test extends TestCase
{
public function testPhp8Support(): void
{
if (PHP_VERSION_ID < 80000) {
self::markTestSkipped();
}

$data = SampleData8::make(['compound_property' => 'Bob']);
self::assertSame(['compound_property' => 'Bob'], $data->toArray());

$data = SampleData8::make(['compound_property' => 10]);
self::assertSame(['compound_property' => 10], $data->toArray());

self::expectException(DataTransferObjectException::class);
SampleData8::make(['compound_property' => false]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Tests\Fixtures\NestedData;
use Tests\Fixtures\SampleData;

class DataTransferObjectTypeTest extends TestCase
class DataTransferObjectTest extends TestCase
{
public function testSimpleProperty(): void
{
Expand Down

0 comments on commit 8088be7

Please sign in to comment.