add cs fixer and PHPStan and activate it (build 436)

This commit is contained in:
crazywhalecc
2022-03-15 18:05:33 +08:00
parent d01bd69aa5
commit 1706afbcd0
163 changed files with 4572 additions and 3588 deletions

View File

@@ -1,9 +1,10 @@
<?php /** @noinspection PhpUnused */
<?php
declare(strict_types=1);
/** @noinspection PhpUnused */
namespace ZM\Utils;
use ZM\Config\ZMConfig;
use ZM\Console\Console;
@@ -13,9 +14,9 @@ class DataProvider
/**
* 返回资源目录
* @return string
*/
public static function getResourceFolder(): string {
public static function getResourceFolder(): string
{
return self::getWorkingDir() . '/resources/';
}
@@ -23,7 +24,8 @@ class DataProvider
* 返回工作目录,不带最右边文件夹的斜杠(/
* @return false|string
*/
public static function getWorkingDir() {
public static function getWorkingDir()
{
return WORKING_DIR;
}
@@ -31,7 +33,8 @@ class DataProvider
* 获取框架所在根目录
* @return false|string
*/
public static function getFrameworkRootDir() {
public static function getFrameworkRootDir()
{
return FRAMEWORK_ROOT_DIR;
}
@@ -39,28 +42,32 @@ class DataProvider
* 获取源码根目录除Phar模式外均与工作目录相同
* @return false|string
*/
public static function getSourceRootDir() {
return defined("SOURCE_ROOT_DIR") ? SOURCE_ROOT_DIR : WORKING_DIR;
public static function getSourceRootDir()
{
return defined('SOURCE_ROOT_DIR') ? SOURCE_ROOT_DIR : WORKING_DIR;
}
/**
* 获取框架反代链接
* @return array|false|mixed|null
* @return null|array|false|mixed
*/
public static function getFrameworkLink() {
return ZMConfig::get("global", "http_reverse_link");
public static function getFrameworkLink()
{
return ZMConfig::get('global', 'http_reverse_link');
}
/**
* 获取zm_data数据目录如果二级目录不为空则自动创建目录并返回
* @param string $second
* @return array|false|mixed|string|null
* @return null|array|false|mixed|string
*/
public static function getDataFolder(string $second = '') {
public static function getDataFolder(string $second = '')
{
if ($second !== '') {
@mkdir(ZM_DATA . $second);
if (!is_dir(ZM_DATA . $second)) return false;
return realpath(ZM_DATA . $second) . "/";
if (!is_dir(ZM_DATA . $second)) {
return false;
}
return realpath(ZM_DATA . $second) . '/';
}
return ZM_DATA;
}
@@ -71,65 +78,74 @@ class DataProvider
* @param $file_array
* @return false|int
*/
public static function saveToJson($filename, $file_array) {
$path = ZMConfig::get("global", "config_dir");
$r = explode("/", $filename);
public static function saveToJson($filename, $file_array)
{
$path = ZMConfig::get('global', 'config_dir');
$r = explode('/', $filename);
if (count($r) == 2) {
$path = $path . $r[0] . "/";
if (!is_dir($path)) mkdir($path);
$path = $path . $r[0] . '/';
if (!is_dir($path)) {
mkdir($path);
}
$name = $r[1];
} elseif (count($r) != 1) {
Console::warning(zm_internal_errcode("E00057") . "存储失败,文件名只能有一级目录");
Console::warning(zm_internal_errcode('E00057') . '存储失败,文件名只能有一级目录');
return false;
} else {
$name = $r[0];
}
return file_put_contents($path . $name . ".json", json_encode($file_array, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
return file_put_contents($path . $name . '.json', json_encode($file_array, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
}
/**
* 从json加载变量到内存
* @param $filename
* @return mixed|null
* @return null|mixed
*/
public static function loadFromJson($filename) {
$path = ZMConfig::get("global", "config_dir");
if (file_exists($path . $filename . ".json")) {
return json_decode(file_get_contents($path . $filename . ".json"), true);
} else {
return null;
public static function loadFromJson($filename)
{
$path = ZMConfig::get('global', 'config_dir');
if (file_exists($path . $filename . '.json')) {
return json_decode(file_get_contents($path . $filename . '.json'), true);
}
return null;
}
/**
* 递归或非递归扫描目录,可返回相对目录的文件列表或绝对目录的文件列表
* @param $dir
* @param bool $recursive
* @param bool|string $relative
* @param bool|string $relative
* @return array|false
* @since 2.5
*/
public static function scanDirFiles($dir, bool $recursive = true, $relative = false) {
$dir = rtrim($dir, "/");
if (!is_dir($dir)) return false;
public static function scanDirFiles($dir, bool $recursive = true, $relative = false)
{
$dir = rtrim($dir, '/');
if (!is_dir($dir)) {
return false;
}
$r = scandir($dir);
if ($r === false) return false;
if ($r === false) {
return false;
}
$list = [];
if ($relative === true) {
$relative = $dir;
}
foreach ($r as $v) {
if ($v == "." || $v == "..") continue;
$sub_file = $dir . "/" . $v;
if ($v == '.' || $v == '..') {
continue;
}
$sub_file = $dir . '/' . $v;
if (is_dir($sub_file) && $recursive) {
$list = array_merge($list, self::scanDirFiles($sub_file, $recursive, $relative));
} elseif (is_file($sub_file)) {
if (is_string($relative) && mb_strpos($sub_file, $relative) === 0) {
$list [] = ltrim(mb_substr($sub_file, mb_strlen($relative)), "/");
$list[] = ltrim(mb_substr($sub_file, mb_strlen($relative)), '/');
} elseif ($relative === false) {
$list[] = $sub_file;
} else {
Console::warning(zm_internal_errcode("E00058") . "Relative path is not generated: wrong base directory ($relative)");
Console::warning(zm_internal_errcode('E00058') . "Relative path is not generated: wrong base directory ({$relative})");
return false;
}
}
@@ -143,7 +159,8 @@ class DataProvider
* @return bool
* @since 2.5
*/
public static function isRelativePath($path) {
public static function isRelativePath($path)
{
return strlen($path) > 0 && $path[0] === '/';
}
}