From 2b939e926e5cf7cb12b7a7961531120b5f200e87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= Date: Sun, 26 Dec 2021 11:12:11 +0800 Subject: [PATCH] Upgrade the minimum php version to `8.0` for `phar` and `paginator`. (#4392) --- src/paginator/src/AbstractPaginator.php | 44 ++++--------- src/paginator/src/LengthAwarePaginator.php | 16 ++--- .../src/Listener/PageResolverListener.php | 8 +-- src/paginator/src/Paginator.php | 6 +- src/paginator/src/UrlWindow.php | 9 +-- src/phar/src/Ast/Ast.php | 10 +-- src/phar/src/BuildCommand.php | 8 +-- src/phar/src/Bundle.php | 11 ++-- src/phar/src/LoggerInterface.php | 4 +- src/phar/src/Package.php | 13 +--- src/phar/src/PharBuilder.php | 65 +++++-------------- src/phar/src/TargetPhar.php | 18 +---- src/scout/tests/Cases/BuilderTest.php | 2 +- 13 files changed, 55 insertions(+), 159 deletions(-) diff --git a/src/paginator/src/AbstractPaginator.php b/src/paginator/src/AbstractPaginator.php index 505ff43a9..501076688 100644 --- a/src/paginator/src/AbstractPaginator.php +++ b/src/paginator/src/AbstractPaginator.php @@ -25,73 +25,53 @@ abstract class AbstractPaginator implements PaginatorInterface /** * The number of links to display on each side of current page link. - * - * @var int */ - public $onEachSide = 3; + public int $onEachSide = 3; /** - * All of the items being paginated. - * - * @var \Hyperf\Utils\Collection + * All the items being paginated. */ - protected $items; + protected Collection $items; /** * The number of items to be shown per page. - * - * @var int */ - protected $perPage; + protected int $perPage; /** * The current page being "viewed". - * - * @var int */ - protected $currentPage; + protected int $currentPage; /** * The base path to assign to all URLs. - * - * @var string */ - protected $path = '/'; + protected string $path = '/'; /** * The query parameters to add to all URLs. - * - * @var array */ - protected $query = []; + protected array $query = []; /** * The URL fragment to add to all URLs. - * - * @var null|string */ - protected $fragment; + protected ?string $fragment = null; /** * The query string variable used to store the page. - * - * @var string */ - protected $pageName = 'page'; + protected string $pageName = 'page'; /** * The current path resolver callback. - * - * @var null|\Closure */ - protected static $currentPathResolver; + protected static ?Closure $currentPathResolver = null; /** * The current page resolver callback. - * - * @var null|\Closure */ - protected static $currentPageResolver; + protected static ?Closure $currentPageResolver = null; /** * Make dynamic calls into the collection. @@ -106,7 +86,7 @@ abstract class AbstractPaginator implements PaginatorInterface */ public function __toString(): string { - return (string) $this->render(); + return $this->render(); } /** diff --git a/src/paginator/src/LengthAwarePaginator.php b/src/paginator/src/LengthAwarePaginator.php index 97ff30b04..749c54351 100644 --- a/src/paginator/src/LengthAwarePaginator.php +++ b/src/paginator/src/LengthAwarePaginator.php @@ -24,28 +24,20 @@ class LengthAwarePaginator extends AbstractPaginator implements Arrayable, Array { /** * The total number of items before slicing. - * - * @var int */ - protected $total; + protected int $total; /** * The last available page. - * - * @var int */ - protected $lastPage; + protected int $lastPage; /** * Create a new paginator instance. * - * @param mixed $items - * @param int $total - * @param int $perPage - * @param null|int $currentPage * @param array $options (path, query, fragment, pageName) */ - public function __construct($items, $total, $perPage, $currentPage = 1, array $options = []) + public function __construct(mixed $items, int $total, int $perPage, ?int $currentPage = 1, array $options = []) { foreach ($options as $key => $value) { $this->{$key} = $value; @@ -163,7 +155,7 @@ class LengthAwarePaginator extends AbstractPaginator implements Arrayable, Array { $currentPage = $currentPage ?: static::resolveCurrentPage($pageName); - return $this->isValidPageNumber($currentPage) ? (int) $currentPage : 1; + return $this->isValidPageNumber($currentPage) ? $currentPage : 1; } /** diff --git a/src/paginator/src/Listener/PageResolverListener.php b/src/paginator/src/Listener/PageResolverListener.php index ca923ef74..c0623d489 100644 --- a/src/paginator/src/Listener/PageResolverListener.php +++ b/src/paginator/src/Listener/PageResolverListener.php @@ -65,13 +65,7 @@ class PageResolverListener implements ListenerInterface } $container = ApplicationContext::getContainer(); - $url = $container->get(RequestInterface::class)->url(); - - if (filter_var($url, FILTER_VALIDATE_URL) !== false) { - return $url; - } - - return $url; + return $container->get(RequestInterface::class)->url(); }); } } diff --git a/src/paginator/src/Paginator.php b/src/paginator/src/Paginator.php index 3f98cc24a..7f6ee6e83 100644 --- a/src/paginator/src/Paginator.php +++ b/src/paginator/src/Paginator.php @@ -23,10 +23,8 @@ class Paginator extends AbstractPaginator implements Arrayable, ArrayAccess, Cou { /** * Determine if there are more items in the data source. - * - * @return bool */ - protected $hasMore; + protected bool $hasMore; /** * Create a new paginator instance. @@ -133,7 +131,7 @@ class Paginator extends AbstractPaginator implements Arrayable, ArrayAccess, Cou { $currentPage = $currentPage ?: static::resolveCurrentPage(); - return $this->isValidPageNumber($currentPage) ? (int) $currentPage : 1; + return $this->isValidPageNumber($currentPage) ? $currentPage : 1; } /** diff --git a/src/paginator/src/UrlWindow.php b/src/paginator/src/UrlWindow.php index 592e780ab..5e52a6929 100644 --- a/src/paginator/src/UrlWindow.php +++ b/src/paginator/src/UrlWindow.php @@ -16,15 +16,10 @@ use Hyperf\Contract\LengthAwarePaginatorInterface; class UrlWindow { /** - * The paginator implementation. - * - * @var AbstractPaginator|LengthAwarePaginatorInterface + * @param LengthAwarePaginatorInterface&AbstractPaginator $paginator */ - protected $paginator; - - public function __construct(LengthAwarePaginatorInterface $paginator) + public function __construct(protected LengthAwarePaginatorInterface $paginator) { - $this->paginator = $paginator; } /** diff --git a/src/phar/src/Ast/Ast.php b/src/phar/src/Ast/Ast.php index 8e2f63ae1..13ab64f63 100644 --- a/src/phar/src/Ast/Ast.php +++ b/src/phar/src/Ast/Ast.php @@ -19,15 +19,9 @@ use PhpParser\PrettyPrinterAbstract; class Ast { - /** - * @var Parser - */ - private $astParser; + private Parser $astParser; - /** - * @var PrettyPrinterAbstract - */ - private $printer; + private PrettyPrinterAbstract $printer; public function __construct() { diff --git a/src/phar/src/BuildCommand.php b/src/phar/src/BuildCommand.php index 8a29f4e66..bf8009819 100644 --- a/src/phar/src/BuildCommand.php +++ b/src/phar/src/BuildCommand.php @@ -20,15 +20,9 @@ use UnexpectedValueException; class BuildCommand extends HyperfCommand { - /** - * @var ContainerInterface - */ - protected $container; - - public function __construct(ContainerInterface $container) + public function __construct(protected ContainerInterface $container) { parent::__construct('phar:build'); - $this->container = $container; } public function configure() diff --git a/src/phar/src/Bundle.php b/src/phar/src/Bundle.php index 4aae36fe7..ac8574f87 100644 --- a/src/phar/src/Bundle.php +++ b/src/phar/src/Bundle.php @@ -22,13 +22,12 @@ class Bundle implements IteratorAggregate /** * @var Finder[]|string[] */ - private $resources = []; + private array $resources = []; /** * Add a file to the resource bundle. - * @return $this */ - public function addFile(string $file) + public function addFile(string $file): static { $this->resources[] = $file; return $this; @@ -36,18 +35,16 @@ class Bundle implements IteratorAggregate /** * @param string[] $dirs - * @return $this */ - public function addDirs(array $dirs) + public function addDirs(array $dirs): static { return $this->addFinder((new Finder())->files()->ignoreVCS(true)->in($dirs)); } /** * Add a directory package to a resource package. - * @return $this */ - public function addFinder(Finder $dir) + public function addFinder(Finder $dir): static { $this->resources[] = $dir; return $this; diff --git a/src/phar/src/LoggerInterface.php b/src/phar/src/LoggerInterface.php index d96f36baa..650caaa4f 100644 --- a/src/phar/src/LoggerInterface.php +++ b/src/phar/src/LoggerInterface.php @@ -11,6 +11,8 @@ declare(strict_types=1); */ namespace Hyperf\Phar; -interface LoggerInterface extends \Psr\Log\LoggerInterface +use Psr\Log; + +interface LoggerInterface extends Log\LoggerInterface { } diff --git a/src/phar/src/Package.php b/src/phar/src/Package.php index 89c29ff36..055fed31d 100644 --- a/src/phar/src/Package.php +++ b/src/phar/src/Package.php @@ -15,19 +15,10 @@ use Symfony\Component\Finder\Finder; class Package { - /** - * @var array - */ - protected $package; + protected string $directory; - /** - * @var string - */ - protected $directory; - - public function __construct(array $package, string $directory) + public function __construct(protected array $package, string $directory) { - $this->package = $package; $this->directory = rtrim($directory, '/') . '/'; } diff --git a/src/phar/src/PharBuilder.php b/src/phar/src/PharBuilder.php index 53188e7fb..9a714285e 100644 --- a/src/phar/src/PharBuilder.php +++ b/src/phar/src/PharBuilder.php @@ -25,39 +25,18 @@ use UnexpectedValueException; class PharBuilder { - /** - * @var LoggerInterface - */ - protected $logger; + private Package $package; - /** - * @var Package - */ - private $package; + private null|string|TargetPhar $target = null; - /** - * @var null|string|TargetPhar - */ - private $target; + private array $mount = []; - /** - * @var array - */ - private $mount = []; + private ?string $version = null; - /** - * @var string - */ - private $version; + private ?string $main = null; - /** - * @var string - */ - private $main; - - public function __construct(string $path, LoggerInterface $logger) + public function __construct(string $path, private LoggerInterface $logger) { - $this->logger = $logger; $this->package = new Package($this->loadJson($path), dirname(realpath($path))); } @@ -78,10 +57,9 @@ class PharBuilder /** * Set the Phar package name. - * @param string|TargetPhar $target * @return $this */ - public function setTarget($target) + public function setTarget(string|TargetPhar $target): static { if (is_dir($target)) { $this->target = null; @@ -91,10 +69,7 @@ class PharBuilder return $this; } - /** - * @return $this - */ - public function setVersion(string $version) + public function setVersion(string $version): static { $this->version = $version; return $this; @@ -123,9 +98,8 @@ class PharBuilder /** * Set the default startup file. - * @return $this */ - public function setMain(string $main) + public function setMain(string $main): static { $this->main = $main; return $this; @@ -139,10 +113,7 @@ class PharBuilder return $this->package; } - /** - * @return $this - */ - public function setMount(array $mount = []) + public function setMount(array $mount = []): static { foreach ($mount as $item) { $items = explode(':', $item); @@ -171,13 +142,13 @@ class PharBuilder if (is_file($vendorPath . 'composer/installed.json')) { $installed = $this->loadJson($vendorPath . 'composer/installed.json'); $installedPackages = $installed; - // Adapte Composer 2.0 + // Adapt Composer 2.0 if (isset($installed['packages'])) { $installedPackages = $installed['packages']; } // Package all of these dependent components into the packages foreach ($installedPackages as $package) { - // support custom install path + // support custom installation path $dir = 'composer/' . ($package['install-path'] ?? '../' . $package['name']) . '/'; if (isset($package['target-dir'])) { @@ -192,7 +163,7 @@ class PharBuilder } /** - * Gets the canonicalize path, like realpath. + * Gets the canonical path, like realpath. * @param mixed $address */ public function canonicalize($address) @@ -200,8 +171,8 @@ class PharBuilder $address = explode('/', $address); $keys = array_keys($address, '..'); - foreach ($keys as $keypos => $key) { - array_splice($address, $key - ($keypos * 2 + 1), 2); + foreach ($keys as $pos => $key) { + array_splice($address, $key - ($pos * 2 + 1), 2); } $address = implode('/', $address); @@ -214,7 +185,7 @@ class PharBuilder public function getPathLocalToBase(string $path): ?string { $root = $this->package->getDirectory(); - if (strpos($path, $root) !== 0) { + if (! str_starts_with($path, $root)) { throw new UnexpectedValueException('Path "' . $path . '" is not within base project path "' . $root . '"'); } $basePath = substr($path, strlen($root)); @@ -293,7 +264,7 @@ EOD; $targetPhar->addBundle($this->package->bundle($finder)); - // Force to turn on ScanCacheable. + // Force turning on ScanCacheable. $this->enableScanCacheable($targetPhar); // Load the Runtime folder separately @@ -337,7 +308,7 @@ EOD; // Add composer autoload files. $targetPhar->buildFromIterator(new GlobIterator($vendorPath . 'composer/*.*', FilesystemIterator::KEY_AS_FILENAME)); - // Add composer depenedencies. + // Add composer dependencies. foreach ($this->getPackagesDependencies() as $package) { $this->logger->info('Adding dependency "' . $package->getName() . '" from "' . $this->getPathLocalToBase($package->getDirectory()) . '"'); // support package symlink diff --git a/src/phar/src/TargetPhar.php b/src/phar/src/TargetPhar.php index 1f95acb4a..ff730513c 100644 --- a/src/phar/src/TargetPhar.php +++ b/src/phar/src/TargetPhar.php @@ -17,21 +17,9 @@ use Traversable; class TargetPhar { - /** - * @var Phar - */ - private $phar; - - /** - * @var PharBuilder - */ - private $pharBuilder; - - public function __construct(Phar $phar, PharBuilder $pharBuilder) + public function __construct(private Phar $phar, private PharBuilder $pharBuilder) { $phar->startBuffering(); - $this->phar = $phar; - $this->pharBuilder = $pharBuilder; } public function __toString(): string @@ -43,7 +31,7 @@ class TargetPhar /** * Start writing the Phar package. */ - public function stopBuffering() + public function stopBuffering(): void { $this->phar->stopBuffering(); } @@ -51,7 +39,7 @@ class TargetPhar /** * Add a resource bundle to the Phar package. */ - public function addBundle(Bundle $bundle) + public function addBundle(Bundle $bundle): void { /** @var Finder|string $resource */ foreach ($bundle as $resource) { diff --git a/src/scout/tests/Cases/BuilderTest.php b/src/scout/tests/Cases/BuilderTest.php index 87cf32b67..e861eb231 100644 --- a/src/scout/tests/Cases/BuilderTest.php +++ b/src/scout/tests/Cases/BuilderTest.php @@ -44,7 +44,7 @@ class BuilderTest extends TestCase $model->shouldReceive('searchableUsing')->andReturn($engine = m::mock()); $engine->shouldReceive('paginate'); $engine->shouldReceive('map')->andReturn($results = Collection::make([new stdClass()])); - $engine->shouldReceive('getTotalCount'); + $engine->shouldReceive('getTotalCount')->andReturn(100); $model->shouldReceive('newCollection')->andReturn($results); $builder->paginate(); }