1- <?php
1+ <?php /** @noinspection UnknownInspectionInspection */
2+
23/**
34 * @noinspection TypeUnsafeArraySearchInspection
45 * @noinspection PhpComposerExtensionStubsInspection
@@ -93,11 +94,11 @@ class Services_JSON
9394 public const DECODE_FIX_ROOT = 128 ;
9495 public const DECODE_NO_QUOTE = 256 ;
9596
96- protected static $ use = 0 ;
97+ protected static int $ use = 0 ;
9798 // private - cache the mbstring lookup results..
98- protected static $ _mb_strlen = false ;
99- protected static $ _mb_substr = false ;
100- protected static $ _mb_convert_encoding = false ;
99+ protected static bool $ _mb_strlen = false ;
100+ protected static bool $ _mb_substr = false ;
101+ protected static bool $ _mb_convert_encoding = false ;
101102
102103 /**
103104 * constructs a new JSON instance. We force this library as static
@@ -128,7 +129,7 @@ protected static function init(?int $use = null): void
128129 * @return string UTF-8 character
129130 * @access private
130131 */
131- protected static function utf162utf8 ($ utf16 ): string
132+ protected static function utf162utf8 (string $ utf16 ): string
132133 {
133134 if (self ::$ _mb_convert_encoding ) {
134135 return mb_convert_encoding ($ utf16 , 'UTF-8 ' , 'UTF-16 ' );
@@ -162,7 +163,7 @@ protected static function utf162utf8($utf16): string
162163 * @return string UTF-16 character
163164 * @access private
164165 */
165- public static function utf82utf16 ($ utf8 ): string
166+ public static function utf82utf16 (string $ utf8 ): string
166167 {
167168 if (self ::$ _mb_convert_encoding ) {
168169 return mb_convert_encoding ($ utf8 , 'UTF-16 ' , 'UTF-8 ' );
@@ -204,7 +205,7 @@ public static function encode($var, $use = 0)
204205 }
205206
206207 /**
207- * encodes an arbitrary variable into JSON format without JSON Header - warning - may allow XSS!!!!)
208+ * encodes an arbitrary variable into JSON format without JSON Header - warning - may allow XSS!!!!
208209 *
209210 * @param mixed $var any number, boolean, string, array, or object to be encoded.
210211 * see argument 1 to Services_JSON() above for array-parsing behavior.
@@ -425,13 +426,13 @@ public static function _encode($var)
425426 * @return string JSON-formatted name-value pair, like '"name":value'
426427 * @access private
427428 */
428- protected static function name_value ($ name , $ value )
429+ protected static function name_value (string $ name , $ value )
429430 {
430431 $ encoded_value = self ::_encode ($ value );
431432 if (self ::isError ($ encoded_value )) {
432433 return $ encoded_value ;
433434 }
434- return self ::_encode (( string ) $ name ) . ': ' . $ encoded_value ;
435+ return self ::_encode ($ name ) . ': ' . $ encoded_value ;
435436 }
436437
437438 /**
@@ -442,7 +443,7 @@ protected static function name_value($name, $value)
442443 * @return string string value stripped of comments and whitespace
443444 * @access private
444445 */
445- protected static function reduce_string ($ str ): string
446+ protected static function reduce_string (string $ str ): string
446447 {
447448 $ str = preg_replace ([// eliminate single line comments in '// ...' form
448449 '#^\s*//(.+)$#m ' , // eliminate multi-line comments in '/* ... */' form, at start of string
@@ -480,10 +481,13 @@ protected static function reduce_string($str): string
480481 * in ASCII or UTF-8 format!<br>
481482 * @access public
482483 */
483- public static function decode ($ str , int $ use = 0 )
484+ public static function decode (string $ str , int $ use = 0 )
484485 {
485486 if ($ use & self ::DECODE_FIX_ROOT ) {
486487 $ str = trim ($ str );
488+ if ($ str ==="" ) {
489+ throw new RuntimeException ("no value to decode " );
490+ }
487491 $ firstChar = $ str [0 ];
488492 if ($ firstChar !== '{ ' && $ firstChar !== '[ ' ) {
489493 // fixing a malformed json-u, if the json-u doesn't start with { [ and ends with ] } then it wraps it
@@ -734,7 +738,7 @@ protected static function isError($data, $code = null): bool
734738 * @param string $str
735739 * @return integer length
736740 */
737- protected static function strlen8 ($ str ): int
741+ protected static function strlen8 (string $ str ): int
738742 {
739743 if (self ::$ _mb_strlen ) {
740744 return mb_strlen ($ str , "8bit " );
@@ -746,10 +750,10 @@ protected static function strlen8($str): int
746750 * Returns part of a string, interpreting $start and $length as number of bytes.
747751 * @param string $string
748752 * @param integer $start start
749- * @param integer $length length
753+ * @param integer|bool $length length
750754 * @return string length
751755 */
752- protected static function substr8 ($ string , $ start , $ length = false ): string
756+ protected static function substr8 (string $ string , int $ start , $ length = false ): string
753757 {
754758 if ($ length === false ) {
755759 $ length = self ::strlen8 ($ string ) - $ start ;
0 commit comments