PHP80 小修 (#187)

* migrate-php80

fix styles
fix static analyse

* fix some bugs
This commit is contained in:
sunxyw
2022-12-19 20:22:47 +08:00
committed by GitHub
parent da516b487d
commit 8ff7da4d23
46 changed files with 180 additions and 366 deletions

View File

@@ -12,12 +12,11 @@ class BoundMethod
* 调用指定闭包、类方法并注入依赖
*
* @param Container $container
* @param callable|string $callback
* @return mixed
* @throws EntryResolutionException|\ReflectionException
* @throws \InvalidArgumentException
*/
public static function call(ContainerInterface $container, $callback, array $parameters = [], string $default_method = null)
public static function call(ContainerInterface $container, callable|string $callback, array $parameters = [], string $default_method = null)
{
if (is_string($callback) && !$default_method && method_exists($callback, '__invoke')) {
$default_method = '__invoke';
@@ -41,10 +40,9 @@ class BoundMethod
/**
* Get all dependencies for a given method.
*
* @param callable|string $callback
* @throws \ReflectionException
*/
protected static function getMethodDependencies(ContainerInterface $container, $callback, array $parameters = []): array
protected static function getMethodDependencies(ContainerInterface $container, callable|string $callback, array $parameters = []): array
{
$dependencies = [];

View File

@@ -10,47 +10,42 @@ use ZM\Utils\ReflectionUtil;
trait ContainerTrait
{
/**
* @var array
*/
protected $shared = [];
protected array $shared = [];
/**
* @var array[]
*/
protected $build_stack = [];
protected array $build_stack = [];
/**
* @var array[]
*/
protected $with = [];
protected array $with = [];
/**
* 日志前缀
*
* @var string
*/
protected $log_prefix;
protected string $log_prefix;
/**
* @var array[]
*/
private static $bindings = [];
private static array $bindings = [];
/**
* @var object[]
*/
private static $instances = [];
private static array $instances = [];
/**
* @var string[]
*/
private static $aliases = [];
private static array $aliases = [];
/**
* @var \Closure[][]
*/
private static $extenders = [];
private static array $extenders = [];
public function __construct()
{
@@ -188,7 +183,7 @@ trait ContainerTrait
* @param mixed $instance 实例
* @return mixed
*/
public function instance(string $abstract, $instance)
public function instance(string $abstract, mixed $instance)
{
if (isset(self::$instances[$abstract])) {
return self::$instances[$abstract];
@@ -211,9 +206,7 @@ trait ContainerTrait
*/
public function factory(string $abstract): \Closure
{
return function () use ($abstract) {
return $this->make($abstract);
};
return fn () => $this->make($abstract);
}
/**
@@ -313,7 +306,7 @@ trait ContainerTrait
* @return mixed
* @throws EntryResolutionException
*/
public function build($concrete)
public function build(\Closure|string $concrete)
{
// 如果传入的是闭包,则直接执行并返回
if ($concrete instanceof \Closure) {
@@ -363,7 +356,7 @@ trait ContainerTrait
* @param null|string $default_method 默认方法
* @return mixed
*/
public function call($callback, array $parameters = [], string $default_method = null)
public function call(callable|string $callback, array $parameters = [], string $default_method = null)
{
if ($this->shouldLog()) {
if (count($parameters)) {
@@ -446,7 +439,7 @@ trait ContainerTrait
*/
public function getLogPrefix(): string
{
return ($this->log_prefix ?: '[WorkerContainer(U)]') . ' ';
return ($this->log_prefix ?? '[WorkerContainer(U)]') . ' ';
}
/**
@@ -674,10 +667,9 @@ trait ContainerTrait
/**
* 获取类名的实际类型
*
* @param string $abstract 类或接口名
* @return \Closure|string
* @param string $abstract 类或接口名
*/
protected function getConcrete(string $abstract)
protected function getConcrete(string $abstract): \Closure|string
{
if (isset(self::$bindings[$abstract])) {
return self::$bindings[$abstract]['concrete'];
@@ -692,7 +684,7 @@ trait ContainerTrait
* @param mixed $concrete 实际类型
* @param string $abstract 类或接口名
*/
protected function isBuildable($concrete, string $abstract): bool
protected function isBuildable(mixed $concrete, string $abstract): bool
{
return $concrete === $abstract || $concrete instanceof \Closure;
}