Skip to content

Commit 0b70abc

Browse files
authored
gen_stub: Implement deep cloning using array_map() (#21555)
This is for forward compatibility when the deep-cloned arrays will become `readonly`. Reassigning each item individually is not allowed then.
1 parent 0ada938 commit 0b70abc

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

build/gen_stub.php

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,24 @@ function array_any(array $array, callable $callback): bool {
5454
}
5555
}
5656

57+
if (function_exists('clone')) {
58+
// Replace (\clone)(...) with \clone(...) once the minimally
59+
// supported PHP version is 8.5.
60+
define('clone', \clone(...));
61+
} else {
62+
define(
63+
'clone',
64+
/**
65+
* @template T
66+
* @param T $o
67+
* @return T
68+
*/
69+
function (object $o): object {
70+
return clone $o;
71+
}
72+
);
73+
}
74+
5775
/**
5876
* @return FileInfo[]
5977
*/
@@ -2184,9 +2202,7 @@ public function toArgInfoCode(?int $minPHPCompatibility): string {
21842202

21852203
public function __clone()
21862204
{
2187-
foreach ($this->args as $key => $argInfo) {
2188-
$this->args[$key] = clone $argInfo;
2189-
}
2205+
$this->args = array_map((\clone)(...), $this->args);
21902206
$this->return = clone $this->return;
21912207
}
21922208
}
@@ -4154,17 +4170,9 @@ private function createIncludeElement(DOMDocument $doc, string $query, int $inde
41544170

41554171
public function __clone()
41564172
{
4157-
foreach ($this->constInfos as $key => $constInfo) {
4158-
$this->constInfos[$key] = clone $constInfo;
4159-
}
4160-
4161-
foreach ($this->propertyInfos as $key => $propertyInfo) {
4162-
$this->propertyInfos[$key] = clone $propertyInfo;
4163-
}
4164-
4165-
foreach ($this->funcInfos as $key => $funcInfo) {
4166-
$this->funcInfos[$key] = clone $funcInfo;
4167-
}
4173+
$this->constInfos = array_map((\clone)(...), $this->constInfos);
4174+
$this->propertyInfos = array_map((\clone)(...), $this->propertyInfos);
4175+
$this->funcInfos = array_map((\clone)(...), $this->funcInfos);
41684176
}
41694177

41704178
/**
@@ -4275,17 +4283,9 @@ public function getAllConstInfos(): array {
42754283

42764284
public function __clone()
42774285
{
4278-
foreach ($this->constInfos as $key => $constInfo) {
4279-
$this->constInfos[$key] = clone $constInfo;
4280-
}
4281-
4282-
foreach ($this->funcInfos as $key => $funcInfo) {
4283-
$this->funcInfos[$key] = clone $funcInfo;
4284-
}
4285-
4286-
foreach ($this->classInfos as $key => $classInfo) {
4287-
$this->classInfos[$key] = clone $classInfo;
4288-
}
4286+
$this->constInfos = array_map((\clone)(...), $this->constInfos);
4287+
$this->funcInfos = array_map((\clone)(...), $this->funcInfos);
4288+
$this->classInfos = array_map((\clone)(...), $this->classInfos);
42894289
}
42904290

42914291
private function getMinimumPhpVersionIdCompatibility(): ?int {

0 commit comments

Comments
 (0)