Skip to content

Commit b8e0321

Browse files
authored
Merge pull request #6 from adhocore/refactor
Refactor - split logic from main loop
2 parents 02a1b00 + c18b0c3 commit b8e0321

File tree

2 files changed

+38
-27
lines changed

2 files changed

+38
-27
lines changed

src/Comment.php

+38-24
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
*/
1010
class Comment
1111
{
12+
/** @var bool If current char is within a string */
13+
protected $inStr = false;
14+
15+
/** @var int Lines of comments 0 = no comment, 1 = single line, 2 = multi lines */
16+
protected $comment = 0;
17+
1218
/**
1319
* Strip comments from JSON string.
1420
*
@@ -22,40 +28,21 @@ public function strip($json)
2228
return $json;
2329
}
2430

25-
$index = -1;
26-
$inStr = false;
27-
$return = '';
28-
$char = '';
29-
$comment = 'none';
31+
list($index, $return, $char) = [-1, '', ''];
3032

3133
while (isset($json[++$index])) {
3234
list($prev, $char) = [$char, $json[$index]];
3335

34-
if ('none' === $comment && $char === '"' && $prev !== '\\') {
35-
$inStr = !$inStr;
36-
}
37-
3836
$charnext = $char . (isset($json[$index + 1]) ? $json[$index + 1] : '');
39-
40-
if (!$inStr && 'none' === $comment) {
41-
$comment = $charnext === '//' ? 'single' : ($charnext === '/*' ? 'multi' : 'none');
42-
}
43-
44-
if ($inStr || 'none' === $comment) {
37+
if ($this->inStringOrCommentEnd($prev, $char, $charnext)) {
4538
$return .= $char;
4639

4740
continue;
4841
}
4942

50-
if (($comment === 'single' && $char == "\n")
51-
|| ($comment === 'multi' && $charnext == '*/')
52-
) {
53-
// Cosmetic fix only!
54-
if ($comment === 'single') {
55-
$return = rtrim($return) . $char;
56-
}
57-
58-
$comment = 'none';
43+
$wasSingle = 1 === $this->comment;
44+
if ($this->hasCommentEnded($char, $charnext) && $wasSingle) {
45+
$return = rtrim($return) . $char;
5946
}
6047

6148
$index += $charnext === '*/' ? 1 : 0;
@@ -64,6 +51,33 @@ public function strip($json)
6451
return $return;
6552
}
6653

54+
protected function inStringOrCommentEnd($prev, $char, $charnext)
55+
{
56+
if (0 === $this->comment && $char === '"' && $prev !== '\\') {
57+
$this->inStr = !$this->inStr;
58+
}
59+
60+
if (!$this->inStr && 0 === $this->comment) {
61+
$this->comment = $charnext === '//' ? 1 : ($charnext === '/*' ? 2 : 0);
62+
}
63+
64+
return $this->inStr || 0 === $this->comment;
65+
}
66+
67+
protected function hasCommentEnded($char, $charnext)
68+
{
69+
$singleEnded = $this->comment === 1 && $char == "\n";
70+
$multiEnded = $this->comment === 2 && $charnext == '*/';
71+
72+
if ($singleEnded || $multiEnded) {
73+
$this->comment = 0;
74+
75+
return true;
76+
}
77+
78+
return false;
79+
}
80+
6781
/**
6882
* Strip comments and decode JSON string.
6983
*

tests/CommentTest.php

-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44

55
use Ahc\Json\Comment;
66

7-
/** @coversDefaultClass \Ahc\Json\Comment */
87
class CommentTest extends \PHPUnit_Framework_TestCase
98
{
109
/**
1110
* @dataProvider theTests
12-
* @covers \Ahc\Json\Comment::strip
1311
*/
1412
public function testStrip($json, $expect)
1513
{
@@ -18,7 +16,6 @@ public function testStrip($json, $expect)
1816

1917
/**
2018
* @dataProvider theTests
21-
* @covers \Ahc\Json\Comment::decode
2219
*/
2320
public function testDecode($json)
2421
{

0 commit comments

Comments
 (0)