Skip to content

Commit 2fd2ecd

Browse files
committed
Upgrade to php 8.1
Upgrade phpunit to 9.3.11
1 parent 8a02608 commit 2fd2ecd

15 files changed

Lines changed: 588 additions & 494 deletions

composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@
1212
],
1313
"autoload": {
1414
"psr-4": {
15-
"CodinPro\\DataTransferObject\\": "src/"
15+
"CodinPro\\DataTransferObject\\": "src/",
16+
"CodinPro\\Tests\\DataTransferObject\\": "tests/"
1617
}
1718
},
19+
"require": {
20+
"php": "^8.1"
21+
},
1822
"require-dev": {
19-
"phpunit/phpunit": "4.8.*"
23+
"phpunit/phpunit": "^9.3.11"
2024
}
2125
}

phpunit.xml

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,19 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit backupGlobals="false"
3-
backupStaticAttributes="false"
4-
bootstrap="vendor/autoload.php"
5-
colors="true"
6-
convertErrorsToExceptions="true"
7-
convertNoticesToExceptions="true"
8-
convertWarningsToExceptions="true"
9-
processIsolation="false"
10-
stopOnFailure="false"
11-
syntaxCheck="true"
12-
>
13-
<filter>
14-
<whitelist processUncoveredFilesFromWhitelist="true">
15-
<directory suffix=".php">./src/</directory>
16-
</whitelist>
17-
</filter>
18-
<logging>
19-
<log type="coverage-clover"
20-
target="tests/coverage-clover.xml"/>
21-
</logging>
22-
<testsuites>
23-
<testsuite name="DTO Tests">
24-
<directory suffix=".php">./tests/</directory>
25-
</testsuite>
26-
</testsuites>
27-
</phpunit>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
bootstrap="vendor/autoload.php"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
5+
<coverage processUncoveredFiles="true">
6+
<include>
7+
<directory suffix=".php">./src/</directory>
8+
</include>
9+
<report>
10+
<clover outputFile="tests/coverage-clover.xml"/>
11+
</report>
12+
</coverage>
13+
<logging/>
14+
<testsuites>
15+
<testsuite name="DTO Tests">
16+
<directory suffix=".php">./tests/</directory>
17+
</testsuite>
18+
</testsuites>
19+
</phpunit>

src/CustomSerializer.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44

55
class CustomSerializer implements DTOSerializerInterface
66
{
7-
87
/**
98
* Serialize given data to string
109
* @param $data
1110
* @return string
1211
*/
13-
public function serialize($data)
12+
public function serialize($data): string
1413
{
1514
return serialize($data);
1615
}

src/DTO.php

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

33
namespace CodinPro\DataTransferObject;
44

5+
use JsonException;
6+
57
class DTO extends DTOBase
68
{
7-
89
/**
910
* DTO constructor.
10-
* @param array|object|string $data
11-
* @param DTOSerializerInterface|null $serializer
12-
* @throws \InvalidArgumentException
11+
* @param array|object|string $data
12+
* @param DTOSerializerInterface|null $serializer
13+
* @throws JsonException
1314
*/
1415
public function __construct($data = [], $serializer = null)
1516
{
@@ -20,7 +21,7 @@ public function __construct($data = [], $serializer = null)
2021
* Get object variables defined in DTO
2122
* @return array
2223
*/
23-
private function collectVariables()
24+
private function collectVariables(): array
2425
{
2526
$currentVariables = get_object_vars($this);
2627
$parentVariables = get_class_vars(parent::class);

src/DTOAccessorTrait.php

Lines changed: 63 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,110 +2,116 @@
22

33
namespace CodinPro\DataTransferObject;
44

5+
use InvalidArgumentException;
6+
57
/**
68
* @property mixed $innerDTOData DTO data
79
* @property array $innerDTODefault DTO keys and default values
810
*/
911
trait DTOAccessorTrait
1012
{
11-
/**
12-
* Get value by offset or nested structure
13-
* @param string $offset
14-
* @return mixed
15-
*/
16-
public abstract function get($offset);
13+
public function __get($key)
14+
{
15+
return $this->offsetGet($key);
16+
}
1717

18-
/**
19-
* Check if offset exists
20-
* @param string $offset
21-
* @return bool
22-
*/
23-
public function offsetExists($offset)
18+
public function __set($key, $value)
2419
{
25-
return isset($this->innerDTOData[$offset]);
20+
$this->offsetSet($key, $value);
2621
}
2722

2823
/**
29-
* Get data at scalar offset or default value instead
30-
* @param string $offset
24+
* Get data at offset or default value instead
25+
* @param string $offset
3126
* @return mixed
32-
* @throws \InvalidArgumentException
27+
* @throws InvalidArgumentException
3328
*/
34-
private function offsetGetScalar($offset)
29+
public function offsetGet($offset): mixed
3530
{
36-
if (array_key_exists($offset, $this->innerDTOData)) {
37-
return $this->innerDTOData[$offset];
38-
}
39-
40-
return $this->getDefaultValue($offset);
31+
return $this->get($offset);
4132
}
4233

4334
/**
44-
* Get default value at offset if set
45-
* @param string $offset
35+
* Get value by offset or nested structure
36+
* @param string $offset
4637
* @return mixed
47-
* @throws \InvalidArgumentException
4838
*/
49-
private function getDefaultValue($offset)
39+
abstract public function get(string $offset): mixed;
40+
41+
/**
42+
* Set data at offset
43+
* @param string $offset
44+
* @param mixed $value
45+
*/
46+
public function offsetSet($offset, mixed $value): void
5047
{
51-
if (array_key_exists($offset, $this->innerDTODefault)) {
52-
return $this->innerDTODefault[$offset];
53-
}
48+
$this->innerDTOData[$offset] = $value;
49+
}
5450

55-
throw new \InvalidArgumentException('Offset '.$offset.' does not exist.');
51+
public function __isset($key)
52+
{
53+
return isset($this->innerDTOData[$key]);
5654
}
5755

5856
/**
59-
* Get data at offset or default value instead
60-
* @param string $offset
61-
* @return mixed
62-
* @throws \InvalidArgumentException
57+
* Count data elements
58+
* @return int
6359
*/
64-
public function offsetGet($offset)
60+
public function count(): int
6561
{
66-
return $this->get($offset);
62+
return count($this->innerDTOData);
6763
}
6864

6965
/**
70-
* Set data at offset
71-
* @param string $offset
72-
* @param mixed $value
66+
* Check if offset exists
67+
* @param string $offset
68+
* @return bool
7369
*/
74-
public function offsetSet($offset, $value)
70+
public function offsetExists($offset): bool
7571
{
76-
$this->innerDTOData[$offset] = $value;
72+
return isset($this->innerDTOData[$offset]);
7773
}
7874

7975
/**
8076
* Remove data at offset
81-
* @param string $offset
77+
* @param string $offset
8278
*/
83-
public function offsetUnset($offset)
79+
public function offsetUnset($offset): void
8480
{
8581
unset($this->innerDTOData[$offset]);
8682
}
8783

8884
/**
89-
* Count data elements
90-
* @return int
85+
* Get data at scalar offset or default value instead
86+
* @param string $offset
87+
* @param bool $strict
88+
* @return mixed
9189
*/
92-
public function count()
90+
private function offsetGetScalar(string $offset, bool $strict = false): mixed
9391
{
94-
return count($this->innerDTOData);
95-
}
92+
if (array_key_exists($offset, $this->innerDTOData)) {
93+
return $this->innerDTOData[$offset];
94+
}
9695

97-
public function __get($key)
98-
{
99-
return $this->offsetGet($key);
96+
return $this->getDefaultValue($offset, $strict);
10097
}
10198

102-
public function __set($key, $value)
99+
/**
100+
* Get default value at offset if set
101+
* @param string $offset
102+
* @param bool $strict
103+
* @return mixed
104+
*/
105+
private function getDefaultValue(string $offset, bool $strict = false): mixed
103106
{
104-
$this->offsetSet($key, $value);
105-
}
107+
if (array_key_exists($offset, $this->innerDTODefault)) {
108+
return $this->innerDTODefault[$offset];
109+
}
106110

107-
public function __isset($key)
108-
{
109-
return isset($this->innerDTOData[$key]);
111+
if ($strict) {
112+
throw new InvalidArgumentException('Offset ' . $offset . ' does not exist.');
113+
}
114+
115+
return null;
110116
}
111117
}

0 commit comments

Comments
 (0)