Skip to content

Commit 88bd130

Browse files
committed
Refactor clone method in NativeCloneWithRunner to cloneWith; update tests for consistency and type validation
1 parent df3fd82 commit 88bd130

3 files changed

Lines changed: 24 additions & 27 deletions

File tree

‎tests/Fixtures/Php85/CloneWithFunctionalFixtures.php‎

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

77
use TypePHP\Tests\Fixtures\Domain\Animal;
88

9-
/**
10-
* Executes native PHP 8.5 clone-with operations so test files don't fail parsing on PHP <= 8.4.
11-
*/
129
class NativeCloneWithRunner
1310
{
14-
public static function clone(object $object, ?array $properties = null): object
11+
/**
12+
* @param array<string, mixed> $properties
13+
*/
14+
public static function cloneWith(object $object, array $properties): object
1515
{
16-
if ($properties !== null) {
17-
return clone($object, $properties);
18-
}
19-
20-
return clone $object;
16+
return clone($object, $properties);
2117
}
2218
}
2319

@@ -172,4 +168,4 @@ class ShapeCloneWithFixture
172168
* @var array{id: positive-int, role: 'admin'|'user'}
173169
*/
174170
public array $session = ['id' => 1, 'role' => 'user'];
175-
}
171+
}

‎tests/TypeChecking/Boundaries/FunctionalCloneWithTest.php‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
describe('Functional Type-Checking for PHP 8.5 "Clone With"', function () {
2727
describe('1. Generic Bound Preservation & Validation (@template T of Animal)', function () {
2828
test('preserves generic template T in WeakMap after wither method clone', function () {
29-
$dog = new Dog();
29+
$dog = new Dog('Rover');
3030
$box = new BoundedGenericBoxFixture($dog);
3131

3232
expect(TypePHP::getGenericType($box))->toBe(Dog::class);
3333

34-
$newDog = new Dog();
34+
$newDog = new Dog('Max');
3535
$clonedBox = $box->withItem($newDog);
3636

3737
expect($clonedBox)->not()->toBe($box)
@@ -41,20 +41,20 @@
4141
});
4242

4343
test('throws TypeError when wither method attempts to clone with incompatible type', function () {
44-
$box = new BoundedGenericBoxFixture(new Dog());
44+
$box = new BoundedGenericBoxFixture(new Dog('Rover'));
4545

4646
expect(fn () => $box->withItem(new Car()))
4747
->toThrow(TypeError::class, 'must be of type ' . Dog::class)
4848
;
4949
});
5050

5151
test('validates external clone with on public generic property', function () {
52-
$box = new BoundedGenericBoxFixture(new Dog());
52+
$box = new BoundedGenericBoxFixture(new Dog('Rover'));
5353

54-
$validClone = clone($box, ['item' => new Dog()]);
54+
$validClone = NativeCloneWithRunner::cloneWith($box, ['item' => new Dog('Buddy')]);
5555
expect(TypePHP::getGenericType($validClone))->toBe(Dog::class);
5656

57-
expect(fn () => clone($box, ['item' => new Car()]))
57+
expect(fn () => NativeCloneWithRunner::cloneWith($box, ['item' => new Car()]))
5858
->toThrow(TypeError::class, 'must be of type ' . Dog::class)
5959
;
6060
});
@@ -79,7 +79,7 @@
7979
test('throws TypeError when clone override violates generic value type V', function () {
8080
$map = new MultiTemplateMapFixture(['user_1' => 100]);
8181

82-
expect(fn () => NativeCloneWithRunner::clone($map, ['data' => ['user_1' => 'not_an_int']]))
82+
expect(fn () => NativeCloneWithRunner::cloneWith($map, ['data' => ['user_1' => 'not_an_int']]))
8383
->toThrow(TypeError::class, "['user_1'] must be of type int")
8484
;
8585
});
@@ -123,7 +123,7 @@
123123
$model = new MagicCloneWithFixture();
124124
expect($model->counter)->toBe(10);
125125

126-
$cloned = NativeCloneWithRunner::clone($model, ['counter' => 50]);
126+
$cloned = NativeCloneWithRunner::cloneWith($model, ['counter' => 50]);
127127

128128
expect($cloned->magicCloneExecuted)->toBeTrue()
129129
->and($cloned->counter)->toBe(50)
@@ -133,7 +133,7 @@
133133
test('validates final overridden state even when __clone() mutated the object first', function () {
134134
$model = new MagicCloneWithFixture();
135135

136-
expect(fn () => NativeCloneWithRunner::clone($model, ['counter' => -100]))
136+
expect(fn () => NativeCloneWithRunner::cloneWith($model, ['counter' => -100]))
137137
->toThrow(TypeError::class, 'positive-int')
138138
;
139139
});
@@ -143,10 +143,10 @@
143143
test('triggers set hook during property write and enforces contract', function () {
144144
$order = new HookedCloneWithFixture();
145145

146-
$cloned = NativeCloneWithRunner::clone($order, ['total' => 250]);
146+
$cloned = NativeCloneWithRunner::cloneWith($order, ['total' => 250]);
147147
expect($cloned->total)->toBe(250);
148148

149-
expect(fn () => NativeCloneWithRunner::clone($order, ['total' => -50]))
149+
expect(fn () => NativeCloneWithRunner::cloneWith($order, ['total' => -50]))
150150
->toThrow(TypeError::class, 'positive-int')
151151
;
152152
});
@@ -156,7 +156,7 @@
156156
test('validates array shape properties on cloned instance', function () {
157157
$fixture = new ShapeCloneWithFixture();
158158

159-
$cloned = NativeCloneWithRunner::clone($fixture, [
159+
$cloned = NativeCloneWithRunner::cloneWith($fixture, [
160160
'session' => ['id' => 42, 'role' => 'admin'],
161161
]);
162162

@@ -166,15 +166,15 @@
166166
test('throws TypeError when array shape id violates positive-int', function () {
167167
$fixture = new ShapeCloneWithFixture();
168168

169-
expect(fn () => NativeCloneWithRunner::clone($fixture, ['session' => ['id' => -1, 'role' => 'user']]))
169+
expect(fn () => NativeCloneWithRunner::cloneWith($fixture, ['session' => ['id' => -1, 'role' => 'user']]))
170170
->toThrow(TypeError::class, "['id'] must be of type positive-int")
171171
;
172172
});
173173

174174
test('throws TypeError when array shape role violates literal union', function () {
175175
$fixture = new ShapeCloneWithFixture();
176176

177-
expect(fn () => NativeCloneWithRunner::clone($fixture, ['session' => ['id' => 10, 'role' => 'superuser']]))
177+
expect(fn () => NativeCloneWithRunner::cloneWith($fixture, ['session' => ['id' => 10, 'role' => 'superuser']]))
178178
->toThrow(TypeError::class, "['role'] must be of type ('admin' | 'user')")
179179
;
180180
});
@@ -206,7 +206,7 @@
206206
Config::set(['on_violation' => 'report']);
207207

208208
$user = new ShapeCloneWithFixture();
209-
$cloned = NativeCloneWithRunner::clone($user, ['session' => ['id' => -999, 'role' => 'user']]);
209+
$cloned = NativeCloneWithRunner::cloneWith($user, ['session' => ['id' => -999, 'role' => 'user']]);
210210

211211
expect($cloned)->toBeInstanceOf(ShapeCloneWithFixture::class);
212212

@@ -217,4 +217,4 @@
217217
;
218218
});
219219
});
220-
});
220+
});

‎tests/TypeChecking/Boundaries/Php85CloneWithTest.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
$cloned->set(new Dog());
6363
expect($cloned->item)->toBeInstanceOf(Dog::class);
6464

65+
6566
expect(fn () => $cloned->set(new Car()))
6667
->toThrow(TypeError::class, Dog::class)
6768
;
@@ -161,4 +162,4 @@
161162
}
162163
});
163164
});
164-
});
165+
});

0 commit comments

Comments
 (0)