Skip to content

Commit 688b9df

Browse files
committed
Merge remote-tracking branch 'origin/master' into issue-6
2 parents e1d0f04 + e583f89 commit 688b9df

19 files changed

+2186
-61
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
/json-diff
44
/json-diff.tar.gz
55
/composer.lock
6-
/composer.phar
6+
/composer.phar
7+
/clover.xml

.travis.yml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@ cache:
1919

2020
# execute any number of scripts before the test run, custom env's are available as variables
2121
before_script:
22+
- ls -la $HOME/.composer/cache
23+
- test -f $HOME/.composer/cache/composer.lock.$(phpenv version-name) && cp $HOME/.composer/cache/composer.lock.$(phpenv version-name) ./composer.lock || echo "No composer.lock cached"
2224
- composer install --dev --no-interaction --prefer-dist
23-
# - cat composer.lock
25+
- test -f $HOME/.composer/cache/composer.lock.$(phpenv version-name) || cp ./composer.lock $HOME/.composer/cache/composer.lock.$(phpenv version-name)
26+
- if [[ $(phpenv version-name) =~ 7.2 ]] ; then test -f $HOME/.composer/cache/phpstan.phar || wget https://github.com/phpstan/phpstan/releases/download/0.9.1/phpstan.phar -O $HOME/.composer/cache/phpstan.phar; fi
27+
- if [[ $(phpenv version-name) =~ 7.2 ]] ; then test -f $HOME/.composer/cache/ocular.phar || wget https://scrutinizer-ci.com/ocular.phar -O $HOME/.composer/cache/ocular.phar; fi
28+
- if [[ $(phpenv version-name) =~ 7.2 ]] ; then test -f $HOME/.composer/cache/cctr || wget https://codeclimate.com/downloads/test-reporter/test-reporter-0.1.4-linux-amd64 -O $HOME/.composer/cache/cctr && chmod +x $HOME/.composer/cache/cctr; fi
29+
- if [[ $(phpenv version-name) =~ 7.2 ]] ; then $HOME/.composer/cache/cctr before-build; fi
2430

2531
matrix:
2632
allow_failures:
@@ -29,11 +35,9 @@ matrix:
2935
fast_finish: true
3036

3137
script:
32-
- mkdir -p build/logs
33-
- ./vendor/bin/phpunit -v --configuration phpunit.xml --coverage-clover build/logs/clover.xml
38+
- ./vendor/bin/phpunit -v --configuration phpunit.xml --coverage-text --coverage-clover clover.xml
39+
- if [[ $(phpenv version-name) =~ 7.2 ]] ; then php $HOME/.composer/cache/phpstan.phar analyze -l 7 ./src; fi
3440

3541
after_script:
36-
- wget https://scrutinizer-ci.com/ocular.phar
37-
- php ocular.phar code-coverage:upload --format=php-clover build/logs/coverage.clover
38-
- if [[ $(phpenv version-name) =~ 7.1 ]] ; then php vendor/bin/coveralls -v; fi
39-
- if [[ $(phpenv version-name) =~ 7.1 ]] ; then php vendor/bin/test-reporter; fi
42+
- if [[ $(phpenv version-name) =~ 7.2 ]] ; then php $HOME/.composer/cache/ocular.phar code-coverage:upload --format=php-clover clover.xml; fi
43+
- if [[ $(phpenv version-name) =~ 7.2 ]] ; then $HOME/.composer/cache/cctr after-build --exit-code $TRAVIS_TEST_RESULT; fi

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ A PHP implementation for finding unordered diff between two `JSON` documents.
55
[![Build Status](https://travis-ci.org/swaggest/json-diff.svg?branch=master)](https://travis-ci.org/swaggest/json-diff)
66
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/swaggest/json-diff/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/swaggest/json-diff/?branch=master)
77
[![Code Climate](https://codeclimate.com/github/swaggest/json-diff/badges/gpa.svg)](https://codeclimate.com/github/swaggest/json-diff)
8-
[![Test Coverage](https://codeclimate.com/github/swaggest/json-diff/badges/coverage.svg)](https://codeclimate.com/github/swaggest/json-diff/coverage)
8+
[![Code Coverage](https://scrutinizer-ci.com/g/swaggest/json-diff/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/swaggest/json-diff/code-structure/master/code-coverage)
99

1010
## Purpose
1111

@@ -54,7 +54,7 @@ $r = new JsonDiff(
5454
On created object you have several handy methods.
5555

5656
### `getPatch`
57-
Returns JsonPatch of difference
57+
Returns `JsonPatch` of difference
5858

5959
### `getRearranged`
6060
Returns new value, rearranged with original order.

src/Cli/Apply.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ public function performAction()
4949
$patch = JsonPatch::import(json_decode($patchJson));
5050
$base = json_decode($baseJson);
5151
$patch->apply($base);
52+
$this->out = $base;
5253
} catch (Exception $e) {
5354
$this->response->error($e->getMessage());
5455
}
55-
$this->out = $base;
5656

5757
$this->postPerform();
5858
}

src/Cli/Base.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Swaggest\JsonDiff\Cli;
44

55

6+
use Swaggest\JsonDiff\Exception;
67
use Swaggest\JsonDiff\JsonDiff;
78
use Yaoi\Command;
89

@@ -48,7 +49,12 @@ protected function prePerform()
4849
if ($this->rearrangeArrays) {
4950
$options += JsonDiff::REARRANGE_ARRAYS;
5051
}
51-
$this->diff = new JsonDiff(json_decode($originalJson), json_decode($newJson), $options);
52+
try {
53+
$this->diff = new JsonDiff(json_decode($originalJson), json_decode($newJson), $options);
54+
} catch (Exception $e) {
55+
$this->response->error($e->getMessage());
56+
return;
57+
}
5258

5359
$this->out = '';
5460
}

src/Cli/Diff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class Diff extends Base
99
{
10-
static function setUpDefinition(Command\Definition $definition, $options)
10+
public static function setUpDefinition(Command\Definition $definition, $options)
1111
{
1212
parent::setUpDefinition($definition, $options);
1313
$definition->description = 'Make patch from two json documents, output to STDOUT';

src/Cli/Rearrange.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class Rearrange extends Base
99
{
10-
static function setUpDefinition(Command\Definition $definition, $options)
10+
public static function setUpDefinition(Command\Definition $definition, $options)
1111
{
1212
parent::setUpDefinition($definition, $options);
1313
$definition->description = 'Rearrange json document in the order of another (original) json document';

src/Exception.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55

66
class Exception extends \Exception
77
{
8-
8+
const EMPTY_PROPERTY_NAME_UNSUPPORTED = 1;
99
}

src/JsonDiff.php

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ class JsonDiff
4545

4646
/**
4747
* Processor constructor.
48-
* @param $original
49-
* @param $new
48+
* @param mixed $original
49+
* @param mixed $new
5050
* @param int $options
51+
* @throws Exception
5152
*/
5253
public function __construct($original, $new, $options = 0)
5354
{
@@ -181,11 +182,21 @@ public function getPatch()
181182
return $this->jsonPatch;
182183
}
183184

185+
/**
186+
* @return array|null|object|\stdClass
187+
* @throws Exception
188+
*/
184189
private function rearrange()
185190
{
186191
return $this->process($this->original, $this->new);
187192
}
188193

194+
/**
195+
* @param mixed $original
196+
* @param mixed $new
197+
* @return array|null|object|\stdClass
198+
* @throws Exception
199+
*/
189200
private function process($original, $new)
190201
{
191202
if (
@@ -194,6 +205,9 @@ private function process($original, $new)
194205
) {
195206
if ($original !== $new) {
196207
$this->modifiedCnt++;
208+
if ($this->options & self::STOP_ON_DIFF) {
209+
return null;
210+
}
197211
$this->modifiedPaths [] = $this->path;
198212

199213
$this->jsonPatch->op(new Test($this->path, $original));
@@ -202,9 +216,6 @@ private function process($original, $new)
202216
JsonPointer::add($this->modifiedOriginal, $this->pathItems, $original);
203217
JsonPointer::add($this->modifiedNew, $this->pathItems, $new);
204218

205-
if ($this->options & self::STOP_ON_DIFF) {
206-
return null;
207-
}
208219
}
209220
return $new;
210221
}
@@ -224,6 +235,12 @@ private function process($original, $new)
224235
$removedOffset = 0;
225236

226237
foreach ($originalKeys as $key => $originalValue) {
238+
if ($this->options & self::STOP_ON_DIFF) {
239+
if ($this->modifiedCnt || $this->addedCnt || $this->removedCnt) {
240+
return null;
241+
}
242+
}
243+
227244
$path = $this->path;
228245
$pathItems = $this->pathItems;
229246
$actualKey = $key;
@@ -238,6 +255,9 @@ private function process($original, $new)
238255
unset($newArray[$key]);
239256
} else {
240257
$this->removedCnt++;
258+
if ($this->options & self::STOP_ON_DIFF) {
259+
return null;
260+
}
241261
$this->removedPaths [] = $this->path;
242262
if ($isArray) {
243263
$removedOffset++;
@@ -246,29 +266,26 @@ private function process($original, $new)
246266
$this->jsonPatch->op(new Remove($this->path));
247267

248268
JsonPointer::add($this->removed, $this->pathItems, $originalValue);
249-
if ($this->options & self::STOP_ON_DIFF) {
250-
return null;
251-
}
252269
}
253270
$this->path = $path;
254271
$this->pathItems = $pathItems;
255272
}
256273

257274
// additions
258275
foreach ($newArray as $key => $value) {
276+
$this->addedCnt++;
277+
if ($this->options & self::STOP_ON_DIFF) {
278+
return null;
279+
}
259280
$newOrdered[$key] = $value;
260281
$path = $this->path . '/' . JsonPointer::escapeSegment($key, $this->options & self::JSON_URI_FRAGMENT_ID);
261282
$pathItems = $this->pathItems;
262283
$pathItems[] = $key;
263284
JsonPointer::add($this->added, $pathItems, $value);
264-
$this->addedCnt++;
265285
$this->addedPaths [] = $path;
266286

267287
$this->jsonPatch->op(new Add($path, $value));
268288

269-
if ($this->options & self::STOP_ON_DIFF) {
270-
return null;
271-
}
272289
}
273290

274291
return is_array($new) ? $newOrdered : (object)$newOrdered;

src/JsonPatch.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,8 @@ class JsonPatch implements \JsonSerializable
2727
* @return JsonPatch
2828
* @throws Exception
2929
*/
30-
public static function import($data)
30+
public static function import(array $data)
3131
{
32-
if (!is_array($data)) {
33-
throw new Exception('Array expected in JsonPatch::import');
34-
}
3532
$result = new JsonPatch();
3633
foreach ($data as $operation) {
3734
/** @var OpPath|OpPathValue|OpPathFrom $operation */
@@ -108,7 +105,7 @@ public function jsonSerialize()
108105
}
109106

110107
/**
111-
* @param $original
108+
* @param mixed $original
112109
* @throws Exception
113110
*/
114111
public function apply(&$original)
@@ -117,32 +114,34 @@ public function apply(&$original)
117114
$pathItems = JsonPointer::splitPath($operation->path);
118115
switch (true) {
119116
case $operation instanceof Add:
120-
JsonPointer::add($original, $pathItems, $operation->value);
117+
JsonPointer::add($original, $pathItems, $operation->value, false);
121118
break;
122119
case $operation instanceof Copy:
123120
$fromItems = JsonPointer::splitPath($operation->from);
124121
$value = JsonPointer::get($original, $fromItems);
125-
JsonPointer::add($original, $pathItems, $value);
122+
JsonPointer::add($original, $pathItems, $value, false);
126123
break;
127124
case $operation instanceof Move:
128125
$fromItems = JsonPointer::splitPath($operation->from);
129126
$value = JsonPointer::get($original, $fromItems);
130-
JsonPointer::add($original, $pathItems, $value);
131127
JsonPointer::remove($original, $fromItems);
128+
JsonPointer::add($original, $pathItems, $value, false);
132129
break;
133130
case $operation instanceof Remove:
134131
JsonPointer::remove($original, $pathItems);
135132
break;
136133
case $operation instanceof Replace:
137134
JsonPointer::get($original, $pathItems);
138-
JsonPointer::add($original, $pathItems, $operation->value);
135+
JsonPointer::remove($original, $pathItems);
136+
JsonPointer::add($original, $pathItems, $operation->value, false);
139137
break;
140138
case $operation instanceof Test:
141139
$value = JsonPointer::get($original, $pathItems);
142140
$diff = new JsonDiff($operation->value, $value,
143141
JsonDiff::STOP_ON_DIFF);
144142
if ($diff->getDiffCnt() !== 0) {
145-
throw new Exception('Test operation ' . json_encode($operation) . ' failed: ' . json_encode($value));
143+
throw new Exception('Test operation ' . json_encode($operation, JSON_UNESCAPED_SLASHES)
144+
. ' failed: ' . json_encode($value));
146145
}
147146
break;
148147
}

0 commit comments

Comments
 (0)