Skip to content

Commit 1d90e78

Browse files
committed
PHPStan Level Up. Store modules as MultiSelect
1 parent 5c86035 commit 1d90e78

File tree

5 files changed

+34
-17
lines changed

5 files changed

+34
-17
lines changed

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ includes:
22
- vendor/craftcms/phpstan/phpstan.neon
33

44
parameters:
5-
level: 4
5+
level: 9
66
paths:
77
- src

src/PhoneHome.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class PhoneHome extends Plugin
2323
public string $schemaVersion = '1.0.0';
2424
public bool $hasCpSettings = true;
2525

26+
/**
27+
* @phpstan-ignore-next-line
28+
*/
2629
public static function config(): array
2730
{
2831
return [

src/endpoints/NotionEndpoint.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class NotionEndpoint implements EndpointInterface
7070
'class' => MultiSelectDb::class,
7171
],
7272
self::PROPERTY_MODULES => [
73-
'class' => RichTextDb::class,
73+
'class' => MultiSelectDb::class,
7474
],
7575
self::PROPERTY_DATE_UPDATED => [
7676
'class' => DateDb::class,
@@ -157,9 +157,8 @@ public function send(SitePayload $payload): void
157157
$page = $page ?? Page::create($parent);
158158

159159
// Update properties
160-
161-
$plugins = $payload->plugins->map(fn(SitePayloadPlugin $plugin) => $plugin->id)->values()->all();
162-
$pluginVersions = $payload->plugins->map(fn(SitePayloadPlugin $plugin) => $plugin->versionedId)->values()->all();
160+
$plugins = $payload->plugins->map(fn(SitePayloadPlugin $plugin) => $plugin->id)->all();
161+
$pluginVersions = $payload->plugins->map(fn(SitePayloadPlugin $plugin) => $plugin->versionedId)->all();
163162

164163
$page = $page->addProperty(self::PROPERTY_NAME, Title::fromString($payload->siteName))
165164
->addProperty(self::PROPERTY_URL, Url::create($payload->siteUrl))
@@ -170,7 +169,7 @@ public function send(SitePayload $payload): void
170169
->addProperty(self::PROPERTY_DB_VERSION, Select::fromName($payload->dbVersion))
171170
->addProperty(self::PROPERTY_PLUGINS, MultiSelect::fromNames(...$plugins))
172171
->addProperty(self::PROPERTY_PLUGIN_VERSIONS, MultiSelect::fromNames(...$pluginVersions))
173-
->addProperty(self::PROPERTY_MODULES, RichTextProperty::fromString($payload->modules))
172+
->addProperty(self::PROPERTY_MODULES, MultiSelect::fromNames(...$payload->modules->all()))
174173
->addProperty(self::PROPERTY_DATE_UPDATED, Date::create(new DateTimeImmutable('now', new DateTimeZone('UTC'))));
175174

176175
if ($isCreate) {

src/models/SitePayload.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,40 @@ public function __construct(
1616
public readonly string $siteUrl,
1717
public readonly string $siteName,
1818
public readonly string $environment,
19+
/** @var string $craftEdition - Solo, Team, Pro, etc */
20+
public readonly string $craftEdition,
21+
/** @var string The version number */
1922
public readonly string $craftVersion,
2023
public readonly string $phpVersion,
2124
public readonly string $dbVersion,
22-
/** @var Collection<SitePayloadPlugin> $plugins */
25+
/** @var Collection<int,SitePayloadPlugin> $plugins */
2326
public readonly Collection $plugins,
24-
public readonly string $modules
27+
/** @var Collection<int,string> $modules */
28+
public readonly Collection $modules
2529
)
2630
{
2731
}
2832

2933
public static function fromSite(Site $site): self
3034
{
35+
$siteUrl = $site->getBaseUrl();
36+
$environment = Craft::$app->env;
37+
38+
if (!$siteUrl || !$environment) {
39+
throw new \Exception('$siteUrl or $environment not found');
40+
}
41+
3142
return new self(
32-
siteUrl: $site->getBaseUrl(),
43+
siteUrl: $siteUrl,
3344
siteName: $site->name,
34-
environment: Craft::$app->env,
35-
craftVersion: App::editionName(Craft::$app->getEdition()),
45+
environment: $environment,
46+
craftEdition: App::editionName(Craft::$app->getEdition()),
47+
craftVersion: App::normalizeVersion(Craft::$app->getVersion()),
3648
phpVersion: App::phpVersion(),
3749
dbVersion: self::_dbDriver(),
3850
plugins: Collection::make(Craft::$app->plugins->getAllPlugins())
39-
->map(SitePayloadPlugin::fromPluginInterface(...)),
51+
->map(SitePayloadPlugin::fromPluginInterface(...))
52+
->values(),
4053
modules: self::_modules()
4154
);
4255
}
@@ -62,9 +75,9 @@ private static function _dbDriver(): string
6275
/**
6376
* Returns the list of modules
6477
*
65-
* @return string
78+
* @return Collection<int,string>
6679
*/
67-
private static function _modules(): string
80+
private static function _modules(): Collection
6881
{
6982
$modules = [];
7083

@@ -82,7 +95,8 @@ private static function _modules(): string
8295
}
8396
}
8497

85-
return implode(PHP_EOL, $modules);
98+
// ->values() forces a 0 indexed array
99+
return Collection::make($modules)->values();
86100
}
87101

88102
}

src/services/PhoneHomeService.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Craft;
66
use craft\helpers\Queue;
7+
use craft\web\Request;
78
use Illuminate\Support\Collection;
89
use viget\phonehome\endpoints\EndpointInterface;
910
use viget\phonehome\jobs\SendPayloadJob;
@@ -29,13 +30,13 @@ public function tryQueuePhoneHome(): void
2930
Craft::$app->getIsInstalled() === false
3031
|| Craft::$app->getRequest()->getIsConsoleRequest()
3132
|| !Craft::$app->getRequest()->getIsCpRequest() // Only run on CP request
32-
|| $request->getIsAjax()
33+
|| $request instanceof Request && $request->getIsAjax()
3334
) {
3435
return;
3536
}
3637

3738
// Only run when the cache is empty (once per day at most)
38-
if (Craft::$app->getCache()->get(self::CACHE_KEY) !== false) {
39+
if (Craft::$app->getCache()?->get(self::CACHE_KEY) !== false) {
3940
return;
4041
}
4142

0 commit comments

Comments
 (0)