8000 psalm support by codeliner · Pull Request #9 · event-engine/php-data · GitHub
[go: up one dir, main page]

Skip to content

psalm support #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
.php_cs.cache
composer.lock
vendor
vendor
38 changes: 38 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
language: php

matrix:
fast_finish: true
include:
- php: 7.4
env:
- DEPENDENCIES=""
- EXECUTE_CS_CHECK=true
- TEST_COVERAGE=true

cache:
directories:
- $HOME/.composer/cache
- $HOME/.php-cs-fixer
- $HOME/.local

before_script:
- mkdir -p "$HOME/.php-cs-fixer"
- phpenv config-rm xdebug.ini
- composer self-update
- composer update --prefer-source $DEPENDENCIES

script:
- if [[ $TEST_COVERAGE == 'true' ]]; then php -dzend_extension=xdebug.so ./vendor/bin/phpunit --coverage-text --coverage-clover ./build/logs/clover.xml; else ./vendor/bin/phpunit; fi
# - if [[ $EXECUTE_CS_CHECK == 'true' ]]; then ./vendor/bin/php-cs-fixer fix -v --diff --dry-run; fi
# - if [[ $EXECUTE_CS_CHECK == 'true' ]]; then ./vendor/bin/docheader check examples/ src/ tests/; fi

after_success:
- if [[ $TEST_COVERAGE == 'true' ]]; then php vendor/bin/coveralls -v; fi

notifications:
webhooks:
urls:
- https://webhooks.gitter.im/e/61c75218816eebde4486
on_success: change # options: [always|never|change] default: always
on_failure: always # options: [always|never|change] default: always
on_start: never # options: [always|never|change] default: always
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# EventEngine\Data

[![Build Status](https://travis-ci.com/event-engine/php-data.svg?branch=master)](https://travis-ci.com/event-engine/php-data)
[![Coverage Status](https://coveralls.io/repos/github/event-engine/php-data/badge.svg?branch=master)](https://coveralls.io/github/event-engine/php-data?branch=master)

Generate Immutable Objects with ease!

![Value Object Template vo_string](https://event-engine.io/api/img/vo_string.gif)
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"require-dev": {
"phpunit/phpunit": "^7.0",
"prooph/php-cs-fixer-config": "^0.3",
"satooshi/php-coveralls": "^1.0",
"satooshi/php-coveralls": "^2.2",
"malukenho/docheader": "^0.1.4"
},
"autoload": {
Expand All @@ -43,8 +43,8 @@
"@test"
],
"docheader": "vendor/bin/docheader check src/ tests/",
"cs": "php-cs-fixer fix -v --diff --dry-run",
"cs-fix": "php-cs-fixer fix -v --diff",
"cs": "php-cs-fixer fix src -v --diff --dry-run",
"cs-fix": "php-cs-fixer fix src -v --diff",
"test": "vendor/bin/phpunit"
}
}
2 changes: 1 addition & 1 deletion src/DataConverter.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* This file is part of event-engine/php-data.
* (c) 2018-2019 prooph software GmbH <contact@prooph.de>
* (c) 2018-2020 prooph software GmbH <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand Down
8 changes: 7 additions & 1 deletion src/ImmutableRecord.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* This file is part of event-engine/php-data.
* (c) 2018-2019 prooph software GmbH <contact@prooph.de>
* (c) 2018-2020 prooph software GmbH <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand All @@ -11,6 +11,12 @@

namespace EventEngine\Data;

/**
* Interface ImmutableRecord
*
* @package EventEngine\Data
* @psalm-immutable
*/
interface ImmutableRecord
{
const PHP_TYPE_STRING = 'string';
Expand Down
4 changes: 2 additions & 2 deletions src/ImmutableRecordDataConverter.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* This file is part of event-engine/php-data.
* (c) 2018-2019 prooph software GmbH <contact@prooph.de>
* (c) 2018-2020 prooph software GmbH <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand Down Expand Up @@ -37,7 +37,7 @@ public function canConvertTypeToData(string $type): bool
{
$class = $this->getClassOfType($type);

if(!class_exists($class)) {
if (!class_exists($class)) {
return false;
}

Expand Down
42 changes: 24 additions & 18 deletions src/ImmutableRecordLogic.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* This file is part of event-engine/php-data.
* (c) 2018-2019 prooph software GmbH <contact@prooph.de>
* (c) 2018-2020 prooph software GmbH <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand All @@ -11,6 +11,12 @@

namespace EventEngine\Data;

/**
* Trait ImmutableRecordLogic
* @package EventEngine\Data
*
* @psalm-immutable
*/
trait ImmutableRecordLogic
{
/**
Expand All @@ -36,7 +42,7 @@ private function init(): void
* @param array $recordData
* @return self
*/
public static function fromRecordData(array $recordData)
public static function fromRecordData(array $recordData): self
{
return new self($recordData);
}
Expand All @@ -45,7 +51,7 @@ public static function fromRecordData(array $recordData)
* @param array $nativeData
* @return self
*/
public static function fromArray(array $nativeData)
public static function fromArray(array $nativeData): self
{
return new self(null, $nativeData);
}
Expand Down Expand Up @@ -73,7 +79,7 @@ private function __construct(array $recordData = null, array $nativeData = null)
* @param array $recordData
* @return self
*/
public function with(array $recordData)
public function with(array $recordData): self
{
$copy = clone $this;
$copy->setRecordData($recordData);
Expand All @@ -94,24 +100,24 @@ public function toArray(): array
case ImmutableRecord::PHP_TYPE_BOOL:
case ImmutableRecord::PHP_TYPE_ARRAY:
if (\array_key_exists($key, $arrayPropItemTypeMap) && ! self::isScalarType($arrayPropItemTypeMap[$key])) {
if ($isNullable && $this->{$key}() === null) {
if ($isNullable && $this->{$key} === null) {
$nativeData[$key] = null;
continue 2;
}

$nativeData[$key] = \array_map(function ($item) use ($key, &$arrayPropItemTypeMap) {
return $this->voTypeToNative($item, $key, $arrayPropItemTypeMap[$key]);
}, $this->{$key}());
}, $this->{$key});
} else {
$nativeData[$key] = $this->{$key}();
$nativeData[$key] = $this->{$key};
}
break;
default:
if ($isNullable && $this->{$key}() === null) {
if ($isNullable && (! isset($this->{$key}))) {
$nativeData[$key] = null;
continue 2;
}
$nativeData[$key] = $this->voTypeToNative($this->{$key}(), $key, $type);
$nativeData[$key] = $this->voTypeToNative($this->{$key}, $key, $type);
}
}

Expand All @@ -120,22 +126,22 @@ public function toArray(): array

public function equals(ImmutableRecord $other): bool
{
if(get_class($this) !== get_class($other)) {
if (get_class($this) !== get_class($other)) {
return false;
}

return $this->toArray() === $other->toArray();
}

private function setRecordData(array $recordData)
private function setRecordData(array $recordData): void
{
foreach ($recordData as $key => $value) {
$this->assertType($key, $value);
$this->{$key} = $value;
}
}

private function setNativeData(array $nativeData)
private function setNativeData(array $nativeData): void
{
$recordData = [];
$arrayPropItemTypeMap = self::getArrayPropItemTypeMapFromMethodOrCache();
Expand Down Expand Up @@ -182,10 +188,10 @@ private function setNativeData(array $nativeData)
$this->setRecordData($recordData);
}

private function assertAllNotNull()
private function assertAllNotNull(): void
{
foreach (self::$__propTypeMap as $key => [$type, $isNative, $isNullable]) {
if (null === $this->{$key} && ! $isNullable) {
if (! isset($this->{$key}) && ! $isNullable) {
throw new \InvalidArgumentException(\sprintf(
'Missing record data for key %s of record %s.',
$key,
Expand All @@ -195,7 +201,7 @@ private function assertAllNotNull()
}
}

private function assertType(string $key, $value)
private function assertType(string $key, $value): void
{
if (! isset(self::$__propTypeMap[$key])) {
throw new \InvalidArgumentException(\sprintf(
Expand Down Expand Up @@ -264,7 +270,7 @@ private function isType(string $type, string $key, $value): bool
}
}

private static function buildPropTypeMap()
private static function buildPropTypeMap(): array
{
$refObj = new \ReflectionClass(__CLASS__);

Expand Down Expand Up @@ -383,12 +389,12 @@ private static function getArrayPropItemTypeMapFromMethodOrCache(): array
}

/**
* @var array
* @var array|null
*/
private static $__propTypeMap;

/**
* @var array
* @var array|null
*/
private static $__arrayPropItemTypeMap;
}
59 changes: 58 additions & 1 deletion tests/ImmutableRecordLogicTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
<?php

/**
* This file is part of event-engine/php-data.
* (c) 2018-2020 prooph software GmbH <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);

namespace EventEngineTest\Data;

use EventEngineTest\Data\Stub\ImmutableItem;
use EventEngineTest\Data\Stub\ImmutableRecordWithNoTypes;
use EventEngineTest\Data\Stub\ImmutableRecordWithTypedGetters;
use EventEngineTest\Data\Stub\TypeHintedImmutableRecord;
use EventEngineTest\Data\Stub\TypeHintedImmutableRecordWithValueObjects;
use EventEngineTest\Data\Stub\ValueObject\Access;
use EventEngineTest\Data\Stub\ValueObject\ItemList;
use EventEngineTest\Data\Stub\ValueObject\Name;
use EventEngineTest\Data\Stub\ValueObject\Percentage;
use EventEngineTest\Data\Stub\ValueObject\Type;
use EventEngineTest\Data\Stub\ValueObject\Version;
use PHPUnit\Framework\TestCase;

final class ImmutableRecordLogicTest extends TestCase
Expand All @@ -20,6 +34,8 @@ protected function setUp()
$this->data = [
'name' => 'test',
'version' => 1,
'itemList' => [['name' => 'one']],
'access' => true,
];
}

Expand All @@ -31,6 +47,7 @@ public function it_detects_type_hinted_properties()
$typeHinted = TypeHintedImmutableRecord::fromArray($this->data);

$this->data['type'] = null;
$this->data['percentage'] = 0.5;

$this->assertEquals(
$this->data,
Expand All @@ -46,13 +63,53 @@ public function it_detects_coupled_getters_for_properties()
$typedGetters = ImmutableRecordWithTypedGetters::fromArray($this->data);

$this->data['type'] = null;
$this->data['percentage'] = 0.5;

$this->assertEquals(
$this->data,
$typedGetters->toArray()
);
}

/**
* @test
*/
public function it_can_handle_value_objects()
{
$valueObjects = TypeHintedImmutableRecordWithValueObjects::fromArray($this->data);

$this->data['type'] = null;
$this->data['percentage'] = 0.5;

$this->assertEquals(
$this->data,
$valueObjects->toArray()
);
}

/**
* @test
*/
public function it_takes_value_object_as_initialization_params()
{
$valueObjects = TypeHintedImmutableRecordWithValueObjects::fromRecordData([
'name' => Name::fromString($this->data['name']),
'type' => Type::fromString('value_object'),
'version' => Version::fromInt($this->data['version']),
'access' => Access::fromBool($this->data['access']),
'percentage' => Percentage::fromFloat(0.9),
'itemList' => ItemList::fromItems(ImmutableItem::fromRecordData(['name' => 'one'])),
]);

$this->data['type'] = 'value_object';
$this->data['percentage'] = 0.9;

$this->assertEquals(
$this->data,
$valueObjects->toArray()
);
}

/**
* @test
*/
Expand Down
Loading
0