Skip to content
This repository was archived by the owner on Oct 28, 2022. It is now read-only.

Commit 3179699

Browse files
committed
Upgrade to PHP 8
1 parent 581bed6 commit 3179699

20 files changed

+590
-419
lines changed

composer.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@
1515
}
1616
},
1717
"require": {
18-
"php": "^7.4",
18+
"php": "^8.0",
1919
"ext-json": "*",
2020
"aws/aws-sdk-php": "^3.130",
21-
"symfony/property-info": "^4.2",
22-
"phpdocumentor/reflection-docblock": "^4.0"
21+
"symfony/property-info": "^4.2"
2322
},
2423
"require-dev": {
25-
"phpunit/phpunit": "^8.5",
26-
"mnapoli/hard-mode": "^0.1.1"
24+
"phpunit/phpunit": "^9.0",
25+
"mnapoli/hard-mode": "^0.3"
2726
}
2827
}

src/Dynamap.php

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@
44

55
use Aws\DynamoDb\DynamoDbClient;
66
use Dynamap\Exception\ItemNotFound;
7+
use Exception;
8+
use InvalidArgumentException;
79

810
class Dynamap
911
{
10-
/** @var DynamoDbClient */
11-
private $dynamoDb;
12-
13-
/** @var Mapping */
14-
private $mapping;
15-
12+
private DynamoDbClient $dynamoDb;
13+
private Mapping $mapping;
1614
/** @var Table[] */
17-
private $tables = [];
15+
private array $tables = [];
1816

1917
public function __construct(DynamoDbClient $dynamoDb, array $mapping)
2018
{
@@ -49,11 +47,10 @@ public function getAll(string $class): array
4947
*
5048
* Throws an exception if the item cannot be found (see `find()` as an alternative).
5149
*
52-
* @param int|string|array $key
53-
* @throws \InvalidArgumentException If the key is invalid.
50+
* @throws InvalidArgumentException If the key is invalid.
5451
* @throws ItemNotFound If the item cannot be found.
5552
*/
56-
public function get(string $class, $key): object
53+
public function get(string $class, array|int|string $key): object
5754
{
5855
return $this->getTable($class)->get($key);
5956
}
@@ -63,10 +60,9 @@ public function get(string $class, $key): object
6360
*
6461
* Returns null if the item cannot be found (see `get()` as an alternative).
6562
*
66-
* @param int|string|array $key
67-
* @throws \InvalidArgumentException If the key is invalid.
63+
* @throws InvalidArgumentException If the key is invalid.
6864
*/
69-
public function find(string $class, $key): ?object
65+
public function find(string $class, array|int|string $key): ?object
7066
{
7167
return $this->getTable($class)->find($key);
7268
}
@@ -82,11 +78,10 @@ public function save(object $object): void
8278
* Warning: if the item has been loaded as a PHP object, the PHP object will not be updated.
8379
* If you want it to be updated you will need to reload it from database.
8480
*
85-
* @param int|string|array $itemKey
8681
* @param array $values Key-value map
87-
* @throws \Exception
82+
* @throws Exception
8883
*/
89-
public function partialUpdate(string $class, $itemKey, array $values): void
84+
public function partialUpdate(string $class, array|int|string $itemKey, array $values): void
9085
{
9186
$this->getTable($class)->partialUpdate($itemKey, $values);
9287
}

src/Exception/ItemNotFound.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44

55
class ItemNotFound extends \Exception
66
{
7-
/**
8-
* @param int|string|array $key
9-
*/
10-
public static function fromKey(string $className, $key): self
7+
public static function fromKey(string $className, int|array|string $key): self
118
{
12-
return new self("Item `$className` not found for key " . json_encode($key));
9+
return new self("Item `$className` not found for key " . json_encode($key, JSON_THROW_ON_ERROR));
1310
}
1411
}

src/Field/BooleanField.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ public function dynamoDbType(): string
1212
/**
1313
* {@inheritdoc}
1414
*/
15-
protected function castValueForDynamoDbFormat($value): bool
15+
protected function castValueForDynamoDbFormat(mixed $value): bool
1616
{
1717
return (bool) $value;
1818
}
1919

2020
/**
2121
* {@inheritdoc}
2222
*/
23-
protected function castValueFromDynamoDbFormat($value): bool
23+
protected function castValueFromDynamoDbFormat(mixed $value): bool
2424
{
2525
return (bool) $value;
2626
}

src/Field/DateTimeField.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Dynamap\Field;
44

5+
use DateTimeInterface;
6+
57
class DateTimeField extends Field
68
{
79
public function dynamoDbType(): string
@@ -13,20 +15,20 @@ public function dynamoDbType(): string
1315
/**
1416
* {@inheritdoc}
1517
*/
16-
protected function castValueForDynamoDbFormat($value): string
18+
protected function castValueForDynamoDbFormat(mixed $value): string
1719
{
18-
if (! $value instanceof \DateTimeInterface) {
20+
if (! $value instanceof DateTimeInterface) {
1921
throw new \InvalidArgumentException('Expected an instance of DateTimeInterface');
2022
}
2123

22-
return $value->format(\DateTimeInterface::ATOM);
24+
return $value->format(DateTimeInterface::ATOM);
2325
}
2426

2527
/**
2628
* {@inheritdoc}
2729
*/
28-
protected function castValueFromDynamoDbFormat($value): \DateTimeInterface
30+
protected function castValueFromDynamoDbFormat(mixed $value): DateTimeInterface
2931
{
30-
return \DateTimeImmutable::createFromFormat(\DateTimeInterface::ATOM, $value);
32+
return \DateTimeImmutable::createFromFormat(DateTimeInterface::ATOM, $value);
3133
}
3234
}

src/Field/Field.php

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
abstract class Field
66
{
7-
/** @var string */
8-
private $name;
7+
private string $name;
98

109
public function __construct(string $name)
1110
{
@@ -17,20 +16,14 @@ public function name(): string
1716
return $this->name;
1817
}
1918

20-
/**
21-
* @return mixed
22-
*/
23-
public function readFieldValue(array $item, string $fieldName)
19+
public function readFieldValue(array $item, string $fieldName): mixed
2420
{
2521
$rawDynamoDbValue = $item[$fieldName][$this->dynamoDbType()];
2622

2723
return $this->castValueFromDynamoDbFormat($rawDynamoDbValue);
2824
}
2925

30-
/**
31-
* @param mixed $fieldValue
32-
*/
33-
public function dynamoDbQueryValue($fieldValue): array
26+
public function dynamoDbQueryValue(mixed $fieldValue): array
3427
{
3528
return [
3629
$this->dynamoDbType() => $this->castValueForDynamoDbFormat($fieldValue),
@@ -39,15 +32,7 @@ public function dynamoDbQueryValue($fieldValue): array
3932

4033
abstract protected function dynamoDbType(): string;
4134

42-
/**
43-
* @param mixed $value
44-
* @return mixed
45-
*/
46-
abstract protected function castValueForDynamoDbFormat($value);
47-
48-
/**
49-
* @param mixed $value
50-
* @return mixed
51-
*/
52-
abstract protected function castValueFromDynamoDbFormat($value);
35+
abstract protected function castValueForDynamoDbFormat(mixed $value): mixed;
36+
37+
abstract protected function castValueFromDynamoDbFormat(mixed $value): mixed;
5338
}

src/Field/FloatField.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function dynamoDbType(): string
1212
/**
1313
* {@inheritdoc}
1414
*/
15-
protected function castValueForDynamoDbFormat($value): string
15+
protected function castValueForDynamoDbFormat(mixed $value): string
1616
{
1717
// Numbers should be sent as strings to DynamoDB
1818
return (string) $value;
@@ -21,7 +21,7 @@ protected function castValueForDynamoDbFormat($value): string
2121
/**
2222
* {@inheritdoc}
2323
*/
24-
protected function castValueFromDynamoDbFormat($value): float
24+
protected function castValueFromDynamoDbFormat(mixed $value): float
2525
{
2626
return (float) $value;
2727
}

src/Field/IntegerField.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function dynamoDbType(): string
1212
/**
1313
* {@inheritdoc}
1414
*/
15-
protected function castValueForDynamoDbFormat($value): string
15+
protected function castValueForDynamoDbFormat(mixed $value): string
1616
{
1717
// Numbers should be sent as strings to DynamoDB
1818
return (string) $value;
@@ -21,7 +21,7 @@ protected function castValueForDynamoDbFormat($value): string
2121
/**
2222
* {@inheritdoc}
2323
*/
24-
protected function castValueFromDynamoDbFormat($value): int
24+
protected function castValueFromDynamoDbFormat(mixed $value): int
2525
{
2626
return (int) $value;
2727
}

src/Field/StringField.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ public function dynamoDbType(): string
1212
/**
1313
* {@inheritdoc}
1414
*/
15-
protected function castValueForDynamoDbFormat($value): string
15+
protected function castValueForDynamoDbFormat(mixed $value): string
1616
{
1717
return (string) $value;
1818
}
1919

2020
/**
2121
* {@inheritdoc}
2222
*/
23-
protected function castValueFromDynamoDbFormat($value): string
23+
protected function castValueFromDynamoDbFormat(mixed $value): string
2424
{
2525
return (string) $value;
2626
}

src/Mapping.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
class Mapping
1111
{
1212
/** @var array|TableMapping[] */
13-
private $tables;
14-
/** @var PropertyInfoExtractor|null */
15-
private $propertyInfo;
13+
private array $tables;
14+
private ?PropertyInfoExtractor $propertyInfo = null;
1615

1716
public function __construct(array $mappingConfig)
1817
{

src/Table.php

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,14 @@
66
use Aws\DynamoDb\Exception\DynamoDbException;
77
use Dynamap\Exception\ItemNotFound;
88
use Dynamap\Exception\TableNotFound;
9+
use InvalidArgumentException;
910

1011
class Table
1112
{
12-
/** @var DynamoDbClient */
13-
private $dynamoDb;
14-
15-
/** @var TableMapping */
16-
private $mapping;
17-
18-
public function __construct(DynamoDbClient $dynamoDb, TableMapping $mapping)
19-
{
20-
$this->dynamoDb = $dynamoDb;
21-
$this->mapping = $mapping;
13+
public function __construct(
14+
private DynamoDbClient $dynamoDb,
15+
private TableMapping $mapping
16+
) {
2217
}
2318

2419
public function getAll(): array
@@ -35,11 +30,10 @@ public function getAll(): array
3530
*
3631
* Throws an exception if the item cannot be found (see `find()` as an alternative).
3732
*
38-
* @param int|string|array $key
39-
* @throws \InvalidArgumentException If the key is invalid.
33+
* @throws InvalidArgumentException If the key is invalid.
4034
* @throws ItemNotFound If the item cannot be found.
4135
*/
42-
public function get($key): object
36+
public function get(array|int|string $key): object
4337
{
4438
$item = $this->find($key);
4539
if ($item === null) {
@@ -54,10 +48,9 @@ public function get($key): object
5448
*
5549
* Returns null if the item cannot be found (see `get()` as an alternative).
5650
*
57-
* @param int|string|array $key
58-
* @throws \InvalidArgumentException If the key is invalid.
51+
* @throws InvalidArgumentException If the key is invalid.
5952
*/
60-
public function find($key): ?object
53+
public function find(array|int|string $key): ?object
6154
{
6255
$table = $this->mapping->getTableName();
6356

@@ -120,11 +113,10 @@ public function save(object $object): void
120113
* Warning: if the item has been loaded as a PHP object, the PHP object will not be updated.
121114
* If you want it to be updated you will need to reload it from database.
122115
*
123-
* @param int|string|array $itemKey
124116
* @param array $values Key-value map
125117
* @throws \Exception
126118
*/
127-
public function partialUpdate($itemKey, array $values): void
119+
public function partialUpdate(array|int|string $itemKey, array $values): void
128120
{
129121
$key = $this->buildKeyQuery($itemKey);
130122

@@ -173,15 +165,14 @@ private function map(array $item): object
173165
}
174166

175167
/**
176-
* @param int|string|array $key
177-
* @throws \InvalidArgumentException
168+
* @throws InvalidArgumentException
178169
*/
179-
private function buildKeyQuery($key): array
170+
private function buildKeyQuery(array|int|string $key): array
180171
{
181172
$keyQuery = [];
182173
if (! is_array($key)) {
183174
if ($this->mapping->isCompositeKey()) {
184-
throw new \InvalidArgumentException('The key is a composite key and only a single value was provided');
175+
throw new InvalidArgumentException('The key is a composite key and only a single value was provided');
185176
}
186177
foreach ($this->mapping->getKeyMapping() as $fieldMapping) {
187178
$keyQuery[$fieldMapping->name()] = $fieldMapping->dynamoDbQueryValue($key);
@@ -190,7 +181,7 @@ private function buildKeyQuery($key): array
190181
foreach ($this->mapping->getKeyMapping() as $fieldMapping) {
191182
$fieldName = $fieldMapping->name();
192183
if (! isset($key[$fieldName])) {
193-
throw new \InvalidArgumentException('The key is incomplete');
184+
throw new InvalidArgumentException('The key is incomplete');
194185
}
195186
$keyQuery[$fieldName] = $fieldMapping->dynamoDbQueryValue($key[$fieldName]);
196187
}

src/TableMapping.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,13 @@
1212

1313
class TableMapping
1414
{
15-
/** @var PropertyInfoExtractorInterface */
16-
private $propertyInfo;
17-
/** @var string */
18-
private $tableName;
19-
/** @var string */
20-
private $className;
15+
private PropertyInfoExtractorInterface $propertyInfo;
16+
private string $tableName;
17+
private string $className;
2118
/** @var Field[] */
22-
private $keys = [];
19+
private array $keys = [];
2320
/** @var Field[] */
24-
private $fields = [];
21+
private array $fields = [];
2522

2623
public function __construct(PropertyInfoExtractorInterface $propertyInfo, string $className, array $mappingConfig)
2724
{

0 commit comments

Comments
 (0)