Skip to content

Commit

Permalink
♻️ phpstan and related tests and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bnomei committed Jul 22, 2024
1 parent 042346a commit 6fb4c68
Show file tree
Hide file tree
Showing 9 changed files with 286 additions and 44 deletions.
14 changes: 9 additions & 5 deletions classes/Khulan.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Kirby\Cms\Collection;
use Kirby\Cms\File;
use Kirby\Cms\Files;
use Kirby\Cms\Language;
use Kirby\Cms\Page;
use Kirby\Cms\Pages;
use Kirby\Cms\Site;
Expand All @@ -21,12 +22,13 @@ public static function index(int $iterations = 2): array
$count = 0;
$time = microtime(true);

/** @var Page $page */
/** @var KhulanPage $page */
foreach (site()->index(true) as $page) {
if ($page->hasKhulan() !== true) {
continue;
}
if (kirby()->multilang()) {
/** @var Language $language */
foreach (kirby()->languages() as $language) {
$content = $page->content($language->code())->toArray();
$page->writeKhulan($content, $language->code());
Expand All @@ -37,12 +39,13 @@ public static function index(int $iterations = 2): array
$count++;
}

/** @var User $user */
/** @var KhulanUser $user */
foreach (kirby()->users() as $user) {
if ($user->hasKhulan() !== true) {
continue;
}
if (kirby()->multilang()) {
/** @var Language $language */
foreach (kirby()->languages() as $language) {
$content = $user->content($language->code())->toArray();
$user->writeKhulan($content, $language->code());
Expand All @@ -53,12 +56,13 @@ public static function index(int $iterations = 2): array
$count++;
}

/** @var File $file */
/** @var KhulanFile $file */
foreach (site()->index(true)->files() as $file) {
if ($file->hasKhulan() !== true) {
continue;
}
if (kirby()->multilang()) {
/** @var Language $language */
foreach (kirby()->languages() as $language) {
$contentFile = $file->root().'.'.$language->code().'.txt';
if (! F::exists($contentFile)) {
Expand Down Expand Up @@ -115,7 +119,7 @@ public static function index(int $iterations = 2): array

public static function flush(): bool
{
mongo()->contentCollection()->drop();
Mongodb::singleton()->contentCollection()->drop();

return true;
}
Expand Down Expand Up @@ -158,7 +162,7 @@ public static function documentsToModels(iterable $documents): Collection|Pages|
return null;
}

public static function documentToModel($document = null): Page|File|User|Site|null
public static function documentToModel(mixed $document = null): Page|File|User|Site|null
{
if (! $document) {
return null;
Expand Down
55 changes: 36 additions & 19 deletions classes/ModelWithKhulan.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ trait ModelWithKhulan
public function hasKhulan(): bool
{
if ($this instanceof File) {
return $this->parent()->hasKhulan() === true;
/** @var \Bnomei\KhulanPage|\Bnomei\KhulanUser $parent */
$parent = $this->parent();

return $parent->hasKhulan() === true;
}

return true;
Expand All @@ -40,7 +43,7 @@ public function keyKhulan(?string $languageCode = null): string
{
$key = $this->id(); // can not use UUID since content not loaded yet
if (! $languageCode) {
$languageCode = kirby()->languages()->count() ? kirby()->language()->code() : null;
$languageCode = kirby()->languages()->count() ? kirby()->language()?->code() : null;
}
if ($languageCode) {
$key = $key.'-'.$languageCode;
Expand Down Expand Up @@ -80,29 +83,35 @@ public function readContent(?string $languageCode = null): array

public function writeKhulan(?array $data = null, ?string $languageCode = null): bool
{
$cache = Mongodb::singleton();
if (! $cache || option('bnomei.mongodb.khulan.write') === false) {
if (option('bnomei.mongodb.khulan.write') === false) {
return true;
}

/*
$modified = null;
if ($this instanceof Site) {
// site()->modified() does crawl index but not return change of its content file
$siteFile = site()->storage()->read(
site()->storage()->defaultVersion(),
kirby()->defaultLanguage()->code()
)[0];
)[0]; // TODO: this might be a bug
$modified = $modified.filemtime($siteFile);
} else {
$modified = $this->modified();
}
*/
$modified = $this->modified();

// in rare case file does not exists or is not readable
if ($modified === false) {
if ($modified === null || $modified === false) {
$this->deleteKhulan(); // whatever was in the cache is no longer valid

return false; // try again another time
}
// kirby can return a timestamp as string
if (is_numeric($modified)) {
$modified = (int) $modified;
}

$modelType = 'page';
if ($this instanceof Site) {
Expand All @@ -114,13 +123,14 @@ public function writeKhulan(?array $data = null, ?string $languageCode = null):
}

$meta = [
'id' => $this->id() ?? null,
'id' => $this->id(),
'modified' => $modified,
'modified{}' => new UTCDateTime($modified * 1000),
'class' => $this::class,
'language' => $languageCode,
'modelType' => $modelType,
];
$this->id();
if ($this instanceof Page) {
$slug = explode('/', $this->id());
$meta['num'] = $this->num() ? (int) $this->num() : null;
Expand All @@ -132,8 +142,10 @@ public function writeKhulan(?array $data = null, ?string $languageCode = null):
$meta['sort'] = A::get($data, 'sort') ? (int) A::get($data, 'sort') : null;
$meta['filename'] = $this->filename();
$meta['template'] = A::get($data, 'template');
$meta['mimeType'] = F::mime($this->root());
$meta['parent{}'] = new ObjectId($this->parent()->keyKhulan($languageCode));
$meta['mimeType'] = $this->root() ? F::mime($this->root()) : null;
/** @var \Bnomei\KhulanPage|\Bnomei\KhulanUser $parent */
$parent = $this->parent();
$meta['parent{}'] = new ObjectId($parent->keyKhulan($languageCode));
} elseif ($this instanceof User) {
$meta['email'] = $this->email();
$meta['name'] = $this->name()->isNotEmpty() ? $this->name()->value() : null;
Expand Down Expand Up @@ -164,8 +176,7 @@ public function writeContent(array $data, ?string $languageCode = null): bool

public function deleteKhulan(): bool
{
$cache = Mongodb::singleton();
if (! $cache) {
if (option('bnomei.mongodb.khulan.write') === false) {
return true;
}

Expand All @@ -182,19 +193,18 @@ public function deleteKhulan(): bool

public function delete(bool $force = false): bool
{
$cache = Mongodb::singleton();
if (! $cache) {
return parent::delete($force);
}

$success = parent::delete($force);
$success = parent::delete($force); // @phpstan-ignore-line
$this->deleteKhulan();

return $success;
}

public function encodeKhulan(array $data, ?string $languageCode = null): array
public function encodeKhulan(?array $data = null, ?string $languageCode = null): array
{
if (! $data) {
return [];
}

$blueprint = null;
if ($this instanceof Page) {
$blueprint = $this->blueprint()->fields();
Expand Down Expand Up @@ -318,7 +328,14 @@ public function decodeKhulan(?array $data = []): array
// flatten to array
$data = iterator_to_array($data);
// and remove any mongodb objects
$data = json_decode(json_encode($data), true);
$json_encode = json_encode($data);
if (! $json_encode) {
throw new Exception('Could not encode data to JSON.');
}
$data = json_decode($json_encode, true);
if (! is_array($data)) {
throw new Exception('Could not decode JSON to array.');
}

$copy = $data;

Expand Down
8 changes: 4 additions & 4 deletions classes/Mongodb.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function key(string $key): string
/**
* {@inheritDoc}
*/
public function set(string $key, $value, int $minutes = 0): bool
public function set(string $key, mixed $value, int $minutes = 0): bool
{
/* SHOULD SET EVEN IN DEBUG
if ($this->option('debug')) {
Expand Down Expand Up @@ -104,7 +104,7 @@ public function retrieve(string $key): ?Value
$this->hasCleanedOnce = true;
}

$value = $value ?? $this->cacheCollection()->findOne([
$value = $this->cacheCollection()->findOne([
'_id' => $this->key($key),
]);

Expand All @@ -125,7 +125,7 @@ public function retrieve(string $key): ?Value
return $value;
}

public function get(string $key, $default = null)
public function get(string $key, mixed $default = null): mixed
{
if ($this->option('debug')) {
return $default;
Expand Down Expand Up @@ -251,7 +251,7 @@ public function contentCollection(): Collection
return $this->collection($this->options['collection-content']);
}

public static $singleton;
public static ?self $singleton = null;

public static function singleton(array $options = []): self
{
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"ext-mongodb": "*"
},
"require-dev": {
"getkirby/cli": "^1.5.0",
"getkirby/cms": "^4.3.0",
"larastan/larastan": "^2.9",
"laravel/pint": "^1.13",
Expand Down
Loading

0 comments on commit 6fb4c68

Please sign in to comment.