Upgrade the minimum php version to 8.0 for circuit-breaker and command. (#4206)

This commit is contained in:
李铭昕 2021-11-03 16:17:14 +08:00 committed by GitHub
parent c93cb99784
commit c1d175a502
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
34 changed files with 75 additions and 229 deletions

View File

@ -12,50 +12,35 @@ declare(strict_types=1);
namespace Hyperf\CircuitBreaker\Annotation; namespace Hyperf\CircuitBreaker\Annotation;
use Attribute; use Attribute;
use Doctrine\Common\Annotations\Annotation\Target;
use Hyperf\CircuitBreaker\Handler\TimeoutHandler; use Hyperf\CircuitBreaker\Handler\TimeoutHandler;
use Hyperf\Di\Annotation\AbstractAnnotation; use Hyperf\Di\Annotation\AbstractAnnotation;
/** /**
* @Annotation
* @Target({"METHOD"})
* @property float $timeout * @property float $timeout
*/ */
#[Attribute(Attribute::TARGET_METHOD)] #[Attribute(Attribute::TARGET_METHOD)]
class CircuitBreaker extends AbstractAnnotation class CircuitBreaker extends AbstractAnnotation
{ {
/** public string $handler = TimeoutHandler::class;
* @var string
*/
public $handler = TimeoutHandler::class;
/** public ?string $fallback = null;
* @var string
*/
public $fallback;
/** /**
* The duration required to reset to a half open or close state. * The duration required to reset to a half open or close state.
* @var float
*/ */
public $duration = 10; public float $duration = 10;
/** /**
* The counter required to reset to a close state. * The counter required to reset to a close state.
* @var int
*/ */
public $successCounter = 10; public int $successCounter = 10;
/** /**
* The counter required to reset to a open state. * The counter required to reset to an open state.
* @var int
*/ */
public $failCounter = 10; public int $failCounter = 10;
/** public array $value;
* @var array
*/
public $value;
public function __construct(...$value) public function __construct(...$value)
{ {

View File

@ -18,20 +18,15 @@ use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint; use Hyperf\Di\Aop\ProceedingJoinPoint;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
/** #[Aspect]
* @Aspect
*/
class BreakerAnnotationAspect extends AbstractAspect class BreakerAnnotationAspect extends AbstractAspect
{ {
public $annotations = [ public $annotations = [
CircuitBreaker::class, CircuitBreaker::class,
]; ];
protected $container; public function __construct(protected ContainerInterface $container)
public function __construct(ContainerInterface $container)
{ {
$this->container = $container;
} }
public function process(ProceedingJoinPoint $proceedingJoinPoint) public function process(ProceedingJoinPoint $proceedingJoinPoint)

View File

@ -15,41 +15,24 @@ use Psr\Container\ContainerInterface;
class CircuitBreaker implements CircuitBreakerInterface class CircuitBreaker implements CircuitBreakerInterface
{ {
/** protected string $name;
* @var string
*/
protected $name;
/** protected State $state;
* @var ContainerInterface
*/
protected $container;
/** protected float $timestamp;
* @var State
*/
protected $state;
/**
* @var float
*/
protected $timestamp;
/** /**
* Failure count. * Failure count.
* @var int
*/ */
protected $failCounter; protected int $failCounter;
/** /**
* Success count. * Success count.
* @var int
*/ */
protected $successCounter; protected int $successCounter;
public function __construct(ContainerInterface $container, string $name) public function __construct(protected ContainerInterface $container, string $name)
{ {
$this->container = $container;
$this->name = $name; $this->name = $name;
$this->state = make(State::class); $this->state = make(State::class);
$this->init(); $this->init();
@ -62,9 +45,7 @@ class CircuitBreaker implements CircuitBreakerInterface
public function attempt(): bool public function attempt(): bool
{ {
/** @var Attempt $attempt */ return $this->container->get(Attempt::class)->attempt();
$attempt = $this->container->get(Attempt::class);
return $attempt->attempt();
} }
public function open(): void public function open(): void

View File

@ -15,13 +15,13 @@ use Psr\Container\ContainerInterface;
class CircuitBreakerFactory class CircuitBreakerFactory
{ {
protected $container; /**
* @var CircuitBreakerInterface[]
*/
protected array $breakers = [];
protected $breakers = []; public function __construct(protected ContainerInterface $container)
public function __construct(ContainerInterface $container)
{ {
$this->container = $container;
} }
public function get(string $name): ?CircuitBreakerInterface public function get(string $name): ?CircuitBreakerInterface

View File

@ -15,18 +15,12 @@ class CircuitBreakerException extends \RuntimeException
{ {
public $result; public $result;
/** public function setResult($result): static
* @param mixed $result
*/
public function setResult($result): self
{ {
$this->result = $result; $this->result = $result;
return $this; return $this;
} }
/**
* @return mixed
*/
public function getResult() public function getResult()
{ {
return $this->result; return $this->result;

View File

@ -20,27 +20,16 @@ use Hyperf\CircuitBreaker\FallbackInterface;
use Hyperf\Contract\StdoutLoggerInterface; use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Di\Aop\ProceedingJoinPoint; use Hyperf\Di\Aop\ProceedingJoinPoint;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
abstract class AbstractHandler implements HandlerInterface abstract class AbstractHandler implements HandlerInterface
{ {
/** protected CircuitBreakerFactory $factory;
* @var CircuitBreakerFactory
*/
protected $factory;
/** protected LoggerInterface $logger;
* @var ContainerInterface
*/
protected $container;
/** public function __construct(protected ContainerInterface $container)
* @var StdoutLoggerInterface
*/
protected $logger;
public function __construct(ContainerInterface $container)
{ {
$this->container = $container;
$this->factory = $container->get(CircuitBreakerFactory::class); $this->factory = $container->get(CircuitBreakerFactory::class);
$this->logger = $container->get(StdoutLoggerInterface::class); $this->logger = $container->get(StdoutLoggerInterface::class);
} }
@ -139,10 +128,7 @@ abstract class AbstractHandler implements HandlerInterface
); );
$this->logger->debug($info); $this->logger->debug($info);
$breaker->halfOpen(); $breaker->halfOpen();
return;
} }
return;
} }
} }

View File

@ -19,7 +19,7 @@ class State
public const OPEN = 2; public const OPEN = 2;
protected $state; protected int $state;
public function __construct() public function __construct()
{ {

View File

@ -14,10 +14,6 @@ namespace Hyperf\Command\Annotation;
use Attribute; use Attribute;
use Hyperf\Di\Annotation\AbstractAnnotation; use Hyperf\Di\Annotation\AbstractAnnotation;
/**
* @Annotation
* @Target({"CLASS"})
*/
#[Attribute(Attribute::TARGET_CLASS)] #[Attribute(Attribute::TARGET_CLASS)]
class Command extends AbstractAnnotation class Command extends AbstractAnnotation
{ {

View File

@ -19,6 +19,7 @@ use Swoole\ExitException;
use Symfony\Component\Console\Command\Command as SymfonyCommand; use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Formatter\OutputFormatterStyle; use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableStyle;
use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
@ -32,58 +33,39 @@ abstract class Command extends SymfonyCommand
/** /**
* The name of the command. * The name of the command.
*
* @var string
*/ */
protected $name; protected ?string $name = null;
protected ?InputInterface $input = null;
/** /**
* @var InputInterface * @var null|SymfonyStyle
*/ */
protected $input; protected ?OutputInterface $output = null;
/**
* @var SymfonyStyle
*/
protected $output;
/** /**
* The default verbosity of output commands. * The default verbosity of output commands.
*
* @var int
*/ */
protected $verbosity = OutputInterface::VERBOSITY_NORMAL; protected int $verbosity = OutputInterface::VERBOSITY_NORMAL;
/** /**
* Execution in a coroutine environment. * Execution in a coroutine environment.
*
* @var bool
*/ */
protected $coroutine = true; protected bool $coroutine = true;
/** protected ?EventDispatcherInterface $eventDispatcher = null;
* @var null|EventDispatcherInterface
*/
protected $eventDispatcher;
/** protected int $hookFlags = -1;
* @var int
*/
protected $hookFlags;
/** /**
* The name and signature of the command. * The name and signature of the command.
*
* @var null|string
*/ */
protected $signature; protected ?string $signature = null;
/** /**
* The mapping between human readable verbosity levels and Symfony's OutputInterface. * The mapping between human-readable verbosity levels and Symfony's OutputInterface.
*
* @var array
*/ */
protected $verbosityMap protected array $verbosityMap
= [ = [
'v' => OutputInterface::VERBOSITY_VERBOSE, 'v' => OutputInterface::VERBOSITY_VERBOSE,
'vv' => OutputInterface::VERBOSITY_VERY_VERBOSE, 'vv' => OutputInterface::VERBOSITY_VERY_VERBOSE,
@ -94,25 +76,21 @@ abstract class Command extends SymfonyCommand
/** /**
* The exit code of the command. * The exit code of the command.
*
* @var int
*/ */
protected $exitCode = 0; protected int $exitCode = 0;
public function __construct(string $name = null) public function __construct(string $name = null)
{ {
if (! $name && $this->name) { $this->name = $name ?? $this->name;
$name = $this->name;
}
if (! is_int($this->hookFlags)) { if ($this->hookFlags < 0) {
$this->hookFlags = swoole_hook_flags(); $this->hookFlags = swoole_hook_flags();
} }
if (isset($this->signature)) { if (isset($this->signature)) {
$this->configureUsingFluentDefinition(); $this->configureUsingFluentDefinition();
} else { } else {
parent::__construct($name); parent::__construct($this->name);
} }
$this->addEnableDispatcherOption(); $this->addEnableDispatcherOption();
@ -138,18 +116,16 @@ abstract class Command extends SymfonyCommand
/** /**
* Prompt the user for input. * Prompt the user for input.
*
* @param null|mixed $default
*/ */
public function ask(string $question, $default = null) public function ask(string $question, string $default = null)
{ {
return $this->output->ask($question, $default); return $this->output->ask($question, $default);
} }
/** /**
* Prompt the user for input with auto completion. * Prompt the user for input with auto-completion.
* *
* @param null|mixed $default * @param null|bool|float|int|string $default
*/ */
public function anticipate(string $question, array $choices, $default = null) public function anticipate(string $question, array $choices, $default = null)
{ {
@ -157,9 +133,9 @@ abstract class Command extends SymfonyCommand
} }
/** /**
* Prompt the user for input with auto completion. * Prompt the user for input with auto-completion.
* *
* @param null|mixed $default * @param null|bool|float|int|string $default
*/ */
public function askWithCompletion(string $question, array $choices, $default = null) public function askWithCompletion(string $question, array $choices, $default = null)
{ {
@ -184,7 +160,7 @@ abstract class Command extends SymfonyCommand
/** /**
* Give the user a multiple choice from an array of answers. * Give the user a multiple choice from an array of answers.
* @param null|mixed $default * @param mixed $default
*/ */
public function choiceMultiple( public function choiceMultiple(
string $question, string $question,
@ -202,7 +178,7 @@ abstract class Command extends SymfonyCommand
/** /**
* Give the user a single choice from an array of answers. * Give the user a single choice from an array of answers.
* *
* @param null|mixed $default * @param mixed $default
*/ */
public function choice( public function choice(
string $question, string $question,
@ -215,11 +191,8 @@ abstract class Command extends SymfonyCommand
/** /**
* Format input to textual table. * Format input to textual table.
*
* @param mixed $rows
* @param mixed $tableStyle
*/ */
public function table(array $headers, $rows, $tableStyle = 'default', array $columnStyles = []): void public function table(array $headers, array|Arrayable $rows, TableStyle|string $tableStyle = 'default', array $columnStyles = []): void
{ {
$table = new Table($this->output); $table = new Table($this->output);
@ -227,7 +200,7 @@ abstract class Command extends SymfonyCommand
$rows = $rows->toArray(); $rows = $rows->toArray();
} }
$table->setHeaders((array) $headers)->setRows($rows)->setStyle($tableStyle); $table->setHeaders($headers)->setRows($rows)->setStyle($tableStyle);
foreach ($columnStyles as $columnIndex => $columnStyle) { foreach ($columnStyles as $columnIndex => $columnStyle) {
$table->setColumnStyle($columnIndex, $columnStyle); $table->setColumnStyle($columnIndex, $columnStyle);
@ -375,7 +348,7 @@ abstract class Command extends SymfonyCommand
} }
/** /**
* Get all of the context passed to the command. * Get all the context passed to the command.
*/ */
protected function context(): array protected function context(): array
{ {
@ -395,9 +368,9 @@ abstract class Command extends SymfonyCommand
*/ */
protected function specifyParameters(): void protected function specifyParameters(): void
{ {
// We will loop through all of the arguments and options for the command and // We will loop through all the arguments and options for the command and
// set them all on the base command instance. This specifies what can get // set them all on the base command instance. This specifies what can get
// passed into these commands as "parameters" to control the execution. // past into these commands as "parameters" to control the execution.
if (method_exists($this, 'getArguments')) { if (method_exists($this, 'getArguments')) {
foreach ($this->getArguments() ?? [] as $arguments) { foreach ($this->getArguments() ?? [] as $arguments) {
call_user_func_array([$this, 'addArgument'], $arguments); call_user_func_array([$this, 'addArgument'], $arguments);

View File

@ -26,7 +26,8 @@ trait EnableEventDispatcher
public function enableDispatcher(InputInterface $input) public function enableDispatcher(InputInterface $input)
{ {
if ($input->getOption('enable-event-dispatcher')) { if ($input->getOption('enable-event-dispatcher')) {
$this->eventDispatcher = ApplicationContext::getContainer()->get(EventDispatcherInterface::class); $dispatcher = ApplicationContext::getContainer()->get(EventDispatcherInterface::class);
$this->eventDispatcher = $dispatcher instanceof EventDispatcherInterface ? $dispatcher : null;
} }
} }
} }

View File

@ -15,14 +15,8 @@ use Hyperf\Command\Command;
abstract class Event abstract class Event
{ {
/** public function __construct(protected Command $command)
* @var Command
*/
protected $command;
public function __construct(Command $command)
{ {
$this->command = $command;
} }
public function getCommand(): Command public function getCommand(): Command

View File

@ -16,16 +16,9 @@ use Throwable;
class FailToHandle extends Event class FailToHandle extends Event
{ {
/** public function __construct(Command $command, protected Throwable $throwable)
* @var Throwable
*/
protected $throwable;
public function __construct(Command $command, Throwable $throwable)
{ {
parent::__construct($command); parent::__construct($command);
$this->throwable = $throwable;
} }
public function getThrowable(): Throwable public function getThrowable(): Throwable

View File

@ -51,7 +51,7 @@ class Parser
} }
/** /**
* Extract all of the parameters from the tokens. * Extract all the parameters from the tokens.
*/ */
protected static function parameters(array $tokens): array protected static function parameters(array $tokens): array
{ {

View File

@ -15,7 +15,7 @@ use Hyperf\Command\Command;
class SwooleFlagsCommand extends Command class SwooleFlagsCommand extends Command
{ {
protected $hookFlags = SWOOLE_HOOK_CURL | SWOOLE_HOOK_ALL; protected int $hookFlags = SWOOLE_HOOK_CURL | SWOOLE_HOOK_ALL;
public function handle() public function handle()
{ {

View File

@ -48,7 +48,7 @@ class CommandTest extends TestCase
public function testExitCodeWhenThrowException() public function testExitCodeWhenThrowException()
{ {
/** @var FooExceptionCommand $command */ /** @var FooExceptionCommand $command */
$command = new ClassInvoker(new FooExceptionCommand()); $command = new ClassInvoker(new FooExceptionCommand('foo'));
$input = Mockery::mock(InputInterface::class); $input = Mockery::mock(InputInterface::class);
$input->shouldReceive('getOption')->andReturnFalse(); $input->shouldReceive('getOption')->andReturnFalse();
$exitCode = $command->execute($input, Mockery::mock(OutputInterface::class)); $exitCode = $command->execute($input, Mockery::mock(OutputInterface::class));

View File

@ -23,10 +23,8 @@ class FreshCommand extends Command
/** /**
* The console command name. * The console command name.
*
* @var string
*/ */
protected $name = 'migrate:fresh'; protected ?string $name = 'migrate:fresh';
/** /**
* The console command description. * The console command description.

View File

@ -18,10 +18,8 @@ class InstallCommand extends BaseCommand
{ {
/** /**
* The console command name. * The console command name.
*
* @var string
*/ */
protected $name = 'migrate:install'; protected ?string $name = 'migrate:install';
/** /**
* The console command description. * The console command description.

View File

@ -19,7 +19,7 @@ class MigrateCommand extends BaseCommand
{ {
use ConfirmableTrait; use ConfirmableTrait;
protected $name = 'migrate'; protected ?string $name = 'migrate';
/** /**
* The console command description. * The console command description.

View File

@ -21,10 +21,8 @@ class RefreshCommand extends Command
/** /**
* The console command name. * The console command name.
*
* @var string
*/ */
protected $name = 'migrate:refresh'; protected ?string $name = 'migrate:refresh';
/** /**
* The console command description. * The console command description.

View File

@ -21,10 +21,8 @@ class ResetCommand extends BaseCommand
/** /**
* The console command name. * The console command name.
*
* @var string
*/ */
protected $name = 'migrate:reset'; protected ?string $name = 'migrate:reset';
/** /**
* The console command description. * The console command description.

View File

@ -21,10 +21,8 @@ class RollbackCommand extends BaseCommand
/** /**
* The console command name. * The console command name.
*
* @var string
*/ */
protected $name = 'migrate:rollback'; protected ?string $name = 'migrate:rollback';
/** /**
* The console command description. * The console command description.

View File

@ -19,10 +19,8 @@ class StatusCommand extends BaseCommand
{ {
/** /**
* The console command name. * The console command name.
*
* @var string
*/ */
protected $name = 'migrate:status'; protected ?string $name = 'migrate:status';
/** /**
* The console command description. * The console command description.

View File

@ -29,7 +29,6 @@ use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class ModelCommand extends Command class ModelCommand extends Command
{ {
@ -58,16 +57,6 @@ class ModelCommand extends Command
*/ */
protected $printer; protected $printer;
/**
* @var SymfonyStyle
*/
protected $output;
/**
* @var InputInterface
*/
protected $input;
public function __construct(ContainerInterface $container) public function __construct(ContainerInterface $container)
{ {
parent::__construct('gen:model'); parent::__construct('gen:model');

View File

@ -21,10 +21,8 @@ class SeedCommand extends BaseCommand
/** /**
* The console command name. * The console command name.
*
* @var string
*/ */
protected $name = 'db:seed'; protected ?string $name = 'db:seed';
/** /**
* The console command description. * The console command description.

View File

@ -14,10 +14,6 @@ namespace Hyperf\Cache\Annotation;
use Attribute; use Attribute;
use Hyperf\Di\Annotation\AbstractAnnotation; use Hyperf\Di\Annotation\AbstractAnnotation;
/**
* @Annotation
* @Target({"METHOD"})
*/
#[Attribute(Attribute::TARGET_METHOD)] #[Attribute(Attribute::TARGET_METHOD)]
class CacheEvict extends AbstractAnnotation class CacheEvict extends AbstractAnnotation
{ {

View File

@ -14,10 +14,6 @@ namespace Hyperf\Cache\Annotation;
use Attribute; use Attribute;
use Hyperf\Di\Annotation\AbstractAnnotation; use Hyperf\Di\Annotation\AbstractAnnotation;
/**
* @Annotation
* @Target({"METHOD"})
*/
#[Attribute(Attribute::TARGET_METHOD)] #[Attribute(Attribute::TARGET_METHOD)]
class CachePut extends AbstractAnnotation class CachePut extends AbstractAnnotation
{ {

View File

@ -14,10 +14,6 @@ namespace Hyperf\Cache\Annotation;
use Attribute; use Attribute;
use Hyperf\Di\Annotation\AbstractAnnotation; use Hyperf\Di\Annotation\AbstractAnnotation;
/**
* @Annotation
* @Target({"METHOD"})
*/
#[Attribute(Attribute::TARGET_METHOD)] #[Attribute(Attribute::TARGET_METHOD)]
class Cacheable extends AbstractAnnotation class Cacheable extends AbstractAnnotation
{ {

View File

@ -14,10 +14,6 @@ namespace Hyperf\Cache\Annotation;
use Attribute; use Attribute;
use Hyperf\Di\Annotation\AbstractAnnotation; use Hyperf\Di\Annotation\AbstractAnnotation;
/**
* @Annotation
* @Target({"METHOD"})
*/
#[Attribute(Attribute::TARGET_METHOD)] #[Attribute(Attribute::TARGET_METHOD)]
class FailCache extends AbstractAnnotation class FailCache extends AbstractAnnotation
{ {

View File

@ -12,12 +12,9 @@ declare(strict_types=1);
namespace Hyperf\CircuitBreaker\Annotation; namespace Hyperf\CircuitBreaker\Annotation;
use Attribute; use Attribute;
use Doctrine\Common\Annotations\Annotation\Target;
use Hyperf\Di\Annotation\AbstractAnnotation; use Hyperf\Di\Annotation\AbstractAnnotation;
/** /**
* @Annotation
* @Target({"METHOD"})
* @property float $timeout * @property float $timeout
*/ */
#[Attribute(Attribute::TARGET_METHOD)] #[Attribute(Attribute::TARGET_METHOD)]

View File

@ -14,10 +14,6 @@ namespace Hyperf\Command\Annotation;
use Attribute; use Attribute;
use Hyperf\Di\Annotation\AbstractAnnotation; use Hyperf\Di\Annotation\AbstractAnnotation;
/**
* @Annotation
* @Target({"CLASS"})
*/
#[Attribute(Attribute::TARGET_CLASS)] #[Attribute(Attribute::TARGET_CLASS)]
class Command extends AbstractAnnotation class Command extends AbstractAnnotation
{ {

View File

@ -22,10 +22,8 @@ class FlushCommand extends HyperfCommand
{ {
/** /**
* The name and signature of the console command. * The name and signature of the console command.
*
* @var string
*/ */
protected $name = 'scout:flush'; protected ?string $name = 'scout:flush';
/** /**
* The console command description. * The console command description.

View File

@ -22,10 +22,8 @@ class ImportCommand extends Command
{ {
/** /**
* The name and signature of the console command. * The name and signature of the console command.
*
* @var string
*/ */
protected $name = 'scout:import'; protected ?string $name = 'scout:import';
/** /**
* The console command description. * The console command description.

View File

@ -19,7 +19,7 @@ use const OpenApi\UNDEFINED;
class GenCommand extends Command class GenCommand extends Command
{ {
protected $name = 'swagger:gen'; protected ?string $name = 'swagger:gen';
public function handle() public function handle()
{ {

View File

@ -15,7 +15,7 @@ use Hyperf\Command\Command as HyperfCommand;
class ViewPublishCommand extends HyperfCommand class ViewPublishCommand extends HyperfCommand
{ {
protected $signature = 'view:publish {--f|force}'; protected ?string $signature = 'view:publish {--f|force}';
protected $packages = [ protected $packages = [
'hyperf/session', 'hyperf/session',