Skip to content

Commit ee7b5e2

Browse files
committed
Refactor NestedAliasService and NestedTypeAliasesTest to remove unused chainedProperty and clean up type error assertions
1 parent c65fa29 commit ee7b5e2

2 files changed

Lines changed: 13 additions & 43 deletions

File tree

tests/Fixtures/Types/NestedAliasService.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,13 @@
1313
* @phpstan-type LocalRecordShape array{id: LocalId, status: LocalStatus}
1414
* @phpstan-type LocalRecordList list<LocalRecordShape>
1515
* @phpstan-type ImportedRecordList list<ImportedRecordShape>
16+
*
1617
* @phpstan-type AdminStatus 'admin_active'
1718
* @phpstan-type UserStatus 'user_active'
1819
* @phpstan-type UnionOfAliases AdminStatus|UserStatus
1920
*/
2021
class NestedAliasService
2122
{
22-
/**
23-
* Property hook validated against 3-class chained imported shape
24-
*
25-
* @var ChainedShape
26-
*/
27-
public array $chainedProperty {
28-
get => $this->_chainedProperty;
29-
set => $this->_chainedProperty = $value;
30-
}
31-
32-
private array $_chainedProperty = ['code' => 1, 'label' => 'init'];
33-
3423
/**
3524
* @param LocalRecordList $records
3625
*/
@@ -62,4 +51,4 @@ public function setUnionStatus(string $status): bool
6251
{
6352
return true;
6453
}
65-
}
54+
}

tests/TypeChecking/ArraysAndShapes/NestedTypeAliasesTest.php

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,22 @@
2222

2323
$invalidRecords = [
2424
['id' => 10, 'status' => 'active'],
25-
['id' => -5, 'status' => 'pending'], // -5 violates LocalId (positive-int)
25+
['id' => -5, 'status' => 'pending'],
2626
];
2727

2828
expect(fn () => $service->saveLocalRecords($invalidRecords))
29-
->toThrow(TypeError::class, "['id'] must be of type positive-int")
30-
;
29+
->toThrow(TypeError::class, "['id'] must be of type positive-int");
3130
});
3231

3332
test('throws TypeError when nested shape item violates local union alias', function () {
3433
$service = new NestedAliasService();
3534

3635
$invalidRecords = [
37-
['id' => 10, 'status' => 'archived'], // 'archived' violates LocalStatus ('active'|'pending')
36+
['id' => 10, 'status' => 'archived'],
3837
];
3938

4039
expect(fn () => $service->saveLocalRecords($invalidRecords))
41-
->toThrow(TypeError::class, "['status'] must be of type ('active' | 'pending')")
42-
;
40+
->toThrow(TypeError::class, "['status'] must be of type ('active' | 'pending')");
4341
});
4442
});
4543

@@ -59,12 +57,11 @@
5957
$service = new NestedAliasService();
6058

6159
$invalidRecords = [
62-
['id' => -100, 'status' => 'active'],
60+
['id' => -100, 'status' => 'active'],
6361
];
6462

6563
expect(fn () => $service->saveImportedRecords($invalidRecords))
66-
->toThrow(TypeError::class, "['id'] must be of type positive-int")
67-
;
64+
->toThrow(TypeError::class, "['id'] must be of type positive-int");
6865
});
6966

7067
test('throws TypeError when nested shape item violates imported union alias', function () {
@@ -75,8 +72,7 @@
7572
];
7673

7774
expect(fn () => $service->saveImportedRecords($invalidRecords))
78-
->toThrow(TypeError::class, "['status'] must be of type ('active' | 'pending')")
79-
;
75+
->toThrow(TypeError::class, "['status'] must be of type ('active' | 'pending')");
8076
});
8177
});
8278

@@ -87,12 +83,10 @@
8783
expect($service->saveChainedData(['code' => 50, 'label' => 'valid']))->toBeTrue();
8884

8985
expect(fn () => $service->saveChainedData(['code' => -10, 'label' => 'valid']))
90-
->toThrow(TypeError::class, "['code'] must be of type positive-int")
91-
;
86+
->toThrow(TypeError::class, "['code'] must be of type positive-int");
9287

9388
expect(fn () => $service->saveChainedData(['code' => 50, 'label' => '']))
94-
->toThrow(TypeError::class, "['label'] must be of type non-empty-string")
95-
;
89+
->toThrow(TypeError::class, "['label'] must be of type non-empty-string");
9690
});
9791
});
9892

@@ -104,20 +98,7 @@
10498
expect($service->setUnionStatus('user_active'))->toBeTrue();
10599

106100
expect(fn () => $service->setUnionStatus('guest_active'))
107-
->toThrow(TypeError::class, "('admin_active' | 'user_active')")
108-
;
101+
->toThrow(TypeError::class, "('admin_active' | 'user_active')");
109102
});
110103
});
111-
112-
describe('Edge Case 3: Property Hooks with Imported Type Aliases', function () {
113-
test('validates PHP 8.4 property hook write against imported 3-tier shape alias', function () {
114-
$service = new NestedAliasService();
115-
116-
$service->chainedProperty = ['code' => 100, 'label' => 'updated'];
117-
expect($service->chainedProperty['code'])->toBe(100);
118-
119-
expect(fn () => $service->chainedProperty = ['code' => -1, 'label' => 'updated'])
120-
->toThrow(TypeError::class, "Property TypePHP\Tests\Fixtures\Types\NestedAliasService::\$chainedProperty['code'] must be of type positive-int");
121-
});
122-
});
123-
});
104+
});

0 commit comments

Comments
 (0)