Skip to content

Commit bb08407

Browse files
committed
Merge branch '5.11.x' into 6.0.x
Signed-off-by: Maurício Meneghini Fauth <mauricio@mfauth.com.br>
2 parents 4f420b9 + eb7eb1f commit bb08407

5 files changed

Lines changed: 207 additions & 3 deletions

File tree

‎.github/workflows/tests.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ jobs:
8787
- {php: '8.2', phpunit: '11'}
8888
- {php: '8.3', phpunit: '12'}
8989
- {php: '8.4', phpunit: '13'}
90+
- {php: '8.5', phpunit: '13'}
9091
steps:
9192
- name: Checkout code
9293
uses: actions/checkout@v7

‎composer.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"phpunit/phpunit": "^11.5",
3838
"psalm/plugin-phpunit": "^0.19.2",
3939
"vimeo/psalm": "^6.0",
40-
"zumba/json-serializer": "^3.2"
40+
"zumba/json-serializer": "~3.2.4"
4141
},
4242
"conflict": {
4343
"phpmyadmin/motranslator": "<5.2"

‎phpstan-baseline.neon‎

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1644,22 +1644,88 @@ parameters:
16441644
count: 1
16451645
path: src/Tools/ContextGenerator.php
16461646

1647+
-
1648+
message: '#^Binary operation "\." between ''Unable to find…'' and mixed results in an error\.$#'
1649+
identifier: binaryOp.invalid
1650+
count: 1
1651+
path: src/Tools/CustomJsonSerializer.php
1652+
1653+
-
1654+
message: '#^Binary operation "\." between non\-falsy\-string and mixed results in an error\.$#'
1655+
identifier: binaryOp.invalid
1656+
count: 1
1657+
path: src/Tools/CustomJsonSerializer.php
1658+
1659+
-
1660+
message: '#^Cannot access offset 0 on mixed\.$#'
1661+
identifier: offsetAccess.nonOffsetAccessible
1662+
count: 1
1663+
path: src/Tools/CustomJsonSerializer.php
1664+
1665+
-
1666+
message: '#^Cannot call method unserialize\(\) on mixed\.$#'
1667+
identifier: method.nonObject
1668+
count: 1
1669+
path: src/Tools/CustomJsonSerializer.php
1670+
16471671
-
16481672
message: '#^Method PhpMyAdmin\\SqlParser\\Tools\\CustomJsonSerializer\:\:extractObjectData\(\) has parameter \$ref with generic class ReflectionClass but does not specify its types\: T$#'
16491673
identifier: missingType.generics
16501674
count: 1
16511675
path: src/Tools/CustomJsonSerializer.php
16521676

1677+
-
1678+
message: '#^Method PhpMyAdmin\\SqlParser\\Tools\\CustomJsonSerializer\:\:unserializeObject\(\) should return object but returns mixed\.$#'
1679+
identifier: return.type
1680+
count: 4
1681+
path: src/Tools/CustomJsonSerializer.php
1682+
1683+
-
1684+
message: '#^Parameter \#1 \$class of function class_exists expects string, mixed given\.$#'
1685+
identifier: argument.type
1686+
count: 1
1687+
path: src/Tools/CustomJsonSerializer.php
1688+
1689+
-
1690+
message: '#^Parameter \#1 \$data of method SplDoublyLinkedList\<mixed\>\:\:unserialize\(\) expects string, mixed given\.$#'
1691+
identifier: argument.type
1692+
count: 1
1693+
path: src/Tools/CustomJsonSerializer.php
1694+
1695+
-
1696+
message: '#^Parameter \#1 \$key of function array_key_exists expects int\|string, mixed given\.$#'
1697+
identifier: argument.type
1698+
count: 1
1699+
path: src/Tools/CustomJsonSerializer.php
1700+
1701+
-
1702+
message: '#^Parameter \#1 \$string of function substr expects string, mixed given\.$#'
1703+
identifier: argument.type
1704+
count: 1
1705+
path: src/Tools/CustomJsonSerializer.php
1706+
1707+
-
1708+
message: '#^Parameter \#2 \$name of method Zumba\\JsonSerializer\\JsonSerializer\:\:getReflectionProperty\(\) expects string, \(int\|string\) given\.$#'
1709+
identifier: argument.type
1710+
count: 1
1711+
path: src/Tools/CustomJsonSerializer.php
1712+
16531713
-
16541714
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\(\)$#'
16551715
identifier: method.childParameterType
16561716
count: 1
16571717
path: src/Tools/CustomJsonSerializer.php
16581718

1719+
-
1720+
message: '#^Variable \$ref might not be defined\.$#'
1721+
identifier: variable.undefined
1722+
count: 1
1723+
path: src/Tools/CustomJsonSerializer.php
1724+
16591725
-
16601726
message: '#^Variable property access on object\.$#'
16611727
identifier: property.dynamicName
1662-
count: 1
1728+
count: 2
16631729
path: src/Tools/CustomJsonSerializer.php
16641730

16651731
-

‎psalm-baseline.xml‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<files psalm-version="6.19.2@fa716e443b23e6fa35bf58907e67ca692ec3c00a">
2+
<files psalm-version="6.20.0@804f03c0c183d149fcd4b8c7c0c9b3fbf836d34c">
33
<file src="src/Components/AlterOperation.php">
44
<PossiblyNullReference>
55
<code><![CDATA[has]]></code>
@@ -907,16 +907,54 @@
907907
</PossiblyFalseIterator>
908908
</file>
909909
<file src="src/Tools/CustomJsonSerializer.php">
910+
<MixedArgument>
911+
<code><![CDATA[$className]]></code>
912+
<code><![CDATA[$className]]></code>
913+
<code><![CDATA[$className]]></code>
914+
<code><![CDATA[$ref]]></code>
915+
<code><![CDATA[$value['value']]]></code>
916+
</MixedArgument>
917+
<MixedArgumentTypeCoercion>
918+
<code><![CDATA[$property]]></code>
919+
</MixedArgumentTypeCoercion>
920+
<MixedArrayAccess>
921+
<code><![CDATA[$className[0]]]></code>
922+
</MixedArrayAccess>
910923
<MixedAssignment>
911924
<code><![CDATA[$data[$property]]]></code>
912925
<code><![CDATA[$data[$property]]]></code>
926+
<code><![CDATA[$obj]]></code>
927+
<code><![CDATA[$obj]]></code>
928+
<code><![CDATA[$obj]]></code>
929+
<code><![CDATA[$propertyValue]]></code>
913930
</MixedAssignment>
931+
<MixedMethodCall>
932+
<code><![CDATA[new $className()]]></code>
933+
<code><![CDATA[unserialize]]></code>
934+
</MixedMethodCall>
935+
<MixedOperand>
936+
<code><![CDATA[$className]]></code>
937+
<code><![CDATA[$value['name']]]></code>
938+
</MixedOperand>
939+
<MixedReturnStatement>
940+
<code><![CDATA[$obj]]></code>
941+
<code><![CDATA[$obj]]></code>
942+
<code><![CDATA[$obj]]></code>
943+
<code><![CDATA[$this->objectMapping[$index]]]></code>
944+
</MixedReturnStatement>
914945
<MoreSpecificImplementedParamType>
915946
<code><![CDATA[$properties]]></code>
916947
</MoreSpecificImplementedParamType>
948+
<PossiblyUndefinedVariable>
949+
<code><![CDATA[$ref]]></code>
950+
</PossiblyUndefinedVariable>
917951
<PropertyNotSetInConstructor>
918952
<code><![CDATA[CustomJsonSerializer]]></code>
919953
</PropertyNotSetInConstructor>
954+
<TypeDoesNotContainType>
955+
<code><![CDATA[$className === 'DateTime']]></code>
956+
<code><![CDATA[$className === 'DateTimeImmutable']]></code>
957+
</TypeDoesNotContainType>
920958
</file>
921959
<file src="src/Tools/TestGenerator.php">
922960
<MixedAssignment>

‎src/Tools/CustomJsonSerializer.php‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +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;
19+
use function method_exists;
20+
use function substr;
1221

1322
/**
1423
* Used for .out files generation
@@ -54,4 +63,94 @@ protected function extractObjectData($value, $ref, $properties): array
5463

5564
return $data;
5665
}
66+
67+
/**
68+
* Convert the serialized array into an object
69+
*
70+
* @param array<mixed> $value
71+
*
72+
* @throws JsonSerializerException
73+
*/
74+
// phpcs:ignore SlevomatCodingStandard.TypeHints
75+
protected function unserializeObject($value): object
76+
{
77+
$className = $value[self::CLASS_IDENTIFIER_KEY];
78+
unset($value[self::CLASS_IDENTIFIER_KEY]);
79+
80+
if ($className[0] === '@') {
81+
$index = substr($className, 1);
82+
83+
return $this->objectMapping[$index];
84+
}
85+
86+
if (array_key_exists($className, $this->customObjectSerializerMap)) {
87+
$obj = $this->customObjectSerializerMap[$className]->unserialize($value);
88+
$this->objectMapping[$this->objectMappingIndex++] = $obj;
89+
90+
return $obj;
91+
}
92+
93+
if (! class_exists($className)) {
94+
throw new JsonSerializerException('Unable to find class ' . $className);
95+
}
96+
97+
if ($this->allowedClasses !== null && ! in_array($className, $this->allowedClasses, true)) {
98+
throw new JsonSerializerException(
99+
'Class ' . $className . ' is not allowed for deserialization. ' .
100+
'Use setAllowedClasses() to configure the list of allowed classes.',
101+
);
102+
}
103+
104+
if ($className === 'DateTime' || $className === 'DateTimeImmutable') {
105+
$obj = $this->restoreUsingUnserialize($className, $value);
106+
$this->objectMapping[$this->objectMappingIndex++] = $obj;
107+
108+
return $obj;
109+
}
110+
111+
if (is_subclass_of($className, UnitEnum::class)) {
112+
$obj = constant($className . '::' . $value['name']);
113+
$this->objectMapping[$this->objectMappingIndex++] = $obj;
114+
115+
return $obj;
116+
}
117+
118+
if (! $this->isSplList($className)) {
119+
$ref = new ReflectionClass($className);
120+
$obj = $ref->newInstanceWithoutConstructor();
121+
} else {
122+
$obj = new $className();
123+
}
124+
125+
if ($obj instanceof SplDoublyLinkedList) {
126+
$obj->unserialize($value['value']);
127+
$this->objectMapping[$this->objectMappingIndex++] = $obj;
128+
129+
return $obj;
130+
}
131+
132+
$this->objectMapping[$this->objectMappingIndex++] = $obj;
133+
foreach ($value as $property => $propertyValue) {
134+
try {
135+
$propRef = $this->getReflectionProperty($ref, $property);
136+
$propRef->setValue($obj, $this->unserializeData($propertyValue));
137+
} catch (ReflectionException) {
138+
switch ($this->undefinedAttributeMode) {
139+
case self::UNDECLARED_PROPERTY_MODE_SET:
140+
$obj->$property = $this->unserializeData($propertyValue);
141+
break;
142+
case self::UNDECLARED_PROPERTY_MODE_IGNORE:
143+
break;
144+
case self::UNDECLARED_PROPERTY_MODE_EXCEPTION:
145+
throw new JsonSerializerException('Undefined attribute detected during unserialization');
146+
}
147+
}
148+
}
149+
150+
if (method_exists($obj, '__wakeup')) {
151+
$obj->__wakeup();
152+
}
153+
154+
return $obj;
155+
}
57156
}

0 commit comments

Comments
 (0)