Skip to content

Commit 028168c

Browse files
committed
feat: Make all final patch operation classes readonly
Will break on 8.1, to be rebased once the project drops 8.1 support. I have intentionally left the base `PatchOperation` class as *not* readonly in case end-users want to extend it with mutable operation DTOs for any reason. Instead, I have just left `op` as a readonly property in the base class.
1 parent c93dff3 commit 028168c

9 files changed

Lines changed: 42 additions & 22 deletions

File tree

src/operations/Add.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
* value: mixed,
1010
* }
1111
*/
12-
final class Add extends PatchOperation
12+
final readonly class Add extends PatchOperation
1313
{
1414
public function __construct(
15-
public readonly string $path,
16-
public readonly mixed $value,
15+
public string $path,
16+
public mixed $value,
1717
) {
1818
parent::__construct('add');
1919
}

src/operations/Copy.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
* from: string,
1010
* }
1111
*/
12-
final class Copy extends PatchOperation
12+
final readonly class Copy extends PatchOperation
1313
{
1414
public function __construct(
15-
public readonly string $path,
16-
public readonly string $from,
15+
public string $path,
16+
public string $from,
1717
) {
1818
parent::__construct('copy');
1919
}

src/operations/Move.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
* from: string,
1010
* }
1111
*/
12-
final class Move extends PatchOperation
12+
final readonly class Move extends PatchOperation
1313
{
1414
public function __construct(
15-
public readonly string $path,
16-
public readonly string $from,
15+
public string $path,
16+
public string $from,
1717
) {
1818
parent::__construct('move');
1919
}

src/operations/PatchOperation.php

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

33
namespace blancks\JsonPatch\operations;
44

5-
abstract class PatchOperation
5+
abstract readonly class PatchOperation
66
{
77
public function __construct(
8-
public readonly string $op,
8+
public string $op,
99
) {}
1010
}

src/operations/PatchOperationList.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
use blancks\JsonPatch\json\handlers\JsonHandlerInterface;
99
use stdClass;
1010

11-
final class PatchOperationList implements \JsonSerializable
11+
final readonly class PatchOperationList implements \JsonSerializable
1212
{
1313
/**
1414
* @phpstan-var list<PatchOperation>
1515
*/
16-
public readonly array $operations;
16+
public array $operations;
1717

1818
/**
1919
* @param string $jsonOperations

src/operations/Remove.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
* path: string,
99
* }
1010
*/
11-
final class Remove extends PatchOperation
11+
final readonly class Remove extends PatchOperation
1212
{
1313
public function __construct(
14-
public readonly string $path,
14+
public string $path,
1515
) {
1616
parent::__construct('remove');
1717
}

src/operations/Replace.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
* value: mixed,
1010
* }
1111
*/
12-
final class Replace extends PatchOperation
12+
final readonly class Replace extends PatchOperation
1313
{
1414
public function __construct(
15-
public readonly string $path,
16-
public readonly mixed $value,
15+
public string $path,
16+
public mixed $value,
1717
) {
1818
parent::__construct('replace');
1919
}

src/operations/Test.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
* value: mixed,
1010
* }
1111
*/
12-
final class Test extends PatchOperation
12+
final readonly class Test extends PatchOperation
1313
{
1414
public function __construct(
15-
public readonly string $path,
16-
public readonly mixed $value,
15+
public string $path,
16+
public mixed $value,
1717
) {
1818
parent::__construct('test');
1919
}

tests/operations/PatchOperationListTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
use blancks\JsonPatchTest\JsonPatchCompliance;
2424
use PHPUnit\Framework\Attributes\CoversClass;
2525
use PHPUnit\Framework\Attributes\DataProvider;
26-
use PHPUnit\Framework\Assert;
2726
use PHPUnit\Framework\Attributes\UsesClass;
27+
use PHPUnit\Framework\Assert;
2828
use stdClass;
2929
use Throwable;
3030

@@ -256,3 +256,23 @@ public function testItThrowsOnAttemptToCreateFromInvalidJson(string $json, strin
256256
PatchOperationList::fromJson($json);
257257
}
258258
}
259+
260+
readonly class Append extends PatchOperation
261+
{
262+
public function __construct(
263+
public string $path,
264+
public string $suffix,
265+
) {
266+
parent::__construct('append');
267+
}
268+
}
269+
270+
readonly class CustomAdd extends PatchOperation
271+
{
272+
public function __construct(
273+
public string $path,
274+
public mixed $value,
275+
) {
276+
parent::__construct('add');
277+
}
278+
}

0 commit comments

Comments
 (0)