Skip to content

Commit eb7eb1f

Browse files
Merge pull request #676 from MauricioFauth/php8.5-deprecation-tests
Fix PHP 8.5 deprecations in tests
2 parents 1c8eb62 + dcfd551 commit eb7eb1f

5 files changed

Lines changed: 202 additions & 3 deletions

File tree

‎.github/workflows/tests.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ jobs:
9292
- {php: '8.2', phpunit: '11'}
9393
- {php: '8.3', phpunit: '12'}
9494
- {php: '8.4', phpunit: '13'}
95+
- {php: '8.5', phpunit: '13'}
9596
steps:
9697
- name: Checkout code
9798
uses: actions/checkout@v7

‎composer.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"phpunit/phpunit": "^8.5 || ^9.6",
3939
"psalm/plugin-phpunit": "^0.16.1",
4040
"vimeo/psalm": "^4.11",
41-
"zumba/json-serializer": "^3.2"
41+
"zumba/json-serializer": "~3.2.4"
4242
},
4343
"conflict": {
4444
"phpmyadmin/motranslator": "<3.0"

‎phpstan-baseline.neon‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1685,21 +1685,71 @@ parameters:
16851685
count: 1
16861686
path: src/Tools/ContextGenerator.php
16871687

1688+
-
1689+
message: "#^Cannot access offset 0 on mixed\\.$#"
1690+
count: 1
1691+
path: src/Tools/CustomJsonSerializer.php
1692+
1693+
-
1694+
message: "#^Cannot call method unserialize\\(\\) on mixed\\.$#"
1695+
count: 1
1696+
path: src/Tools/CustomJsonSerializer.php
1697+
1698+
-
1699+
message: "#^Class UnitEnum not found\\.$#"
1700+
count: 1
1701+
path: src/Tools/CustomJsonSerializer.php
1702+
16881703
-
16891704
message: "#^Method PhpMyAdmin\\\\SqlParser\\\\Tools\\\\CustomJsonSerializer\\:\\:extractObjectData\\(\\) has parameter \\$ref with generic class ReflectionClass but does not specify its types\\: T$#"
16901705
count: 1
16911706
path: src/Tools/CustomJsonSerializer.php
16921707

1708+
-
1709+
message: "#^Method PhpMyAdmin\\\\SqlParser\\\\Tools\\\\CustomJsonSerializer\\:\\:unserializeObject\\(\\) should return object but returns mixed\\.$#"
1710+
count: 4
1711+
path: src/Tools/CustomJsonSerializer.php
1712+
1713+
-
1714+
message: "#^Parameter \\#1 \\$classname of function class_exists expects string, mixed given\\.$#"
1715+
count: 1
1716+
path: src/Tools/CustomJsonSerializer.php
1717+
1718+
-
1719+
message: "#^Parameter \\#1 \\$data of method SplDoublyLinkedList\\<mixed\\>\\:\\:unserialize\\(\\) expects string, mixed given\\.$#"
1720+
count: 1
1721+
path: src/Tools/CustomJsonSerializer.php
1722+
1723+
-
1724+
message: "#^Parameter \\#1 \\$key of function array_key_exists expects int\\|string, mixed given\\.$#"
1725+
count: 1
1726+
path: src/Tools/CustomJsonSerializer.php
1727+
1728+
-
1729+
message: "#^Parameter \\#1 \\$string of function substr expects string, mixed given\\.$#"
1730+
count: 1
1731+
path: src/Tools/CustomJsonSerializer.php
1732+
1733+
-
1734+
message: "#^Parameter \\#2 \\$name of method Zumba\\\\JsonSerializer\\\\JsonSerializer\\:\\:getReflectionProperty\\(\\) expects string, \\(int\\|string\\) given\\.$#"
1735+
count: 1
1736+
path: src/Tools/CustomJsonSerializer.php
1737+
16931738
-
16941739
message: "#^Parameter \\#3 \\$properties \\(array\\<string\\>\\) of method PhpMyAdmin\\\\SqlParser\\\\Tools\\\\CustomJsonSerializer\\:\\:extractObjectData\\(\\) should be contravariant with parameter \\$properties \\(array\\) of method Zumba\\\\JsonSerializer\\\\JsonSerializer\\:\\:extractObjectData\\(\\)$#"
16951740
count: 1
16961741
path: src/Tools/CustomJsonSerializer.php
16971742

16981743
-
1699-
message: "#^Variable property access on object\\.$#"
1744+
message: "#^Variable \\$ref might not be defined\\.$#"
17001745
count: 1
17011746
path: src/Tools/CustomJsonSerializer.php
17021747

1748+
-
1749+
message: "#^Variable property access on object\\.$#"
1750+
count: 2
1751+
path: src/Tools/CustomJsonSerializer.php
1752+
17031753
-
17041754
message: "#^Argument of an invalid type array\\<int, string\\>\\|false supplied for foreach, only iterables are supported\\.$#"
17051755
count: 1

‎psalm-baseline.xml‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1201,16 +1201,61 @@
12011201
</RedundantConditionGivenDocblockType>
12021202
</file>
12031203
<file src="src/Tools/CustomJsonSerializer.php">
1204-
<MixedAssignment occurrences="2">
1204+
<MixedArgument occurrences="6">
1205+
<code>$className</code>
1206+
<code>$className</code>
1207+
<code>$className</code>
1208+
<code>$ref</code>
1209+
<code>$value['value']</code>
1210+
<code>UnitEnum::class</code>
1211+
</MixedArgument>
1212+
<MixedArgumentTypeCoercion occurrences="1">
1213+
<code>$property</code>
1214+
</MixedArgumentTypeCoercion>
1215+
<MixedArrayAccess occurrences="1">
1216+
<code>$className[0]</code>
1217+
</MixedArrayAccess>
1218+
<MixedAssignment occurrences="6">
12051219
<code>$data[$property]</code>
12061220
<code>$data[$property]</code>
1221+
<code>$obj</code>
1222+
<code>$obj</code>
1223+
<code>$obj</code>
1224+
<code>$propertyValue</code>
12071225
</MixedAssignment>
1226+
<MixedInferredReturnType occurrences="1">
1227+
<code>object</code>
1228+
</MixedInferredReturnType>
1229+
<MixedMethodCall occurrences="2">
1230+
<code>new $className()</code>
1231+
<code>unserialize</code>
1232+
</MixedMethodCall>
1233+
<MixedOperand occurrences="2">
1234+
<code>$className</code>
1235+
<code>$value['name']</code>
1236+
</MixedOperand>
1237+
<MixedReturnStatement occurrences="4">
1238+
<code>$obj</code>
1239+
<code>$obj</code>
1240+
<code>$obj</code>
1241+
<code>$this-&gt;objectMapping[$index]</code>
1242+
</MixedReturnStatement>
12081243
<MoreSpecificImplementedParamType occurrences="1">
12091244
<code>$properties</code>
12101245
</MoreSpecificImplementedParamType>
1246+
<PossiblyUndefinedVariable occurrences="1">
1247+
<code>$ref</code>
1248+
</PossiblyUndefinedVariable>
12111249
<PropertyNotSetInConstructor occurrences="1">
12121250
<code>CustomJsonSerializer</code>
12131251
</PropertyNotSetInConstructor>
1252+
<TypeDoesNotContainType occurrences="2">
1253+
<code>$className === 'DateTime'</code>
1254+
<code>$className === 'DateTimeImmutable'</code>
1255+
</TypeDoesNotContainType>
1256+
<UndefinedClass occurrences="1">
1257+
<code>UnitEnum</code>
1258+
</UndefinedClass>
12141259
</file>
12151260
<file src="src/Tools/TestGenerator.php">
12161261
<MixedOperand occurrences="1">

‎src/Tools/CustomJsonSerializer.php‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@
66

77
use ReflectionClass;
88
use ReflectionException;
9+
use SplDoublyLinkedList;
10+
use UnitEnum;
11+
use Zumba\JsonSerializer\Exception\JsonSerializerException;
912
use Zumba\JsonSerializer\JsonSerializer;
1013

14+
use function array_key_exists;
15+
use function class_exists;
16+
use function constant;
1117
use function in_array;
18+
use function is_subclass_of;
1219
use function method_exists;
20+
use function substr;
1321

1422
use const PHP_VERSION_ID;
1523

@@ -84,4 +92,99 @@ protected function extractObjectData($value, $ref, $properties)
8492

8593
return $data;
8694
}
95+
96+
/**
97+
* Convert the serialized array into an object
98+
*
99+
* @param array<mixed> $value
100+
*
101+
* @return object
102+
*
103+
* @throws JsonSerializerException
104+
*/
105+
protected function unserializeObject($value)
106+
{
107+
$className = $value[self::CLASS_IDENTIFIER_KEY];
108+
unset($value[self::CLASS_IDENTIFIER_KEY]);
109+
110+
if ($className[0] === '@') {
111+
$index = substr($className, 1);
112+
113+
return $this->objectMapping[$index];
114+
}
115+
116+
if (array_key_exists($className, $this->customObjectSerializerMap)) {
117+
$obj = $this->customObjectSerializerMap[$className]->unserialize($value);
118+
$this->objectMapping[$this->objectMappingIndex++] = $obj;
119+
120+
return $obj;
121+
}
122+
123+
if (! class_exists($className)) {
124+
throw new JsonSerializerException('Unable to find class ' . $className);
125+
}
126+
127+
if ($this->allowedClasses !== null && ! in_array($className, $this->allowedClasses, true)) {
128+
throw new JsonSerializerException(
129+
'Class ' . $className . ' is not allowed for deserialization. ' .
130+
'Use setAllowedClasses() to configure the list of allowed classes.'
131+
);
132+
}
133+
134+
if ($className === 'DateTime' || $className === 'DateTimeImmutable') {
135+
$obj = $this->restoreUsingUnserialize($className, $value);
136+
$this->objectMapping[$this->objectMappingIndex++] = $obj;
137+
138+
return $obj;
139+
}
140+
141+
if (is_subclass_of($className, UnitEnum::class)) {
142+
$obj = constant($className . '::' . $value['name']);
143+
$this->objectMapping[$this->objectMappingIndex++] = $obj;
144+
145+
return $obj;
146+
}
147+
148+
if (! $this->isSplList($className)) {
149+
$ref = new ReflectionClass($className);
150+
$obj = $ref->newInstanceWithoutConstructor();
151+
} else {
152+
$obj = new $className();
153+
}
154+
155+
if ($obj instanceof SplDoublyLinkedList) {
156+
$obj->unserialize($value['value']);
157+
$this->objectMapping[$this->objectMappingIndex++] = $obj;
158+
159+
return $obj;
160+
}
161+
162+
$this->objectMapping[$this->objectMappingIndex++] = $obj;
163+
foreach ($value as $property => $propertyValue) {
164+
try {
165+
$propRef = $this->getReflectionProperty($ref, $property);
166+
if (PHP_VERSION_ID < 80100) {
167+
$propRef->setAccessible(true);
168+
}
169+
170+
$propRef->setValue($obj, $this->unserializeData($propertyValue));
171+
} catch (ReflectionException $e) {
172+
switch ($this->undefinedAttributeMode) {
173+
case self::UNDECLARED_PROPERTY_MODE_SET:
174+
$obj->$property = $this->unserializeData($propertyValue);
175+
break;
176+
case self::UNDECLARED_PROPERTY_MODE_IGNORE:
177+
break;
178+
case self::UNDECLARED_PROPERTY_MODE_EXCEPTION:
179+
throw new JsonSerializerException('Undefined attribute detected during unserialization');
180+
}
181+
}
182+
}
183+
184+
if (method_exists($obj, '__wakeup')) {
185+
$obj->__wakeup();
186+
}
187+
188+
return $obj;
189+
}
87190
}

0 commit comments

Comments
 (0)