diff --git a/src/App.php b/src/App.php index ea86abd2..b49482b4 100644 --- a/src/App.php +++ b/src/App.php @@ -72,10 +72,9 @@ class App * * @var array */ - private static $aliases - = [ - '@swoft' => __DIR__, - ]; + private static $aliases = [ + '@swoft' => __DIR__, + ]; /** * 获取mysqlBean对象 diff --git a/src/Bootstrap/Server/ServerTrait.php b/src/Bootstrap/Server/ServerTrait.php index 12b4e447..50e1dbbb 100644 --- a/src/Bootstrap/Server/ServerTrait.php +++ b/src/Bootstrap/Server/ServerTrait.php @@ -25,7 +25,7 @@ trait ServerTrait */ protected function beforeServerStart() { - $this->fireServerListener(SwooleEvent::ON_BEFORE_START, [$this]); + $this->fireServerEvent(SwooleEvent::ON_BEFORE_START, [$this]); } /** @@ -41,7 +41,7 @@ public function onStart(Server $server) ProcessHelper::setProcessTitle($this->serverSetting['pname'] . ' master process (' . $this->scriptFile . ')'); - $this->fireServerListener(SwooleEvent::ON_START, [$server]); + $this->fireServerEvent(SwooleEvent::ON_START, [$server]); } /** @@ -52,7 +52,7 @@ public function onStart(Server $server) */ public function onManagerStart(Server $server) { - $this->fireServerListener(SwooleEvent::ON_MANAGER_START, [$server]); + $this->fireServerEvent(SwooleEvent::ON_MANAGER_START, [$server]); ProcessHelper::setProcessTitle($this->serverSetting['pname'] . ' manager process'); } @@ -81,7 +81,7 @@ public function onWorkerStart(Server $server, int $workerId) ProcessHelper::setProcessTitle($this->serverSetting['pname'] . ' worker process'); } - $this->fireServerListener(SwooleEvent::ON_WORKER_START, [$server, $workerId, $isWorker]); + $this->fireServerEvent(SwooleEvent::ON_WORKER_START, [$server, $workerId, $isWorker]); $this->beforeWorkerStart($server, $workerId, $isWorker); } @@ -144,7 +144,7 @@ protected function reloadBean(bool $isWorker) * @param string $event * @param array $params */ - protected function fireServerListener(string $event, array $params) + protected function fireServerEvent(string $event, array $params) { /** @var array[] $collector */ $collector = ServerListenerCollector::getCollector(); @@ -153,31 +153,10 @@ protected function fireServerListener(string $event, array $params) return; } - foreach ($collector[$event] as $listenerBeanName) { - $listener = App::getBean($listenerBeanName); + foreach ($collector[$event] as $beanClass) { + $listener = App::getBean($beanClass); $method = SwooleEvent::getHandlerFunction($event); $listener->$method(...$params); } } - - /** - * fire swoole event listeners - * - * @param string $event - * @param array $params - * @param string $type - */ - protected function fireSwooleListener(string $event, array $params, string $type = SwooleEvent::TYPE_SERVER) - { - $collector = SwooleListenerCollector::getCollector(); - - if (!isset($collector[$type][$event]) || empty($collector[$type][$event])) { - return; - } - - $beanClass = $collector[$type][$event]; - $listener = App::getBean($beanClass); - $method = SwooleEvent::getHandlerFunction($event); - $listener->$method(...$params); - } } diff --git a/src/Helper/ArrayHelper.php b/src/Helper/ArrayHelper.php index dc72dea0..cc8601dd 100644 --- a/src/Helper/ArrayHelper.php +++ b/src/Helper/ArrayHelper.php @@ -134,7 +134,6 @@ public static function merge($a, $b) * getting value from an object. * * @return mixed the value of the element if found, default value otherwise - * @throws InvalidParamException if $array is neither an array nor an object. */ public static function getValue($array, $key, $default = null) { @@ -156,18 +155,20 @@ public static function getValue($array, $key, $default = null) if (($pos = strrpos($key, '.')) !== false) { $array = static::getValue($array, substr($key, 0, $pos), $default); - $key = substr($key, $pos + 1); + $key = (string)substr($key, $pos + 1); } if (\is_object($array)) { // this is expected to fail if the property does not exist, or __get() is not implemented // it is not reliably possible to check whether a property is accessable beforehand return $array->$key; - } elseif (\is_array($array)) { + } + + if (\is_array($array)) { return (isset($array[$key]) || array_key_exists($key, $array)) ? $array[$key] : $default; - } else { - return $default; } + + return $default; } /** diff --git a/src/Helper/ComponentHelper.php b/src/Helper/ComponentHelper.php index 724d08e6..64f083eb 100644 --- a/src/Helper/ComponentHelper.php +++ b/src/Helper/ComponentHelper.php @@ -28,7 +28,7 @@ public static function getComponentNamespace(string $component, string $path):st */ public static function getComponentNs(string $component): string { - if ($component == 'framework') { + if ($component === 'framework') { return ''; } @@ -48,11 +48,14 @@ public static function getComponentNs(string $component): string private static function parseAutoloadFromComposerFile($filename): array { $json = file_get_contents($filename); + $mapping = []; + try { $content = JsonHelper::decode($json, true); } catch (\InvalidArgumentException $e) { $content = []; } + // only compatible with psr-4 now //TODO compatible with the another autoload standard if (isset($content['autoload']['psr-4'])) { @@ -63,6 +66,7 @@ private static function parseAutoloadFromComposerFile($filename): array $mapping[$key] = $value[$valueLength - 1] === '\\' ? substr($value, 0, $valueLength - 1) : $value; } } + return \is_array($mapping) ? $mapping : []; } @@ -75,6 +79,7 @@ private static function getDefaultNamespace($component): string $componentNs = ComponentHelper::getComponentNs($component); $componentNs = self::handlerFrameworkNamespace($componentNs); $namespace = "Swoft{$componentNs}"; + return $namespace; } @@ -85,9 +90,10 @@ private static function getDefaultNamespace($component): string */ private static function handlerFrameworkNamespace(string $componentNs):string { - if($componentNs == '\Swoft\Framework'){ - return ""; + if($componentNs === '\Swoft\Framework'){ + return ''; } + return $componentNs; } -} \ No newline at end of file +} diff --git a/src/Helper/FileHelper.php b/src/Helper/FileHelper.php index 67a2f173..5db950bc 100644 --- a/src/Helper/FileHelper.php +++ b/src/Helper/FileHelper.php @@ -14,7 +14,6 @@ */ class FileHelper { - /** * 获得文件扩展名、后缀名 * @param $filename @@ -34,13 +33,13 @@ public static function getSuffix($filename, $clearPoint = false): string */ public static function isAbsPath($path): bool { - if (!$path || !is_string($path)) { + if (!$path || !\is_string($path)) { return false; } if ( $path{0} === '/' || // linux/mac - 1 === preg_match('#^[a-z]:[\/|\\\]{1}.+#i', $path) // windows + 1 === \preg_match('#^[a-z]:[\/|\\\]{1}.+#i', $path) // windows ) { return true; } @@ -58,13 +57,13 @@ public static function isAbsPath($path): bool public static function md5File($dir) { if (!is_dir($dir)) { - return ""; + return ''; } $md5File = array(); $d = dir($dir); while (false !== ($entry = $d->read())) { - if ($entry != '.' && $entry != '..') { + if ($entry !== '.' && $entry !== '..') { if (is_dir($dir . '/' . $entry)) { $md5File[] = self::md5File($dir . '/' . $entry); } elseif (substr($entry, -4) === '.php') { diff --git a/src/Helper/JsonHelper.php b/src/Helper/JsonHelper.php index a180c525..96fe91aa 100644 --- a/src/Helper/JsonHelper.php +++ b/src/Helper/JsonHelper.php @@ -48,7 +48,7 @@ public static function decode($json, $assoc = false, $depth = 512, $options = 0) * @throws \InvalidArgumentException if the JSON cannot be encoded. * @link http://www.php.net/manual/en/function.json-encode.php */ - public static function encode($value, $options = 0, $depth = 512) + public static function encode($value, $options = 0, $depth = 512): string { if ($value instanceof Arrayable) { $value = $value->toArray(); diff --git a/src/Helper/PhpHelper.php b/src/Helper/PhpHelper.php index 6ea059d0..c6c04cb0 100644 --- a/src/Helper/PhpHelper.php +++ b/src/Helper/PhpHelper.php @@ -28,7 +28,7 @@ public static function isCli(): bool * * @return bool */ - public static function isMac() + public static function isMac(): bool { return stripos(PHP_OS, 'Darwin') !== false; } @@ -43,11 +43,11 @@ public static function isMac() */ public static function call($cb, array $args = []) { - if (is_object($cb) || (is_string($cb) && function_exists($cb))) { + if (\is_object($cb) || (\is_string($cb) && \function_exists($cb))) { $ret = $cb(...$args); - } elseif (is_array($cb)) { + } elseif (\is_array($cb)) { list($obj, $mhd) = $cb; - $ret = is_object($obj) ? $obj->$mhd(...$args) : $obj::$mhd(...$args); + $ret = \is_object($obj) ? $obj->$mhd(...$args) : $obj::$mhd(...$args); } else { $ret = \Swoole\Coroutine::call_user_func_array($cb, $args); }