Skip to content

Commit f1e5bdc

Browse files
committed
Raise an error when non-array is passed to withProperties
1 parent 16aab10 commit f1e5bdc

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/main/php/lang/ast/emit/RewriteCloneWith.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ protected function emitClone($result, $clone) {
1212
// Wrap clone with, e.g. clone($x, ['id' => 6100]), inside an IIFE which
1313
/// iterates over the property-value pairs, assigning them to the clone.
1414
if ($with) {
15-
$result->out->write('(function($c, $a) { foreach ($a as $p=>$v) { $c->$p= $v; } return $c;})(clone ');
15+
$result->out->write('(function($c, array $a) { foreach ($a as $p=>$v) { $c->$p= $v; } return $c;})(clone ');
1616
$this->emitOne($result, $expr);
1717
$result->out->write(',');
1818
$this->emitOne($result, $with);

src/test/php/lang/ast/unittest/emit/CloningTest.class.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php namespace lang\ast\unittest\emit;
22

33
use lang\Error;
4-
use test\{Assert, Before, Ignore, Test, Values};
4+
use test\{Assert, Before, Expect, Ignore, Test, Values};
55

66
/** @see https://www.php.net/manual/en/language.oop5.cloning.php */
77
class CloningTest extends EmittingTest {
@@ -124,4 +124,22 @@ public function run() {
124124
}
125125
}'));
126126
}
127+
128+
#[Test, Expect(Error::class)]
129+
public function clone_null_object() {
130+
$this->run('class %T {
131+
public function run() {
132+
return clone(null);
133+
}
134+
}');
135+
}
136+
137+
#[Test, Expect(Error::class)]
138+
public function clone_with_null_properties() {
139+
$this->run('class %T {
140+
public function run() {
141+
return clone($this, null);
142+
}
143+
}');
144+
}
127145
}

0 commit comments

Comments
 (0)