perf: DynamicFields reads each post type's fields once per check (review M-12 b)

reason() and note() run per key and type of a body check; they now share one
offered() result per post type, so the default storage callback loads each
pod once instead of per call.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01At3V6SjwbMe6eMzVGW3Hs7
This commit is contained in:
2026-09-27 23:12:14 +02:00
co-authored by Claude Opus 5.5
parent 14bc954ecc
commit 6288acf0e6
2 changed files with 38 additions and 2 deletions
+16 -2
View File
@@ -38,6 +38,9 @@ class DynamicFields
/** @var Closure(string): ?string */
private Closure $podStorage;
/** @var array<string, array{0: list<array<string, string>>, 1: list<array<string, string>>}> offered() by type. */
private array $known = [];
/** @param (Closure(string): ?string)|null $podStorage Post type → Pods storage ("meta", "table"), null: none. */
public function __construct(private ContentSchema $schema, ?Closure $podStorage = null)
{
@@ -72,10 +75,21 @@ class DynamicFields
return [$fields, $omitted];
}
/**
* offered() once per post type for reason()/note() – they run per key and type of a check, and the default
* storage callback loads the pod each time (review M-12 b).
*
* @return array{0: list<array<string, string>>, 1: list<array<string, string>>}
*/
private function known(string $postType): array
{
return $this->known[$postType] ??= $this->offered($postType);
}
/** Why {{field:$key}} would not show as text for $postType; null when it does. */
public function reason(string $postType, string $key): ?string
{
[$fields, $omitted] = $this->offered($postType);
[$fields, $omitted] = $this->known($postType);
foreach ($fields as $field) {
if ($field['key'] === $key) {
return null;
@@ -93,7 +107,7 @@ class DynamicFields
/** What to know about an offered field of $postType ($key) that shows other text than expected; else null. */
public function note(string $postType, string $key): ?string
{
[$fields] = $this->offered($postType);
[$fields] = $this->known($postType);
foreach ($fields as $field) {
if ($field['key'] === $key && $field['type'] === 'choice') {
return 'Auswahlfeld – Divi zeigt den gespeicherten Wert, nicht die Beschriftung';