1
1
<?php
2
2
3
+ declare (strict_types=1 );
4
+
3
5
/*
4
6
* This file is part of the PHP-JSON-COMMENT package.
5
7
*
@@ -37,7 +39,7 @@ class Comment
37
39
*
38
40
* @return string The comment stripped JSON.
39
41
*/
40
- public function strip ($ json )
42
+ public function strip (string $ json ): string
41
43
{
42
44
if (!\preg_match ('%\/(\/|\*)% ' , $ json ) && !\preg_match ('/,\s*(\}|\])/ ' , $ json )) {
43
45
return $ json ;
@@ -55,7 +57,7 @@ protected function reset()
55
57
$ this ->comment = 0 ;
56
58
}
57
59
58
- protected function doStrip ($ json )
60
+ protected function doStrip (string $ json ): string
59
61
{
60
62
$ return = '' ;
61
63
@@ -81,16 +83,16 @@ protected function doStrip($json)
81
83
return $ return ;
82
84
}
83
85
84
- protected function getSegments ($ json )
86
+ protected function getSegments (string $ json ): array
85
87
{
86
88
return [
87
- isset ( $ json [$ this ->index - 1 ]) ? $ json [ $ this -> index - 1 ] : '' ,
89
+ $ json [$ this ->index - 1 ] ?? '' ,
88
90
$ json [$ this ->index ],
89
- isset ( $ json [$ this ->index + 1 ]) ? $ json [ $ this -> index + 1 ] : '' ,
91
+ $ json [$ this ->index + 1 ] ?? '' ,
90
92
];
91
93
}
92
94
93
- protected function checkTrail ($ char , $ json )
95
+ protected function checkTrail (string $ char , string $ json ): string
94
96
{
95
97
if ($ char === ', ' || $ this ->commaPos === -1 ) {
96
98
$ this ->commaPos = $ this ->commaPos + ($ char === ', ' ? 1 : 0 );
@@ -112,12 +114,12 @@ protected function checkTrail($char, $json)
112
114
return $ json ;
113
115
}
114
116
115
- protected function inStringOrCommentEnd ($ prev , $ char , $ next )
117
+ protected function inStringOrCommentEnd (string $ prev , string $ char , string $ next ): bool
116
118
{
117
119
return $ this ->inString ($ char , $ prev ) || $ this ->inCommentEnd ($ next );
118
120
}
119
121
120
- protected function inString ($ char , $ prev )
122
+ protected function inString (string $ char , string $ prev ): bool
121
123
{
122
124
if (0 === $ this ->comment && $ char === '" ' && $ prev !== '\\' ) {
123
125
$ this ->inStr = !$ this ->inStr ;
@@ -126,7 +128,7 @@ protected function inString($char, $prev)
126
128
return $ this ->inStr ;
127
129
}
128
130
129
- protected function inCommentEnd ($ next )
131
+ protected function inCommentEnd (string $ next ): bool
130
132
{
131
133
if (!$ this ->inStr && 0 === $ this ->comment ) {
132
134
$ this ->comment = $ next === '// ' ? 1 : ($ next === '/* ' ? 2 : 0 );
@@ -135,7 +137,7 @@ protected function inCommentEnd($next)
135
137
return 0 === $ this ->comment ;
136
138
}
137
139
138
- protected function hasCommentEnded ($ char , $ next )
140
+ protected function hasCommentEnded (string $ char , string $ next ): bool
139
141
{
140
142
$ singleEnded = $ this ->comment === 1 && $ char == "\n" ;
141
143
$ multiEnded = $ this ->comment === 2 && $ next == '*/ ' ;
@@ -152,18 +154,18 @@ protected function hasCommentEnded($char, $next)
152
154
/**
153
155
* Strip comments and decode JSON string.
154
156
*
155
- * @param string $json
156
- * @param bool|bool $assoc
157
- * @param int|int $depth
158
- * @param int|int $options
157
+ * @param string $json
158
+ * @param bool $assoc
159
+ * @param int $depth
160
+ * @param int $options
159
161
*
160
162
* @see http://php.net/json_decode [JSON decode native function]
161
163
*
162
164
* @throws \RuntimeException When decode fails.
163
165
*
164
166
* @return mixed
165
167
*/
166
- public function decode ($ json , $ assoc = false , $ depth = 512 , $ options = 0 )
168
+ public function decode (string $ json , bool $ assoc = false , int $ depth = 512 , int $ options = 0 )
167
169
{
168
170
$ decoded = \json_decode ($ this ->strip ($ json ), $ assoc , $ depth , $ options );
169
171
@@ -183,7 +185,7 @@ public function decode($json, $assoc = false, $depth = 512, $options = 0)
183
185
/**
184
186
* Static alias of decode().
185
187
*/
186
- public static function parse ($ json , $ assoc = false , $ depth = 512 , $ options = 0 )
188
+ public static function parse (string $ json , bool $ assoc = false , int $ depth = 512 , int $ options = 0 )
187
189
{
188
190
static $ parser ;
189
191
0 commit comments