• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

extphprs / ext-php-rs / 23614891300

26 Mar 2026 07:51PM UTC coverage: 34.063% (-1.4%) from 35.42%
23614891300

Pull #699

github

web-flow
Merge 5b02f8d6d into 139f812b5
Pull Request #699: perf(macros): zero-alloc fast path for #[php_function] codegen

1 of 166 new or added lines in 2 files covered. (0.6%)

30 existing lines in 3 files now uncovered.

2211 of 6491 relevant lines covered (34.06%)

22.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

17.65
/crates/macros/src/function.rs
1
use std::collections::HashMap;
2

3
use darling::{FromAttributes, ToTokens};
4
use proc_macro2::{Ident, Span, TokenStream};
5
use quote::{format_ident, quote, quote_spanned};
6
use syn::spanned::Spanned as _;
7
use syn::{Expr, FnArg, GenericArgument, ItemFn, PatType, PathArguments, Type, TypePath};
8

9
use crate::helpers::get_docs;
10
use crate::parsing::{
11
    PhpNameContext, PhpRename, RenameRule, Visibility, ident_to_php_name, validate_php_name,
12
};
13
use crate::prelude::*;
14
use crate::syn_ext::DropLifetimes;
15

16
/// Checks if the return type is a reference to Self (`&Self` or `&mut Self`).
17
/// This is used to detect methods that return `$this` in PHP.
18
fn returns_self_ref(output: Option<&Type>) -> bool {
3✔
19
    let Some(ty) = output else {
6✔
20
        return false;
×
21
    };
22
    if let Type::Reference(ref_) = ty
3✔
23
        && let Type::Path(path) = &*ref_.elem
×
24
        && path.path.segments.len() == 1
×
25
        && let Some(segment) = path.path.segments.last()
×
26
    {
27
        return segment.ident == "Self";
×
28
    }
29
    false
3✔
30
}
31

32
/// Checks if the return type is `Self` (not a reference).
33
/// This is used to detect methods that return a new instance of the same class.
34
fn returns_self(output: Option<&Type>) -> bool {
3✔
35
    let Some(ty) = output else {
6✔
36
        return false;
×
37
    };
38
    if let Type::Path(path) = ty
6✔
39
        && path.path.segments.len() == 1
3✔
40
        && let Some(segment) = path.path.segments.last()
6✔
41
    {
42
        return segment.ident == "Self";
3✔
43
    }
44
    false
×
45
}
46

47
pub fn wrap(input: &syn::Path) -> Result<TokenStream> {
×
48
    let Some(func_name) = input.get_ident() else {
×
49
        bail!(input => "Pass a PHP function name into `wrap_function!()`.");
×
50
    };
51
    let builder_func = format_ident!("_internal_{func_name}");
×
52

53
    Ok(quote! {{
×
54
        (<#builder_func as ::ext_php_rs::internal::function::PhpFunction>::FUNCTION_ENTRY)()
×
55
    }})
56
}
57

58
#[derive(FromAttributes, Default, Debug)]
59
#[darling(default, attributes(php), forward_attrs(doc))]
60
struct PhpFunctionAttribute {
61
    #[darling(flatten)]
62
    rename: PhpRename,
63
    defaults: HashMap<Ident, Expr>,
64
    optional: Option<Ident>,
65
    vis: Option<Visibility>,
66
    attrs: Vec<syn::Attribute>,
67
}
68

69
pub fn parser(mut input: ItemFn) -> Result<TokenStream> {
×
70
    let php_attr = PhpFunctionAttribute::from_attributes(&input.attrs)?;
×
71
    input.attrs.retain(|attr| !attr.path().is_ident("php"));
×
72

73
    let args = Args::parse_from_fnargs(input.sig.inputs.iter(), php_attr.defaults)?;
×
74
    if let Some(ReceiverArg { span, .. }) = args.receiver {
×
75
        bail!(span => "Receiver arguments are invalid on PHP functions. See `#[php_impl]`.");
×
76
    }
77

78
    let docs = get_docs(&php_attr.attrs)?;
×
79

80
    let func_name = php_attr
×
81
        .rename
×
82
        .rename(ident_to_php_name(&input.sig.ident), RenameRule::Snake);
×
83
    validate_php_name(&func_name, PhpNameContext::Function, input.sig.ident.span())?;
×
84
    let func = Function::new(&input.sig, func_name, args, php_attr.optional, docs);
×
85
    let function_impl = func.php_function_impl();
×
86

87
    Ok(quote! {
×
88
        #input
×
89
        #function_impl
×
90
    })
91
}
92

93
#[derive(Debug)]
94
pub struct Function<'a> {
95
    /// Identifier of the Rust function associated with the function.
96
    pub ident: &'a Ident,
97
    /// Name of the function in PHP.
98
    pub name: String,
99
    /// Function arguments.
100
    pub args: Args<'a>,
101
    /// Function outputs.
102
    pub output: Option<&'a Type>,
103
    /// The first optional argument of the function.
104
    pub optional: Option<Ident>,
105
    /// Doc comments for the function.
106
    pub docs: Vec<String>,
107
}
108

109
#[derive(Debug)]
110
pub enum CallType<'a> {
111
    Function,
112
    Method {
113
        class: &'a syn::Path,
114
        receiver: MethodReceiver,
115
    },
116
}
117

118
/// Type of receiver on the method.
119
#[derive(Debug)]
120
pub enum MethodReceiver {
121
    /// Static method - has no receiver.
122
    Static,
123
    /// Class method, takes `&self` or `&mut self`.
124
    Class,
125
    /// Class method, takes `&mut ZendClassObject<Self>`.
126
    ZendClassObject,
127
}
128

129
impl<'a> Function<'a> {
130
    /// Parse a function.
131
    ///
132
    /// # Parameters
133
    ///
134
    /// * `sig` - Function signature.
135
    /// * `name` - Function name in PHP land.
136
    /// * `args` - Function arguments.
137
    /// * `optional` - The ident of the first optional argument.
138
    pub fn new(
3✔
139
        sig: &'a syn::Signature,
140
        name: String,
141
        args: Args<'a>,
142
        optional: Option<Ident>,
143
        docs: Vec<String>,
144
    ) -> Self {
145
        Self {
146
            ident: &sig.ident,
3✔
147
            name,
148
            args,
149
            output: match &sig.output {
3✔
150
                syn::ReturnType::Default => None,
151
                syn::ReturnType::Type(_, ty) => Some(&**ty),
152
            },
153
            optional,
154
            docs,
155
        }
156
    }
157

158
    /// Generates an internal identifier for the function.
159
    pub fn internal_ident(&self) -> Ident {
×
160
        format_ident!("_internal_{}", &self.ident)
×
161
    }
162

163
    pub fn abstract_function_builder(&self) -> TokenStream {
3✔
164
        let name = &self.name;
6✔
165
        let (required, not_required) = self.args.split_args(self.optional.as_ref());
15✔
166

167
        // `entry` impl
168
        let required_args = required
6✔
169
            .iter()
170
            .map(TypedArg::arg_builder)
3✔
171
            .collect::<Vec<_>>();
172
        let not_required_args = not_required
6✔
173
            .iter()
174
            .map(TypedArg::arg_builder)
3✔
175
            .collect::<Vec<_>>();
176

177
        let returns = self.build_returns(None);
12✔
178
        let docs = if self.docs.is_empty() {
9✔
179
            quote! {}
2✔
180
        } else {
181
            let docs = &self.docs;
2✔
182
            quote! {
1✔
183
                .docs(&[#(#docs),*])
×
184
            }
185
        };
186

187
        quote! {
3✔
188
            ::ext_php_rs::builders::FunctionBuilder::new_abstract(#name)
×
189
            #(.arg(#required_args))*
×
190
            .not_required()
×
191
            #(.arg(#not_required_args))*
×
192
            #returns
×
193
            #docs
×
194
        }
195
    }
196

197
    /// Generates the function builder for the function.
198
    pub fn function_builder(&self, call_type: &CallType) -> TokenStream {
×
199
        let name = &self.name;
×
200
        let (required, not_required) = self.args.split_args(self.optional.as_ref());
×
201

202
        // `handler` impl
203
        let arg_declarations = self
×
204
            .args
×
205
            .typed
×
206
            .iter()
207
            .map(TypedArg::arg_declaration)
×
208
            .collect::<Vec<_>>();
209

210
        // `entry` impl
211
        let required_args = required
×
212
            .iter()
213
            .map(TypedArg::arg_builder)
×
214
            .collect::<Vec<_>>();
215
        let not_required_args = not_required
×
216
            .iter()
217
            .map(TypedArg::arg_builder)
×
218
            .collect::<Vec<_>>();
219

220
        let returns = self.build_returns(Some(call_type));
×
221
        let result = self.build_result(call_type, required, not_required);
×
222
        let docs = if self.docs.is_empty() {
×
223
            quote! {}
×
224
        } else {
225
            let docs = &self.docs;
×
226
            quote! {
×
227
                .docs(&[#(#docs),*])
×
228
            }
229
        };
230

231
        // Static methods cannot return &Self or &mut Self
232
        if returns_self_ref(self.output)
×
233
            && let CallType::Method {
×
234
                receiver: MethodReceiver::Static,
×
235
                ..
×
236
            } = call_type
×
237
            && let Some(output) = self.output
×
238
        {
239
            return quote_spanned! { output.span() =>
×
240
                compile_error!(
×
241
                    "Static methods cannot return `&Self` or `&mut Self`. \
×
242
                     Only instance methods can use fluent interface pattern returning `$this`."
×
243
                )
244
            };
245
        }
246

247
        // Check if this method returns &Self or &mut Self
248
        // In that case, we need to return `this` (the ZendClassObject) directly
249
        let returns_this = returns_self_ref(self.output)
×
250
            && matches!(
×
251
                call_type,
×
252
                CallType::Method {
×
253
                    receiver: MethodReceiver::Class | MethodReceiver::ZendClassObject,
×
254
                    ..
×
255
                }
256
            );
257

NEW
258
        let handler_body = if self.is_fast_path_eligible(call_type) {
×
NEW
259
            self.build_fast_handler_body(call_type)
×
NEW
260
        } else if returns_this {
×
261
            quote! {
×
262
                use ::ext_php_rs::convert::IntoZval;
×
263

264
                #(#arg_declarations)*
×
265
                #result
×
266

267
                // The method returns &Self or &mut Self, use `this` directly
268
                if let Err(e) = this.set_zval(retval, false) {
×
269
                    let e: ::ext_php_rs::exception::PhpException = e.into();
×
270
                    e.throw().expect("Failed to throw PHP exception.");
×
271
                }
272
            }
273
        } else {
274
            quote! {
×
275
                use ::ext_php_rs::convert::IntoZval;
×
276

277
                #(#arg_declarations)*
×
278
                let result = {
×
279
                    #result
×
280
                };
281

282
                if let Err(e) = result.set_zval(retval, false) {
×
283
                    let e: ::ext_php_rs::exception::PhpException = e.into();
×
284
                    e.throw().expect("Failed to throw PHP exception.");
×
285
                }
286
            }
287
        };
288

289
        quote! {
×
290
            ::ext_php_rs::builders::FunctionBuilder::new(#name, {
×
291
                ::ext_php_rs::zend_fastcall! {
×
NEW
292
                    #[allow(clippy::used_underscore_binding)]
×
293
                    extern fn handler(
×
294
                        ex: &mut ::ext_php_rs::zend::ExecuteData,
×
295
                        retval: &mut ::ext_php_rs::types::Zval,
×
296
                    ) {
297
                        use ::ext_php_rs::zend::try_catch;
×
298
                        use ::std::panic::AssertUnwindSafe;
×
299

300
                        // Wrap the handler body with try_catch to ensure Rust destructors
301
                        // are called if a bailout occurs (issue #537)
302
                        let catch_result = try_catch(AssertUnwindSafe(|| {
×
303
                            #handler_body
×
304
                        }));
305

306
                        // If there was a bailout, run BailoutGuard cleanups and re-trigger
307
                        if catch_result.is_err() {
×
308
                            ::ext_php_rs::zend::run_bailout_cleanups();
×
309
                            unsafe { ::ext_php_rs::zend::bailout(); }
×
310
                        }
311
                    }
312
                }
313
                handler
×
314
            })
315
            #(.arg(#required_args))*
×
316
            .not_required()
×
317
            #(.arg(#not_required_args))*
×
318
            #returns
×
319
            #docs
×
320
        }
321
    }
322

323
    fn build_returns(&self, call_type: Option<&CallType>) -> TokenStream {
3✔
324
        let Some(output) = self.output.cloned() else {
6✔
325
            // PHP magic methods __destruct and __clone cannot have return types
326
            // (only applies to class methods, not standalone functions)
327
            if matches!(call_type, Some(CallType::Method { .. }))
×
328
                && (self.name == "__destruct" || self.name == "__clone")
×
329
            {
330
                return quote! {};
×
331
            }
332
            // No return type means void in PHP
333
            return quote! {
×
334
                .returns(::ext_php_rs::flags::DataType::Void, false, false)
×
335
            };
336
        };
337

338
        let mut output = output;
6✔
339
        output.drop_lifetimes();
6✔
340

341
        // If returning &Self or &mut Self from a method, use the class type
342
        // for return type information since we return `this` (ZendClassObject)
343
        if returns_self_ref(self.output)
6✔
344
            && let Some(CallType::Method { class, .. }) = call_type
×
345
        {
346
            return quote! {
×
347
                .returns(
×
348
                    <&mut ::ext_php_rs::types::ZendClassObject<#class> as ::ext_php_rs::convert::IntoZval>::TYPE,
×
349
                    false,
×
350
                    <&mut ::ext_php_rs::types::ZendClassObject<#class> as ::ext_php_rs::convert::IntoZval>::NULLABLE,
×
351
                )
352
            };
353
        }
354

355
        // If returning Self (new instance) from a method, replace Self with
356
        // the actual class type since Self won't resolve in generated code
357
        if returns_self(self.output)
6✔
358
            && let Some(CallType::Method { class, .. }) = call_type
×
359
        {
360
            return quote! {
×
361
                .returns(
×
362
                    <#class as ::ext_php_rs::convert::IntoZval>::TYPE,
×
363
                    false,
×
364
                    <#class as ::ext_php_rs::convert::IntoZval>::NULLABLE,
×
365
                )
366
            };
367
        }
368

369
        quote! {
3✔
370
            .returns(
×
371
                <#output as ::ext_php_rs::convert::IntoZval>::TYPE,
×
372
                false,
×
373
                <#output as ::ext_php_rs::convert::IntoZval>::NULLABLE,
×
374
            )
375
        }
376
    }
377

378
    fn build_result(
×
379
        &self,
380
        call_type: &CallType,
381
        required: &[TypedArg<'_>],
382
        not_required: &[TypedArg<'_>],
383
    ) -> TokenStream {
384
        let ident = self.ident;
×
385
        let required_arg_names: Vec<_> = required.iter().map(|arg| arg.name).collect();
×
386
        let not_required_arg_names: Vec<_> = not_required.iter().map(|arg| arg.name).collect();
×
387

388
        let variadic_bindings = self.args.typed.iter().filter_map(|arg| {
×
389
            if arg.variadic {
×
390
                let name = arg.name;
×
391
                let variadic_name = format_ident!("__variadic_{}", name);
×
392
                let clean_ty = arg.clean_ty();
×
393
                Some(quote! {
×
394
                    let #variadic_name = #name.variadic_vals::<#clean_ty>();
×
395
                })
396
            } else {
397
                None
×
398
            }
399
        });
400

401
        let arg_accessors = self.args.typed.iter().map(|arg| {
×
402
            arg.accessor(|e| {
×
403
                quote! {
×
404
                    #e.throw().expect("Failed to throw PHP exception.");
×
405
                    return;
×
406
                }
407
            })
408
        });
409

410
        // Check if this method returns &Self or &mut Self
411
        let returns_this = returns_self_ref(self.output);
×
412

413
        match call_type {
×
414
            CallType::Function => quote! {
×
415
                let parse = ex.parser()
×
416
                    #(.arg(&mut #required_arg_names))*
×
417
                    .not_required()
×
418
                    #(.arg(&mut #not_required_arg_names))*
×
419
                    .parse();
×
420
                if parse.is_err() {
×
421
                    return;
×
422
                }
423
                #(#variadic_bindings)*
×
424

425
                #ident(#({#arg_accessors}),*)
×
426
            },
427
            CallType::Method { class, receiver } => {
×
428
                let this = match receiver {
×
429
                    MethodReceiver::Static => quote! {
×
430
                        let parse = ex.parser();
×
431
                    },
432
                    MethodReceiver::ZendClassObject | MethodReceiver::Class => quote! {
×
433
                        let (parse, this) = ex.parser_method::<#class>();
×
434
                        let this = match this {
×
435
                            Some(this) => this,
×
436
                            None => {
×
437
                                ::ext_php_rs::exception::PhpException::default("Failed to retrieve reference to `$this`".into())
×
438
                                    .throw()
×
439
                                    .unwrap();
×
440
                                return;
×
441
                            }
442
                        };
443
                    },
444
                };
445

446
                // When returning &Self or &mut Self, discard the return value
447
                // (we'll use `this` directly in the handler)
448
                let call = match (receiver, returns_this) {
×
449
                    (MethodReceiver::Static, _) => {
×
450
                        quote! { #class::#ident(#({#arg_accessors}),*) }
×
451
                    }
452
                    (MethodReceiver::Class, true) => {
×
453
                        quote! { let _ = this.#ident(#({#arg_accessors}),*); }
×
454
                    }
455
                    (MethodReceiver::Class, false) => {
×
456
                        quote! { this.#ident(#({#arg_accessors}),*) }
×
457
                    }
458
                    (MethodReceiver::ZendClassObject, true) => {
×
459
                        // Explicit scope helps with mutable borrow lifetime when
460
                        // the method returns `&mut Self`
461
                        quote! {
×
462
                            {
463
                                let _ = #class::#ident(this, #({#arg_accessors}),*);
×
464
                            }
465
                        }
466
                    }
467
                    (MethodReceiver::ZendClassObject, false) => {
×
468
                        quote! { #class::#ident(this, #({#arg_accessors}),*) }
×
469
                    }
470
                };
471

472
                quote! {
×
473
                    #this
×
474
                    let parse_result = parse
×
475
                        #(.arg(&mut #required_arg_names))*
×
476
                        .not_required()
×
477
                        #(.arg(&mut #not_required_arg_names))*
×
478
                        .parse();
×
479
                    if parse_result.is_err() {
×
480
                        return;
×
481
                    }
482
                    #(#variadic_bindings)*
×
483

484
                    #call
×
485
                }
486
            }
487
        }
488
    }
489

490
    /// Whether this function is eligible for the zero-alloc fast path.
491
    /// Requires: no variadic parameters.
NEW
492
    fn is_fast_path_eligible(&self, call_type: &CallType) -> bool {
×
NEW
493
        let no_variadic = !self.args.typed.iter().any(|arg| arg.variadic);
×
NEW
494
        let supported_call_type = matches!(
×
NEW
495
            call_type,
×
NEW
496
            CallType::Function
×
NEW
497
                | CallType::Method {
×
NEW
498
                    receiver: MethodReceiver::Static
×
NEW
499
                        | MethodReceiver::Class
×
NEW
500
                        | MethodReceiver::ZendClassObject,
×
NEW
501
                    ..
×
502
                }
503
        );
NEW
504
        no_variadic && supported_call_type
×
505
    }
506

507
    /// Generates a zero-alloc fast path handler body.
508
    ///
509
    /// Instead of building `ArgParser` with `Vec`/`String` heap allocations,
510
    /// reads zvals directly from the call frame via pointer arithmetic
511
    /// and converts with `FromZvalMut` inline. Matches the pattern used by
512
    /// PHP's `ZEND_PARSE_PARAMETERS_START`/`END` C macros.
NEW
513
    fn restore_mutability(ty: &Type) -> Type {
×
NEW
514
        if let Type::Reference(r) = ty {
×
NEW
515
            let mut mref = r.clone();
×
NEW
516
            mref.mutability = Some(syn::token::Mut::default());
×
NEW
517
            Type::Reference(mref)
×
518
        } else {
NEW
519
            ty.clone()
×
520
        }
521
    }
522

NEW
523
    fn build_fast_arg_binding(i: usize, arg: &TypedArg<'_>, min_num_args: usize) -> TokenStream {
×
NEW
524
        let name = arg.name;
×
NEW
525
        let ty = arg.clean_ty();
×
NEW
526
        let zval_ident = format_ident!("__zval_{}", i);
×
527

528
        // parse_typed unwraps Option<T> → T and strips &mut → &.
529
        // Restore mutability for as_ref args so FromZvalMut resolves correctly.
NEW
530
        let convert_ty = if arg.as_ref {
×
NEW
531
            Self::restore_mutability(&ty)
×
532
        } else {
NEW
533
            ty.clone()
×
534
        };
535

NEW
536
        let binding_ty: Type = if !arg.nullable {
×
NEW
537
            ty.clone()
×
NEW
538
        } else if arg.as_ref {
×
NEW
539
            let mty = Self::restore_mutability(&ty);
×
NEW
540
            syn::parse_quote! { Option<#mty> }
×
541
        } else {
NEW
542
            syn::parse_quote! { Option<#ty> }
×
543
        };
544

NEW
545
        let read_zval = quote! {
×
NEW
546
            let #zval_ident = unsafe { ex.zend_call_arg(#i) };
×
NEW
547
            let Some(#zval_ident) = #zval_ident else { return; };
×
548
        };
549

NEW
550
        let from_zval = quote! {
×
NEW
551
            <#convert_ty as ::ext_php_rs::convert::FromZvalMut>::from_zval_mut(
×
NEW
552
                #zval_ident.dereference_mut()
×
553
            )
554
        };
555

NEW
556
        let convert = if arg.nullable {
×
NEW
557
            from_zval.clone()
×
558
        } else {
NEW
559
            quote! {
×
NEW
560
                match #from_zval {
×
NEW
561
                    Some(val) => val,
×
NEW
562
                    None => {
×
NEW
563
                        ::ext_php_rs::exception::PhpException::default(
×
NEW
564
                            concat!("Invalid value given for argument `", stringify!(#name), "`.").into()
×
NEW
565
                        ).throw().expect("Failed to throw PHP exception.");
×
NEW
566
                        return;
×
567
                    }
568
                }
569
            }
570
        };
571

NEW
572
        let throw_invalid = quote! {
×
NEW
573
            ::ext_php_rs::exception::PhpException::default(
×
NEW
574
                concat!("Invalid value given for argument `", stringify!(#name), "`.").into()
×
NEW
575
            ).throw().expect("Failed to throw PHP exception.");
×
NEW
576
            return;
×
577
        };
578

NEW
579
        let throw_null = quote! {
×
NEW
580
            ::ext_php_rs::exception::PhpException::new(
×
NEW
581
                concat!("Argument `$", stringify!(#name), "` must not be null").into(),
×
NEW
582
                0,
×
NEW
583
                ::ext_php_rs::zend::ce::type_error(),
×
NEW
584
            ).throw().expect("Failed to throw PHP exception.");
×
NEW
585
            return;
×
586
        };
587

588
        // Required arg — always present
NEW
589
        if i < min_num_args {
×
NEW
590
            return quote! {
×
NEW
591
                #read_zval
×
NEW
592
                let #name: #binding_ty = #convert;
×
593
            };
594
        }
595

596
        // Optional arg — may be omitted
NEW
597
        let fallback = match (&arg.default, arg.nullable) {
×
NEW
598
            (Some(expr), _) => quote! { #expr },
×
NEW
599
            (None, true) => quote! { None },
×
NEW
600
            (None, false) => throw_invalid.clone(),
×
601
        };
602

603
        // Non-nullable with default: explicit null must throw TypeError
NEW
604
        if !arg.nullable && arg.default.is_some() {
×
NEW
605
            return quote! {
×
NEW
606
                let #name: #binding_ty = if __num_args > #i {
×
NEW
607
                    #read_zval
×
NEW
608
                    if #zval_ident.is_null() { #throw_null }
×
NEW
609
                    #convert
×
610
                } else {
NEW
611
                    #fallback
×
612
                };
613
            };
614
        }
615

NEW
616
        quote! {
×
NEW
617
            let #name: #binding_ty = if __num_args > #i {
×
NEW
618
                #read_zval
×
NEW
619
                #convert
×
620
            } else {
NEW
621
                #fallback
×
622
            };
623
        }
624
    }
625

NEW
626
    fn build_fast_count_check(min_num_args: usize, max_num_args: usize) -> TokenStream {
×
NEW
627
        let min_u32 = u32::try_from(min_num_args).expect("too many args");
×
NEW
628
        let max_u32 = u32::try_from(max_num_args).expect("too many args");
×
629

NEW
630
        if min_num_args == max_num_args {
×
NEW
631
            quote! {
×
NEW
632
                let __num_args = unsafe { ex.This.u2.num_args } as usize;
×
NEW
633
                if __num_args != #min_num_args {
×
NEW
634
                    unsafe {
×
NEW
635
                        ::ext_php_rs::ffi::zend_wrong_parameters_count_error(#min_u32, #max_u32);
×
636
                    };
NEW
637
                    return;
×
638
                }
639
            }
640
        } else {
NEW
641
            quote! {
×
NEW
642
                let __num_args = unsafe { ex.This.u2.num_args } as usize;
×
NEW
643
                if !(#min_num_args..=#max_num_args).contains(&__num_args) {
×
NEW
644
                    unsafe {
×
NEW
645
                        ::ext_php_rs::ffi::zend_wrong_parameters_count_error(#min_u32, #max_u32);
×
646
                    };
NEW
647
                    return;
×
648
                }
649
            }
650
        }
651
    }
652

NEW
653
    fn build_fast_handler_body(&self, call_type: &CallType) -> TokenStream {
×
NEW
654
        let ident = self.ident;
×
NEW
655
        let (required, _not_required) = self.args.split_args(self.optional.as_ref());
×
NEW
656
        let min_num_args = required.len();
×
NEW
657
        let max_num_args = self.args.typed.len();
×
658

659
        // Arg count validation (matches zend_wrong_parameters_count_error)
NEW
660
        let count_check = Self::build_fast_count_check(min_num_args, max_num_args);
×
661

NEW
662
        let arg_bindings: Vec<TokenStream> = self
×
NEW
663
            .args
×
NEW
664
            .typed
×
665
            .iter()
666
            .enumerate()
NEW
667
            .map(|(i, arg)| Self::build_fast_arg_binding(i, arg, min_num_args))
×
668
            .collect();
669

NEW
670
        let arg_names: Vec<_> = self.args.typed.iter().map(|arg| arg.name).collect();
×
671

NEW
672
        let this_error = quote! {
×
NEW
673
            ::ext_php_rs::exception::PhpException::default(
×
NEW
674
                "Failed to retrieve reference to `$this`".into()
×
NEW
675
            ).throw().unwrap();
×
NEW
676
            return;
×
677
        };
678

NEW
679
        let returns_this = returns_self_ref(self.output);
×
680

NEW
681
        let (this_binding, call) = match call_type {
×
NEW
682
            CallType::Function => (quote! {}, quote! { #ident(#(#arg_names),*) }),
×
NEW
683
            CallType::Method {
×
NEW
684
                class,
×
NEW
685
                receiver: MethodReceiver::Static,
×
NEW
686
                ..
×
NEW
687
            } => (quote! {}, quote! { #class::#ident(#(#arg_names),*) }),
×
NEW
688
            CallType::Method {
×
NEW
689
                class,
×
NEW
690
                receiver: MethodReceiver::Class,
×
NEW
691
                ..
×
NEW
692
            } => (
×
NEW
693
                quote! {
×
NEW
694
                    let __this = match ex.get_object::<#class>() {
×
NEW
695
                        Some(v) => v,
×
NEW
696
                        None => { #this_error }
×
697
                    };
698
                },
NEW
699
                if returns_this {
×
NEW
700
                    quote! { let _ = __this.#ident(#(#arg_names),*); }
×
701
                } else {
NEW
702
                    quote! { __this.#ident(#(#arg_names),*) }
×
703
                },
704
            ),
NEW
705
            CallType::Method {
×
NEW
706
                class,
×
NEW
707
                receiver: MethodReceiver::ZendClassObject,
×
NEW
708
                ..
×
NEW
709
            } => (
×
NEW
710
                quote! {
×
NEW
711
                    let __this = match ex.get_object::<#class>() {
×
NEW
712
                        Some(v) => v,
×
NEW
713
                        None => { #this_error }
×
714
                    };
715
                },
NEW
716
                if returns_this {
×
NEW
717
                    quote! { { let _ = #class::#ident(__this, #(#arg_names),*); } }
×
718
                } else {
NEW
719
                    quote! { #class::#ident(__this, #(#arg_names),*) }
×
720
                },
721
            ),
722
        };
723

NEW
724
        if returns_this {
×
NEW
725
            quote! {
×
NEW
726
                use ::ext_php_rs::convert::{FromZvalMut, IntoZval};
×
727

NEW
728
                #count_check
×
NEW
729
                #(#arg_bindings)*
×
NEW
730
                #this_binding
×
NEW
731
                #call
×
732

NEW
733
                if let Err(e) = __this.set_zval(retval, false) {
×
NEW
734
                    let e: ::ext_php_rs::exception::PhpException = e.into();
×
NEW
735
                    e.throw().expect("Failed to throw PHP exception.");
×
736
                }
737
            }
738
        } else {
NEW
739
            quote! {
×
NEW
740
                use ::ext_php_rs::convert::{FromZvalMut, IntoZval};
×
741

NEW
742
                #count_check
×
NEW
743
                #(#arg_bindings)*
×
NEW
744
                #this_binding
×
NEW
745
                let __result = { #call };
×
746

NEW
747
                if let Err(e) = __result.set_zval(retval, false) {
×
NEW
748
                    let e: ::ext_php_rs::exception::PhpException = e.into();
×
NEW
749
                    e.throw().expect("Failed to throw PHP exception.");
×
750
                }
751
            }
752
        }
753
    }
754

755
    /// Generates a struct and impl for the `PhpFunction` trait.
756
    pub fn php_function_impl(&self) -> TokenStream {
×
757
        let internal_ident = self.internal_ident();
×
758
        let builder = self.function_builder(&CallType::Function);
×
759

760
        quote! {
×
761
            #[doc(hidden)]
×
762
            #[allow(non_camel_case_types)]
×
763
            struct #internal_ident;
×
764

765
            impl ::ext_php_rs::internal::function::PhpFunction for #internal_ident {
×
766
                const FUNCTION_ENTRY: fn() -> ::ext_php_rs::builders::FunctionBuilder<'static> = {
×
767
                    fn entry() -> ::ext_php_rs::builders::FunctionBuilder<'static>
×
768
                    {
769
                        #builder
×
770
                    }
771
                    entry
×
772
                };
773
            }
774
        }
775
    }
776

777
    /// Returns a constructor metadata object for this function. This doesn't
778
    /// check if the function is a constructor, however.
779
    pub fn constructor_meta(
×
780
        &self,
781
        class: &syn::Path,
782
        visibility: Option<&Visibility>,
783
    ) -> TokenStream {
784
        let ident = self.ident;
×
785
        let (required, not_required) = self.args.split_args(self.optional.as_ref());
×
786
        let required_args = required
×
787
            .iter()
788
            .map(TypedArg::arg_builder)
×
789
            .collect::<Vec<_>>();
790
        let not_required_args = not_required
×
791
            .iter()
792
            .map(TypedArg::arg_builder)
×
793
            .collect::<Vec<_>>();
794

795
        let required_arg_names: Vec<_> = required.iter().map(|arg| arg.name).collect();
×
796
        let not_required_arg_names: Vec<_> = not_required.iter().map(|arg| arg.name).collect();
×
797
        let arg_declarations = self
×
798
            .args
×
799
            .typed
×
800
            .iter()
801
            .map(TypedArg::arg_declaration)
×
802
            .collect::<Vec<_>>();
803
        let variadic_bindings = self.args.typed.iter().filter_map(|arg| {
×
804
            if arg.variadic {
×
805
                let name = arg.name;
×
806
                let variadic_name = format_ident!("__variadic_{}", name);
×
807
                let clean_ty = arg.clean_ty();
×
808
                Some(quote! {
×
809
                    let #variadic_name = #name.variadic_vals::<#clean_ty>();
×
810
                })
811
            } else {
812
                None
×
813
            }
814
        });
815
        let arg_accessors = self.args.typed.iter().map(|arg| {
×
816
            arg.accessor(
×
817
                |e| quote! { return ::ext_php_rs::class::ConstructorResult::Exception(#e); },
×
818
            )
819
        });
820
        let variadic = self.args.typed.iter().any(|arg| arg.variadic).then(|| {
×
821
            quote! {
×
822
                .variadic()
×
823
            }
824
        });
825
        let docs = &self.docs;
×
826
        let flags = visibility.option_tokens();
×
827

828
        quote! {
×
829
            ::ext_php_rs::class::ConstructorMeta {
×
830
                constructor: {
×
831
                    fn inner(ex: &mut ::ext_php_rs::zend::ExecuteData) -> ::ext_php_rs::class::ConstructorResult<#class> {
×
832
                        use ::ext_php_rs::zend::try_catch;
×
833
                        use ::std::panic::AssertUnwindSafe;
×
834

835
                        // Wrap the constructor body with try_catch to ensure Rust destructors
836
                        // are called if a bailout occurs (issue #537)
837
                        let catch_result = try_catch(AssertUnwindSafe(|| {
×
838
                            #(#arg_declarations)*
×
839
                            let parse = ex.parser()
×
840
                                #(.arg(&mut #required_arg_names))*
×
841
                                .not_required()
×
842
                                #(.arg(&mut #not_required_arg_names))*
×
843
                                #variadic
×
844
                                .parse();
×
845
                            if parse.is_err() {
×
846
                                return ::ext_php_rs::class::ConstructorResult::ArgError;
×
847
                            }
848
                            #(#variadic_bindings)*
×
849
                            #class::#ident(#({#arg_accessors}),*).into()
×
850
                        }));
851

852
                        // If there was a bailout, run BailoutGuard cleanups and re-trigger
853
                        match catch_result {
×
854
                            Ok(result) => result,
×
855
                            Err(_) => {
×
856
                                ::ext_php_rs::zend::run_bailout_cleanups();
×
857
                                unsafe { ::ext_php_rs::zend::bailout() }
×
858
                            }
859
                        }
860
                    }
861
                    inner
×
862
                },
863
                build_fn: {
×
864
                    fn inner(func: ::ext_php_rs::builders::FunctionBuilder) -> ::ext_php_rs::builders::FunctionBuilder {
×
865
                        func
×
866
                            .docs(&[#(#docs),*])
×
867
                            #(.arg(#required_args))*
×
868
                            .not_required()
×
869
                            #(.arg(#not_required_args))*
×
870
                            #variadic
×
871
                    }
872
                    inner
×
873
                },
874
                flags: #flags
×
875
            }
876
        }
877
    }
878
}
879

880
#[derive(Debug)]
881
pub struct ReceiverArg {
882
    pub _mutable: bool,
883
    pub span: Span,
884
}
885

886
#[derive(Debug)]
887
pub struct TypedArg<'a> {
888
    pub name: &'a Ident,
889
    pub ty: Type,
890
    pub nullable: bool,
891
    pub default: Option<Expr>,
892
    pub as_ref: bool,
893
    pub variadic: bool,
894
}
895

896
#[derive(Debug)]
897
pub struct Args<'a> {
898
    pub receiver: Option<ReceiverArg>,
899
    pub typed: Vec<TypedArg<'a>>,
900
}
901

902
impl<'a> Args<'a> {
903
    pub fn parse_from_fnargs(
3✔
904
        args: impl Iterator<Item = &'a FnArg>,
905
        mut defaults: HashMap<Ident, Expr>,
906
    ) -> Result<Self> {
907
        let mut result = Self {
908
            receiver: None,
909
            typed: vec![],
3✔
910
        };
911
        for arg in args {
8✔
912
            match arg {
5✔
913
                FnArg::Receiver(receiver) => {
3✔
914
                    if receiver.reference.is_none() {
6✔
915
                        bail!(receiver => "PHP objects are heap-allocated and cannot be passed by value. Try using `&self` or `&mut self`.");
×
916
                    } else if result.receiver.is_some() {
6✔
917
                        bail!(receiver => "Too many receivers specified.")
×
918
                    }
919
                    result.receiver.replace(ReceiverArg {
9✔
920
                        _mutable: receiver.mutability.is_some(),
9✔
921
                        span: receiver.span(),
3✔
922
                    });
923
                }
924
                FnArg::Typed(PatType { pat, ty, .. }) => {
4✔
925
                    let syn::Pat::Ident(syn::PatIdent { ident, .. }) = &**pat else {
4✔
926
                        bail!(pat => "Unsupported argument.");
×
927
                    };
928

929
                    // If the variable is `&[&Zval]` treat it as the variadic argument.
930
                    let default = defaults.remove(ident);
8✔
931
                    let nullable = type_is_nullable(ty.as_ref())?;
6✔
932
                    let (variadic, as_ref, ty) = Self::parse_typed(ty);
8✔
933
                    result.typed.push(TypedArg {
6✔
934
                        name: ident,
4✔
935
                        ty,
4✔
936
                        nullable,
4✔
937
                        default,
4✔
938
                        as_ref,
2✔
939
                        variadic,
2✔
940
                    });
941
                }
942
            }
943
        }
944
        Ok(result)
3✔
945
    }
946

947
    fn parse_typed(ty: &Type) -> (bool, bool, Type) {
2✔
948
        match ty {
2✔
949
            Type::Reference(ref_) => {
×
950
                let as_ref = ref_.mutability.is_some();
×
951
                match ref_.elem.as_ref() {
×
952
                    Type::Slice(slice) => (
×
953
                        // TODO: Allow specifying the variadic type.
954
                        slice.elem.to_token_stream().to_string() == "& Zval",
×
955
                        as_ref,
×
956
                        ty.clone(),
×
957
                    ),
958
                    _ => (false, as_ref, ty.clone()),
×
959
                }
960
            }
961
            Type::Path(TypePath { path, .. }) => {
2✔
962
                let mut as_ref = false;
4✔
963

964
                // For for types that are `Option<&mut T>` to turn them into
965
                // `Option<&T>`, marking the Arg as as "passed by reference".
966
                let ty = path
4✔
967
                    .segments
2✔
968
                    .last()
969
                    .filter(|seg| seg.ident == "Option")
6✔
970
                    .and_then(|seg| {
2✔
971
                        if let PathArguments::AngleBracketed(args) = &seg.arguments {
×
972
                            args.args
×
973
                                .iter()
×
974
                                .find(|arg| matches!(arg, GenericArgument::Type(_)))
×
975
                                .and_then(|ga| match ga {
×
976
                                    GenericArgument::Type(ty) => Some(match ty {
×
977
                                        Type::Reference(r) => {
×
978
                                            // Only mark as_ref for mutable references
979
                                            // (Option<&mut T>), not immutable ones (Option<&T>)
980
                                            as_ref = r.mutability.is_some();
×
981
                                            let mut new_ref = r.clone();
×
982
                                            new_ref.mutability = None;
×
983
                                            Type::Reference(new_ref)
×
984
                                        }
985
                                        _ => ty.clone(),
×
986
                                    }),
987
                                    _ => None,
×
988
                                })
989
                        } else {
990
                            None
×
991
                        }
992
                    })
993
                    .unwrap_or_else(|| ty.clone());
6✔
994
                (false, as_ref, ty.clone())
4✔
995
            }
996
            _ => (false, false, ty.clone()),
×
997
        }
998
    }
999

1000
    /// Splits the typed arguments into two slices:
1001
    ///
1002
    /// 1. Required arguments.
1003
    /// 2. Non-required arguments.
1004
    ///
1005
    /// # Parameters
1006
    ///
1007
    /// * `optional` - The first optional argument. If [`None`], the optional
1008
    ///   arguments will be from the first optional argument (nullable or has
1009
    ///   default) after the last required argument to the end of the arguments.
1010
    pub fn split_args(&self, optional: Option<&Ident>) -> (&[TypedArg<'a>], &[TypedArg<'a>]) {
3✔
1011
        let mut mid = None;
6✔
1012
        for (i, arg) in self.typed.iter().enumerate() {
10✔
1013
            // An argument is optional if it's nullable (Option<T>) or has a default value.
1014
            let is_optional = arg.nullable || arg.default.is_some();
8✔
1015
            if let Some(optional) = optional {
2✔
1016
                if optional == arg.name {
×
1017
                    mid.replace(i);
×
1018
                }
1019
            } else if mid.is_none() && is_optional {
6✔
1020
                mid.replace(i);
×
1021
            } else if !is_optional {
4✔
1022
                mid.take();
2✔
1023
            }
1024
        }
1025
        match mid {
3✔
1026
            Some(mid) => (&self.typed[..mid], &self.typed[mid..]),
×
1027
            None => (&self.typed[..], &self.typed[0..0]),
6✔
1028
        }
1029
    }
1030
}
1031

1032
impl TypedArg<'_> {
1033
    /// Returns a 'clean type' with the lifetimes removed. This allows the type
1034
    /// to be used outside of the original function context.
1035
    fn clean_ty(&self) -> Type {
2✔
1036
        let mut ty = self.ty.clone();
6✔
1037
        ty.drop_lifetimes();
4✔
1038

1039
        // Variadic arguments are passed as &[&Zval], so we need to extract the
1040
        // inner type.
1041
        if self.variadic {
2✔
1042
            let Type::Reference(reference) = &ty else {
×
1043
                return ty;
×
1044
            };
1045

1046
            if let Type::Slice(inner) = &*reference.elem {
×
1047
                return *inner.elem.clone();
×
1048
            }
1049
        }
1050

1051
        ty
2✔
1052
    }
1053

1054
    /// Returns a token stream containing an argument declaration, where the
1055
    /// name of the variable holding the arg is the name of the argument.
1056
    fn arg_declaration(&self) -> TokenStream {
×
1057
        let name = self.name;
×
1058
        let val = self.arg_builder();
×
1059
        quote! {
×
1060
            let mut #name = #val;
1061
        }
1062
    }
1063

1064
    /// Returns a token stream containing the `Arg` definition to be passed to
1065
    /// `ext-php-rs`.
1066
    fn arg_builder(&self) -> TokenStream {
2✔
1067
        let name = ident_to_php_name(self.name);
6✔
1068
        let ty = self.clean_ty();
6✔
1069
        let null = if self.nullable {
4✔
1070
            Some(quote! { .allow_null() })
×
1071
        } else {
1072
            None
2✔
1073
        };
1074
        let default = self.default.as_ref().map(|val| {
8✔
1075
            let val = expr_to_php_stub(val);
×
1076
            quote! {
×
1077
                .default(#val)
1078
            }
1079
        });
1080
        let as_ref = if self.as_ref {
4✔
1081
            Some(quote! { .as_ref() })
×
1082
        } else {
1083
            None
2✔
1084
        };
1085
        let variadic = self.variadic.then(|| quote! { .is_variadic() });
6✔
1086
        quote! {
2✔
1087
            ::ext_php_rs::args::Arg::new(#name, <#ty as ::ext_php_rs::convert::FromZvalMut>::TYPE)
1088
                #null
1089
                #default
1090
                #as_ref
1091
                #variadic
1092
        }
1093
    }
1094

1095
    /// Get the accessor used to access the value of the argument.
1096
    fn accessor(&self, bail_fn: impl Fn(TokenStream) -> TokenStream) -> TokenStream {
×
1097
        let name = self.name;
×
1098
        if let Some(default) = &self.default {
×
1099
            if self.nullable {
×
1100
                // For nullable types with defaults, null is acceptable
1101
                quote! {
×
1102
                    #name.val().unwrap_or(#default.into())
×
1103
                }
1104
            } else {
1105
                // For non-nullable types with defaults:
1106
                // - If argument was omitted: use default
1107
                // - If null was explicitly passed: throw TypeError
1108
                // - If a value was passed: try to convert it
1109
                let bail_null = bail_fn(quote! {
×
1110
                    ::ext_php_rs::exception::PhpException::new(
×
1111
                        concat!("Argument `$", stringify!(#name), "` must not be null").into(),
×
1112
                        0,
×
1113
                        ::ext_php_rs::zend::ce::type_error(),
×
1114
                    )
1115
                });
1116
                let bail_invalid = bail_fn(quote! {
×
1117
                    ::ext_php_rs::exception::PhpException::default(
×
1118
                        concat!("Invalid value given for argument `", stringify!(#name), "`.").into()
×
1119
                    )
1120
                });
1121
                quote! {
×
1122
                    match #name.zval() {
×
1123
                        Some(zval) if zval.is_null() => {
×
1124
                            // Null was explicitly passed to a non-nullable parameter
1125
                            #bail_null
×
1126
                        }
1127
                        Some(_) => {
×
1128
                            // A value was passed, try to convert it
1129
                            match #name.val() {
×
1130
                                Some(val) => val,
×
1131
                                None => {
×
1132
                                    #bail_invalid
×
1133
                                }
1134
                            }
1135
                        }
1136
                        None => {
×
1137
                            // Argument was omitted, use default
1138
                            #default.into()
×
1139
                        }
1140
                    }
1141
                }
1142
            }
1143
        } else if self.variadic {
×
1144
            let variadic_name = format_ident!("__variadic_{}", name);
×
1145
            quote! {
×
1146
                #variadic_name.as_slice()
×
1147
            }
1148
        } else if self.nullable {
×
1149
            // Originally I thought we could just use the below case for `null` options, as
1150
            // `val()` will return `Option<Option<T>>`, however, this isn't the case when
1151
            // the argument isn't given, as the underlying zval is null.
1152
            quote! {
×
1153
                #name.val()
×
1154
            }
1155
        } else {
1156
            let bail = bail_fn(quote! {
×
1157
                ::ext_php_rs::exception::PhpException::default(
×
1158
                    concat!("Invalid value given for argument `", stringify!(#name), "`.").into()
×
1159
                )
1160
            });
1161
            quote! {
×
1162
                match #name.val() {
×
1163
                    Some(val) => val,
×
1164
                    None => {
×
1165
                        #bail;
×
1166
                    }
1167
                }
1168
            }
1169
        }
1170
    }
1171
}
1172

1173
/// Converts a Rust expression to a PHP stub-compatible default value string.
1174
///
1175
/// This function handles common Rust patterns and converts them to valid PHP
1176
/// syntax for use in generated stub files:
1177
///
1178
/// - `None` → `"null"`
1179
/// - `Some(expr)` → converts the inner expression
1180
/// - `42`, `3.14` → numeric literals as-is
1181
/// - `true`/`false` → as-is
1182
/// - `"string"` → `"string"`
1183
/// - `"string".to_string()` or `String::from("string")` → `"string"`
1184
fn expr_to_php_stub(expr: &Expr) -> String {
14✔
1185
    match expr {
14✔
1186
        // Handle None -> null
1187
        Expr::Path(path) => {
1✔
1188
            let path_str = path.path.to_token_stream().to_string();
3✔
1189
            if path_str == "None" {
1✔
1190
                "null".to_string()
2✔
1191
            } else if path_str == "true" || path_str == "false" {
×
1192
                path_str
×
1193
            } else {
1194
                // For other paths (constants, etc.), use the raw representation
1195
                path_str
×
1196
            }
1197
        }
1198

1199
        // Handle Some(expr) -> convert inner expression
1200
        Expr::Call(call) => {
1✔
1201
            if let Expr::Path(func_path) = &*call.func {
2✔
1202
                let func_name = func_path.path.to_token_stream().to_string();
3✔
1203

1204
                // Some(value) -> convert inner value
1205
                if func_name == "Some"
1✔
1206
                    && let Some(arg) = call.args.first()
2✔
1207
                {
1208
                    return expr_to_php_stub(arg);
2✔
1209
                }
1210

1211
                // String::from("...") -> "..."
1212
                if (func_name == "String :: from" || func_name == "String::from")
×
1213
                    && let Some(arg) = call.args.first()
×
1214
                {
1215
                    return expr_to_php_stub(arg);
×
1216
                }
1217
            }
1218

1219
            // Default: use raw representation
1220
            expr.to_token_stream().to_string()
×
1221
        }
1222

1223
        // Handle method calls like "string".to_string()
1224
        Expr::MethodCall(method_call) => {
×
1225
            let method_name = method_call.method.to_string();
×
1226

1227
            // "...".to_string() or "...".to_owned() or "...".into() -> "..."
1228
            if method_name == "to_string" || method_name == "to_owned" || method_name == "into" {
×
1229
                return expr_to_php_stub(&method_call.receiver);
×
1230
            }
1231

1232
            // Default: use raw representation
1233
            expr.to_token_stream().to_string()
×
1234
        }
1235

1236
        // String literals -> keep as-is (already valid PHP)
1237
        Expr::Lit(lit) => match &lit.lit {
20✔
1238
            syn::Lit::Str(s) => format!(
×
1239
                "\"{}\"",
1240
                s.value().replace('\\', "\\\\").replace('"', "\\\"")
×
1241
            ),
1242
            // Use base10_digits() to strip Rust type suffixes like _usize, _i32, etc.
1243
            syn::Lit::Int(i) => i.base10_digits().to_string(),
18✔
1244
            syn::Lit::Float(f) => f.base10_digits().to_string(),
12✔
1245
            syn::Lit::Bool(b) => if b.value { "true" } else { "false" }.to_string(),
×
1246
            syn::Lit::Char(c) => format!("\"{}\"", c.value()),
×
1247
            _ => expr.to_token_stream().to_string(),
×
1248
        },
1249

1250
        // Handle arrays: [] or vec![]
1251
        Expr::Array(arr) => {
×
1252
            if arr.elems.is_empty() {
×
1253
                "[]".to_string()
×
1254
            } else {
1255
                let elems: Vec<String> = arr.elems.iter().map(expr_to_php_stub).collect();
×
1256
                format!("[{}]", elems.join(", "))
×
1257
            }
1258
        }
1259

1260
        // Handle vec![] macro
1261
        Expr::Macro(m) => {
×
1262
            let macro_name = m.mac.path.to_token_stream().to_string();
×
1263
            if macro_name == "vec" {
×
1264
                let tokens = m.mac.tokens.to_string();
×
1265
                if tokens.trim().is_empty() {
×
1266
                    return "[]".to_string();
×
1267
                }
1268
            }
1269
            // Default: use raw representation
1270
            expr.to_token_stream().to_string()
×
1271
        }
1272

1273
        // Handle unary expressions like -42
1274
        Expr::Unary(unary) => {
2✔
1275
            let inner = expr_to_php_stub(&unary.expr);
6✔
1276
            format!("{}{}", unary.op.to_token_stream(), inner)
6✔
1277
        }
1278

1279
        // Default: use raw representation
1280
        _ => expr.to_token_stream().to_string(),
×
1281
    }
1282
}
1283

1284
/// Returns true if the given type is nullable in PHP (i.e., it's an
1285
/// `Option<T>`).
1286
///
1287
/// Note: Having a default value does NOT make a type nullable. A parameter with
1288
/// a default value is optional (can be omitted), but passing `null` explicitly
1289
/// should still be rejected unless the type is `Option<T>`.
1290
// TODO(david): Eventually move to compile-time constants for this (similar to
1291
// FromZval::NULLABLE).
1292
pub fn type_is_nullable(ty: &Type) -> Result<bool> {
2✔
1293
    Ok(match ty {
2✔
1294
        Type::Path(path) => path
4✔
1295
            .path
2✔
1296
            .segments
2✔
1297
            .iter()
2✔
1298
            .next_back()
2✔
1299
            .is_some_and(|seg| seg.ident == "Option"),
6✔
1300
        Type::Reference(_) => false, /* Reference cannot be nullable unless */
×
1301
        // wrapped in `Option` (in that case it'd be a Path).
1302
        _ => bail!(ty => "Unsupported argument type."),
×
1303
    })
1304
}
1305

1306
#[cfg(test)]
1307
mod tests {
1308
    use super::*;
1309

1310
    #[test]
1311
    fn test_expr_to_php_stub_strips_numeric_suffixes() {
1312
        // Test integer suffixes are stripped (issue #492)
1313
        let expr: Expr = syn::parse_quote!(42_usize);
1314
        assert_eq!(expr_to_php_stub(&expr), "42");
1315

1316
        let expr: Expr = syn::parse_quote!(42_i32);
1317
        assert_eq!(expr_to_php_stub(&expr), "42");
1318

1319
        let expr: Expr = syn::parse_quote!(42_u64);
1320
        assert_eq!(expr_to_php_stub(&expr), "42");
1321

1322
        // Test float suffixes are stripped
1323
        let expr: Expr = syn::parse_quote!(3.14_f64);
1324
        assert_eq!(expr_to_php_stub(&expr), "3.14");
1325

1326
        let expr: Expr = syn::parse_quote!(3.14_f32);
1327
        assert_eq!(expr_to_php_stub(&expr), "3.14");
1328

1329
        // Test literals without suffixes still work
1330
        let expr: Expr = syn::parse_quote!(42);
1331
        assert_eq!(expr_to_php_stub(&expr), "42");
1332

1333
        let expr: Expr = syn::parse_quote!(3.14);
1334
        assert_eq!(expr_to_php_stub(&expr), "3.14");
1335
    }
1336

1337
    #[test]
1338
    fn test_expr_to_php_stub_negative_numbers() {
1339
        let expr: Expr = syn::parse_quote!(-42_i32);
1340
        assert_eq!(expr_to_php_stub(&expr), "-42");
1341

1342
        let expr: Expr = syn::parse_quote!(-3.14_f64);
1343
        assert_eq!(expr_to_php_stub(&expr), "-3.14");
1344
    }
1345

1346
    #[test]
1347
    fn test_expr_to_php_stub_none_and_some() {
1348
        let expr: Expr = syn::parse_quote!(None);
1349
        assert_eq!(expr_to_php_stub(&expr), "null");
1350

1351
        let expr: Expr = syn::parse_quote!(Some(42_usize));
1352
        assert_eq!(expr_to_php_stub(&expr), "42");
1353
    }
1354
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc