Upgrade the minimum php version to 8.0 for phar and paginator. (#4392)

This commit is contained in:
李铭昕 2021-12-26 11:12:11 +08:00 committed by GitHub
parent a18db95c62
commit 2b939e926e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 55 additions and 159 deletions

View File

@ -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();
}
/**

View File

@ -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;
}
/**

View File

@ -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();
});
}
}

View File

@ -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;
}
/**

View File

@ -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;
}
/**

View File

@ -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()
{

View File

@ -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()

View File

@ -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;

View File

@ -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
{
}

View File

@ -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, '/') . '/';
}

View File

@ -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

View File

@ -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) {

View File

@ -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();
}