Skip to content

Commit e702bde

Browse files
committedAug 1, 2018
refactor(Comment): split strip() for clarity
1 parent fca8499 commit e702bde

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed
 

‎src/Comment.php

+29-12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
*/
1010
class Comment
1111
{
12+
/** @var int The current index being scanned */
13+
protected $index = -1;
14+
1215
/** @var bool If current char is within a string */
1316
protected $inStr = false;
1417

@@ -28,35 +31,49 @@ public function strip($json)
2831
return $json;
2932
}
3033

31-
list($index, $return, $char) = [-1, '', ''];
34+
$this->reset();
3235

33-
while (isset($json[++$index])) {
34-
list($prev, $char) = [$char, $json[$index]];
36+
return $this->doStrip($json);
37+
}
38+
39+
protected function reset()
40+
{
41+
$this->index = -1;
42+
$this->inStr = false;
43+
$this->comment = 0;
44+
}
3545

36-
$charnext = $char . (isset($json[$index + 1]) ? $json[$index + 1] : '');
37-
if ($this->inStringOrCommentEnd($prev, $char, $charnext)) {
46+
protected function doStrip($json)
47+
{
48+
$return = '';
49+
50+
while (isset($json[++$this->index])) {
51+
list($prev, $char, $next) = $this->getSegments($json);
52+
53+
if ($this->inStringOrCommentEnd($prev, $char, $char . $next)) {
3854
$return .= $char;
3955

4056
continue;
4157
}
4258

4359
$wasSingle = 1 === $this->comment;
44-
if ($this->hasCommentEnded($char, $charnext) && $wasSingle) {
60+
if ($this->hasCommentEnded($char, $char . $next) && $wasSingle) {
4561
$return = \rtrim($return) . $char;
4662
}
4763

48-
$index += $charnext === '*/' ? 1 : 0;
64+
$this->index += $char . $next === '*/' ? 1 : 0;
4965
}
5066

51-
$this->reset();
52-
5367
return $return;
5468
}
5569

56-
protected function reset()
70+
protected function getSegments($json)
5771
{
58-
$this->comment = 0;
59-
$this->inStr = false;
72+
return [
73+
isset($json[$this->index - 1]) ? $json[$this->index - 1] : '',
74+
$json[$this->index],
75+
isset($json[$this->index + 1]) ? $json[$this->index + 1] : '',
76+
];
6077
}
6178

6279
protected function inStringOrCommentEnd($prev, $char, $charnext)

0 commit comments

Comments
 (0)
Please sign in to comment.