|
| 1 | +//! Eloquent `where{PropertyName}()` dynamic method synthesis. |
| 2 | +//! |
| 3 | +//! Laravel's `Builder::__call()` translates calls like |
| 4 | +//! `whereBrandId($value)` into `where('brand_id', $value)` and returns |
| 5 | +//! `$this`. This module synthesizes those dynamic methods from a |
| 6 | +//! model's known column names so that PHPantom resolves them instead of |
| 7 | +//! reporting `unknown_member` diagnostics. |
| 8 | +//! |
| 9 | +//! Column names are gathered directly from the raw `ClassInfo` and its |
| 10 | +//! `LaravelMetadata` (without full resolution) to avoid recursive |
| 11 | +//! cycles with `LaravelModelProvider::provide()`. The sources are: |
| 12 | +//! |
| 13 | +//! - `$casts` definitions |
| 14 | +//! - `$dates` definitions |
| 15 | +//! - `$attributes` defaults |
| 16 | +//! - `$fillable`/`$guarded`/`$hidden`/`$appends` column names |
| 17 | +//! - Timestamp columns (`created_at`, `updated_at` unless disabled) |
| 18 | +//! - `@property` annotations (already on the raw `ClassInfo`) |
| 19 | +//! - Declared (non-static, non-private) properties on the class itself |
| 20 | +//! |
| 21 | +//! Each column `foo_bar` produces a method `whereFooBar($value)` that |
| 22 | +//! accepts one parameter and returns `Builder<ConcreteModel>`. |
| 23 | +
|
| 24 | +use std::collections::HashSet; |
| 25 | + |
| 26 | +use crate::php_type::PhpType; |
| 27 | +use crate::types::{ClassInfo, MethodInfo, ParameterInfo}; |
| 28 | + |
| 29 | +use super::ELOQUENT_BUILDER_FQN; |
| 30 | +use super::helpers::snake_to_pascal; |
| 31 | + |
| 32 | +/// Collect all known column names from a raw (unresolved) model class. |
| 33 | +/// |
| 34 | +/// This reads directly from `LaravelMetadata` fields and from the |
| 35 | +/// class's own declared/virtual properties, avoiding a full |
| 36 | +/// `resolve_class_fully` call (which would recurse through |
| 37 | +/// `LaravelModelProvider`). |
| 38 | +fn collect_column_names(class: &ClassInfo) -> Vec<String> { |
| 39 | + let mut seen = HashSet::new(); |
| 40 | + let mut columns = Vec::new(); |
| 41 | + |
| 42 | + // Helper to insert a column if not already seen. |
| 43 | + let mut push = |name: &str| { |
| 44 | + if seen.insert(name.to_string()) { |
| 45 | + columns.push(name.to_string()); |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + // ── LaravelMetadata sources ───────────────────────────────────── |
| 50 | + if let Some(laravel) = class.laravel() { |
| 51 | + // $casts |
| 52 | + for (col, _) in &laravel.casts_definitions { |
| 53 | + push(col); |
| 54 | + } |
| 55 | + |
| 56 | + // $dates |
| 57 | + for col in &laravel.dates_definitions { |
| 58 | + push(col); |
| 59 | + } |
| 60 | + |
| 61 | + // $attributes defaults |
| 62 | + for (col, _) in &laravel.attributes_definitions { |
| 63 | + push(col); |
| 64 | + } |
| 65 | + |
| 66 | + // $fillable, $guarded, $hidden, $appends |
| 67 | + for col in &laravel.column_names { |
| 68 | + push(col); |
| 69 | + } |
| 70 | + |
| 71 | + // Timestamp columns (unless explicitly disabled). |
| 72 | + let timestamps_enabled = laravel.timestamps.unwrap_or(true); |
| 73 | + if timestamps_enabled { |
| 74 | + let created_col = match &laravel.created_at_name { |
| 75 | + Some(Some(name)) => Some(name.as_str()), |
| 76 | + Some(None) => None, // explicitly null |
| 77 | + None => Some("created_at"), // default |
| 78 | + }; |
| 79 | + let updated_col = match &laravel.updated_at_name { |
| 80 | + Some(Some(name)) => Some(name.as_str()), |
| 81 | + Some(None) => None, // explicitly null |
| 82 | + None => Some("updated_at"), // default |
| 83 | + }; |
| 84 | + for col in [created_col, updated_col].into_iter().flatten() { |
| 85 | + push(col); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // ── Properties already on the class ───────────────────────────── |
| 91 | + // This catches @property annotations (parsed into `properties` by |
| 92 | + // the PHPDoc parser) and any explicitly declared properties. |
| 93 | + // We skip properties that look like relationship properties (they |
| 94 | + // are objects, not columns) by checking for uppercase first char, |
| 95 | + // but we include everything — the worst case is an extra `where` |
| 96 | + // method that doesn't hurt. |
| 97 | + for prop in class.properties.iter() { |
| 98 | + push(&prop.name); |
| 99 | + } |
| 100 | + |
| 101 | + columns |
| 102 | +} |
| 103 | + |
| 104 | +/// Build `where{PropertyName}()` virtual methods for a model's columns. |
| 105 | +/// |
| 106 | +/// Reads column names directly from the raw `class` (no recursive |
| 107 | +/// resolution) and synthesizes a `where{StudlyCase}()` method for each. |
| 108 | +/// Each method accepts a single `$value` parameter (typed `mixed`) and |
| 109 | +/// returns `Builder<ConcreteModel>`. |
| 110 | +/// |
| 111 | +/// Methods whose name would collide with an entry in |
| 112 | +/// `existing_method_names` are skipped. |
| 113 | +pub fn build_where_property_methods_for_class( |
| 114 | + class: &ClassInfo, |
| 115 | + existing_method_names: &HashSet<String>, |
| 116 | +) -> Vec<MethodInfo> { |
| 117 | + let columns = collect_column_names(class); |
| 118 | + |
| 119 | + if columns.is_empty() { |
| 120 | + return Vec::new(); |
| 121 | + } |
| 122 | + |
| 123 | + // Build the return type: Builder<ConcreteModel>. |
| 124 | + let return_type = PhpType::Generic( |
| 125 | + ELOQUENT_BUILDER_FQN.to_string(), |
| 126 | + vec![PhpType::Named(class.name.clone())], |
| 127 | + ) |
| 128 | + .to_string(); |
| 129 | + |
| 130 | + let value_param = ParameterInfo { |
| 131 | + name: "$value".to_string(), |
| 132 | + is_required: true, |
| 133 | + type_hint: Some(PhpType::Named("mixed".to_string())), |
| 134 | + native_type_hint: None, |
| 135 | + description: None, |
| 136 | + default_value: None, |
| 137 | + is_variadic: false, |
| 138 | + is_reference: false, |
| 139 | + closure_this_type: None, |
| 140 | + }; |
| 141 | + |
| 142 | + let mut methods = Vec::new(); |
| 143 | + let mut seen_methods = HashSet::new(); |
| 144 | + |
| 145 | + for col in &columns { |
| 146 | + let method_name = format!("where{}", snake_to_pascal(col)); |
| 147 | + |
| 148 | + // Skip if a method with this name already exists on the target. |
| 149 | + if existing_method_names.contains(&method_name.to_ascii_lowercase()) { |
| 150 | + continue; |
| 151 | + } |
| 152 | + |
| 153 | + // Skip if we already synthesized a method with this name |
| 154 | + // (can happen when a column appears in multiple sources). |
| 155 | + if !seen_methods.insert(method_name.clone()) { |
| 156 | + continue; |
| 157 | + } |
| 158 | + |
| 159 | + let method = MethodInfo { |
| 160 | + parameters: vec![value_param.clone()], |
| 161 | + description: Some(format!("Find models where `{col}` equals the given value.",)), |
| 162 | + ..MethodInfo::virtual_method(&method_name, Some(&return_type)) |
| 163 | + }; |
| 164 | + |
| 165 | + methods.push(method); |
| 166 | + } |
| 167 | + |
| 168 | + methods |
| 169 | +} |
| 170 | + |
| 171 | +/// Collect lowercase method names from a slice for dedup lookups. |
| 172 | +pub fn lowercase_method_names(methods: &[MethodInfo]) -> HashSet<String> { |
| 173 | + methods |
| 174 | + .iter() |
| 175 | + .map(|m| m.name.to_ascii_lowercase()) |
| 176 | + .collect() |
| 177 | +} |
| 178 | + |
| 179 | +// ─── Tests ────────────────────────────────────────────────────────────────── |
| 180 | + |
| 181 | +#[cfg(test)] |
| 182 | +#[path = "where_property_tests.rs"] |
| 183 | +mod tests; |
0 commit comments