Skip to content

Commit

Permalink
Increase phpstan level (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
garak authored Mar 25, 2024
1 parent 51f7053 commit 9b1dcfb
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/.git* export-ignore
/.php-cs-fixer.* export-ignore
/*.md export-ignore
/phpstan.* export-ignore
/phpstan* export-ignore
/phpunit.xml.dist export-ignore
/docs export-ignore
/tests export-ignore
32 changes: 32 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
parameters:
ignoreErrors:
-
message: "#^Method Knp\\\\Component\\\\Pager\\\\Event\\\\Subscriber\\\\Sortable\\\\ArraySubscriber\\:\\:sortFunction\\(\\) has parameter \\$object1 with no value type specified in iterable type array\\.$#"
count: 1
path: src/Knp/Component/Pager/Event/Subscriber/Sortable/ArraySubscriber.php

-
message: "#^Method Knp\\\\Component\\\\Pager\\\\Event\\\\Subscriber\\\\Sortable\\\\ArraySubscriber\\:\\:sortFunction\\(\\) has parameter \\$object2 with no value type specified in iterable type array\\.$#"
count: 1
path: src/Knp/Component/Pager/Event/Subscriber/Sortable/ArraySubscriber.php

-
message: "#^Class Knp\\\\Component\\\\Pager\\\\Pagination\\\\AbstractPagination implements generic interface Iterator but does not specify its types\\: TKey, TValue$#"
count: 1
path: src/Knp/Component/Pager/Pagination/AbstractPagination.php

-
message: "#^PHPDoc tag @var for property Knp\\\\Component\\\\Pager\\\\Pagination\\\\AbstractPagination\\:\\:\\$items with type iterable\\<int, mixed\\>\\|object is not subtype of native type iterable\\.$#"
count: 1
path: src/Knp/Component/Pager/Pagination/AbstractPagination.php

-
message: "#^Property Knp\\\\Component\\\\Pager\\\\Pagination\\\\AbstractPagination\\:\\:\\$items type has no value type specified in iterable type iterable\\.$#"
count: 1
path: src/Knp/Component/Pager/Pagination/AbstractPagination.php

-
message: "#^Interface Knp\\\\Component\\\\Pager\\\\Pagination\\\\PaginationInterface extends generic interface ArrayAccess but does not specify its types\\: TKey, TValue$#"
count: 1
path: src/Knp/Component/Pager/Pagination/PaginationInterface.php

4 changes: 3 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
parameters:
level: 5
level: 6
paths:
- src
includes:
- phpstan-baseline.neon
7 changes: 7 additions & 0 deletions src/Knp/Component/Pager/Event/AfterEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@
*/
final class AfterEvent extends Event
{
/** @var PaginationInterface<int, mixed> */
private PaginationInterface $pagination;

/**
* @param PaginationInterface<int, mixed> $paginationView
*/
public function __construct(PaginationInterface $paginationView)
{
$this->pagination = $paginationView;
}

/**
* @return PaginationInterface<int, mixed>
*/
public function getPaginationView(): PaginationInterface
{
return $this->pagination;
Expand Down
9 changes: 8 additions & 1 deletion src/Knp/Component/Pager/Event/ItemsEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class ItemsEvent extends Event
public mixed $target = null;

/**
* List of options
* @var array<string, mixed>
*/
public array $options;

Expand All @@ -31,6 +31,10 @@ final class ItemsEvent extends Event

private int $offset;
private int $limit;

/**
* @var array<string, mixed>
*/
private array $customPaginationParams = [];

public function __construct(int $offset, int $limit)
Expand All @@ -44,6 +48,9 @@ public function setCustomPaginationParameter(string $name, mixed $value): void
$this->customPaginationParams[$name] = $value;
}

/**
* @return array<string, mixed>
*/
public function getCustomPaginationParameters(): array
{
return $this->customPaginationParams;
Expand Down
11 changes: 10 additions & 1 deletion src/Knp/Component/Pager/Event/PaginationEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,26 @@ final class PaginationEvent extends Event
public mixed $target = null;

/**
* List of options
* @var array<string, mixed>
*/
public array $options;

/**
* @var PaginationInterface<int, mixed>
*/
private PaginationInterface $pagination;

/**
* @param PaginationInterface<int, mixed> $pagination
*/
public function setPagination(PaginationInterface $pagination): void
{
$this->pagination = $pagination;
}

/**
* @return PaginationInterface<int, mixed>
*/
public function getPagination(): PaginationInterface
{
return $this->pagination;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public function getPaginationCount(): int
return call_user_func($this->count);
}

/**
* @return array<int, mixed>
*/
public function getPaginationItems(int $offset, int $limit): array
{
return call_user_func($this->items, $offset, $limit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public function items(ItemsEvent $event): void
]);
}

/**
* @param array<string, mixed> $options
*/
private function getSortDirection(array $options): string
{
if (!$this->argumentAccess->has($options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME])) {
Expand All @@ -83,7 +86,7 @@ private function getSortDirection(array $options): string
return 'desc';
}

private function proxySortFunction(&$target, $sortField, $sortDirection): bool
private function proxySortFunction(mixed &$target, string $sortField, string $sortDirection): bool
{
$this->currentSortingField = $sortField;
$this->sortDirection = $sortDirection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static function getSubscribedEvents(): array
];
}

private function getSortDirection($event): string
private function getSortDirection(ItemsEvent $event): string
{
$sortDir = $event->options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME];

Expand Down
3 changes: 3 additions & 0 deletions src/Knp/Component/Pager/Pagination/AbstractPagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ abstract class AbstractPagination implements Iterator, PaginationInterface
{
protected ?int $currentPageNumber = null;
protected ?int $numItemsPerPage = null;
/** @var iterable<int, mixed>|object */
protected iterable $items = [];
protected ?int $totalCount = null;
/** @var array<string, mixed> */
protected ?array $paginatorOptions = null;
/** @var array<string, mixed> */
protected ?array $customParameters = null;

public function rewind(): void
Expand Down
4 changes: 2 additions & 2 deletions src/Knp/Component/Pager/Pagination/PaginationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function setItems(iterable $items): void;
public function getItems(): iterable;

/**
* @param array $options
* @param array<string, mixed> $options
*/
public function setPaginatorOptions(array $options): void;

Expand All @@ -60,7 +60,7 @@ public function setPaginatorOptions(array $options): void;
public function getPaginatorOption(string $name): mixed;

/**
* @param array $parameters
* @param array<string, mixed> $parameters
*/
public function setCustomParameters(array $parameters): void;

Expand Down
5 changes: 4 additions & 1 deletion src/Knp/Component/Pager/Pagination/SlidingPagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @template-extends AbstractPagination<TKey, TValue>
*/
final class SlidingPagination extends AbstractPagination
final class SlidingPagination extends AbstractPagination implements \Stringable
{
/**
* Pagination page range
Expand Down Expand Up @@ -45,6 +45,9 @@ public function __toString(): string
return $output;
}

/**
* @return array<string, mixed>
*/
public function getPaginationData(): array
{
$pageCount = (int) ceil($this->totalCount / $this->numItemsPerPage);
Expand Down
12 changes: 9 additions & 3 deletions src/Knp/Component/Pager/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,27 @@ public function __construct(EventDispatcherInterface $eventDispatcher, ArgumentA
}

/**
* Override the default paginator options
* to be reused for paginations
* Override the default paginator options to be reused for paginations
*
* @param array<string, string|int|bool> $options
*/
public function setDefaultPaginatorOptions(array $options): void
{
$this->defaultOptions = \array_merge($this->defaultOptions, $options);
}

/**
* @param array<string, mixed> $options
*
* @return PaginationInterface<int, mixed>
*/
public function paginate($target, int $page = 1, int $limit = null, array $options = []): PaginationInterface
{
if ($page <= 0) {
throw PageNumberInvalidException::create($page);
}

$limit = $limit ?? $this->defaultOptions[self::DEFAULT_LIMIT];
$limit = $limit ?? (int) $this->defaultOptions[self::DEFAULT_LIMIT];
if ($limit <= 0) {
throw PageLimitInvalidException::create($limit);
}
Expand Down
16 changes: 9 additions & 7 deletions src/Knp/Component/Pager/PaginatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ interface PaginatorInterface
* pagination object (might be aggregated helper object)
* responsible for the pagination result representation
*
* @param mixed $target anything what needs to be paginated
* @param int $page page number, starting from 1
* @param int|null $limit number of items per page
* @param array $options less used options:
* bool $distinct default true for distinction of results
* string $alias pagination alias, default none
* array $sortFieldAllowList sortable allow list for target fields being paginated
* @param mixed $target anything what needs to be paginated
* @param int $page page number, starting from 1
* @param int|null $limit number of items per page
* @param array<string, mixed> $options less used options:
* bool $distinct default true for distinction of results
* string $alias pagination alias, default none
* array $sortFieldAllowList sortable allow list for target fields being paginated
*
* @return PaginationInterface<int, mixed>
*/
public function paginate(mixed $target, int $page = 1, int $limit = null, array $options = []): PaginationInterface;
}
8 changes: 1 addition & 7 deletions tests/Test/Fixture/Document/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,14 @@ final class Image
private $file;

/**
* Set file.
*
* @param int|string $file
*/
public function setFile($file): self
public function setFile($file): void
{
$this->file = $file;

return $this;
}

/**
* Get file.
*
* @return int|string
*/
public function getFile()
Expand Down

0 comments on commit 9b1dcfb

Please sign in to comment.