11<?php
22
3+ declare (strict_types=1 );
4+
35/*
46 * This file is part of the PHP-JSON-COMMENT package.
57 *
@@ -37,7 +39,7 @@ class Comment
3739 *
3840 * @return string The comment stripped JSON.
3941 */
40- public function strip ($ json )
42+ public function strip (string $ json ): string
4143 {
4244 if (!\preg_match ('%\/(\/|\*)% ' , $ json ) && !\preg_match ('/,\s*(\}|\])/ ' , $ json )) {
4345 return $ json ;
@@ -55,7 +57,7 @@ protected function reset()
5557 $ this ->comment = 0 ;
5658 }
5759
58- protected function doStrip ($ json )
60+ protected function doStrip (string $ json ): string
5961 {
6062 $ return = '' ;
6163
@@ -81,16 +83,16 @@ protected function doStrip($json)
8183 return $ return ;
8284 }
8385
84- protected function getSegments ($ json )
86+ protected function getSegments (string $ json ): array
8587 {
8688 return [
87- isset ( $ json [$ this ->index - 1 ]) ? $ json [ $ this -> index - 1 ] : '' ,
89+ $ json [$ this ->index - 1 ] ?? '' ,
8890 $ json [$ this ->index ],
89- isset ( $ json [$ this ->index + 1 ]) ? $ json [ $ this -> index + 1 ] : '' ,
91+ $ json [$ this ->index + 1 ] ?? '' ,
9092 ];
9193 }
9294
93- protected function checkTrail ($ char , $ json )
95+ protected function checkTrail (string $ char , string $ json ): string
9496 {
9597 if ($ char === ', ' || $ this ->commaPos === -1 ) {
9698 $ this ->commaPos = $ this ->commaPos + ($ char === ', ' ? 1 : 0 );
@@ -112,12 +114,12 @@ protected function checkTrail($char, $json)
112114 return $ json ;
113115 }
114116
115- protected function inStringOrCommentEnd ($ prev , $ char , $ next )
117+ protected function inStringOrCommentEnd (string $ prev , string $ char , string $ next ): bool
116118 {
117119 return $ this ->inString ($ char , $ prev ) || $ this ->inCommentEnd ($ next );
118120 }
119121
120- protected function inString ($ char , $ prev )
122+ protected function inString (string $ char , string $ prev ): bool
121123 {
122124 if (0 === $ this ->comment && $ char === '" ' && $ prev !== '\\' ) {
123125 $ this ->inStr = !$ this ->inStr ;
@@ -126,7 +128,7 @@ protected function inString($char, $prev)
126128 return $ this ->inStr ;
127129 }
128130
129- protected function inCommentEnd ($ next )
131+ protected function inCommentEnd (string $ next ): bool
130132 {
131133 if (!$ this ->inStr && 0 === $ this ->comment ) {
132134 $ this ->comment = $ next === '// ' ? 1 : ($ next === '/* ' ? 2 : 0 );
@@ -135,7 +137,7 @@ protected function inCommentEnd($next)
135137 return 0 === $ this ->comment ;
136138 }
137139
138- protected function hasCommentEnded ($ char , $ next )
140+ protected function hasCommentEnded (string $ char , string $ next ): bool
139141 {
140142 $ singleEnded = $ this ->comment === 1 && $ char == "\n" ;
141143 $ multiEnded = $ this ->comment === 2 && $ next == '*/ ' ;
@@ -152,18 +154,18 @@ protected function hasCommentEnded($char, $next)
152154 /**
153155 * Strip comments and decode JSON string.
154156 *
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
159161 *
160162 * @see http://php.net/json_decode [JSON decode native function]
161163 *
162164 * @throws \RuntimeException When decode fails.
163165 *
164166 * @return mixed
165167 */
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 )
167169 {
168170 $ decoded = \json_decode ($ this ->strip ($ json ), $ assoc , $ depth , $ options );
169171
@@ -183,7 +185,7 @@ public function decode($json, $assoc = false, $depth = 512, $options = 0)
183185 /**
184186 * Static alias of decode().
185187 */
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 )
187189 {
188190 static $ parser ;
189191
0 commit comments