DX: fix phpstan errors

This commit is contained in:
2023-02-04 00:50:58 +01:00
parent a01cc23c14
commit 856eea37ee
16 changed files with 94 additions and 120 deletions

View File

@@ -21,37 +21,36 @@ class Page implements PageInterface
/**
* the number of item per page.
*
* @var int
*/
protected $itemPerPage;
protected int $itemPerPage;
/**
* the number of the current page.
*
* @var int
*/
protected $number;
protected int $number;
/**
* The route for the current page.
*
* @var string
*/
protected $route;
protected string $route;
/**
* Parameters for the route to the current page.
*
* @var array
*/
protected $routeParameters;
protected array $routeParameters;
/**
* The number of items in the whole iteration.
*
* @var int
*/
protected $totalItems;
protected int $totalItems;
/**
* @var UrlGeneratorInterface
@@ -59,12 +58,12 @@ class Page implements PageInterface
protected $urlGenerator;
public function __construct(
$number,
$itemPerPage,
int $number,
int $itemPerPage,
UrlGeneratorInterface $urlGenerator,
$route,
string $route,
array $routeParameters,
$totalItems
int $totalItems
) {
$this->urlGenerator = $urlGenerator;
$this->number = $number;
@@ -74,24 +73,24 @@ class Page implements PageInterface
$this->totalItems = $totalItems;
}
public function generateUrl()
public function generateUrl(): string
{
return $this->urlGenerator->generate($this->route, $this->routeParameters);
}
public function getFirstItemNumber()
public function getFirstItemNumber(): int
{
return ($this->number - 1) * $this->itemPerPage;
}
public function getLastItemNumber()
public function getLastItemNumber(): int
{
$last = $this->number * $this->itemPerPage - 1;
return $last < $this->totalItems ? $last : $this->totalItems;
}
public function getNumber()
public function getNumber(): int
{
return $this->number;
}

View File

@@ -27,27 +27,27 @@ class PageGenerator implements Iterator
$this->paginator = $paginator;
}
public function current()
public function current(): Page
{
return $this->paginator->getPage($current);
return $this->paginator->getPage($this->current);
}
public function key()
public function key(): int
{
return $this->current;
}
public function next()
public function next(): void
{
++$this->current;
}
public function rewind()
public function rewind(): void
{
$this->current = 1;
}
public function valid()
public function valid(): bool
{
return 0 < $this->current
&& $this->paginator->countPages() >= $this->current;

View File

@@ -26,21 +26,21 @@ class Paginator implements PaginatorInterface
*
* @var int
*/
protected $currentPageNumber;
protected int $currentPageNumber;
/**
* the number of items on a single page.
*
* @var int
*/
protected $itemPerPage;
protected int $itemPerPage;
/**
* the key in the GET parameter to indicate the number of item per page.
*
* @var string
*/
protected $itemPerPageKey;
protected string $itemPerPageKey;
/**
* the key in the GET parameter to indicate the page number in
@@ -48,45 +48,45 @@ class Paginator implements PaginatorInterface
*
* @var string
*/
protected $pageKey;
protected string $pageKey;
/**
* the route of the pages.
*
* @var string
*/
protected $route;
protected string $route;
/**
* the parameters of the route.
*
* @var string[]
*/
protected $routeParameters;
protected array $routeParameters;
/**
* The number of total items.
*
* @var int
*/
protected $totalItems;
protected int $totalItems;
/**
* the generator for url.
*
* @var UrlGeneratorInterface
*/
protected $urlGenerator;
protected UrlGeneratorInterface $urlGenerator;
public function __construct(
$totalItems,
$itemPerPage,
$currentPageNumber,
$route,
int $totalItems,
int $itemPerPage,
int $currentPageNumber,
string $route,
array $routeParameters,
UrlGeneratorInterface $urlGenerator,
$pageKey,
$itemPerPageKey
string $pageKey,
string $itemPerPageKey
) {
$this->totalItems = $totalItems;
$this->itemPerPage = $itemPerPage;
@@ -98,12 +98,12 @@ class Paginator implements PaginatorInterface
$this->itemPerPageKey = $itemPerPageKey;
}
public function count()
public function count(): int
{
return $this->countPages();
}
public function countPages()
public function countPages(): int
{
if (0 === $this->itemPerPage) {
return 1;
@@ -122,20 +122,17 @@ class Paginator implements PaginatorInterface
return 0 === $nb ? 1 : (int) $nb;
}
/**
* @return \Chill\MainBundle\Pagination\Page
*/
public function getCurrentPage()
public function getCurrentPage(): Page
{
return $this->getPage($this->currentPageNumber);
}
public function getCurrentPageFirstItemNumber()
public function getCurrentPageFirstItemNumber(): int
{
return $this->getCurrentPage()->getFirstItemNumber();
}
public function getItemsPerPage()
public function getItemsPerPage(): int
{
return $this->itemPerPage;
}
@@ -145,7 +142,7 @@ class Paginator implements PaginatorInterface
*
* @return \Chill\MainBundle\Pagination\Page
*/
public function getNextPage()
public function getNextPage(): Page
{
if (!$this->hasNextPage()) {
throw new RuntimeException('this page has no next page');
@@ -155,11 +152,10 @@ class Paginator implements PaginatorInterface
}
/**
* @param type $number
*
* @return \Chill\MainBundle\Pagination\Page
*/
public function getPage($number)
public function getPage(int $number): Page
{
if (!$this->hasPage($number)) {
throw new RuntimeException("The page with number {$number} does not "
@@ -179,7 +175,7 @@ class Paginator implements PaginatorInterface
);
}
public function getPagesGenerator()
public function getPagesGenerator(): iterable
{
for ($i = 1; $this->countPages() >= $i; ++$i) {
yield $this->getPage($i);
@@ -191,7 +187,7 @@ class Paginator implements PaginatorInterface
*
* @return \Chill\MainBundle\Pagination\Page
*/
public function getPreviousPage()
public function getPreviousPage(): PageInterface
{
if (!$this->hasPreviousPage()) {
throw new RuntimeException('this page has no previous page');
@@ -200,7 +196,7 @@ class Paginator implements PaginatorInterface
return $this->getPage($this->currentPageNumber - 1);
}
public function getTotalItems()
public function getTotalItems(): int
{
return $this->totalItems;
}
@@ -208,12 +204,12 @@ class Paginator implements PaginatorInterface
/**
* @return bool
*/
public function hasNextPage()
public function hasNextPage(): bool
{
return $this->hasPage($this->currentPageNumber + 1);
}
public function hasPage($number)
public function hasPage($number): bool
{
if (0 === $this->totalItems) {
return 1 === $number;
@@ -226,18 +222,18 @@ class Paginator implements PaginatorInterface
/**
* @return bool
*/
public function hasPreviousPage()
public function hasPreviousPage(): bool
{
return $this->hasPage($this->currentPageNumber - 1);
}
public function isCurrentPage(PageInterface $page)
public function isCurrentPage(PageInterface $page): bool
{
return $page->getNumber() === $this->currentPageNumber;
}
public function setItemsPerPage($itemPerPage)
public function setItemsPerPage(int $itemsPerPage)
{
$this->itemPerPage = $itemPerPage;
$this->itemPerPage = $itemsPerPage;
}
}

View File

@@ -32,26 +32,26 @@ interface PaginatorInterface extends Countable
*
* @return int
*/
public function countPages();
public function countPages(): int;
/**
* get the current page.
*
* @return PageInterface
*/
public function getCurrentPage();
public function getCurrentPage(): PageInterface;
/**
* get the first result for the current page.
*
* @return int
*/
public function getCurrentPageFirstItemNumber();
public function getCurrentPageFirstItemNumber(): int;
/*
* get the number of items per page
*/
public function getItemsPerPage();
public function getItemsPerPage(): int;
/**
* get the next page.
@@ -60,7 +60,7 @@ interface PaginatorInterface extends Countable
*
* @return PageInterface
*/
public function getNextPage();
public function getNextPage(): PageInterface;
/**
* get page by his number.
@@ -69,14 +69,14 @@ interface PaginatorInterface extends Countable
*
* @throws RuntimeException if the pagination has no page with specified number
*/
public function getPage($number);
public function getPage(int $number): PageInterface;
/**
* get a generator to generate pages.
*
* @return Generator which return PageInterface elements
*/
public function getPagesGenerator();
public function getPagesGenerator(): iterable;
/**
* get the previous page.
@@ -85,35 +85,34 @@ interface PaginatorInterface extends Countable
*
* @return PageInterface
*/
public function getPreviousPage();
public function getPreviousPage(): PageInterface;
/**
* get the number of results for this paginator.
*
* @return int
*/
public function getTotalItems();
public function getTotalItems(): int;
/**
* check if the current page has a next page.
*
* @return bool
*/
public function hasNextPage();
public function hasNextPage(): bool;
/**
* check if the page with the given number exists.
*
* @param int $number
*/
public function hasPage($number);
public function hasPage($number): bool;
/**
* check if the current page has a page before.
*
* @return bool
*/
public function hasPreviousPage();
public function hasPreviousPage(): bool;
/**
* check if the given page is the current page.
@@ -122,10 +121,10 @@ interface PaginatorInterface extends Countable
*
* @return bool
*/
public function isCurrentPage(PageInterface $page);
public function isCurrentPage(PageInterface $page): bool;
/*
* set the number of items per page
*/
public function setItemsPerPage($itemsPerPage);
public function setItemsPerPage(int $itemsPerPage);
}

View File

@@ -27,7 +27,7 @@ class Counter implements JsonSerializable
return $this->counter;
}
public function jsonSerialize()
public function jsonSerialize(): array
{
return ['count' => $this->counter];
}

View File

@@ -70,23 +70,23 @@ class DelegatedBlockRenderingEvent extends Event implements ArrayAccess
return $this->content;
}
public function offsetExists($offset)
public function offsetExists($offset): bool
{
return isset($this->context[$offset]);
}
public function offsetGet($offset)
public function offsetGet($offset): mixed
{
return $this->context[$offset];
}
public function offsetSet($offset, $value)
public function offsetSet($offset, $value): void
{
throw new RuntimeException('The event context is read-only, you are not '
. 'allowed to update it.');
}
public function offsetUnset($offset)
public function offsetUnset($offset): void
{
throw new RuntimeException('The event context is read-only, you are not '
. 'allowed to update it.');