Skip to content

Commit baa943e

Browse files
committed
Polish new import Exceptions
* rename Invalid... -> Unknown... * make PHP 5.6 compatible * create message inside Exceptions
1 parent cf5bac4 commit baa943e

5 files changed

+67
-49
lines changed

src/InvalidOperationException.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/JsonPatch.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ public static function import(array $data)
6565
}
6666

6767
if (!isset($operation->op)) {
68-
throw new MissingFieldException('op', $operation, 'Missing "op" in operation data');
68+
throw new MissingFieldException('op', $operation);
6969
}
7070
if (!isset($operation->path)) {
71-
throw new MissingFieldException('path', $operation, 'Missing "path" in operation data');
71+
throw new MissingFieldException('path', $operation);
7272
}
7373

7474
$op = null;
@@ -92,18 +92,18 @@ public static function import(array $data)
9292
$op = new Test();
9393
break;
9494
default:
95-
throw new InvalidOperationException($operation, 'Invalid "op": ' . $operation->op);
95+
throw new UnknownOperationException($operation);
9696
}
9797
$op->path = $operation->path;
9898
if ($op instanceof OpPathValue) {
9999
if (property_exists($operation, 'value')) {
100100
$op->value = $operation->value;
101101
} else {
102-
throw new MissingFieldException('value', $operation, 'Missing "value" in operation data');
102+
throw new MissingFieldException('value', $operation);
103103
}
104104
} elseif ($op instanceof OpPathFrom) {
105105
if (!isset($operation->from)) {
106-
throw new MissingFieldException('from', $operation, 'Missing "from" in operation data');
106+
throw new MissingFieldException('from', $operation);
107107
}
108108
$op->from = $operation->from;
109109
}

src/MissingFieldException.php

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,42 @@
66

77
class MissingFieldException extends Exception
88
{
9-
private string $missingField;
10-
private object $operationObject;
9+
/** @var string */
10+
private $missingField;
11+
/** @var object */
12+
private $operation;
1113

14+
/**
15+
* @param string $missingField
16+
* @param object $operation
17+
* @param int $code
18+
* @param Throwable|null $previous
19+
*/
1220
public function __construct(
13-
string $missingField,
14-
object $operationObject,
15-
string $message = '',
16-
int $code = 0,
21+
$missingField,
22+
$operation,
23+
$code = 0,
1724
Throwable $previous = null
1825
)
1926
{
20-
parent::__construct($message, $code, $previous);
27+
parent::__construct('Missing "' . $missingField . '" in operation data', $code, $previous);
2128
$this->missingField = $missingField;
22-
$this->operationObject = $operationObject;
29+
$this->operation = $operation;
2330
}
2431

25-
public function getMissingField(): string
32+
/**
33+
* @return string
34+
*/
35+
public function getMissingField()
2636
{
2737
return $this->missingField;
2838
}
2939

30-
public function getOperationObject(): object
40+
/**
41+
* @return object
42+
*/
43+
public function getOperation()
3144
{
32-
return $this->operationObject;
45+
return $this->operation;
3346
}
3447
}

src/UnknownOperationException.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Swaggest\JsonDiff;
4+
5+
6+
use Throwable;
7+
8+
class UnknownOperationException extends Exception
9+
{
10+
/** @var object */
11+
private $operation;
12+
13+
/**
14+
* @param object $operation
15+
* @param int $code
16+
* @param Throwable|null $previous
17+
*/
18+
public function __construct(
19+
$operation,
20+
$code = 0,
21+
Throwable $previous = null
22+
)
23+
{
24+
// @phpstan-ignore-next-line MissingFieldOperation will be thrown if op is not set
25+
parent::__construct('Unknown "op": ' . $operation->op, $code, $previous);
26+
$this->operation = $operation;
27+
}
28+
29+
/**
30+
* @return object
31+
*/
32+
public function getOperation()
33+
{
34+
return $this->operation;
35+
}
36+
}

tests/src/JsonPatchTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Swaggest\JsonDiff\JsonPatch;
88
use Swaggest\JsonDiff\MissingFieldException;
99
use Swaggest\JsonDiff\PatchTestOperationFailedException;
10+
use Swaggest\JsonDiff\UnknownOperationException;
1011

1112
class JsonPatchTest extends \PHPUnit_Framework_TestCase
1213
{
@@ -88,7 +89,7 @@ public function testMissingPath()
8889

8990
public function testInvalidOp()
9091
{
91-
$this->setExpectedException(get_class(new Exception()), 'Invalid "op": wat');
92+
$this->setExpectedException(UnknownOperationException::class, 'Unknown "op": wat');
9293
JsonPatch::import(array((object)array('op' => 'wat', 'path' => '/123')));
9394
}
9495

0 commit comments

Comments
 (0)