Skip to content

Commit 075e62b

Browse files
committed
refactor: Use phpstan type aliases to reference operations from handlers
Now that there are explicit operation classes for each standard operation, it feels appropriate to define the expected schema for unserialized operations alongside the DTO and import it to the handler. This more clearly shows the relationship between these objects.
1 parent 9455f99 commit 075e62b

File tree

12 files changed

+74
-87
lines changed

12 files changed

+74
-87
lines changed

src/operations/Add.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
namespace blancks\JsonPatch\operations;
44

5+
/**
6+
* @phpstan-type TAddOperationObject object{
7+
* op:string,
8+
* path: string,
9+
* value: mixed,
10+
* }
11+
*/
512
final class Add extends PatchOperation
613
{
714
public function __construct(

src/operations/Copy.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
namespace blancks\JsonPatch\operations;
44

5+
/**
6+
* @phpstan-type TCopyOperationObject object{
7+
* op:string,
8+
* path: string,
9+
* from: string,
10+
* }
11+
*/
512
final class Copy extends PatchOperation
613
{
714
public function __construct(

src/operations/Move.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
namespace blancks\JsonPatch\operations;
44

5+
/**
6+
* @phpstan-type TMoveOperationObject object{
7+
* op:string,
8+
* path: string,
9+
* from: string,
10+
* }
11+
*/
512
final class Move extends PatchOperation
613
{
714
public function __construct(

src/operations/Remove.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
namespace blancks\JsonPatch\operations;
44

5+
/**
6+
* @phpstan-type TRemoveOperationObject object{
7+
* op:string,
8+
* path: string,
9+
* }
10+
*/
511
final class Remove extends PatchOperation
612
{
713
public function __construct(

src/operations/Replace.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
namespace blancks\JsonPatch\operations;
44

5+
/**
6+
* @phpstan-type TReplaceOperationObject object{
7+
* op:string,
8+
* path: string,
9+
* value: mixed,
10+
* }
11+
*/
512
final class Replace extends PatchOperation
613
{
714
public function __construct(

src/operations/Test.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
namespace blancks\JsonPatch\operations;
44

5+
/**
6+
* @phpstan-type TTestOperationObject object{
7+
* op:string,
8+
* path: string,
9+
* value: mixed,
10+
* }
11+
*/
512
final class Test extends PatchOperation
613
{
714
public function __construct(

src/operations/handlers/AddHandler.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@
33
namespace blancks\JsonPatch\operations\handlers;
44

55
use blancks\JsonPatch\json\accessors\UndefinedValue;
6+
use blancks\JsonPatch\operations\Add;
67

78
/**
89
* @internal
10+
* @phpstan-import-type TAddOperationObject from Add
911
*/
1012
final class AddHandler extends PatchOperationHandler
1113
{
1214
private mixed $previous;
1315

1416
/**
15-
* @param object{
16-
* op:string,
17-
* path: string,
18-
* value: mixed,
19-
* } $patch
17+
* @param object&TAddOperationObject $patch
2018
* @return void
2119
*/
2220
public function validate(object $patch): void
@@ -28,11 +26,7 @@ public function validate(object $patch): void
2826

2927
/**
3028
* @param mixed $document
31-
* @param object{
32-
* op:string,
33-
* path: string,
34-
* value: mixed,
35-
* } $patch
29+
* @param object&TAddOperationObject $patch
3630
* @return void
3731
*/
3832
public function apply(mixed &$document, object $patch): void
@@ -41,11 +35,7 @@ public function apply(mixed &$document, object $patch): void
4135
}
4236

4337
/**
44-
* @param object{
45-
* op:string,
46-
* path: string,
47-
* value: mixed,
48-
* } $patch
38+
* @param object&TAddOperationObject $patch
4939
* @return null|array{
5040
* op:string,
5141
* path: string,

src/operations/handlers/CopyHandler.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@
33
namespace blancks\JsonPatch\operations\handlers;
44

55
use blancks\JsonPatch\json\accessors\UndefinedValue;
6+
use blancks\JsonPatch\operations\Copy;
67

78
/**
89
* @internal
10+
* @phpstan-import-type TCopyOperationObject from Copy
911
*/
1012
final class CopyHandler extends PatchOperationHandler
1113
{
1214
private mixed $previous;
1315

1416
/**
15-
* @param object{
16-
* op:string,
17-
* path: string,
18-
* from: string,
19-
* } $patch
17+
* @param object&TCopyOperationObject $patch
2018
* @return void
2119
*/
2220
public function validate(object $patch): void
@@ -28,11 +26,7 @@ public function validate(object $patch): void
2826

2927
/**
3028
* @param mixed $document
31-
* @param object{
32-
* op:string,
33-
* path: string,
34-
* from: string,
35-
* } $patch
29+
* @param object&TCopyOperationObject $patch
3630
* @return void
3731
*/
3832
public function apply(mixed &$document, object $patch): void
@@ -42,11 +36,7 @@ public function apply(mixed &$document, object $patch): void
4236
}
4337

4438
/**
45-
* @param object{
46-
* op:string,
47-
* path: string,
48-
* from: string,
49-
* } $patch
39+
* @param object&TCopyOperationObject $patch
5040
* @return null|array{
5141
* op:string,
5242
* path: string,

src/operations/handlers/MoveHandler.php

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22

33
namespace blancks\JsonPatch\operations\handlers;
44

5+
use blancks\JsonPatch\operations\Move;
6+
57
/**
68
* @internal
9+
* @phpstan-import-type TMoveOperationObject from Move
710
*/
811
final class MoveHandler extends PatchOperationHandler
912
{
1013
/**
11-
* @param object{
12-
* op:string,
13-
* path: string,
14-
* from: string,
15-
* } $patch
14+
* @param object&TMoveOperationObject $patch
1615
* @return void
1716
*/
1817
public function validate(object $patch): void
@@ -24,11 +23,7 @@ public function validate(object $patch): void
2423

2524
/**
2625
* @param mixed $document
27-
* @param object{
28-
* op:string,
29-
* path: string,
30-
* from: string,
31-
* } $patch
26+
* @param object&TMoveOperationObject $patch
3227
* @return void
3328
*/
3429
public function apply(mixed &$document, object $patch): void
@@ -38,11 +33,7 @@ public function apply(mixed &$document, object $patch): void
3833
}
3934

4035
/**
41-
* @param object{
42-
* op:string,
43-
* path: string,
44-
* from: string,
45-
* } $patch
36+
* @param object&TMoveOperationObject $patch
4637
* @return null|array{
4738
* op:string,
4839
* path: string,

src/operations/handlers/RemoveHandler.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
namespace blancks\JsonPatch\operations\handlers;
44

5+
use blancks\JsonPatch\operations\Remove;
6+
57
/**
68
* @internal
9+
* @phpstan-import-type TRemoveOperationObject from Remove
710
*/
811
final class RemoveHandler extends PatchOperationHandler
912
{
1013
private mixed $previous;
1114

1215
/**
13-
* @param object{
14-
* op:string,
15-
* path: string,
16-
* } $patch
16+
* @param object&TRemoveOperationObject $patch
1717
* @return void
1818
*/
1919
public function validate(object $patch): void
@@ -24,10 +24,7 @@ public function validate(object $patch): void
2424

2525
/**
2626
* @param mixed $document
27-
* @param object{
28-
* op:string,
29-
* path: string,
30-
* } $patch
27+
* @param object&TRemoveOperationObject $patch
3128
* @return void
3229
*/
3330
public function apply(mixed &$document, object $patch): void
@@ -36,10 +33,7 @@ public function apply(mixed &$document, object $patch): void
3633
}
3734

3835
/**
39-
* @param object{
40-
* op:string,
41-
* path: string,
42-
* } $patch
36+
* @param object&TRemoveOperationObject $patch
4337
* @return null|array{
4438
* op:string,
4539
* path: string,

0 commit comments

Comments
 (0)