$sanitized_line) { $line_number = $line_num + 1; // Skip if the line is empty in sanitized version (was a comment) if (trim($sanitized_line) === '') { continue; } // Check for DB::table( usage in sanitized content if (preg_match('/DB\s*::\s*table\s*\(/i', $sanitized_line)) { // Try to extract table name from the parameter (use sanitized line) $table_name = null; if (preg_match('/DB\s*::\s*table\s*\(\s*[\'"]([^\'"]+)[\'"]\s*\)/i', $sanitized_line, $matches)) { $table_name = $matches[1]; } // Skip Laravel's internal migrations table if ($table_name === 'migrations') { continue; } // Skip Laravel's sessions table - managed by framework if ($table_name === 'sessions') { continue; } // Use original line for display in error message $original_line = $original_lines[$line_num] ?? $sanitized_line; // Determine which directory to suggest for model creation $model_location = str_contains($file_path, '/rsx/') ? './rsx/models' : './app/Models'; // Build resolution message based on whether we found the table name $resolution = "Direct database table access via DB::table() violates framework architecture principles.\n\n"; if ($table_name) { // Check if a model exists for this table $model_exists = false; $model_class = null; try { $model_class = ModelHelper::get_model_by_table($table_name); $model_exists = true; } catch (\Exception $e) { // Model doesn't exist $model_exists = false; } if ($model_exists) { $resolution .= "RECOMMENDED SOLUTION:\n"; $resolution .= "Use the existing '{$model_class}' model instead of DB::table('{$table_name}').\n"; $resolution .= "Example: {$model_class}::where('column', \$value)->get();\n\n"; } else { $resolution .= "RECOMMENDED SOLUTION:\n"; $resolution .= "No model detected for table '{$table_name}'. Create a new model class extending Rsx_Model_Abstract in {$model_location}.\n"; $resolution .= "Example model definition:\n"; $resolution .= " class " . ucfirst(str_replace('_', '', ucwords($table_name, '_'))) . " extends Rsx_Model_Abstract {\n"; $resolution .= " protected \$table = '{$table_name}';\n"; $resolution .= " }\n\n"; } } else { $resolution .= "RECOMMENDED SOLUTION:\n"; $resolution .= "Create an ORM model extending Rsx_Model_Abstract in {$model_location} for the target table.\n\n"; } $resolution .= "ALTERNATIVE (for complex reporting queries only):\n"; $resolution .= "If ORM is genuinely inappropriate due to query complexity (e.g., multi-table aggregations, complex reporting):\n"; $resolution .= "- Use DB::select() with raw SQL and prepared parameters for read operations\n"; $resolution .= "- Use DB::statement() with prepared parameters for write operations\n"; $resolution .= "Example: DB::select('SELECT * FROM table WHERE id = ?', [\$id]);\n\n"; $resolution .= "RATIONALE:\n"; $resolution .= "- ORM models provide data integrity, relationships, and business logic encapsulation\n"; $resolution .= "- Query builder (DB::table()) offers no advantages over raw queries for complex operations\n"; $resolution .= "- Consistent use of models maintains architectural coherence"; $this->add_violation( $file_path, $line_number, "DB::table() usage detected. Framework requires ORM models for database access.", trim($original_line), $resolution, 'high' ); } } } }