Skip to content

Commit

Permalink
Merge branch 'release/1.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
amorphine committed Nov 7, 2020
2 parents deb7a2a + cb881d8 commit 5bc86f7
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 97 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ class PostData extends DataTransferObject
* No type, which allows everything
*/
public $property;

/**
* PHP types declaration supported
*/
public ?int $property;

}
```

Expand Down Expand Up @@ -208,7 +214,6 @@ composer test
```

### Limitations
- PHP 7.4 types are not supported: neither type checks are performed on value assign nor value cast
- Opcache: types and data sources are resolved through PHP comments so ensure `opcache.save_comments` equals `1`

## License
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"minimum-stability": "stable",
"require": {
"php": "^7.0"
"php": "^7.4"
},
"require-dev": {
"phpunit/phpunit": "^9.2"
Expand Down
67 changes: 34 additions & 33 deletions src/DocTypedProperty.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php


namespace Amorphine\DataTransferObject;

use Amorphine\DataTransferObject\Helpers\ClassUseResolver;
use ReflectionClass;
use ReflectionNamedType;
use ReflectionProperty;

/**
Expand All @@ -13,33 +13,10 @@
*
* @package Amorphine\DataTransferObject
*/
final class DocTypedProperty extends Property
class DocTypedProperty extends Property
{
const VAR_DOCBLOCK_REGEX = '/@var ((?:(?:[\w?|\\\\<>])+(?:\[])?)+)/';

const SCALAR_INT = 'int';
const SCALAR_INTEGER = 'integer';
const SCALAR_BOOL = 'bool';
const SCALAR_BOOLEAN = 'boolean';
const SCALAR_FLOAT = 'float';
const SCALAR_DOUBLE = 'double';

const SCALAR_TYPES = [
self::SCALAR_INT,
self::SCALAR_INTEGER,
self::SCALAR_BOOL,
self::SCALAR_BOOLEAN,
self::SCALAR_FLOAT,
self::SCALAR_DOUBLE,
];

const NORMALIZING_TYPE_MAP = [
self::SCALAR_INT => 'integer',
self::SCALAR_BOOL => 'boolean',
self::SCALAR_FLOAT => 'float',
self::SCALAR_DOUBLE => 'float',
];

/**
* Type declaration as string
*
Expand All @@ -51,6 +28,28 @@ public function __construct(ReflectionProperty $property)
{
parent::__construct($property);

$this->types = $this->extractTypes($property);

$this->typeDeclaration = implode('|', $this->types);

$this->isNullable = $this->checkIsNullable($this->types);

$this->isMixed = $this->checkIsMixed($this->types);

$this->isMixedArray = $this->checkIsMixedArray($this->types);

$this->arrayTypes = $this->resolveArrayTypes($this->types);
}

/**
* Get array of property types
*
* @param ReflectionProperty $property
* @return array
*/
private function extractTypes(ReflectionProperty $property): array
{
// extract types from doc comment
$docComment = $property->getDocComment();

preg_match(
Expand All @@ -61,17 +60,19 @@ public function __construct(ReflectionProperty $property)

$typeDeclaration = $varStrMatches[1] ?? null;

$this->types = $this->normalizeTypes(explode('|', $typeDeclaration), $property->getDeclaringClass());

$this->typeDeclaration = implode('|', $this->types);

$this->isNullable = $this->checkIsNullable($this->types);
// since PHP 7.4 we should take into account declared type
if (method_exists($property, 'getType') && $reflectionType = $property->getType()) {

$this->isMixed = $this->checkIsMixed($this->types);
if ($reflectionType->allowsNull()) {
$typeDeclaration .= '|null';
}

$this->isMixedArray = $this->checkIsMixedArray($this->types);
if ($reflectionType instanceof ReflectionNamedType) {
$typeDeclaration .= '|' . $reflectionType->getName();
}
}

$this->arrayTypes = $this->resolveArrayTypes($this->types);
return $this->normalizeTypes(explode('|', $typeDeclaration), $property->getDeclaringClass());
}


Expand Down
23 changes: 23 additions & 0 deletions src/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,29 @@ abstract class Property implements IProperty
{
const SOURCE_DOCBLOCK_REGEX = '/@source ((?:(?:[\w?|\\\\<>])+(?:\[])?)+)/';

const SCALAR_INT = 'int';
const SCALAR_INTEGER = 'integer';
const SCALAR_BOOL = 'bool';
const SCALAR_BOOLEAN = 'boolean';
const SCALAR_FLOAT = 'float';
const SCALAR_DOUBLE = 'double';

const SCALAR_TYPES = [
self::SCALAR_INT,
self::SCALAR_INTEGER,
self::SCALAR_BOOL,
self::SCALAR_BOOLEAN,
self::SCALAR_FLOAT,
self::SCALAR_DOUBLE,
];

const NORMALIZING_TYPE_MAP = [
self::SCALAR_INT => 'integer',
self::SCALAR_BOOL => 'boolean',
self::SCALAR_FLOAT => 'float',
self::SCALAR_DOUBLE => 'float',
];

/**
* @var string
*/
Expand Down
37 changes: 0 additions & 37 deletions tests/Classes/PropertyFullDataTransferObject.php

This file was deleted.

25 changes: 0 additions & 25 deletions tests/Classes/TestPlainArrayDataTransferObject.php

This file was deleted.

11 changes: 11 additions & 0 deletions tests/DocTypedPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,15 @@ public function testGetArrayTypes()
});
$this->assertEquals(['Amorphine\DataTransferObject\Tests\Classes\DtoC'], $field->getArrayTypes());
}

public function testNativePropertiesAreIncluded() {
list($prop) = $this->getProperties(new class() {
/** @var string $prop */
public ?int $prop;
});

$this->assertTrue($prop->isNullable());
$this->assertTrue(in_array('integer', $prop->getTypes()));
$this->assertTrue(in_array('string', $prop->getTypes()));
}
}

0 comments on commit 5bc86f7

Please sign in to comment.