diff --git a/var/AutoP.php b/var/AutoP.php index 0efd0b1b..35cf27e7 100644 --- a/var/AutoP.php +++ b/var/AutoP.php @@ -10,7 +10,7 @@ class AutoP { // 作为段落的标签 - const BLOCK = 'p|pre|div|blockquote|form|ul|ol|dd|table|ins|h1|h2|h3|h4|h5|h6'; + private const BLOCK = 'p|pre|div|blockquote|form|ul|ol|dd|table|ins|h1|h2|h3|h4|h5|h6'; /** * 唯一id @@ -18,7 +18,7 @@ class AutoP * @access private * @var integer */ - private $_uniqueId = 0; + private $uniqueId = 0; /** * 存储的段落 @@ -26,7 +26,7 @@ class AutoP * @access private * @var array */ - private $_blocks = []; + private $blocks = []; /** * 替换段落的回调函数 @@ -35,14 +35,17 @@ class AutoP * @param array $matches 匹配值 * @return string */ - public function replaceBlockCallback($matches) + public function replaceBlockCallback(array $matches): string { $tagMatch = '|' . $matches[1] . '|'; $text = $matches[4]; switch (true) { /** 用br处理换行 */ - case false !== strpos('|li|dd|dt|td|p|a|span|cite|strong|sup|sub|small|del|u|i|b|ins|h1|h2|h3|h4|h5|h6|', $tagMatch): + case false !== strpos( + '|li|dd|dt|td|p|a|span|cite|strong|sup|sub|small|del|u|i|b|ins|h1|h2|h3|h4|h5|h6|', + $tagMatch + ): $text = nl2br(trim($text)); break; /** 用段落处理换行 */ @@ -63,7 +66,7 @@ class AutoP $key = '
'; } - $this->_blocks[$key] = "<{$matches[1]}{$matches[3]}>{$text}{$matches[1]}>"; + $this->blocks[$key] = "<{$matches[1]}{$matches[3]}>{$text}{$matches[1]}>"; return $key; } @@ -121,8 +124,8 @@ class AutoP public function parse($text) { /** 重置计数器 */ - $this->_uniqueId = 0; - $this->_blocks = []; + $this->uniqueId = 0; + $this->blocks = []; /** 将已有的段落后面的换行处理掉 */ $text = preg_replace(["/<\/p>\s+\s*/is"], ["
"], trim($text)); @@ -173,7 +176,7 @@ class AutoP } $text = $this->cutByBlock($text); - $blocks = array_reverse($this->_blocks); + $blocks = array_reverse($this->blocks); foreach ($blocks as $blockKey => $blockValue) { $text = str_replace($blockKey, $blockValue, $text); @@ -190,7 +193,7 @@ class AutoP */ private function makeUniqueId() { - return ':' . str_pad($this->_uniqueId ++, 4, '0', STR_PAD_LEFT); + return ':' . str_pad($this->uniqueId ++, 4, '0', STR_PAD_LEFT); } } diff --git a/var/Helper.php b/var/Helper.php index 89cb1054..9b35b07e 100644 --- a/var/Helper.php +++ b/var/Helper.php @@ -18,7 +18,7 @@ class Helper */ public static function security() { - return Typecho_Widget::widget('Widget_Security'); + return \Typecho\Widget::widget('Widget_Security'); } /** diff --git a/var/Json.php b/var/Json.php index 158bc7d1..f9859fb2 100644 --- a/var/Json.php +++ b/var/Json.php @@ -1,49 +1,90 @@ self::SERVICES_JSON_SLICE, + array_push($stk, [ + 'what' => self::SERVICES_JSON_SLICE, 'where' => 0, - 'delim' => false]); + 'delim' => false + ]); - $chrs = substr($str, 1, - 1); - $chrs = self::_reduce_string($chrs); + $chrs = substr($str, 1, -1); + $chrs = self::reduceString($chrs); if ($chrs == '') { if (reset($stk) == self::SERVICES_JSON_IN_ARR) { return $arr; - } else { return $obj; - } } $strlen_chrs = strlen($chrs); - for ($c = 0; $c <= $strlen_chrs; ++ $c) { - + for ($c = 0; $c <= $strlen_chrs; ++$c) { $top = end($stk); $substr_chrs_c_2 = substr($chrs, $c, 2); - if (($c == $strlen_chrs) || (($chrs [$c] == ',') && ($top['what'] == self::SERVICES_JSON_SLICE))) { + if ( + ($c == $strlen_chrs) || (($chrs [$c] == ',') + && ($top['what'] == self::SERVICES_JSON_SLICE)) + ) { $slice = substr($chrs, $top['where'], ($c - $top['where'])); - array_push($stk, ['what' => self::SERVICES_JSON_SLICE, 'where' => ($c + 1), 'delim' => false]); + array_push( + $stk, + [ + 'what' => self::SERVICES_JSON_SLICE, 'where' => ($c + 1), + 'delim' => false + ] + ); if (reset($stk) == self::SERVICES_JSON_IN_ARR) { - array_push($arr, self::_decode($slice)); - + array_push($arr, self::decodeStr($slice)); } elseif (reset($stk) == self::SERVICES_JSON_IN_OBJ) { $parts = []; if (preg_match('/^\s*(["\'].*[^\\\]["\'])\s*:\s*(\S.*),?$/Uis', $slice, $parts)) { - $key = self::_decode($parts[1]); - $val = self::_decode($parts[2]); + $key = self::decodeStr($parts[1]); + $val = self::decodeStr($parts[2]); $obj->$key = $val; } elseif (preg_match('/^\s*(\w+)\s*:\s*(\S.*),?$/Uis', $slice, $parts)) { $key = $parts[1]; - $val = self::_decode($parts[2]); + $val = self::decodeStr($parts[2]); $obj->$key = $val; } - } - - } elseif ((($chrs [$c] == '"') || ($chrs[$c] == "'")) && ($top['what'] != self::SERVICES_JSON_IN_STR)) { - array_push($stk, ['what' => self::SERVICES_JSON_IN_STR, 'where' => $c, 'delim' => $chrs[$c]]); - - } elseif (($chrs [$c] == $top['delim']) && + } elseif ( + (($chrs [$c] == '"') || ($chrs[$c] == "'")) + && ($top['what'] != self::SERVICES_JSON_IN_STR) + ) { + array_push( + $stk, + ['what' => self::SERVICES_JSON_IN_STR, 'where' => $c, 'delim' => $chrs[$c]] + ); + } elseif ( + ($chrs [$c] == $top['delim']) && ($top['what'] == self::SERVICES_JSON_IN_STR) && - ((strlen(substr($chrs, 0, $c)) - strlen(rtrim(substr($chrs, 0, $c), '\\'))) % 2 != 1)) { + ((strlen(substr($chrs, 0, $c)) - strlen(rtrim(substr($chrs, 0, $c), '\\'))) % 2 != 1) + ) { array_pop($stk); - - } elseif (($chrs [$c] == '[') && - in_array($top['what'], [self::SERVICES_JSON_SLICE, self::SERVICES_JSON_IN_ARR, self::SERVICES_JSON_IN_OBJ])) { + } elseif ( + ($chrs [$c] == '[') && + in_array( + $top['what'], + [self::SERVICES_JSON_SLICE, self::SERVICES_JSON_IN_ARR, self::SERVICES_JSON_IN_OBJ] + ) + ) { array_push($stk, ['what' => self::SERVICES_JSON_IN_ARR, 'where' => $c, 'delim' => false]); } elseif (($chrs [$c] == ']') && ($top['what'] == self::SERVICES_JSON_IN_ARR)) { array_pop($stk); - } elseif (($chrs [$c] == '{') && - in_array($top['what'], [self::SERVICES_JSON_SLICE, self::SERVICES_JSON_IN_ARR, self::SERVICES_JSON_IN_OBJ])) { + } elseif ( + ($chrs [$c] == '{') && + in_array( + $top['what'], + [self::SERVICES_JSON_SLICE, self::SERVICES_JSON_IN_ARR, self::SERVICES_JSON_IN_OBJ] + ) + ) { array_push($stk, ['what' => self::SERVICES_JSON_IN_OBJ, 'where' => $c, 'delim' => false]); } elseif (($chrs [$c] == '}') && ($top['what'] == self::SERVICES_JSON_IN_OBJ)) { array_pop($stk); - } elseif (($substr_chrs_c_2 == '/*') && - in_array($top['what'], [self::SERVICES_JSON_SLICE, self::SERVICES_JSON_IN_ARR, self::SERVICES_JSON_IN_OBJ])) { + } elseif ( + ($substr_chrs_c_2 == '/*') && + in_array( + $top['what'], + [self::SERVICES_JSON_SLICE, self::SERVICES_JSON_IN_ARR, self::SERVICES_JSON_IN_OBJ] + ) + ) { array_push($stk, ['what' => self::SERVICES_JSON_IN_CMT, 'where' => $c, 'delim' => false]); - $c ++; + $c++; } elseif (($substr_chrs_c_2 == '*/') && ($top['what'] == self::SERVICES_JSON_IN_CMT)) { array_pop($stk); - $c ++; - for ($i = $top['where']; $i <= $c; ++ $i) + $c++; + for ($i = $top['where']; $i <= $c; ++$i) { $chrs = substr_replace($chrs, ' ', $i, 1); + } } - } if (reset($stk) == self::SERVICES_JSON_IN_ARR) { return $arr; - } elseif (reset($stk) == self::SERVICES_JSON_IN_OBJ) { return $obj; - } - } } } @@ -498,11 +532,10 @@ class Json /** * 清除特殊格式 * - * @access private - * @param string $str 待处理字符串 - * @return string + * @param string|null $str 待处理字符串 + * @return string|null */ - private static function _reduce_string($str) + private static function reduceString(?string $str): ?string { $str = preg_replace([ '#^\s*//(.+)$#m', @@ -515,11 +548,10 @@ class Json /** * 将utf16转换为utf8 * - * @access private - * @param string $utf16 utf16字符串 - * @return string + * @param string|null $utf16 utf16字符串 + * @return string|null */ - private static function utf162utf8($utf16) + private static function utf162utf8(?string $utf16): ?string { if (function_exists('mb_convert_encoding')) { return mb_convert_encoding($utf16, 'UTF-8', 'UTF-16'); @@ -542,4 +574,3 @@ class Json return ''; } } -