Enforcing module boundaries with a custom PHPStan rule

In a modular monolith, documentation stating that “Module A must not call Module B directly” is ignored the moment a tight deadline arrives.

// A custom PHPStan rule checking namespace imports:
public function processNode(Node $node, Scope $scope): array
{
    if (!$node instanceof NodeStmtUse_) return [];

    $currentNamespace = $scope->getNamespace() ?? '';
    foreach ($node->uses as $use) {
        $importedClass = $use->name->toString();
        if (str_starts_with($currentNamespace, 'AppBilling') 
            && str_starts_with($importedClass, 'AppInventoryInternal')) {
            return ["Billing cannot import internal Inventory class {$importedClass}. Use InventoryApiInterface."];
        }
    }
    return [];
}

Enforcing architectural boundaries mechanically in static analysis stops architectural erosion permanently without requiring manual code review diligence.