Skip to content

Golden Rules - FSM checker "promoter" #83

@noelsaw1

Description

@noelsaw1

This is a thoughtful architectural question. Let me break down feasibility and value:

Feasibility Assessment

Yes, it's technically possible. The Golden Rules Analyzer already does cross-file token analysis. Here's what we could detect:

Detectable Signals via Tokenization

Signal Detection Method Complexity
Multiple write sites to same option/meta Track update_option('key'), update_post_meta($id, 'key') across files Medium
Status-like variable writes Match keys containing status, state, phase, step, mode Low
Scattered transition logic Count functions with names like set_*_status, transition_to_* Low
Complex status conditionals Detect switch/if-else chains on status-like variables Medium-High

Rough Implementation Sketch

// In golden-rules-analyzer.php

// Track state writes: ['option_key' => ['file:line', 'file:line', ...]]
$state_write_sites = [];

// During token stream processing:
if ($token_is_function_call && in_array($func_name, ['update_option', 'update_post_meta', 'set_transient'])) {
    $key = extract_first_string_arg($tokens, $position);
    if (preg_match('/status|state|phase|step|mode|stage/i', $key)) {
        $state_write_sites[$key][] = "$current_file:$line_number";
    }
}

// After processing all files:
foreach ($state_write_sites as $key => $locations) {
    if (count($locations) >= 3 && count(array_unique(array_map('dirname', $locations))) >= 2) {
        // 3+ writes across 2+ directories = complexity signal
        emit_advisory('fsm-complexity-signal', $key, $locations);
    }
}

Value Assessment

Mixed—here's my honest take:

Arguments For

  1. Proactive architectural guidance — catches complexity before it becomes unmanageable
  2. Educational — introduces FSM thinking to developers who may not know the pattern
  3. Differentiator — no other PHP scanner does architectural recommendations like this
  4. Fits the "Golden Rules" philosophy — deeper insights beyond simple pattern matching

Arguments Against

  1. High false positive risk — legitimate multi-path writes are common (settings pages, REST endpoints, CLI commands all updating same option)
  2. Subjective threshold — when is 3 writes "too many"? 5? Depends entirely on context
  3. Not actionable without context — unlike "unbounded query" which has a clear fix, this just says "maybe refactor?"
  4. Scope creep — this is architectural consulting, not code quality scanning

My Recommendation

Worth exploring as a "beta/experimental" advisory feature, but not as a core pattern.

Practical Implementation Path

  1. Start narrow: Only detect update_option() / update_post_meta() with explicitly status-like keys
  2. High threshold: Require 4+ distinct write sites across 3+ files before flagging
  3. Advisory output: Separate from violations—maybe a "Complexity Insights" section in the report
  4. Link to guidance: Include a brief "What is FSM?" explainer or link to documentation

Output Format Concept

{
  "advisory_insights": [
    {
      "type": "complexity-signal",
      "category": "state-management",
      "message": "Option 'plugin_sync_status' is written from 5 locations across 4 files",
      "recommendation": "Consider centralizing state transitions with a state machine pattern",
      "locations": ["sync.php:142", "ajax.php:89", "cron.php:201", "admin.php:55", "rest.php:178"],
      "learn_more": "https://docs.wpcode.check/patterns/fsm-recommendation"
    }
  ]
}

Bottom Line

Question Answer
Is it possible? ✅ Yes, with medium effort
Is it worthwhile? ⚠️ As an experimental advisory, yes. As a core pattern, probably not yet
Should you build it now? 🤔 I'd prioritize it below the Laravel expansion and TypeScript patterns

If you want to experiment, I'd suggest adding it as a "hidden" feature flag first (--experimental-insights) and collecting real-world data on whether the detections are useful before promoting it to a standard feature.

Want me to draft a more detailed implementation spec, or would you rather focus on the higher-priority roadmap items first?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions