Skip to content

Commit d21f496

Browse files
committed
refactor! Rename operation classes to xxxHandler
Adds a `Handler` suffix to all current final operation handler class names, and moves them to a `handlers` namespace. This is not expected to affect end-users as the in-built handler classes are not generally expected to be used outside the package. This will avoid naming conflicts and confusion when we subsequently introduce support for representing a patch as a set of operation DTOs.
1 parent f431de7 commit d21f496

23 files changed

Lines changed: 162 additions & 148 deletions

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Test
1+
name: TestHandler
22

33
on: push
44

src/FastJsonPatch.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424
use blancks\JsonPatch\operations\{
2525
PatchOperationInterface,
2626
PatchValidationTrait,
27-
Add,
28-
Copy,
29-
Move,
30-
Remove,
31-
Replace,
32-
Test
27+
handlers\AddHandler,
28+
handlers\CopyHandler,
29+
handlers\MoveHandler,
30+
handlers\RemoveHandler,
31+
handlers\ReplaceHandler,
32+
handlers\TestHandler
3333
};
3434

3535
/**
@@ -91,12 +91,12 @@ public function __construct(
9191

9292
$this->setJsonPointerHandler($JsonPointerHandler);
9393
$this->setJsonHandler($JsonHandler);
94-
$this->registerOperation(new Add);
95-
$this->registerOperation(new Copy);
96-
$this->registerOperation(new Move);
97-
$this->registerOperation(new Remove);
98-
$this->registerOperation(new Replace);
99-
$this->registerOperation(new Test);
94+
$this->registerOperation(new AddHandler);
95+
$this->registerOperation(new CopyHandler);
96+
$this->registerOperation(new MoveHandler);
97+
$this->registerOperation(new RemoveHandler);
98+
$this->registerOperation(new ReplaceHandler);
99+
$this->registerOperation(new TestHandler);
100100
}
101101

102102
/**

src/operations/PatchOperation.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@ abstract class PatchOperation implements
1818

1919
/**
2020
* Returns the operation name that the class will handle.
21-
* Please note that this method will assume the class short name as the name of the operation,
21+
* Please note the default implementation takes this from the lowercased class short name, removing any "Handler"
22+
* suffix.
2223
* feel free to override if this is not the behaviour you want for your operation handler class.
2324
* @return string
2425
*/
2526
public function getOperation(): string
2627
{
27-
return strtolower((new \ReflectionClass($this))->getShortName());
28+
$name = strtolower((new \ReflectionClass($this))->getShortName());
29+
if (str_ends_with($name, 'handler')) {
30+
$name = substr($name, 0, -7);
31+
}
32+
return $name;
2833
}
2934
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?php declare(strict_types=1);
22

3-
namespace blancks\JsonPatch\operations;
3+
namespace blancks\JsonPatch\operations\handlers;
44

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

7-
final class Add extends PatchOperation
8+
final class AddHandler extends PatchOperation
89
{
910
private mixed $previous;
1011

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?php declare(strict_types=1);
22

3-
namespace blancks\JsonPatch\operations;
3+
namespace blancks\JsonPatch\operations\handlers;
44

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

7-
final class Copy extends PatchOperation
8+
final class CopyHandler extends PatchOperation
89
{
910
private mixed $previous;
1011

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php declare(strict_types=1);
22

3-
namespace blancks\JsonPatch\operations;
3+
namespace blancks\JsonPatch\operations\handlers;
44

5-
final class Move extends PatchOperation
5+
use blancks\JsonPatch\operations\PatchOperation;
6+
7+
final class MoveHandler extends PatchOperation
68
{
79
/**
810
* @param object{
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php declare(strict_types=1);
22

3-
namespace blancks\JsonPatch\operations;
3+
namespace blancks\JsonPatch\operations\handlers;
44

5-
final class Remove extends PatchOperation
5+
use blancks\JsonPatch\operations\PatchOperation;
6+
7+
final class RemoveHandler extends PatchOperation
68
{
79
private mixed $previous;
810

src/operations/Replace.php renamed to src/operations/handlers/ReplaceHandler.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php declare(strict_types=1);
22

3-
namespace blancks\JsonPatch\operations;
3+
namespace blancks\JsonPatch\operations\handlers;
44

5-
final class Replace extends PatchOperation
5+
use blancks\JsonPatch\operations\PatchOperation;
6+
7+
final class ReplaceHandler extends PatchOperation
68
{
79
private mixed $previous;
810

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?php declare(strict_types=1);
22

3-
namespace blancks\JsonPatch\operations;
3+
namespace blancks\JsonPatch\operations\handlers;
44

55
use blancks\JsonPatch\exceptions\FailedTestException;
6+
use blancks\JsonPatch\operations\PatchOperation;
67

7-
final class Test extends PatchOperation
8+
final class TestHandler extends PatchOperation
89
{
910
/**
1011
* @param object{
@@ -37,7 +38,7 @@ public function apply(mixed &$document, object $patch): void
3738
if (!$this->isJsonEquals($item, $patch->value)) {
3839
throw new FailedTestException(
3940
sprintf(
40-
'Test operation failed asserting that "%s" equals "%s"',
41+
'TestHandler operation failed asserting that "%s" equals "%s"',
4142
$this->JsonHandler->encode($item),
4243
$this->JsonHandler->encode($patch->value)
4344
),

tests/FastJsonPatchTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
};
2323
use blancks\JsonPatch\operations\{
2424
PatchOperation,
25-
Add,
26-
Copy,
27-
Move,
28-
Remove,
29-
Replace,
30-
Test
25+
handlers\AddHandler,
26+
handlers\CopyHandler,
27+
handlers\MoveHandler,
28+
handlers\RemoveHandler,
29+
handlers\ReplaceHandler,
30+
handlers\TestHandler
3131
};
3232
use blancks\JsonPatch\FastJsonPatch;
3333
use PHPUnit\Framework\Attributes\{
@@ -52,12 +52,12 @@
5252
#[UsesClass(BasicJsonHandler::class)]
5353
#[UsesClass(JsonPointer6901::class)]
5454
#[UsesClass(PatchOperation::class)]
55-
#[UsesClass(Add::class)]
56-
#[UsesClass(Copy::class)]
57-
#[UsesClass(Move::class)]
58-
#[UsesClass(Remove::class)]
59-
#[UsesClass(Replace::class)]
60-
#[UsesClass(Test::class)]
55+
#[UsesClass(AddHandler::class)]
56+
#[UsesClass(CopyHandler::class)]
57+
#[UsesClass(MoveHandler::class)]
58+
#[UsesClass(RemoveHandler::class)]
59+
#[UsesClass(ReplaceHandler::class)]
60+
#[UsesClass(TestHandler::class)]
6161
final class FastJsonPatchTest extends JsonPatchCompliance
6262
{
6363
public function testValidPatch(): void

0 commit comments

Comments
 (0)