Skip to content

Commit

Permalink
♻️ make PHPStan happy
Browse files Browse the repository at this point in the history
  • Loading branch information
bnomei committed Feb 14, 2025
1 parent e04dfef commit 4fecf0b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
14 changes: 8 additions & 6 deletions classes/APIRecords.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ class APIRecords

private string $cacheKey;

public function __construct(protected ?Page $page = null)
public function __construct(protected ?APIRecordsPage $page = null)
{
$this->records = null;

$this->endpointUrl = $this->config('url', resolveClosures: true);
$this->endpointParams = $this->config('params', [], true);
$this->recordsDataQuery = $this->config('query', resolveClosures: true);
$this->recordsDataMap = $this->config('map'); // closure resolving here would break the mapping by closure
$this->recordsCacheExpire = $this->config('expire', intval(option('bnomei.api-pages.expire')), true);
$this->recordsCacheExpire = $this->config('expire', intval(option('bnomei.api-pages.expire')), true); // @phpstan-ignore-line
$this->recordTemplate = $this->config('template');
$this->recordModel = $this->config('model');
$this->cacheKey = md5($this->endpointUrl.json_encode($this->endpointParams));
$this->cacheKey = md5(implode('', [$this->endpointUrl, json_encode($this->endpointParams)]));
}

public function page(): ?Page
Expand All @@ -53,7 +53,8 @@ public function config(string $key, mixed $default = null, bool $resolveClosures
$result = null;

// try from model itself
if ($config = $this->page?->recordsConfig()) {
$config = $this->page?->recordsConfig();
if (! empty($config)) {
if (is_array($config)) {
$result = A::get($config, $key);
} elseif ($config instanceof Field) {
Expand Down Expand Up @@ -101,7 +102,8 @@ public function toArray(): array

// handle the data like Kirby's OptionApi does to allow for the entry query with sorting etc.
$data = Nest::create($data);
$data = Query::factory($this->recordsDataQuery)->resolve($data)?->toArray() ?? [];
$result = Query::factory($this->recordsDataQuery)->resolve($data);
$data = $result?->toArray() ?? []; // @phpstan-ignore-line

$records = array_map(function (array $data) use ($map) {
// create the record object which resolves data with the map
Expand Down Expand Up @@ -143,7 +145,7 @@ public function fetch(): array

if ($remote->code() >= 200 && $remote->code() <= 300) {
$json = $remote->json() ?? [];
if ($expire >= 0) {
if (! is_null($expire) && $expire >= 0) {
kirby()->cache('bnomei.api-pages')->set($this->cacheKey, $json, $expire);
}

Expand Down
12 changes: 11 additions & 1 deletion classes/APIRecordsPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@

use Kirby\Cms\Page;
use Kirby\Cms\Pages;
use Kirby\Content\Field;

class APIRecordsPage extends Page
{
protected function records(): APIRecords
public function recordsConfig(): Field|array
{
// if you do not use the blueprint to config your API records
// you can return the array here. that is helpful if you want
// to use environment variable or other dynamic options.

return [];
}

public function records(): APIRecords
{
return new APIRecords($this);
}
Expand Down
3 changes: 1 addition & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ parameters:
- tests

ignoreErrors:
# - '#Undefined variable: \$this#'
- '#Call to an undefined method Kirby\\Cms\\Ingredients::#'
- '#Property Bnomei\\APIRecords::#'
-
identifier: missingType.iterableValue

0 comments on commit 4fecf0b

Please sign in to comment.