Skip to content

Commit

Permalink
refactor: Break out into getBlockFields() to make PHPstan happy
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Jan 29, 2024
1 parent 06b6739 commit 221021d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
14 changes: 12 additions & 2 deletions src/migrations/m231108_024521_change_to_json_column.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);

Check failure on line 126 in src/migrations/m231108_024521_change_to_json_column.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method lsst\cantodamassets\migrations\m231108_024521_change_to_json_column::getBlockFields() should return array<craft\fields\Matrix> but returns array<craft\base\FieldInterface>.
}
}
16 changes: 12 additions & 4 deletions src/services/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);

Check failure on line 193 in src/services/Assets.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method lsst\cantodamassets\services\Assets::getBlockFields() should return array<craft\fields\Matrix> but returns array<craft\base\FieldInterface>.
}
}

0 comments on commit 221021d

Please sign in to comment.