Return the raw text representation of this expression. This is used as a bridge while callers are migrated: they can parse a string into `SubjectExpr`, match on it, and still pass the original text to functions that haven't been converted yet.
(&self)
| 260 | /// parse a string into `SubjectExpr`, match on it, and still pass |
| 261 | /// the original text to functions that haven't been converted yet. |
| 262 | pub fn to_subject_text(&self) -> String { |
| 263 | match self { |
| 264 | SubjectExpr::This => "$this".to_string(), |
| 265 | SubjectExpr::SelfKw => "self".to_string(), |
| 266 | SubjectExpr::StaticKw => "static".to_string(), |
| 267 | SubjectExpr::Parent => "parent".to_string(), |
| 268 | SubjectExpr::Variable(v) => v.clone(), |
| 269 | SubjectExpr::PropertyChain { base, property } => { |
| 270 | format!("{}->{}", base.to_subject_text(), property) |
| 271 | } |
| 272 | SubjectExpr::CallExpr { callee, args_text } => { |
| 273 | // Wrap the callee in parentheses when it is an |
| 274 | // expression form that is not naturally callable by |
| 275 | // name. Without this, `PropertyChain { $this, "prop" }` |
| 276 | // serialises as `$this->prop(args)` (a method call) |
| 277 | // instead of the correct `($this->prop)(args)` (invoke |
| 278 | // property as callable via __invoke). |
| 279 | let needs_parens = matches!( |
| 280 | callee.as_ref(), |
| 281 | SubjectExpr::PropertyChain { .. } |
| 282 | | SubjectExpr::This |
| 283 | | SubjectExpr::SelfKw |
| 284 | | SubjectExpr::StaticKw |
| 285 | | SubjectExpr::Parent |
| 286 | | SubjectExpr::ArrayAccess { .. } |
| 287 | | SubjectExpr::InlineArray { .. } |
| 288 | | SubjectExpr::CallExpr { .. } |
| 289 | ); |
| 290 | if needs_parens { |
| 291 | format!("({})({})", callee.to_subject_text(), args_text) |
| 292 | } else { |
| 293 | format!("{}({})", callee.to_subject_text(), args_text) |
| 294 | } |
| 295 | } |
| 296 | SubjectExpr::MethodCall { base, method } => { |
| 297 | format!("{}->{}", base.to_subject_text(), method) |
| 298 | } |
| 299 | SubjectExpr::StaticMethodCall { class, method } => { |
| 300 | format!("{}::{}", class, method) |
| 301 | } |
| 302 | SubjectExpr::StaticAccess { class, member } => { |
| 303 | format!("{}::{}", class, member) |
| 304 | } |
| 305 | SubjectExpr::NewExpr { class_name } => { |
| 306 | format!("new {}", class_name) |
| 307 | } |
| 308 | SubjectExpr::ClassName(name) => name.clone(), |
| 309 | SubjectExpr::FunctionCall(name) => name.clone(), |
| 310 | SubjectExpr::ArrayAccess { base, segments } => { |
| 311 | let mut s = base.to_subject_text(); |
| 312 | for seg in segments { |
| 313 | match seg { |
| 314 | BracketSegment::StringKey(k) => { |
| 315 | s.push_str(&format!("['{}']", k)); |
| 316 | } |
| 317 | BracketSegment::ElementAccess => { |
| 318 | s.push_str("[]"); |
| 319 | } |
no test coverage detected