From 221021d7de33a788a5e0a151df1400a5852fdf51 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Sun, 28 Jan 2024 23:13:20 -0500 Subject: [PATCH] refactor: Break out into `getBlockFields()` to make PHPstan happy --- .../m231108_024521_change_to_json_column.php | 14 ++++++++++++-- src/services/Assets.php | 16 ++++++++++++---- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/migrations/m231108_024521_change_to_json_column.php b/src/migrations/m231108_024521_change_to_json_column.php index 063ce89..05d52dd 100644 --- a/src/migrations/m231108_024521_change_to_json_column.php +++ b/src/migrations/m231108_024521_change_to_json_column.php @@ -63,10 +63,9 @@ public function safeDown(): bool */ private function changeBlockTypeToJsonColumn(string $fieldType): void { - $blockFields = Craft::$app->getFields()->getFieldsByType($fieldType); + $blockFields = $this->getBlockFields($fieldType); foreach ($blockFields as $blockField) { // Block types have the same methods as Matrix - /* @var Matrix $blockField */ $fields = $blockField->getBlockTypeFields(); // Filter out any non-CantoDamAsset fields $fields = (new Collection($fields))->filter(fn($value) => $value instanceof CantoDamAsset)->toArray(); @@ -115,4 +114,15 @@ private function alterColumnUsingPgsql($table, $column, $type, $using): void $cmd->execute(); $this->endCommand($time); } + + /** + * Block type fields have the same methods as Matrix + * + * @param string $fieldType + * @return Matrix[] + */ + private function getBlockFields(string $fieldType): array + { + return Craft::$app->getFields()->getFieldsByType($fieldType); + } } diff --git a/src/services/Assets.php b/src/services/Assets.php index 9e4c111..1e6eac5 100644 --- a/src/services/Assets.php +++ b/src/services/Assets.php @@ -143,12 +143,9 @@ protected function updateEntryContent(string $value, CantoFieldData $cantoFieldD */ protected function updateBlockTypeContent(string $fieldType, string $value, CantoFieldData $cantoFieldData, ?string $columnKey): void { - $blockFields = Craft::$app->getFields()->getFieldsByType($fieldType); + $blockFields = $this->getBlockFields($fieldType); foreach ($blockFields as $blockField) { - // Block types have the same methods as Matrix - /* @var Matrix $blockField */ $contentTableName = $blockField->contentTable; - /* @var Matrix $blockField */ $fields = $blockField->getBlockTypeFields(); // Filter out any non-CantoDamAsset fields $fields = (new Collection($fields))->filter(fn($value) => $value instanceof CantoDamAsset)->toArray(); @@ -184,4 +181,15 @@ protected function updateContent(string $value, CantoFieldData $cantoFieldData, } } } + + /** + * Block type fields have the same methods as Matrix + * + * @param string $fieldType + * @return Matrix[] + */ + private function getBlockFields(string $fieldType): array + { + return Craft::$app->getFields()->getFieldsByType($fieldType); + } }