Skip to content

Commit 670c0d0

Browse files
authored
Merge pull request #3 from colinodell/code-coverage
Code coverage
2 parents 2d1db61 + 1bf1ac2 commit 670c0d0

File tree

3 files changed

+80
-80
lines changed

3 files changed

+80
-80
lines changed

.travis.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,5 @@ script:
2727
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover
2828

2929
after_script:
30-
- |
31-
if [[ "$TRAVIS_PHP_VERSION" != '7.0' ]]; then
32-
wget https://scrutinizer-ci.com/ocular.phar
33-
php ocular.phar code-coverage:upload --format=php-clover coverage.clover
34-
fi
30+
- wget https://scrutinizer-ci.com/ocular.phar
31+
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover

src/Json5Decoder.php

Lines changed: 61 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -245,20 +245,14 @@ private function number()
245245

246246
// support for Infinity
247247
if ($this->ch === 'I') {
248-
$number = $this->word();
249-
if ($number === null) {
250-
$this->throwSyntaxError('Unexpected word for number');
251-
}
248+
$this->word();
252249

253250
return ($sign === '-') ? -INF : INF;
254251
}
255252

256253
// support for NaN
257254
if ($this->ch === 'N') {
258255
$number = $this->word();
259-
if ($number !== NAN) {
260-
$this->throwSyntaxError('expected word to be NaN');
261-
}
262256

263257
// ignore sign as -NaN also is NaN
264258
return $number;
@@ -483,41 +477,37 @@ private function arr()
483477
{
484478
$arr = [];
485479

486-
if ($this->ch === '[') {
487-
if (++$this->depth > $this->maxDepth) {
488-
$this->throwSyntaxError('Maximum stack depth exceeded');
489-
}
480+
if (++$this->depth > $this->maxDepth) {
481+
$this->throwSyntaxError('Maximum stack depth exceeded');
482+
}
490483

491-
$this->nextOrFail('[');
492-
$this->white();
493-
while ($this->ch !== null) {
494-
if ($this->ch === ']') {
495-
$this->nextOrFail(']');
496-
$this->depth--;
497-
return $arr; // Potentially empty array
498-
}
499-
// ES5 allows omitting elements in arrays, e.g. [,] and
500-
// [,null]. We don't allow this in JSON5.
501-
if ($this->ch === ',') {
502-
$this->throwSyntaxError('Missing array element');
503-
}
484+
$this->nextOrFail('[');
485+
$this->white();
486+
while ($this->ch !== null) {
487+
if ($this->ch === ']') {
488+
$this->nextOrFail(']');
489+
$this->depth--;
490+
return $arr; // Potentially empty array
491+
}
492+
// ES5 allows omitting elements in arrays, e.g. [,] and
493+
// [,null]. We don't allow this in JSON5.
494+
if ($this->ch === ',') {
495+
$this->throwSyntaxError('Missing array element');
496+
}
504497

505-
$arr[] = $this->value();
498+
$arr[] = $this->value();
506499

507-
$this->white();
508-
// If there's no comma after this value, this needs to
509-
// be the end of the array.
510-
if ($this->ch !== ',') {
511-
$this->nextOrFail(']');
512-
$this->depth--;
513-
return $arr;
514-
}
515-
$this->nextOrFail(',');
516-
$this->white();
500+
$this->white();
501+
// If there's no comma after this value, this needs to
502+
// be the end of the array.
503+
if ($this->ch !== ',') {
504+
$this->nextOrFail(']');
505+
$this->depth--;
506+
return $arr;
517507
}
508+
$this->nextOrFail(',');
509+
$this->white();
518510
}
519-
520-
$this->throwSyntaxError('Bad array');
521511
}
522512

523513
/**
@@ -527,49 +517,45 @@ private function obj()
527517
{
528518
$object = $this->associative ? [] : new \stdClass;
529519

530-
if ($this->ch === '{') {
531-
if (++$this->depth > $this->maxDepth) {
532-
$this->throwSyntaxError('Maximum stack depth exceeded');
533-
}
520+
if (++$this->depth > $this->maxDepth) {
521+
$this->throwSyntaxError('Maximum stack depth exceeded');
522+
}
534523

535-
$this->nextOrFail('{');
536-
$this->white();
537-
while ($this->ch) {
538-
if ($this->ch === '}') {
539-
$this->nextOrFail('}');
540-
$this->depth--;
541-
return $object; // Potentially empty object
542-
}
524+
$this->nextOrFail('{');
525+
$this->white();
526+
while ($this->ch !== null) {
527+
if ($this->ch === '}') {
528+
$this->nextOrFail('}');
529+
$this->depth--;
530+
return $object; // Potentially empty object
531+
}
543532

544-
// Keys can be unquoted. If they are, they need to be
545-
// valid JS identifiers.
546-
if ($this->ch === '"' || $this->ch === "'") {
547-
$key = $this->string();
548-
} else {
549-
$key = $this->identifier();
550-
}
533+
// Keys can be unquoted. If they are, they need to be
534+
// valid JS identifiers.
535+
if ($this->ch === '"' || $this->ch === "'") {
536+
$key = $this->string();
537+
} else {
538+
$key = $this->identifier();
539+
}
551540

552-
$this->white();
553-
$this->nextOrFail(':');
554-
if ($this->associative) {
555-
$object[$key] = $this->value();
556-
} else {
557-
$object->{$key} = $this->value();
558-
}
559-
$this->white();
560-
// If there's no comma after this pair, this needs to be
561-
// the end of the object.
562-
if ($this->ch !== ',') {
563-
$this->nextOrFail('}');
564-
$this->depth--;
565-
return $object;
566-
}
567-
$this->nextOrFail(',');
568-
$this->white();
541+
$this->white();
542+
$this->nextOrFail(':');
543+
if ($this->associative) {
544+
$object[$key] = $this->value();
545+
} else {
546+
$object->{$key} = $this->value();
569547
}
548+
$this->white();
549+
// If there's no comma after this pair, this needs to be
550+
// the end of the object.
551+
if ($this->ch !== ',') {
552+
$this->nextOrFail('}');
553+
$this->depth--;
554+
return $object;
555+
}
556+
$this->nextOrFail(',');
557+
$this->white();
570558
}
571-
572-
$this->throwSyntaxError('Bad object');
573559
}
574560

575561
/**

test/Functional/ParseTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,23 @@ public function dataForTestInvalidES5WhichIsAlsoInvalidJson5()
155155
return $tests;
156156
}
157157

158+
public function testNaNWithSign()
159+
{
160+
$this->assertTrue(is_nan(Json5Decoder::decode('+NaN')));
161+
}
162+
163+
public function testBadNumberStartingWithN()
164+
{
165+
$this->setExpectedException('ColinODell\\Json5\\SyntaxError');
166+
Json5Decoder::decode('NotANumber');
167+
}
168+
169+
public function testBadNumberStartingWithI()
170+
{
171+
$this->setExpectedException('ColinODell\\Json5\\SyntaxError');
172+
Json5Decoder::decode('+Indigo');
173+
}
174+
158175
private function getErrorSpec($file)
159176
{
160177
$errorSpec = str_replace('.txt', '.errorSpec', $file);

0 commit comments

Comments
 (0)