Skip to main content

ext_php_rs_derive/
lib.rs

1//! Macros for the `php-ext` crate.
2mod class;
3mod constant;
4mod enum_;
5mod extern_;
6mod fastcall;
7mod function;
8mod helpers;
9mod impl_;
10mod impl_interface;
11mod interface;
12mod module;
13mod parsing;
14mod syn_ext;
15mod zval;
16
17use darling::FromMeta;
18use proc_macro::TokenStream;
19use proc_macro2::TokenStream as TokenStream2;
20use syn::{
21    DeriveInput, ItemConst, ItemEnum, ItemFn, ItemForeignMod, ItemImpl, ItemStruct, ItemTrait,
22};
23
24extern crate proc_macro;
25
26// BEGIN DOCS FROM classes.md
27/// # `#[php_class]` Attribute
28///
29/// Structs can be exported to PHP as classes with the `#[php_class]` attribute
30/// macro. This attribute derives the `RegisteredClass` trait on your struct, as
31/// well as registering the class to be registered with the `#[php_module]`
32/// macro.
33///
34/// ## Options
35///
36/// There are additional macros that modify the class. These macros **must** be
37/// placed underneath the `#[php_class]` attribute.
38///
39/// - `name` - Changes the name of the class when exported to PHP. The Rust
40///   struct name is kept the same. If no name is given, the name of the struct
41///   is used. Useful for namespacing classes.
42/// - `change_case` - Changes the case of the class name when exported to PHP.
43/// - `readonly` - Marks the class as readonly (PHP 8.2+). All properties in a
44///   readonly class are implicitly readonly.
45/// - `flags` - Sets class flags using `ClassFlags`, e.g. `#[php(flags =
46///   ClassFlags::Final)]` for a final class.
47/// - `#[php(extends(...))]` - Sets the parent class of the class. Can only be
48///   used once. Two forms are supported:
49///   - Simple type form: `#[php(extends(MyBaseClass))]` - For Rust-defined
50///     classes that implement `RegisteredClass`.
51///   - Explicit form: `#[php(extends(ce = ce_fn, stub = "ParentClass"))]` - For
52///     built-in PHP classes. `ce_fn` must be a function with the signature
53///     `fn() -> &'static ClassEntry`.
54/// - `#[php(implements(...))]` - Implements the given interface on the class.
55///   Can be used multiple times. Two forms are supported:
56///   - Simple type form: `#[php(implements(MyInterface))]` — For Rust-defined
57///     interfaces that implement `RegisteredClass`.
58///   - Explicit form: `#[php(implements(ce = ce_fn, stub = "InterfaceName"))]`
59///     — For built-in PHP interfaces. `ce_fn` must be a valid function with the
60///     signature `fn() -> &'static ClassEntry`.
61/// - Generated stubs write each parent class and interface name as a fully
62///   qualified name, with a leading `\`. A leading `\` in `stub = "..."` is
63///   optional.
64///
65/// You may also use the `#[php(prop)]` attribute on a struct field to use the
66/// field as a PHP property. By default, the property is public and its name is
67/// the field name converted to camelCase: the field `first_name` gives the
68/// property `firstName`. Property types must implement `IntoZval` and
69/// `FromZval`.
70///
71/// You can customize properties with these options:
72///
73/// - `name` - Allows you to rename the property, e.g. `#[php(prop, name =
74///   "new_name")]`
75/// - `change_case` - Allows you to rename the property using rename rules, e.g.
76///   `#[php(prop, change_case = "PascalCase")]`
77/// - `static` - Makes the property static (shared across all instances), e.g.
78///   `#[php(prop, static)]`
79/// - `flags` - Sets property visibility flags, e.g. `#[php(prop, flags =
80///   ext_php_rs::flags::PropertyFlags::Private)]`
81///
82/// ### How PHP code uses a Rust property
83///
84/// The value of a `#[php(prop)]` field is in the Rust struct. PHP reads the
85/// value with the getter and writes the value with the setter.
86///
87/// These operations use the getter and the setter:
88///
89/// - `$obj->prop += 1`, `$obj->prop .= 'x'`, `$obj->prop++` and `$obj->prop ??=
90///   1`.
91/// - `==` and `foreach`, which use the current Rust values. Thus `==` calls
92///   each getter of the two objects.
93///
94/// These operations change only a copy, and the Rust value does not change:
95///
96/// - `$obj->prop[] = 1`, `$obj->prop['key'] = 1` and `$r = &$obj->prop`. PHP
97///   gives the notice "Indirect modification of overloaded property", as for
98///   `__get`.
99/// - `foreach ($obj as &$value)`.
100///
101/// These operations throw an `Error`:
102///
103/// - `$obj->prop = &$value`.
104/// - `unset($obj->prop)`, because the Rust value cannot be removed.
105///
106/// If a PHP subclass declares the property again with hooks, PHP uses the Rust
107/// getter and setter, not the hooks.
108///
109/// ## Restrictions
110///
111/// ### No lifetime parameters
112///
113/// Rust lifetimes are used by the Rust compiler to reason about a program's
114/// memory safety. They are a compile-time only concept;
115/// there is no way to access Rust lifetimes at runtime from a dynamic language
116/// like PHP.
117///
118/// As soon as Rust data is exposed to PHP,
119/// there is no guarantee which the Rust compiler can make on how long the data
120/// will live. PHP is a reference-counted language and those references can be
121/// held for an arbitrarily long time, which is untraceable by the Rust
122/// compiler. The only possible way to express this correctly is to require that
123/// any `#[php_class]` does not borrow data for any lifetime shorter than the
124/// `'static` lifetime, i.e. the `#[php_class]` cannot have any lifetime
125/// parameters.
126///
127/// When you need to share ownership of data between PHP and Rust,
128/// instead of using borrowed references with lifetimes, consider using
129/// reference-counted smart pointers such as [Arc](https://doc.rust-lang.org/std/sync/struct.Arc.html).
130///
131/// ### No generic parameters
132///
133/// A Rust struct `Foo<T>` with a generic parameter `T` generates new compiled
134/// implementations each time it is used with a different concrete type for `T`.
135/// These new implementations are generated by the compiler at each usage site.
136/// This is incompatible with wrapping `Foo` in PHP,
137/// where there needs to be a single compiled implementation of `Foo` which is
138/// integrated with the PHP interpreter.
139///
140/// ## Example
141///
142/// This example creates a PHP class `Human`, adding a PHP property `address`.
143///
144/// ```rust,no_run,ignore
145/// # #![cfg_attr(windows, feature(abi_vectorcall))]
146/// # extern crate ext_php_rs;
147/// use ext_php_rs::prelude::*;
148///
149/// #[php_class]
150/// pub struct Human {
151///     name: String,
152///     age: i32,
153///     #[php(prop)]
154///     address: String,
155/// }
156///
157/// #[php_module]
158/// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
159///     module.class::<Human>()
160/// }
161/// # fn main() {}
162/// ```
163///
164/// Create a custom exception `RedisException`, which extends `Exception`, and
165/// put it in the `Redis\Exception` namespace:
166///
167/// ```rust,no_run,ignore
168/// # #![cfg_attr(windows, feature(abi_vectorcall))]
169/// # extern crate ext_php_rs;
170/// use ext_php_rs::{
171///     prelude::*,
172///     exception::PhpException,
173///     zend::ce
174/// };
175///
176/// #[php_class]
177/// #[php(name = "Redis\\Exception\\RedisException")]
178/// #[php(extends(ce = ce::exception, stub = "\\Exception"))]
179/// #[derive(Default)]
180/// pub struct RedisException;
181///
182/// // Throw our newly created exception
183/// #[php_function]
184/// pub fn throw_exception() -> PhpResult<i32> {
185///     Err(PhpException::from_class::<RedisException>("Not good!".into()))
186/// }
187///
188/// #[php_module]
189/// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
190///     module
191///         .class::<RedisException>()
192///         .function(wrap_function!(throw_exception))
193/// }
194/// # fn main() {}
195/// ```
196///
197/// ### Extending a Rust-defined Class
198///
199/// When extending another Rust-defined class, you can use the simpler type
200/// syntax:
201///
202/// ```rust,ignore
203/// # #![cfg_attr(windows, feature(abi_vectorcall))]
204/// # extern crate ext_php_rs;
205/// use ext_php_rs::prelude::*;
206///
207/// #[php_class]
208/// #[derive(Default)]
209/// pub struct Animal;
210///
211/// #[php_impl]
212/// impl Animal {
213///     pub fn speak(&self) -> &'static str {
214///         "..."
215///     }
216/// }
217///
218/// #[php_class]
219/// #[php(extends(Animal))]
220/// #[derive(Default)]
221/// pub struct Dog;
222///
223/// #[php_impl]
224/// impl Dog {
225///     pub fn speak(&self) -> &'static str {
226///         "Woof!"
227///     }
228/// }
229///
230/// #[php_module]
231/// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
232///     module
233///         .class::<Animal>()
234///         .class::<Dog>()
235/// }
236/// # fn main() {}
237/// ```
238///
239/// #### Sharing Methods Between Parent and Child Classes
240///
241/// When both parent and child are Rust-defined classes, methods defined only in
242/// the parent won't automatically work when called on a child instance. This is
243/// because each Rust type has its own object handlers.
244///
245/// The recommended workaround is to use a Rust trait for shared behavior:
246///
247/// ```rust,ignore
248/// # #![cfg_attr(windows, feature(abi_vectorcall))]
249/// # extern crate ext_php_rs;
250/// use ext_php_rs::prelude::*;
251///
252/// /// Trait for shared behavior
253/// trait AnimalBehavior {
254///     fn speak(&self) -> &'static str {
255///         "..."
256///     }
257/// }
258///
259/// #[php_class]
260/// #[derive(Default)]
261/// pub struct Animal;
262///
263/// impl AnimalBehavior for Animal {}
264///
265/// #[php_impl]
266/// impl Animal {
267///     pub fn speak(&self) -> &'static str {
268///         AnimalBehavior::speak(self)
269///     }
270/// }
271///
272/// #[php_class]
273/// #[php(extends(Animal))]
274/// #[derive(Default)]
275/// pub struct Dog;
276///
277/// impl AnimalBehavior for Dog {
278///     fn speak(&self) -> &'static str {
279///         "Woof!"
280///     }
281/// }
282///
283/// #[php_impl]
284/// impl Dog {
285///     // Re-export the method so it works on Dog instances
286///     pub fn speak(&self) -> &'static str {
287///         AnimalBehavior::speak(self)
288///     }
289/// }
290///
291/// #[php_module]
292/// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
293///     module
294///         .class::<Animal>()
295///         .class::<Dog>()
296/// }
297/// # fn main() {}
298/// ```
299///
300/// This pattern ensures that:
301/// - `$animal->speak()` returns `"..."`
302/// - `$dog->speak()` returns `"Woof!"`
303/// - `$dog instanceof Animal` is `true`
304///
305/// ## Implementing an Interface
306///
307/// To implement an interface, use `#[php(implements(...))]`. For built-in PHP
308/// interfaces, use the explicit form with `ce` and `stub`. For Rust-defined
309/// interfaces, you can use the simple type form. The following example implements [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php):
310///
311/// ````rust,no_run,ignore
312/// # #![cfg_attr(windows, feature(abi_vectorcall))]
313/// # extern crate ext_php_rs;
314/// use ext_php_rs::{
315///     prelude::*,
316///     exception::PhpResult,
317///     types::Zval,
318///     zend::ce,
319/// };
320///
321/// #[php_class]
322/// #[php(implements(ce = ce::arrayaccess, stub = "\\ArrayAccess"))]
323/// #[derive(Default)]
324/// pub struct EvenNumbersArray;
325///
326/// /// Returns `true` if the array offset is an even number.
327/// /// Usage:
328/// /// ```php
329/// /// $arr = new EvenNumbersArray();
330/// /// var_dump($arr[0]); // true
331/// /// var_dump($arr[1]); // false
332/// /// var_dump($arr[2]); // true
333/// /// var_dump($arr[3]); // false
334/// /// var_dump($arr[4]); // true
335/// /// var_dump($arr[5] = true); // Fatal error:  Uncaught Exception: Setting values is not supported
336/// /// ```
337/// #[php_impl]
338/// impl EvenNumbersArray {
339///     pub fn __construct() -> EvenNumbersArray {
340///         EvenNumbersArray {}
341///     }
342///     // We need to use `Zval` because ArrayAccess needs $offset to be a `mixed`
343///     pub fn offset_exists(&self, offset: &'_ Zval) -> bool {
344///         offset.is_long()
345///     }
346///     pub fn offset_get(&self, offset: &'_ Zval) -> PhpResult<bool> {
347///         let integer_offset = offset.long().ok_or("Expected integer offset")?;
348///         Ok(integer_offset % 2 == 0)
349///     }
350///     pub fn offset_set(&mut self, _offset: &'_ Zval, _value: &'_ Zval) -> PhpResult {
351///         Err("Setting values is not supported".into())
352///     }
353///     pub fn offset_unset(&mut self, _offset: &'_ Zval) -> PhpResult {
354///         Err("Setting values is not supported".into())
355///     }
356/// }
357///
358/// #[php_module]
359/// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
360///     module.class::<EvenNumbersArray>()
361/// }
362/// # fn main() {}
363/// ````
364///
365/// ## Static Properties
366///
367/// Static properties are shared across all instances of a class. Use
368/// `#[php(prop, static)]` to declare a static property. Unlike instance
369/// properties, static properties are managed entirely by PHP and do not use
370/// Rust property handlers.
371///
372/// You can specify a default value using the `default` attribute:
373///
374/// ```rust,no_run,ignore
375/// # #![cfg_attr(windows, feature(abi_vectorcall))]
376/// # extern crate ext_php_rs;
377/// use ext_php_rs::prelude::*;
378/// use ext_php_rs::class::RegisteredClass;
379///
380/// #[php_class]
381/// pub struct Counter {
382///     #[php(prop)]
383///     pub instance_value: i32,
384///     #[php(prop, static, default = 0)]
385///     pub count: i32,
386///     #[php(prop, static, flags = ext_php_rs::flags::PropertyFlags::Private)]
387///     pub internal_state: String,
388/// }
389///
390/// #[php_impl]
391/// impl Counter {
392///     pub fn __construct(value: i32) -> Self {
393///         Self {
394///             instance_value: value,
395///             count: 0,
396///             internal_state: String::new(),
397///         }
398///     }
399///
400///     /// Increment the static counter from Rust
401///     pub fn increment() {
402///         let ce = Self::get_metadata().ce();
403///         let current: i64 = ce.get_static_property("count").unwrap_or(0);
404///         ce.set_static_property("count", current + 1).unwrap();
405///     }
406///
407///     /// Get the current count
408///     pub fn get_count() -> i64 {
409///         let ce = Self::get_metadata().ce();
410///         ce.get_static_property("count").unwrap_or(0)
411///     }
412/// }
413///
414/// #[php_module]
415/// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
416///     module.class::<Counter>()
417/// }
418/// # fn main() {}
419/// ```
420///
421/// From PHP, you can access static properties directly on the class:
422///
423/// ```php
424/// // No need to initialize - count already has default value of 0
425/// Counter::increment();
426/// Counter::increment();
427/// echo Counter::$count; // 2
428/// echo Counter::getCount(); // 2
429/// ```
430///
431/// ## Abstract Classes
432///
433/// Abstract classes cannot be instantiated directly and may contain abstract
434/// methods that must be implemented by subclasses. Use `#[php(flags =
435/// ClassFlags::Abstract)]` to declare an abstract class:
436///
437/// ```rust,ignore
438/// use ext_php_rs::prelude::*;
439/// use ext_php_rs::flags::ClassFlags;
440///
441/// #[php_class]
442/// #[php(flags = ClassFlags::Abstract)]
443/// pub struct AbstractAnimal;
444///
445/// #[php_impl]
446/// impl AbstractAnimal {
447///     // Protected constructor for subclasses
448///     #[php(vis = "protected")]
449///     pub fn __construct() -> Self {
450///         Self
451///     }
452///
453///     // Abstract method - must be implemented by subclasses.
454///     // Body is never called; use unimplemented!() as a placeholder.
455///     #[php(abstract)]
456///     pub fn speak(&self) -> String {
457///         unimplemented!()
458///     }
459///
460///     // Concrete method - inherited by subclasses
461///     pub fn breathe(&self) {
462///         println!("Breathing...");
463///     }
464/// }
465/// ```
466///
467/// From PHP, you can extend this abstract class:
468///
469/// ```php
470/// class Dog extends AbstractAnimal {
471///     public function __construct() {
472///         parent::__construct();
473///     }
474///
475///     public function speak(): string {
476///         return "Woof!";
477///     }
478/// }
479///
480/// $dog = new Dog();
481/// echo $dog->speak(); // "Woof!"
482/// $dog->breathe();    // "Breathing..."
483///
484/// // This would cause an error:
485/// // $animal = new AbstractAnimal(); // Cannot instantiate abstract class
486/// ```
487///
488/// See the [impl documentation](./impl.md#abstract-methods) for more details on
489/// abstract methods.
490///
491/// ## Final Classes
492///
493/// Final classes cannot be extended. Use `#[php(flags = ClassFlags::Final)]` to
494/// declare a final class:
495///
496/// ```rust,ignore
497/// use ext_php_rs::prelude::*;
498/// use ext_php_rs::flags::ClassFlags;
499///
500/// #[php_class]
501/// #[php(flags = ClassFlags::Final)]
502/// pub struct FinalClass;
503/// ```
504///
505/// ## Readonly Classes (PHP 8.2+)
506///
507/// PHP 8.2 introduced [readonly classes](https://www.php.net/manual/en/language.oop5.basic.php#language.oop5.basic.class.readonly),
508/// where all properties are implicitly readonly. You can create a readonly
509/// class using the `#[php(readonly)]` attribute:
510///
511/// ```rust,ignore
512/// # #![cfg_attr(windows, feature(abi_vectorcall))]
513/// # extern crate ext_php_rs;
514/// use ext_php_rs::prelude::*;
515///
516/// #[php_class]
517/// #[php(readonly)]
518/// pub struct ImmutablePoint {
519///     x: f64,
520///     y: f64,
521/// }
522///
523/// #[php_impl]
524/// impl ImmutablePoint {
525///     pub fn __construct(x: f64, y: f64) -> Self {
526///         Self { x, y }
527///     }
528///
529///     pub fn get_x(&self) -> f64 {
530///         self.x
531///     }
532///
533///     pub fn get_y(&self) -> f64 {
534///         self.y
535///     }
536///
537///     pub fn distance_from_origin(&self) -> f64 {
538///         (self.x * self.x + self.y * self.y).sqrt()
539///     }
540/// }
541///
542/// #[php_module]
543/// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
544///     module.class::<ImmutablePoint>()
545/// }
546/// # fn main() {}
547/// ```
548///
549/// From PHP:
550///
551/// ```php
552/// $point = new ImmutablePoint(3.0, 4.0);
553/// echo $point->getX(); // 3.0
554/// echo $point->getY(); // 4.0
555/// echo $point->distanceFromOrigin(); // 5.0
556///
557/// // On PHP 8.2+, you can verify the class is readonly:
558/// $reflection = new ReflectionClass(ImmutablePoint::class);
559/// var_dump($reflection->isReadOnly()); // true
560/// ```
561///
562/// The `readonly` attribute is compatible with other class attributes:
563///
564/// ```rust,ignore
565/// # #![cfg_attr(windows, feature(abi_vectorcall))]
566/// # extern crate ext_php_rs;
567/// use ext_php_rs::prelude::*;
568/// use ext_php_rs::flags::ClassFlags;
569///
570/// // Readonly + Final class
571/// #[php_class]
572/// #[php(readonly)]
573/// #[php(flags = ClassFlags::Final)]
574/// pub struct FinalImmutableData {
575///     value: String,
576/// }
577/// # fn main() {}
578/// ```
579///
580/// **Note:** The `readonly` attribute requires PHP 8.2 or later. Using it when
581/// compiling against an earlier PHP version will result in a compile error.
582///
583/// ### Conditional Compilation for Multi-Version Support
584///
585/// If your extension needs to support both PHP 8.1 and PHP 8.2+, you can use
586/// conditional compilation to only enable readonly on supported versions.
587///
588/// First, add `ext-php-rs-build` as a build dependency in your `Cargo.toml`:
589///
590/// ```toml
591/// [build-dependencies]
592/// ext-php-rs-build = "0.1"
593/// anyhow = "1"
594/// ```
595///
596/// Then create a `build.rs` that detects the PHP version and emits cfg flags:
597///
598/// ```rust,ignore
599/// use ext_php_rs_build::{find_php, PHPInfo, ApiVersion, emit_php_cfg_flags, emit_check_cfg};
600///
601/// fn main() -> anyhow::Result<()> {
602///     let php = find_php()?;
603///     let info = PHPInfo::get(&php)?;
604///     let version: ApiVersion = info.zend_version()?.try_into()?;
605///
606///     emit_check_cfg();
607///     emit_php_cfg_flags(version);
608///     Ok(())
609/// }
610/// ```
611///
612/// Now you can use `#[cfg(php82)]` to conditionally apply the readonly
613/// attribute:
614///
615/// ```rust,ignore
616/// #[php_class]
617/// #[cfg_attr(php82, php(readonly))]
618/// pub struct MaybeReadonlyClass {
619///     value: String,
620/// }
621/// ```
622///
623/// The `ext-php-rs-build` crate provides several useful utilities:
624///
625/// - `find_php()` - Locates the PHP executable (respects the `PHP` env var)
626/// - `PHPInfo::get()` - Runs `php -i` and parses the output
627/// - `ApiVersion` - Enum for the PHP versions (Php80, Php81, Php82, etc.). It
628///   is `#[non_exhaustive]`, so a `match` on it must have a `_` arm.
629/// - `emit_php_cfg_flags()` - Emits `cargo:rustc-cfg=phpXX` for all supported
630///   versions
631/// - `emit_check_cfg()` - Emits check-cfg to avoid unknown cfg warnings
632///
633/// This is **optional** - if your extension only targets PHP 8.2+, you can use
634/// `#[php(readonly)]` directly without any build script setup.
635///
636/// ## Cloning
637///
638/// PHP's native `clone` operator is supported for `#[php_class]` structs that
639/// implement `Clone`. A derive or a hand-written `impl Clone` both work, and
640/// the spelling of the derive path does not matter. The cloned PHP object
641/// contains a copy of the Rust data:
642///
643/// ```rust,ignore
644/// use ext_php_rs::prelude::*;
645///
646/// #[php_class]
647/// #[derive(Clone)]
648/// pub struct Style {
649///     #[php(prop)]
650///     pub font_size: f64,
651///     #[php(prop)]
652///     pub color: String,
653/// }
654///
655/// #[php_impl]
656/// impl Style {
657///     pub fn __construct(font_size: f64, color: String) -> Self {
658///         Self { font_size, color }
659///     }
660/// }
661/// ```
662///
663/// ```php
664/// $style = new Style(12.0, 'red');
665/// $copy = clone $style;
666///
667/// $copy->fontSize = 16.0;
668/// echo $style->fontSize;  // 12.0 — original is unchanged
669/// ```
670///
671/// Structs that do not implement `Clone` throw an error when cloned:
672///
673/// ```php
674/// // If MyClass does not implement Clone:
675/// $obj = new MyClass();
676/// $copy = clone $obj; // Error: Trying to clone an uncloneable object of class MyClass
677/// ```
678///
679/// ## Implementing Iterator
680///
681/// To make a Rust class usable with PHP's `foreach` loop, implement the
682/// [`Iterator`](https://www.php.net/manual/en/class.iterator.php) interface.
683/// This requires implementing five methods: `current()`, `key()`, `next()`,
684/// `rewind()`, and `valid()`.
685///
686/// The following example creates a `RangeIterator` that iterates over a range
687/// of integers:
688///
689/// ````rust,no_run,ignore
690/// # #![cfg_attr(windows, feature(abi_vectorcall))]
691/// # extern crate ext_php_rs;
692/// use ext_php_rs::{prelude::*, zend::ce};
693///
694/// #[php_class]
695/// #[php(implements(ce = ce::iterator, stub = "\\Iterator"))]
696/// pub struct RangeIterator {
697///     start: i64,
698///     end: i64,
699///     current: i64,
700///     index: i64,
701/// }
702///
703/// #[php_impl]
704/// impl RangeIterator {
705///     /// Create a new range iterator from start to end (inclusive).
706///     pub fn __construct(start: i64, end: i64) -> Self {
707///         Self {
708///             start,
709///             end,
710///             current: start,
711///             index: 0,
712///         }
713///     }
714///
715///     /// Return the current element.
716///     pub fn current(&self) -> i64 {
717///         self.current
718///     }
719///
720///     /// Return the key of the current element.
721///     pub fn key(&self) -> i64 {
722///         self.index
723///     }
724///
725///     /// Move forward to next element.
726///     pub fn next(&mut self) {
727///         self.current += 1;
728///         self.index += 1;
729///     }
730///
731///     /// Rewind the Iterator to the first element.
732///     pub fn rewind(&mut self) {
733///         self.current = self.start;
734///         self.index = 0;
735///     }
736///
737///     /// Checks if current position is valid.
738///     pub fn valid(&self) -> bool {
739///         self.current <= self.end
740///     }
741/// }
742///
743/// #[php_module]
744/// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
745///     module.class::<RangeIterator>()
746/// }
747/// # fn main() {}
748/// ````
749///
750/// Using the iterator in PHP:
751///
752/// ```php
753/// <?php
754///
755/// $range = new RangeIterator(1, 5);
756///
757/// // Use with foreach
758/// foreach ($range as $key => $value) {
759///     echo "$key => $value\n";
760/// }
761/// // Output:
762/// // 0 => 1
763/// // 1 => 2
764/// // 2 => 3
765/// // 3 => 4
766/// // 4 => 5
767///
768/// // Works with iterator functions
769/// $arr = iterator_to_array(new RangeIterator(10, 12));
770/// // [0 => 10, 1 => 11, 2 => 12]
771///
772/// $count = iterator_count(new RangeIterator(1, 100));
773/// // 100
774/// ```
775///
776/// ### Iterator with Mixed Types
777///
778/// You can return different types for keys and values. The following example
779/// uses string keys:
780///
781/// ````rust,no_run,ignore
782/// # #![cfg_attr(windows, feature(abi_vectorcall))]
783/// # extern crate ext_php_rs;
784/// use ext_php_rs::{prelude::*, zend::ce};
785///
786/// #[php_class]
787/// #[php(implements(ce = ce::iterator, stub = "\\Iterator"))]
788/// pub struct MapIterator {
789///     keys: Vec<String>,
790///     values: Vec<String>,
791///     index: usize,
792/// }
793///
794/// #[php_impl]
795/// impl MapIterator {
796///     pub fn __construct() -> Self {
797///         Self {
798///             keys: vec!["first".into(), "second".into(), "third".into()],
799///             values: vec!["one".into(), "two".into(), "three".into()],
800///             index: 0,
801///         }
802///     }
803///
804///     pub fn current(&self) -> Option<String> {
805///         self.values.get(self.index).cloned()
806///     }
807///
808///     pub fn key(&self) -> Option<String> {
809///         self.keys.get(self.index).cloned()
810///     }
811///
812///     pub fn next(&mut self) {
813///         self.index += 1;
814///     }
815///
816///     pub fn rewind(&mut self) {
817///         self.index = 0;
818///     }
819///
820///     pub fn valid(&self) -> bool {
821///         self.index < self.keys.len()
822///     }
823/// }
824///
825/// #[php_module]
826/// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
827///     module.class::<MapIterator>()
828/// }
829/// # fn main() {}
830/// ````
831///
832/// ```php
833/// <?php
834///
835/// $map = new MapIterator();
836/// foreach ($map as $key => $value) {
837///     echo "$key => $value\n";
838/// }
839/// // Output:
840/// // first => one
841/// // second => two
842/// // third => three
843/// ```
844// END DOCS FROM classes.md
845#[proc_macro_attribute]
846pub fn php_class(args: TokenStream, input: TokenStream) -> TokenStream {
847    php_class_internal(args.into(), input.into()).into()
848}
849
850#[allow(clippy::needless_pass_by_value)]
851fn php_class_internal(args: TokenStream2, input: TokenStream2) -> TokenStream2 {
852    let input = parse_macro_input2!(input as ItemStruct);
853    if let Err(e) = reject_args(&args, "php_class", PHP_ATTR_HINT) {
854        return e.to_compile_error();
855    }
856
857    class::parser(input).unwrap_or_else(|e| e.to_compile_error())
858}
859
860// BEGIN DOCS FROM enum.md
861/// # `#[php_enum]` Attribute
862///
863/// Enums can be exported to PHP as enums with the `#[php_enum]` attribute
864/// macro. This attribute derives the `RegisteredClass` and `PhpEnum` traits on
865/// your enum. To register the enum use the `enumeration::<EnumName>()` method
866/// on the `ModuleBuilder` in the `#[php_module]` macro.
867///
868/// ## Options
869///
870/// The `#[php_enum]` attribute can be configured with the following options:
871/// - `#[php(name = "EnumName")]` or `#[php(change_case = "snake_case")]`: Sets
872///   the name of the enum in PHP. The default is the `PascalCase` name of the
873///   enum.
874/// - `#[php(allow_native_discriminants)]`: Allows the use of native Rust
875///   discriminants (e.g., `Hearts = 1`).
876/// - `#[php(change_cases_case = "snake_case")]`: Sets the rename rule for every
877///   case that has no `name` or `change_case` of its own. The old name of this
878///   option, `rename_cases`, is a compile error.
879///
880/// PHP enums are always public. `#[php(vis = "...")]` on an enum is a compile
881/// error, and so is any argument passed to the macro itself, such as
882/// `#[php_enum(name = "Suit")]`.
883///
884/// The cases of the enum can be configured with the following options:
885/// - `#[php(name = "CaseName")]` or `#[php(change_case = "snake_case")]`: Sets
886///   the name of the enum case in PHP. The default is the `PascalCase` name of
887///   the case.
888/// - `#[php(value = "value")]` or `#[php(value = 123)]`: Sets the discriminant
889///   value for the enum case. This can be a string or an integer. If not set,
890///   the case will be exported as a simple enum case without a discriminant.
891///
892/// ### Example
893///
894/// This example creates a PHP enum `Suit`.
895///
896/// ```rust,no_run,ignore
897/// # #![cfg_attr(windows, feature(abi_vectorcall))]
898/// # extern crate ext_php_rs;
899/// use ext_php_rs::prelude::*;
900///
901/// #[php_enum]
902/// pub enum Suit {
903///     Hearts,
904///     Diamonds,
905///     Clubs,
906///     Spades,
907/// }
908///
909/// #[php_module]
910/// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
911///     module.enumeration::<Suit>()
912/// }
913/// # fn main() {}
914/// ```
915///
916/// ## Backed Enums
917/// Enums can also be backed by either `i64` or `&'static str`. Those values can
918/// be set using the `#[php(value = "value")]` or `#[php(value = 123)]`
919/// attributes on the enum variants.
920///
921/// All variants must have a value of the same type, either all `i64` or all
922/// `&'static str`.
923///
924/// A backed enum converts to and from its value. The macro implements
925/// `From<Enum>` for the value type, and `TryFrom<i64>` or `TryFrom<&str>` for
926/// the enum. `TryFrom` returns `Error::InvalidEnumCase` when no case has the
927/// value.
928///
929/// ```rust,no_run,ignore
930/// # #![cfg_attr(windows, feature(abi_vectorcall))]
931/// # extern crate ext_php_rs;
932/// use ext_php_rs::prelude::*;
933///
934/// #[php_enum]
935/// pub enum Suit {
936///     #[php(value = "hearts")]
937///     Hearts,
938///     #[php(value = "diamonds")]
939///     Diamonds,
940///     #[php(value = "clubs")]
941///     Clubs,
942///     #[php(value = "spades")]
943///     Spades,
944/// }
945/// #[php_module]
946/// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
947///     module.enumeration::<Suit>()
948/// }
949/// # fn main() {}
950/// ```
951///
952/// ### 'Native' Discriminators
953/// Native rust discriminants are currently not supported and will not be
954/// exported to PHP.
955///
956/// To avoid confusion a compiler error will be raised if you try to use a
957/// native discriminant. You can ignore this error by adding the
958/// `#[php(allow_native_discriminants)]` attribute to your enum.
959///
960/// ```rust,no_run,ignore
961/// # #![cfg_attr(windows, feature(abi_vectorcall))]
962/// # extern crate ext_php_rs;
963/// use ext_php_rs::prelude::*;
964///
965/// #[php_enum]
966/// #[php(allow_native_discriminants)]
967/// pub enum Suit {
968///     Hearts = 1,
969///     Diamonds = 2,
970///     Clubs = 3,
971///     Spades = 4,
972/// }
973///
974/// #[php_module]
975/// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
976///     module.enumeration::<Suit>()
977/// }
978/// # fn main() {}
979/// ```
980///
981///
982/// TODO: Add backed enums example
983// END DOCS FROM enum.md
984#[proc_macro_attribute]
985pub fn php_enum(args: TokenStream, input: TokenStream) -> TokenStream {
986    php_enum_internal(args.into(), input.into()).into()
987}
988
989#[allow(clippy::needless_pass_by_value)]
990fn php_enum_internal(args: TokenStream2, input: TokenStream2) -> TokenStream2 {
991    let input = parse_macro_input2!(input as ItemEnum);
992    if let Err(e) = reject_args(&args, "php_enum", PHP_ATTR_HINT) {
993        return e.to_compile_error();
994    }
995
996    enum_::parser(input).unwrap_or_else(|e| e.to_compile_error())
997}
998
999// BEGIN DOCS FROM interface.md
1000/// # `#[php_interface]` Attribute
1001///
1002/// You can export a `Trait` block to PHP. This exports all methods as well as
1003/// constants to PHP on the interface. Trait method SHOULD NOT contain default
1004/// implementations, as these are not supported in PHP interfaces.
1005///
1006/// ## Options
1007///
1008/// By default all constants are renamed to `UPPER_CASE` and all methods are
1009/// renamed to `camelCase`. This can be changed by passing the
1010/// `change_method_case` and `change_constant_case` as `#[php]` attributes on
1011/// the `impl` block. The options are:
1012///
1013/// - `#[php(change_method_case = "snake_case")]` - Renames the method to snake
1014///   case.
1015/// - `#[php(change_constant_case = "snake_case")]` - Renames the constant to
1016///   snake case.
1017///
1018/// See the [`name` and `change_case`](./php.md#name-and-change_case) section
1019/// for a list of all available cases.
1020///
1021/// The macro itself takes no arguments: `#[php_interface(name = "Foo")]` is a
1022/// compile error. Put the options in `#[php(...)]` on the trait.
1023///
1024/// ## Methods
1025///
1026/// A trait method accepts the `name`, `change_case`, `defaults`, `optional`,
1027/// `sensitive` and `vis` options. Interfaces cannot declare property accessors
1028/// or constructors, so `getter`, `setter` and `constructor` are compile errors
1029/// here. See [`php_impl`](./impl.md#) for how each accepted option behaves.
1030///
1031/// ## Constants
1032///
1033/// A trait constant accepts the `name` and `change_case` options. Interface
1034/// constants are always public, so `vis` is a compile error here. See
1035/// [`php_impl`](./impl.md#constants) for the value types.
1036///
1037/// ## Example
1038///
1039/// Define an example trait with methods and constant:
1040///
1041/// ```rust,no_run,ignore
1042/// # #![cfg_attr(windows, feature(abi_vectorcall))]
1043/// # extern crate ext_php_rs;
1044/// use ext_php_rs::{prelude::*, types::ZendClassObject};
1045///
1046///
1047/// #[php_interface]
1048/// #[php(name = "Rust\\TestInterface")]
1049/// trait Test {
1050///     const TEST: &'static str = "TEST";
1051///
1052///     fn co();
1053///
1054///     #[php(defaults(value = 0))]
1055///     fn set_value(&mut self, value: i32);
1056/// }
1057///
1058/// #[php_module]
1059/// pub fn module(module: ModuleBuilder) -> ModuleBuilder {
1060///     module
1061///         .interface::<PhpInterfaceTest>()
1062/// }
1063///
1064/// # fn main() {}
1065/// ```
1066///
1067/// Using our newly created interface in PHP:
1068///
1069/// ```php
1070/// <?php
1071///
1072/// assert(interface_exists("Rust\TestInterface"));
1073///
1074/// class B implements Rust\TestInterface {
1075///
1076///     public static function co() {}
1077///
1078///     public function setValue(?int $value = 0) {
1079///
1080///     }
1081/// }
1082/// ```
1083///
1084/// ## Interface Inheritance
1085///
1086/// PHP interfaces can extend other interfaces. You can achieve this in two
1087/// ways:
1088///
1089/// ### Using `#[php(extends(...))]`
1090///
1091/// Use the `extends` attribute to extend a built-in PHP interface or another
1092/// Rust-defined interface. Generated stubs write each parent interface name as
1093/// a fully qualified name, with a leading `\`. A leading `\` in `stub = "..."`
1094/// is optional.
1095///
1096/// For built-in PHP interfaces, use the explicit form:
1097///
1098/// ```rust,no_run,ignore
1099/// # #![cfg_attr(windows, feature(abi_vectorcall))]
1100/// # extern crate ext_php_rs;
1101/// use ext_php_rs::prelude::*;
1102/// use ext_php_rs::zend::ce;
1103///
1104/// #[php_interface]
1105/// #[php(extends(ce = ce::throwable, stub = "\\Throwable"))]
1106/// #[php(name = "MyException")]
1107/// trait MyExceptionInterface {
1108///     fn get_error_code(&self) -> i32;
1109/// }
1110///
1111/// # fn main() {}
1112/// ```
1113///
1114/// For Rust-defined interfaces, you can use the simpler type syntax:
1115///
1116/// ```rust,ignore
1117/// # #![cfg_attr(windows, feature(abi_vectorcall))]
1118/// # extern crate ext_php_rs;
1119/// use ext_php_rs::prelude::*;
1120///
1121/// #[php_interface]
1122/// trait BaseInterface {
1123///     fn base_method(&self) -> i32;
1124/// }
1125///
1126/// #[php_interface]
1127/// #[php(extends(BaseInterface))]
1128/// trait ExtendedInterface {
1129///     fn extended_method(&self) -> String;
1130/// }
1131///
1132/// # fn main() {}
1133/// ```
1134///
1135/// ### Using Rust Trait Bounds
1136///
1137/// You can also use Rust's trait bound syntax. When a trait marked with
1138/// `#[php_interface]` has supertraits, the PHP interface will automatically
1139/// extend those parent interfaces:
1140///
1141/// ```rust,no_run,ignore
1142/// # #![cfg_attr(windows, feature(abi_vectorcall))]
1143/// # extern crate ext_php_rs;
1144/// use ext_php_rs::prelude::*;
1145///
1146/// #[php_interface]
1147/// #[php(name = "Rust\\ParentInterface")]
1148/// trait ParentInterface {
1149///     fn parent_method(&self) -> String;
1150/// }
1151///
1152/// // ChildInterface extends ParentInterface in PHP
1153/// #[php_interface]
1154/// #[php(name = "Rust\\ChildInterface")]
1155/// trait ChildInterface: ParentInterface {
1156///     fn child_method(&self) -> String;
1157/// }
1158///
1159/// #[php_module]
1160/// pub fn module(module: ModuleBuilder) -> ModuleBuilder {
1161///     module
1162///         .interface::<PhpInterfaceParentInterface>()
1163///         .interface::<PhpInterfaceChildInterface>()
1164/// }
1165///
1166/// # fn main() {}
1167/// ```
1168///
1169/// In PHP:
1170///
1171/// ```php
1172/// <?php
1173///
1174/// // ChildInterface extends ParentInterface
1175/// assert(is_a('Rust\ChildInterface', 'Rust\ParentInterface', true));
1176/// ```
1177///
1178/// # `#[php_impl_interface]` Attribute
1179///
1180/// The `#[php_impl_interface]` attribute allows a Rust class to implement a
1181/// custom PHP interface defined with `#[php_interface]`. This creates a
1182/// relationship where PHP's `instanceof` and `is_a()` recognize the
1183/// implementation.
1184///
1185/// **Key feature**: The macro automatically registers the trait methods as PHP
1186/// methods on the class. You don't need to duplicate them in a separate
1187/// `#[php_impl]` block.
1188///
1189/// ## Example
1190///
1191/// ```rust,no_run,ignore
1192/// # #![cfg_attr(windows, feature(abi_vectorcall))]
1193/// # extern crate ext_php_rs;
1194/// use ext_php_rs::prelude::*;
1195///
1196/// // Define a custom interface
1197/// #[php_interface]
1198/// #[php(name = "Rust\\Greetable")]
1199/// trait Greetable {
1200///     fn greet(&self) -> String;
1201/// }
1202///
1203/// // Define a class
1204/// #[php_class]
1205/// #[php(name = "Rust\\Greeter")]
1206/// pub struct Greeter {
1207///     name: String,
1208/// }
1209///
1210/// #[php_impl]
1211/// impl Greeter {
1212///     pub fn __construct(name: String) -> Self {
1213///         Self { name }
1214///     }
1215///
1216///     // Note: No need to add greet() here - it's automatically
1217///     // registered by #[php_impl_interface] below
1218/// }
1219///
1220/// // Implement the interface for the class
1221/// // This automatically registers greet() as a PHP method
1222/// #[php_impl_interface]
1223/// impl Greetable for Greeter {
1224///     fn greet(&self) -> String {
1225///         format!("Hello, {}!", self.name)
1226///     }
1227/// }
1228///
1229/// #[php_module]
1230/// pub fn module(module: ModuleBuilder) -> ModuleBuilder {
1231///     module
1232///         .interface::<PhpInterfaceGreetable>()
1233///         .class::<Greeter>()
1234/// }
1235///
1236/// # fn main() {}
1237/// ```
1238///
1239/// Using in PHP:
1240///
1241/// ```php
1242/// <?php
1243///
1244/// $greeter = new Rust\Greeter("World");
1245///
1246/// // instanceof works
1247/// assert($greeter instanceof Rust\Greetable);
1248///
1249/// // is_a() works
1250/// assert(is_a($greeter, 'Rust\Greetable'));
1251///
1252/// // The greet() method is available (registered by #[php_impl_interface])
1253/// echo $greeter->greet(); // Output: Hello, World!
1254///
1255/// // Can be used as type hint
1256/// function greet(Rust\Greetable $obj): void {
1257///     echo $obj->greet();
1258/// }
1259///
1260/// greet($greeter);
1261/// ```
1262///
1263/// ## When to Use
1264///
1265/// - Use `#[php_impl_interface]` for custom interfaces you define with
1266///   `#[php_interface]`
1267/// - Use `#[php(implements(ce = ...))]` on `#[php_class]` for built-in PHP
1268///   interfaces like `Iterator`, `ArrayAccess`, `Countable`, etc.
1269///
1270/// See the [Classes documentation](./classes.md#implementing-an-interface) for
1271/// examples of implementing built-in interfaces.
1272///
1273/// ## Cross-Crate Support
1274///
1275/// The `#[php_impl_interface]` macro supports cross-crate interface discovery
1276/// via the [`inventory`](https://crates.io/crates/inventory) crate. This means you can define
1277/// an interface in one crate and implement it in another crate, and the
1278/// implementation will be automatically discovered at link time.
1279///
1280/// ### Example: Defining an Interface in a Library Crate
1281///
1282/// First, create a library crate that defines the interface:
1283///
1284/// ```toml
1285/// # my-interfaces/Cargo.toml
1286/// [package]
1287/// name = "my-interfaces"
1288/// version = "0.1.0"
1289///
1290/// [dependencies]
1291/// ext-php-rs = "0.15"
1292/// ```
1293///
1294/// ```rust,no_run,ignore,ignore
1295/// // my-interfaces/src/lib.rs
1296/// use ext_php_rs::prelude::*;
1297///
1298/// /// A serializable interface that can convert objects to JSON.
1299/// #[php_interface]
1300/// #[php(name = "MyInterfaces\\Serializable")]
1301/// pub trait Serializable {
1302///     fn to_json(&self) -> String;
1303/// }
1304///
1305/// // Re-export the generated PHP interface struct for consumers
1306/// pub use PhpInterfaceSerializable;
1307/// ```
1308///
1309/// ### Example: Implementing the Interface in Another Crate
1310///
1311/// Now create your extension crate that implements the interface:
1312///
1313/// ```toml
1314/// # my-extension/Cargo.toml
1315/// [package]
1316/// name = "my-extension"
1317/// version = "0.1.0"
1318///
1319/// [lib]
1320/// crate-type = ["cdylib"]
1321///
1322/// [dependencies]
1323/// ext-php-rs = "0.15"
1324/// my-interfaces = { path = "../my-interfaces" }
1325/// ```
1326///
1327/// ```rust,no_run,ignore,ignore
1328/// // my-extension/src/lib.rs
1329/// use ext_php_rs::prelude::*;
1330/// use my_interfaces::Serializable;
1331///
1332/// #[php_class]
1333/// #[php(name = "MyExtension\\User")]
1334/// pub struct User {
1335///     name: String,
1336///     email: String,
1337/// }
1338///
1339/// #[php_impl]
1340/// impl User {
1341///     pub fn __construct(name: String, email: String) -> Self {
1342///         Self { name, email }
1343///     }
1344///
1345///     // Note: No need to add to_json() here - it's automatically
1346///     // registered by #[php_impl_interface] below
1347/// }
1348///
1349/// // Register the interface implementation
1350/// // This automatically registers to_json() as a PHP method
1351/// #[php_impl_interface]
1352/// impl Serializable for User {
1353///     fn to_json(&self) -> String {
1354///         format!(r#"{{"name":"{}","email":"{}"}}"#, self.name, self.email)
1355///     }
1356/// }
1357///
1358/// #[php_module]
1359/// pub fn module(module: ModuleBuilder) -> ModuleBuilder {
1360///     module
1361///         // Register the interface from the library crate
1362///         .interface::<my_interfaces::PhpInterfaceSerializable>()
1363///         .class::<User>()
1364/// }
1365/// ```
1366///
1367/// ### Using in PHP
1368///
1369/// ```php
1370/// <?php
1371///
1372/// use MyExtension\User;
1373/// use MyInterfaces\Serializable;
1374///
1375/// $user = new User("John", "john@example.com");
1376///
1377/// // instanceof works across crates
1378/// assert($user instanceof Serializable);
1379///
1380/// // Type hints work
1381/// function serialize_object(Serializable $obj): string {
1382///     return $obj->toJson();
1383/// }
1384///
1385/// echo serialize_object($user);
1386/// // Output: {"name":"John","email":"john@example.com"}
1387/// ```
1388///
1389/// ### Important Notes
1390///
1391/// 1. **Automatic method registration**: The `#[php_impl_interface]` macro
1392///    automatically registers all trait methods as PHP methods on the class.
1393///    You don't need to duplicate them in a `#[php_impl]` block.
1394///
1395/// 2. **Interface registration**: The interface must be registered in the
1396///    `#[php_module]` function using `.interface::<PhpInterfaceName>()`.
1397///
1398/// 3. **Link-time discovery**: The `inventory` crate uses link-time
1399///    registration for interface discovery, so all implementations are
1400///    automatically discovered when the final binary is linked.
1401///
1402/// 4. **Method names**: Each method gets the PHP name that `#[php_interface]`
1403///    declared for it. The trait's `change_method_case` and the `name` or
1404///    `change_case` of a trait method apply to the class too.
1405///    `#[php_impl_interface]` takes no arguments.
1406///
1407/// 5. **Sensitive parameters**: `#[php(sensitive(...))]` on a trait method also
1408///    applies to the class method, so exception traces from the class hide the
1409///    same arguments. The macro marks the argument at the same position, thus
1410///    the parameter names in the `impl` can be different. A PHP class that
1411///    implements the interface does not get the attribute, because PHP does not
1412///    copy parameter attributes from an interface. Declare
1413///    `#[\SensitiveParameter]` on its method.
1414// END DOCS FROM interface.md
1415#[proc_macro_attribute]
1416pub fn php_interface(args: TokenStream, input: TokenStream) -> TokenStream {
1417    php_interface_internal(args.into(), input.into()).into()
1418}
1419
1420#[allow(clippy::needless_pass_by_value)]
1421fn php_interface_internal(args: TokenStream2, input: TokenStream2) -> TokenStream2 {
1422    let input = parse_macro_input2!(input as ItemTrait);
1423    if let Err(e) = reject_args(&args, "php_interface", PHP_ATTR_HINT) {
1424        return e.to_compile_error();
1425    }
1426
1427    interface::parser(input).unwrap_or_else(|e| e.to_compile_error())
1428}
1429
1430// BEGIN DOCS FROM function.md
1431/// # `#[php_function]` Attribute
1432///
1433/// Used to annotate functions which should be exported to PHP. Note that this
1434/// should not be used on class methods - see the `#[php_impl]` macro for that.
1435///
1436/// See the [list of types](../types/index.md) that are valid as parameter and
1437/// return types. If the caller passes a value that the parameter type does not
1438/// accept, the call throws a `TypeError`.
1439///
1440/// A function accepts the `name`, `change_case`, `defaults`, `optional` and
1441/// `sensitive` options. PHP functions have no visibility, so `#[php(vis =
1442/// "...")]` is a compile error on a function.
1443///
1444/// ## Optional parameters
1445///
1446/// An `Option<T>` parameter accepts `null` and can be omitted. The macro reads
1447/// this from the type through `FromZvalMut::NULLABLE`, so a type alias such as
1448/// `type MaybeAge = Option<i64>` or the qualified path
1449/// `std::option::Option<i64>` behaves like `Option<i64>`. The trailing run of
1450/// parameters that are nullable or have a default value is optional.
1451///
1452/// ```rust,no_run,ignore
1453/// # #![cfg_attr(windows, feature(abi_vectorcall))]
1454/// # extern crate ext_php_rs;
1455/// use ext_php_rs::prelude::*;
1456///
1457/// #[php_function]
1458/// pub fn greet(name: String, age: Option<i32>) -> String {
1459///     let mut greeting = format!("Hello, {}!", name);
1460///
1461///     if let Some(age) = age {
1462///         greeting += &format!(" You are {} years old.", age);
1463///     }
1464///
1465///     greeting
1466/// }
1467///
1468/// #[php_module]
1469/// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
1470///     module.function(wrap_function!(greet))
1471/// }
1472/// # fn main() {}
1473/// ```
1474///
1475/// Default parameter values can also be set for optional parameters. This is
1476/// done through the `#[php(defaults)]` attribute option. When an optional
1477/// parameter has a default, it does not need to be a variant of `Option`. The
1478/// default expression is converted into the parameter type with `Into`, and the
1479/// type must implement `StubLiteral`, which every scalar, string, `Option`,
1480/// `Vec` and `HashMap` does. The module renders the value once at load time
1481/// into the stub file and into `ReflectionParameter::getDefaultValue()`. Each
1482/// key in `defaults` must name a parameter of the function. If a key names no
1483/// parameter, the build fails:
1484///
1485/// ```rust,no_run,ignore
1486/// # #![cfg_attr(windows, feature(abi_vectorcall))]
1487/// # extern crate ext_php_rs;
1488/// use ext_php_rs::prelude::*;
1489///
1490/// #[php_function]
1491/// #[php(defaults(offset = 0))]
1492/// pub fn rusty_strpos(haystack: &str, needle: &str, offset: i64) -> Option<usize> {
1493///     let haystack: String = haystack.chars().skip(offset as usize).collect();
1494///     haystack.find(needle)
1495/// }
1496///
1497/// #[php_module]
1498/// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
1499///     module.function(wrap_function!(rusty_strpos))
1500/// }
1501/// # fn main() {}
1502/// ```
1503///
1504/// Note that if there is a non-optional argument after an argument that is a
1505/// variant of `Option<T>`, the `Option<T>` argument will be deemed a nullable
1506/// argument rather than an optional argument.
1507///
1508/// ```rust,no_run,ignore
1509/// # #![cfg_attr(windows, feature(abi_vectorcall))]
1510/// # extern crate ext_php_rs;
1511/// use ext_php_rs::prelude::*;
1512///
1513/// /// `age` will be deemed required and nullable rather than optional.
1514/// #[php_function]
1515/// pub fn greet(name: String, age: Option<i32>, description: String) -> String {
1516///     let mut greeting = format!("Hello, {}!", name);
1517///
1518///     if let Some(age) = age {
1519///         greeting += &format!(" You are {} years old.", age);
1520///     }
1521///
1522///     greeting += &format!(" {}.", description);
1523///     greeting
1524/// }
1525///
1526/// #[php_module]
1527/// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
1528///     module.function(wrap_function!(greet))
1529/// }
1530/// # fn main() {}
1531/// ```
1532///
1533/// You can also specify the optional arguments if you want to have nullable
1534/// arguments before optional arguments. This is done through the `optional`
1535/// attribute option, which names the first optional parameter. PHP callers can
1536/// omit that parameter and every parameter after it. Each of these parameters
1537/// must be an `Option<T>`, have a default, or be the variadic `&[T]` tail.
1538/// Otherwise the build fails with an error on the type of that parameter. If
1539/// `optional` names no parameter, the build also fails:
1540///
1541/// ```rust,no_run,ignore
1542/// # #![cfg_attr(windows, feature(abi_vectorcall))]
1543/// # extern crate ext_php_rs;
1544/// use ext_php_rs::prelude::*;
1545///
1546/// /// `age` will be deemed required and nullable rather than optional,
1547/// /// while description will be optional.
1548/// #[php_function]
1549/// #[php(optional = "description")]
1550/// pub fn greet(name: String, age: Option<i32>, description: Option<String>) -> String {
1551///     let mut greeting = format!("Hello, {}!", name);
1552///
1553///     if let Some(age) = age {
1554///         greeting += &format!(" You are {} years old.", age);
1555///     }
1556///
1557///     if let Some(description) = description {
1558///         greeting += &format!(" {}.", description);
1559///     }
1560///
1561///     greeting
1562/// }
1563///
1564/// #[php_module]
1565/// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
1566///     module.function(wrap_function!(greet))
1567/// }
1568/// # fn main() {}
1569/// ```
1570///
1571/// ## Sensitive parameters
1572///
1573/// `#[php(sensitive(...))]` names parameters that PHP declares
1574/// [`#[\SensitiveParameter]`](https://www.php.net/manual/en/class.sensitiveparameter.php),
1575/// as it does for `password_hash()`'s password. When an exception is thrown,
1576/// its trace holds a `SensitiveParameterValue` in place of the value, so a
1577/// password or connection string does not reach logs or error trackers. Each
1578/// name must be a parameter of the function. It has no effect before PHP 8.2,
1579/// which introduced the attribute.
1580///
1581/// ```rust,no_run,ignore
1582/// # #![cfg_attr(windows, feature(abi_vectorcall))]
1583/// # extern crate ext_php_rs;
1584/// use ext_php_rs::prelude::*;
1585///
1586/// #[php_function]
1587/// #[php(sensitive(password))]
1588/// pub fn login(user: String, password: String) -> PhpResult<bool> {
1589///     let _ = password;
1590///     Err(PhpException::from_message(format!("Unknown user {user}")))
1591/// }
1592///
1593/// #[php_module]
1594/// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
1595///     module.function(wrap_function!(login))
1596/// }
1597/// # fn main() {}
1598/// ```
1599///
1600/// ## Variadic Functions
1601///
1602/// A slice parameter `&[T]` in last position is variadic, the equivalent of the
1603/// PHP `...$args` syntax. `T` is any argument type: `&[&Zval]` receives the raw
1604/// values, `&[i64]` receives converted integers. If one of the values cannot be
1605/// converted into `T`, the call throws a `TypeError`. A variadic parameter that
1606/// is not the last parameter is a compile error. The slice must be spelled in
1607/// the signature; a type alias that hides the slice is not detected.
1608///
1609/// ```rust,no_run,ignore
1610/// # #![cfg_attr(windows, feature(abi_vectorcall))]
1611/// # extern crate ext_php_rs;
1612/// use ext_php_rs::{prelude::*, types::Zval};
1613///
1614/// /// This can be called from PHP as `add(1, 2, 3, 4, 5)`
1615/// #[php_function]
1616/// pub fn add(number: u32, numbers:&[&Zval]) -> u32 {
1617///     // numbers is a slice of 4 Zvals all of type long
1618///     number
1619/// }
1620///
1621/// #[php_module]
1622/// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
1623///     module.function(wrap_function!(add))
1624/// }
1625/// # fn main() {}
1626/// ```
1627///
1628/// ## Performance
1629///
1630/// The `#[php_function]` macro generates a zero-allocation fast path that reads
1631/// arguments directly from the PHP call frame, matching how native C extensions
1632/// parse parameters. This applies automatically — no configuration needed.
1633///
1634/// The same fast path is used for class methods defined with `#[php_impl]`,
1635/// including instance methods (`&self`, `&mut self`) and static methods.
1636///
1637/// ```rust,no_run,ignore
1638/// # #![cfg_attr(windows, feature(abi_vectorcall))]
1639/// # extern crate ext_php_rs;
1640/// # use ext_php_rs::prelude::*;
1641/// // Fast path: standalone functions
1642/// #[php_function]
1643/// pub fn fast(name: String, age: Option<i32>) -> String { name }
1644///
1645/// // Fast path: parameters with defaults
1646/// #[php_function]
1647/// #[php(defaults(offset = 0))]
1648/// pub fn also_fast(haystack: &str, offset: i64) -> i64 { offset }
1649/// # fn main() {}
1650/// ```
1651///
1652/// ```rust,ignore
1653/// #[php_impl]
1654/// impl MyClass {
1655///     // Fast path: static methods
1656///     pub fn create(n: i32) -> Self { /* ... */ }
1657///
1658///     // Fast path: instance methods
1659///     pub fn get_value(&self, offset: i32) -> i32 { /* ... */ }
1660/// }
1661/// ```
1662///
1663/// The only case that falls back to the runtime argument parser is when using
1664/// variadic arguments (`&[T]`, the Rust equivalent of PHP's `...$args`):
1665///
1666/// ```rust,no_run,ignore
1667/// # #![cfg_attr(windows, feature(abi_vectorcall))]
1668/// # extern crate ext_php_rs;
1669/// # use ext_php_rs::{prelude::*, types::Zval};
1670/// // Slower path: variadic arguments require runtime parsing
1671/// #[php_function]
1672/// pub fn add(first: u32, rest: &[&Zval]) -> u32 { first }
1673/// # fn main() {}
1674/// ```
1675///
1676/// ## Returning `Result<T, E>`
1677///
1678/// You can also return a `Result` from the function. The error variant will be
1679/// translated into an exception and thrown. See the section on
1680/// [exceptions](../exceptions.md) for more details.
1681// END DOCS FROM function.md
1682#[proc_macro_attribute]
1683pub fn php_function(args: TokenStream, input: TokenStream) -> TokenStream {
1684    php_function_internal(args.into(), input.into()).into()
1685}
1686
1687#[allow(clippy::needless_pass_by_value)]
1688fn php_function_internal(args: TokenStream2, input: TokenStream2) -> TokenStream2 {
1689    let input = parse_macro_input2!(input as ItemFn);
1690    if let Err(e) = reject_args(&args, "php_function", PHP_ATTR_HINT) {
1691        return e.to_compile_error();
1692    }
1693
1694    function::parser(input).unwrap_or_else(|e| e.to_compile_error())
1695}
1696
1697// BEGIN DOCS FROM constant.md
1698/// # `#[php_const]` Attribute
1699///
1700/// Exports a Rust constant as a global PHP constant. The constant can be any
1701/// type that implements `IntoConst`.
1702///
1703/// The `wrap_constant!()` macro can be used to simplify the registration of
1704/// constants. It sets the name and doc comments for the constant.
1705///
1706/// You can rename the const with options:
1707///
1708/// - `name` - Allows you to rename the property, e.g. `#[php(name =
1709///   "new_name")]`
1710/// - `change_case` - Allows you to rename the property using rename rules, e.g.
1711///   `#[php(change_case = "PascalCase")]`
1712///
1713/// A global constant has no visibility. `#[php(vis = "...")]` on a
1714/// `#[php_const]` is a compile error. Use `vis` on constants inside a
1715/// `#[php_impl]` block.
1716///
1717/// ## Examples
1718///
1719/// ```rust,no_run,ignore
1720/// # #![cfg_attr(windows, feature(abi_vectorcall))]
1721/// # extern crate ext_php_rs;
1722/// use ext_php_rs::prelude::*;
1723///
1724/// #[php_const]
1725/// const TEST_CONSTANT: i32 = 100;
1726///
1727/// #[php_const]
1728/// #[php(name = "I_AM_RENAMED")]
1729/// const TEST_CONSTANT_THE_SECOND: i32 = 42;
1730///
1731/// #[php_const]
1732/// const ANOTHER_STRING_CONST: &'static str = "Hello world!";
1733///
1734/// #[php_module]
1735/// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
1736///     module
1737///         .constant(wrap_constant!(TEST_CONSTANT))
1738///         .constant(wrap_constant!(TEST_CONSTANT_THE_SECOND))
1739///         .constant(("MANUAL_CONSTANT", ANOTHER_STRING_CONST, &[]))
1740/// }
1741/// # fn main() {}
1742/// ```
1743///
1744/// ## PHP usage
1745///
1746/// ```php
1747/// <?php
1748///
1749/// var_dump(TEST_CONSTANT); // int(100)
1750/// var_dump(I_AM_RENAMED); // int(42)
1751/// var_dump(MANUAL_CONSTANT); // string(12) "Hello world!"
1752/// ```
1753// END DOCS FROM constant.md
1754#[proc_macro_attribute]
1755pub fn php_const(args: TokenStream, input: TokenStream) -> TokenStream {
1756    php_const_internal(args.into(), input.into()).into()
1757}
1758
1759#[allow(clippy::needless_pass_by_value)]
1760fn php_const_internal(args: TokenStream2, input: TokenStream2) -> TokenStream2 {
1761    let input = parse_macro_input2!(input as ItemConst);
1762    if let Err(e) = reject_args(&args, "php_const", PHP_ATTR_HINT) {
1763        return e.to_compile_error();
1764    }
1765
1766    constant::parser(input).unwrap_or_else(|e| e.to_compile_error())
1767}
1768
1769// BEGIN DOCS FROM module.md
1770/// # `#[php_module]` Attribute
1771///
1772/// The module macro is used to annotate the `get_module` function, which is
1773/// used by the PHP interpreter to retrieve information about your extension,
1774/// including the name, version, functions and extra initialization functions.
1775/// Regardless if you use this macro, your extension requires a `extern "C" fn
1776/// get_module()` so that PHP can get this information.
1777///
1778/// The function is renamed to `get_module` if you have used another name. The
1779/// macro also exports a crate-prefixed `<crate_name>_get_module` alias, used
1780/// when [statically linking the extension into
1781/// php-src](../advanced/static_linking.md). Building with
1782/// `EXT_PHP_RS_STATIC_EXT=1` removes the unmangled `get_module` export (the
1783/// function itself stays callable from Rust) so a statically linked
1784/// extension cannot collide with another extension exporting `get_module`.
1785/// Dynamic builds (`extension=` / `dl()`) must not set the variable.
1786///
1787/// The function is passed an instance of `ModuleBuilder` which allows you to
1788/// register the following (if required):
1789///
1790/// - Functions, classes, and constants
1791/// - Extension and request startup and shutdown functions.
1792///   - Read more about the PHP extension lifecycle [here](https://www.phpinternalsbook.com/php7/extensions_design/php_lifecycle.html).
1793/// - PHP extension information function
1794///   - Used by the `phpinfo()` function to get information about your
1795///     extension.
1796///
1797/// Classes and constants are not registered with PHP in the `get_module`
1798/// function. These are registered inside the extension startup function.
1799///
1800/// ## Startup failures
1801///
1802/// If a constant, an interface, a class or an enum cannot be registered, the
1803/// generated startup function logs the cause as an `E_CORE_WARNING` and returns
1804/// `FAILURE`. PHP then reports `Unable to start <extension> module`. At engine
1805/// startup PHP exits. From `dl()` the request fails. A panic during
1806/// registration gets the same treatment. The process does not abort in either
1807/// case.
1808///
1809/// `get_module` cannot report a failure, because PHP reads the returned entry
1810/// without a check. If the `ModuleBuilder` cannot become a module entry, for
1811/// example because a function name, an argument name or the module name
1812/// contains a NUL byte, the macro returns a placeholder entry with the name of
1813/// the crate. The startup function of that entry logs the build error and fails
1814/// as described above.
1815///
1816/// The `startup` function that you name in `#[php(startup = ...)]` is your own
1817/// `extern "C"` function. A panic inside it is not caught.
1818///
1819/// You can also give a startup function to `ModuleBuilder::startup_function`.
1820/// The generated startup function calls it after the `#[php(startup = ...)]`
1821/// function. The two functions run before the macro registers the constants,
1822/// interfaces, classes and enums.
1823///
1824/// ## Usage
1825///
1826/// ```rust,no_run,ignore
1827/// # #![cfg_attr(windows, feature(abi_vectorcall))]
1828/// # extern crate ext_php_rs;
1829/// use ext_php_rs::{
1830///     prelude::*,
1831///     zend::ModuleEntry,
1832///     info_table_start,
1833///     info_table_row,
1834///     info_table_end
1835/// };
1836///
1837/// #[php_const]
1838/// pub const MY_CUSTOM_CONST: &'static str = "Hello, world!";
1839///
1840/// #[php_class]
1841/// pub struct Test {
1842///     a: i32,
1843///     b: i32
1844/// }
1845/// #[php_function]
1846/// pub fn hello_world() -> &'static str {
1847///     "Hello, world!"
1848/// }
1849///
1850/// /// Used by the `phpinfo()` function and when you run `php -i`.
1851/// /// This will probably be simplified with another macro eventually!
1852/// pub extern "C" fn php_module_info(_module: *mut ModuleEntry) {
1853///     info_table_start!();
1854///     info_table_row!("my extension", "enabled");
1855///     info_table_end!();
1856/// }
1857///
1858/// #[php_module]
1859/// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
1860///     module
1861///         .constant(wrap_constant!(MY_CUSTOM_CONST))
1862///         .class::<Test>()
1863///         .function(wrap_function!(hello_world))
1864///         .info_function(php_module_info)
1865/// }
1866/// # fn main() {}
1867/// ```
1868// END DOCS FROM module.md
1869#[proc_macro_attribute]
1870pub fn php_module(args: TokenStream, input: TokenStream) -> TokenStream {
1871    php_module_internal(args.into(), input.into()).into()
1872}
1873
1874#[allow(clippy::needless_pass_by_value)]
1875fn php_module_internal(args: TokenStream2, input: TokenStream2) -> TokenStream2 {
1876    let input = parse_macro_input2!(input as ItemFn);
1877    if let Err(e) = reject_args(&args, "php_module", PHP_ATTR_HINT) {
1878        return e.to_compile_error();
1879    }
1880
1881    module::parser(input).unwrap_or_else(|e| e.to_compile_error())
1882}
1883
1884// BEGIN DOCS FROM impl.md
1885/// # `#[php_impl]` Attribute
1886///
1887/// You can export an entire `impl` block to PHP. This exports all methods as
1888/// well as constants to PHP on the class that it is implemented on. This
1889/// requires the `#[php_class]` macro to already be used on the underlying
1890/// struct. Trait implementations cannot be exported to PHP. Only one `impl`
1891/// block can be exported per class.
1892///
1893/// If you do not want a function exported to PHP, you should place it in a
1894/// separate `impl` block.
1895///
1896/// If you want to use async Rust, use `#[php_async_impl]`, instead: see [here
1897/// &raquo;](./async_impl.md) for more info.
1898///
1899/// ## Options
1900///
1901/// By default all constants are renamed to `UPPER_CASE` and all methods are
1902/// renamed to camelCase. This can be changed by passing the
1903/// `change_method_case` and `change_constant_case` as `#[php]` attributes on
1904/// the `impl` block. The options are:
1905///
1906/// - `#[php(change_method_case = "snake_case")]` - Renames the method to snake
1907///   case.
1908/// - `#[php(change_constant_case = "snake_case")]` - Renames the constant to
1909///   snake case.
1910///
1911/// See the [`name` and `change_case`](./php.md#name-and-change_case) section
1912/// for a list of all available cases.
1913///
1914/// ## Methods
1915///
1916/// Methods basically follow the same rules as functions, so read about the
1917/// [`php_function`] macro first. The primary difference between functions and
1918/// methods is they are bounded by their class object.
1919///
1920/// Both instance and static methods benefit from the same zero-allocation fast
1921/// path as standalone functions. See the
1922/// [Performance](./function.md#performance) section for details.
1923///
1924/// Class methods can take a `&self` or `&mut self` parameter. They cannot take
1925/// a consuming `self` parameter. Static methods can omit this `self` parameter.
1926///
1927/// To access the underlying Zend object, you can take a reference to a
1928/// `ZendClassObject<T>` in place of the self parameter, where the parameter
1929/// must be named `self_`. This can also be used to return a reference to
1930/// `$this`.
1931///
1932/// The rest of the options are passed as separate attributes:
1933///
1934/// - `#[php(defaults(i = 5, b = "\"hello\""))]` - Sets the default value for
1935///   parameter(s). Each key must name a parameter.
1936/// - `#[php(optional = i)]` - Sets the first optional parameter. Note that this
1937///   also sets the remaining parameters as optional, so each of them must be an
1938///   `Option<T>` or have a default. Otherwise the build fails.
1939/// - `#[php(sensitive(password))]` - Declares the named parameter(s)
1940///   `#[\SensitiveParameter]`, so exception traces hide their values. See
1941///   [sensitive parameters](./function.md#sensitive-parameters).
1942/// - `#[php(vis = "public")]`, `#[php(vis = "protected")]` and `#[php(vis =
1943///   "private")]` - Sets the visibility of the method.
1944/// - `#[php(name = "method_name")]` - Renames the PHP method to a different
1945///   identifier, without renaming the Rust method name.
1946/// - `#[php(final)]` - Makes the method final (cannot be overridden in
1947///   subclasses).
1948/// - `#[php(abstract)]` - Makes the method abstract (must be implemented by
1949///   subclasses). Can only be used in abstract classes.
1950///
1951/// The `#[php(defaults)]` and `#[php(optional)]` attributes operate the same as
1952/// the equivalent function attribute parameters. A `self_` parameter is the PHP
1953/// object, not a PHP parameter, so `defaults` and `optional` cannot name it. A
1954/// `getter` or `setter` method accepts neither option.
1955///
1956/// ### Static Methods
1957///
1958/// Methods that do not take a `&self` or `&mut self` parameter are
1959/// automatically exported as static methods. These can be called on the class
1960/// itself without creating an instance.
1961///
1962/// ```rust,ignore
1963/// #[php_impl]
1964/// impl MyClass {
1965///     // Static method - no self parameter
1966///     pub fn create_default() -> Self {
1967///         Self { /* ... */ }
1968///     }
1969///
1970///     // Instance method - takes &self
1971///     pub fn get_value(&self) -> i32 {
1972///         self.value
1973///     }
1974/// }
1975/// ```
1976///
1977/// From PHP:
1978///
1979/// ```php
1980/// $obj = MyClass::createDefault(); // Static call
1981/// $val = $obj->getValue();         // Instance call
1982/// ```
1983///
1984/// ### Final Methods
1985///
1986/// Methods marked with `#[php(final)]` cannot be overridden in subclasses. This
1987/// is useful when you want to prevent modification of critical functionality.
1988///
1989/// ```rust,ignore
1990/// use ext_php_rs::prelude::*;
1991///
1992/// #[php_class]
1993/// pub struct SecureClass;
1994///
1995/// #[php_impl]
1996/// impl SecureClass {
1997///     #[php(final)]
1998///     pub fn secure_method(&self) -> &str {
1999///         "This cannot be overridden"
2000///     }
2001///
2002///     // Final static methods are also supported
2003///     #[php(final)]
2004///     pub fn secure_static() -> i32 {
2005///         42
2006///     }
2007/// }
2008/// ```
2009///
2010/// ### Abstract Methods
2011///
2012/// Methods marked with `#[php(abstract)]` must be implemented by subclasses.
2013/// Abstract methods can only be defined in abstract classes (classes with
2014/// `ClassFlags::Abstract`).
2015///
2016/// ```rust,ignore
2017/// use ext_php_rs::prelude::*;
2018/// use ext_php_rs::flags::ClassFlags;
2019///
2020/// #[php_class]
2021/// #[php(flags = ClassFlags::Abstract)]
2022/// pub struct AbstractShape;
2023///
2024/// #[php_impl]
2025/// impl AbstractShape {
2026///     // Protected constructor for subclasses
2027///     #[php(vis = "protected")]
2028///     pub fn __construct() -> Self {
2029///         Self
2030///     }
2031///
2032///     // Abstract method - subclasses must implement this.
2033///     // The body is never called; use unimplemented!() as a placeholder.
2034///     #[php(abstract)]
2035///     pub fn area(&self) -> f64 {
2036///         unimplemented!()
2037///     }
2038///
2039///     // Concrete method in abstract class
2040///     pub fn describe(&self) -> String {
2041///         format!("A shape with area {}", self.area())
2042///     }
2043/// }
2044/// ```
2045///
2046/// **Note:** Abstract method bodies are never called - they exist only because
2047/// Rust syntax requires a body for methods in `impl` blocks. Use
2048/// `unimplemented!()` as a clear placeholder.
2049///
2050/// **Note:** If you try to use `#[php(abstract)]` on a method in a non-abstract
2051/// class, you will get a compile-time error.
2052///
2053/// **Note:** PHP does not support abstract static methods. If you need static
2054/// behavior that can be customized by subclasses, use a regular instance method
2055/// or the [late static binding](https://www.php.net/manual/en/language.oop5.late-static-bindings.php)
2056/// pattern in PHP.
2057///
2058/// ### Constructors
2059///
2060/// By default, if a class does not have a constructor, it is not constructable
2061/// from PHP. It can only be returned from a Rust function to PHP.
2062///
2063/// Constructors are Rust methods which can take any amount of parameters and
2064/// returns either `Self` or `Result<Self, E>`, where `E: Into<PhpException>`.
2065/// When the error variant of `Result` is encountered, it is thrown as an
2066/// exception and the class is not constructed.
2067///
2068/// Constructors are designated by either naming the method `__construct` or by
2069/// annotating a method with the `#[php(constructor)]` attribute. Note that when
2070/// using the attribute, the function is not exported to PHP like a regular
2071/// method.
2072///
2073/// Constructors cannot use the visibility or rename attributes listed above.
2074///
2075/// ## Constants
2076///
2077/// Constants are defined as regular Rust `impl` constants. Any type that
2078/// implements `IntoZval` can be used as a constant. A constant accepts the
2079/// `name`, `change_case` and `vis` options. The default visibility is `public`.
2080///
2081/// ```rust,no_run,ignore
2082/// # #![cfg_attr(windows, feature(abi_vectorcall))]
2083/// # extern crate ext_php_rs;
2084/// # use ext_php_rs::prelude::*;
2085/// #[php_class]
2086/// pub struct Limits;
2087///
2088/// #[php_impl]
2089/// impl Limits {
2090///     const MAX_USERS: i64 = 100;
2091///
2092///     #[php(vis = "protected")]
2093///     const MAX_RETRIES: i64 = 3;
2094///
2095///     #[php(vis = "private", name = "SEED")]
2096///     const RANDOM_SEED: i64 = 42;
2097/// }
2098/// # fn main() {}
2099/// ```
2100///
2101/// PHP sees `public const MAX_USERS`, `protected const MAX_RETRIES` and
2102/// `private const SEED`. Reflection reports the same visibility, and the
2103/// generated stubs print it.
2104///
2105/// ## Property getters and setters
2106///
2107/// A class can expose a property whose value comes from Rust methods. Mark the
2108/// method that reads the value with `#[php(getter)]` and the method that writes
2109/// it with `#[php(setter)]`. The property name is the method name without its
2110/// `get_` or `set_` prefix, converted to camelCase. This is the same rule as
2111/// for `#[php(prop)]` fields: `get_first_name` gives the property `firstName`.
2112/// Properties are declared in the order in which their first
2113/// getter or setter appears in the `impl` block, after the `#[php(prop)]` field
2114/// properties of the struct.
2115///
2116/// If you want a different property name, pass a `name` or `change_case` option
2117/// to the `#[php]` attribute: `#[php(getter, name = "first_name")]` or
2118/// `#[php(getter, change_case = "snake_case")]`.
2119///
2120/// A property does not need both methods. A property with only a getter is
2121/// read-only. A property with only a setter is write-only.
2122///
2123/// One method cannot be both a getter and a setter. A property cannot have two
2124/// getters or two setters. The getter and the setter of one property must have
2125/// the same visibility. Each of these cases is a compile error.
2126///
2127/// A method property cannot have the same name as a `#[php(prop)]` field of the
2128/// struct. The two macros are expanded separately, so this is not a compile
2129/// error. The extension fails to load with this error:
2130///
2131/// ```text
2132/// property `Book::$title` is declared twice
2133/// ```
2134///
2135/// The PHP type of the property comes from the return type of the getter. If
2136/// the property has no getter, the type comes from the argument of the setter.
2137/// The documentation of the property comes from the getter, or from the setter
2138/// when the getter has none. Like field properties, the type must implement
2139/// `IntoZval` and `FromZval`.
2140///
2141/// ## Example
2142///
2143/// Continuing on from our `Human` example in the structs section, we will
2144/// define a constructor, as well as getters for the properties. We will also
2145/// define a constant for the maximum age of a `Human`.
2146///
2147/// ```rust,no_run,ignore
2148/// # #![cfg_attr(windows, feature(abi_vectorcall))]
2149/// # extern crate ext_php_rs;
2150/// use ext_php_rs::{prelude::*, types::ZendClassObject};
2151///
2152/// #[php_class]
2153/// #[derive(Debug, Default)]
2154/// pub struct Human {
2155///     name: String,
2156///     age: i32,
2157///     #[php(prop)]
2158///     address: String,
2159/// }
2160///
2161/// #[php_impl]
2162/// impl Human {
2163///     const MAX_AGE: i32 = 100;
2164///
2165///     // No `#[constructor]` attribute required here - the name is `__construct`.
2166///     pub fn __construct(name: String, age: i32) -> Self {
2167///         Self {
2168///             name,
2169///             age,
2170///             address: String::new()
2171///         }
2172///     }
2173///
2174///     #[php(getter)]
2175///     pub fn get_name(&self) -> String {
2176///         self.name.to_string()
2177///     }
2178///
2179///     #[php(setter)]
2180///     pub fn set_name(&mut self, name: String) {
2181///         self.name = name;
2182///     }
2183///
2184///     #[php(getter)]
2185///     pub fn get_age(&self) -> i32 {
2186///         self.age
2187///     }
2188///
2189///     pub fn introduce(&self) {
2190///         println!("My name is {} and I am {} years old. I live at {}.", self.name, self.age, self.address);
2191///     }
2192///
2193///     pub fn get_raw_obj(self_: &mut ZendClassObject<Human>) -> &mut ZendClassObject<Human> {
2194///         dbg!(self_)
2195///     }
2196///
2197///     pub fn get_max_age() -> i32 {
2198///         Self::MAX_AGE
2199///     }
2200/// }
2201/// #[php_module]
2202/// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
2203///     module.class::<Human>()
2204/// }
2205/// # fn main() {}
2206/// ```
2207///
2208/// Using our newly created class in PHP:
2209///
2210/// ```php
2211/// <?php
2212///
2213/// $me = new Human('David', 20);
2214///
2215/// $me->introduce(); // My name is David and I am 20 years old.
2216/// var_dump(Human::get_max_age()); // int(100)
2217/// var_dump(Human::MAX_AGE); // int(100)
2218/// ```
2219///
2220/// [`php_async_impl`]: ./async_impl.md
2221// END DOCS FROM impl.md
2222#[proc_macro_attribute]
2223pub fn php_impl(args: TokenStream, input: TokenStream) -> TokenStream {
2224    php_impl_internal(args.into(), input.into()).into()
2225}
2226
2227#[allow(clippy::needless_pass_by_value)]
2228fn php_impl_internal(args: TokenStream2, input: TokenStream2) -> TokenStream2 {
2229    let input = parse_macro_input2!(input as ItemImpl);
2230    if let Err(e) = reject_args(&args, "php_impl", PHP_ATTR_HINT) {
2231        return e.to_compile_error();
2232    }
2233
2234    impl_::parser(input).unwrap_or_else(|e| e.to_compile_error())
2235}
2236
2237/// # `#[php_impl_interface]` Attribute
2238///
2239/// Marks a trait implementation as implementing a PHP interface. This allows
2240/// Rust structs marked with `#[php_class]` to implement Rust traits marked
2241/// with `#[php_interface]`, and have PHP recognize the relationship.
2242///
2243/// **Key feature**: The macro automatically registers the trait methods as PHP
2244/// methods on the class. You don't need to duplicate them in a separate
2245/// `#[php_impl]` block.
2246///
2247/// ## Usage
2248///
2249/// ```rust,no_run,ignore
2250/// # #![cfg_attr(windows, feature(abi_vectorcall))]
2251/// # extern crate ext_php_rs;
2252/// use ext_php_rs::prelude::*;
2253///
2254/// #[php_interface]
2255/// trait MyInterface {
2256///     fn my_method(&self) -> String;
2257/// }
2258///
2259/// #[php_class]
2260/// struct MyClass;
2261///
2262/// // The trait method my_method() is automatically registered as a PHP method
2263/// #[php_impl_interface]
2264/// impl MyInterface for MyClass {
2265///     fn my_method(&self) -> String {
2266///         "Hello from MyClass!".to_string()
2267///     }
2268/// }
2269///
2270/// #[php_module]
2271/// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
2272///     module
2273///         .interface::<PhpInterfaceMyInterface>()
2274///         .class::<MyClass>()
2275/// }
2276/// # fn main() {}
2277/// ```
2278///
2279/// After registration, PHP's `is_a($obj, 'MyInterface')` will return `true`
2280/// for instances of `MyClass`, and `$obj->myMethod()` will be callable.
2281///
2282/// ## Method names
2283///
2284/// Each method is registered under the name that `#[php_interface]` gave it.
2285/// The trait's `change_method_case` and the `name` or `change_case` of a trait
2286/// method apply to the class too. The macro takes no arguments:
2287/// `#[php_impl_interface(change_method_case = "...")]` is a compile error.
2288///
2289/// ## Requirements
2290///
2291/// - The trait must be marked with `#[php_interface]`
2292/// - The struct must be marked with `#[php_class]`
2293/// - The interface must be registered before the class in the module builder
2294#[proc_macro_attribute]
2295pub fn php_impl_interface(args: TokenStream, input: TokenStream) -> TokenStream {
2296    php_impl_interface_internal(args.into(), input.into()).into()
2297}
2298
2299#[allow(clippy::needless_pass_by_value)]
2300fn php_impl_interface_internal(args: TokenStream2, input: TokenStream2) -> TokenStream2 {
2301    let input = parse_macro_input2!(input as ItemImpl);
2302    let attr_args = match darling::ast::NestedMeta::parse_meta_list(args) {
2303        Ok(v) => v,
2304        Err(e) => return e.to_compile_error(),
2305    };
2306    let args = match impl_interface::PhpImplInterfaceArgs::from_list(&attr_args) {
2307        Ok(v) => v,
2308        Err(e) => return e.write_errors(),
2309    };
2310
2311    impl_interface::parser(&args, &input).unwrap_or_else(|e| e.to_compile_error())
2312}
2313
2314// BEGIN DOCS FROM extern.md
2315/// # `#[php_extern]` Attribute
2316///
2317/// Attribute used to annotate `extern` blocks which are deemed as PHP
2318/// functions.
2319///
2320/// This allows you to 'import' PHP functions into Rust so that they can be
2321/// called like regular Rust functions. Parameters can be any type that
2322/// implements [`IntoZval`], and the return type can be anything that implements
2323/// [`From<Zval>`] (notice how [`Zval`] is consumed rather than borrowed in this
2324/// case).
2325///
2326/// Unlike most other attributes, this does not need to be placed inside a
2327/// `#[php_module]` block.
2328///
2329/// # Panics
2330///
2331/// The function can panic when called under a few circumstances:
2332///
2333/// * The function could not be found or was not callable.
2334/// * One of the parameters could not be converted into a [`Zval`].
2335/// * The actual function call failed internally.
2336/// * The output [`Zval`] could not be parsed into the output type.
2337///
2338/// Inside a function that PHP calls, these panics become a PHP `Error`. The
2339/// process does not abort. See [Exceptions](../exceptions.md#panics).
2340///
2341/// The last point can be important when interacting with functions that return
2342/// unions, such as [`strpos`] which can return an integer or a boolean. In this
2343/// case, a [`Zval`] should be returned as parsing a boolean to an integer is
2344/// invalid, and vice versa.
2345///
2346/// # Example
2347///
2348/// This `extern` block imports the [`strpos`] function from PHP. Notice that
2349/// the string parameters can take either [`String`] or [`&str`], the optional
2350/// parameter `offset` is an [`Option<i64>`], and the return value is a [`Zval`]
2351/// as the return type is an integer-boolean union.
2352///
2353/// ```rust,no_run,ignore
2354/// # #![cfg_attr(windows, feature(abi_vectorcall))]
2355/// # extern crate ext_php_rs;
2356/// use ext_php_rs::{
2357///     prelude::*,
2358///     types::Zval,
2359/// };
2360///
2361/// #[php_extern]
2362/// extern "C" {
2363///     fn strpos(haystack: &str, needle: &str, offset: Option<i64>) -> Zval;
2364/// }
2365///
2366/// #[php_function]
2367/// pub fn my_strpos() {
2368///     assert_eq!(unsafe { strpos("Hello", "e", None) }.long(), Some(1));
2369/// }
2370///
2371/// #[php_module]
2372/// pub fn module(module: ModuleBuilder) -> ModuleBuilder {
2373///     module.function(wrap_function!(my_strpos))
2374/// }
2375/// # fn main() {}
2376/// ```
2377///
2378/// [`strpos`]: https://www.php.net/manual/en/function.strpos.php
2379/// [`IntoZval`]: crate::convert::IntoZval
2380/// [`Zval`]: crate::types::Zval
2381// END DOCS FROM extern.md
2382#[proc_macro_attribute]
2383pub fn php_extern(args: TokenStream, input: TokenStream) -> TokenStream {
2384    php_extern_internal(args.into(), input.into()).into()
2385}
2386
2387#[allow(clippy::needless_pass_by_value)]
2388fn php_extern_internal(args: TokenStream2, input: TokenStream2) -> TokenStream2 {
2389    let input = parse_macro_input2!(input as ItemForeignMod);
2390    if let Err(e) = reject_args(&args, "php_extern", NO_ARGS_HINT) {
2391        return e.to_compile_error();
2392    }
2393
2394    extern_::parser(input).unwrap_or_else(|e| e.to_compile_error())
2395}
2396
2397// BEGIN DOCS FROM zval_convert.md
2398/// # `ZvalConvert` Derive Macro
2399///
2400/// The `#[derive(ZvalConvert)]` macro derives the `FromZval` and `IntoZval`
2401/// traits on a struct or enum.
2402///
2403/// ## Structs
2404///
2405/// When used on a struct, the `FromZendObject` and `IntoZendObject` traits are
2406/// also implemented, mapping fields to properties in both directions. All
2407/// fields on the struct must implement `FromZval` as well. Generics are allowed
2408/// on structs that use the derive macro, however, the implementation will add a
2409/// `FromZval` bound to all generics types.
2410///
2411/// ### Examples
2412///
2413/// ```rust,no_run,ignore
2414/// # #![cfg_attr(windows, feature(abi_vectorcall))]
2415/// # extern crate ext_php_rs;
2416/// use ext_php_rs::prelude::*;
2417///
2418/// #[derive(ZvalConvert)]
2419/// pub struct ExampleClass<'a> {
2420///     a: i32,
2421///     b: String,
2422///     c: &'a str
2423/// }
2424///
2425/// #[php_function]
2426/// pub fn take_object(obj: ExampleClass) {
2427///     dbg!(obj.a, obj.b, obj.c);
2428/// }
2429///
2430/// #[php_function]
2431/// pub fn give_object() -> ExampleClass<'static> {
2432///     ExampleClass {
2433///         a: 5,
2434///         b: "String".to_string(),
2435///         c: "Borrowed",
2436///     }
2437/// }
2438///
2439/// #[php_module]
2440/// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
2441///     module
2442///         .function(wrap_function!(take_object))
2443///         .function(wrap_function!(give_object))
2444/// }
2445/// # fn main() {}
2446/// ```
2447///
2448/// Calling from PHP:
2449///
2450/// ```php
2451/// <?php
2452///
2453/// $obj = new stdClass;
2454/// $obj->a = 5;
2455/// $obj->b = 'Hello, world!';
2456/// $obj->c = 'another string';
2457///
2458/// take_object($obj);
2459/// var_dump(give_object());
2460/// ```
2461///
2462/// Another example involving generics:
2463///
2464/// ```rust,no_run,ignore
2465/// # #![cfg_attr(windows, feature(abi_vectorcall))]
2466/// # extern crate ext_php_rs;
2467/// use ext_php_rs::prelude::*;
2468///
2469/// // T must implement both `PartialEq<i32>` and `FromZval`.
2470/// #[derive(Debug, ZvalConvert)]
2471/// pub struct CompareVals<T: PartialEq<i32>> {
2472///     a: T,
2473///     b: T
2474/// }
2475///
2476/// #[php_function]
2477/// pub fn take_object(obj: CompareVals<i32>) {
2478///     dbg!(obj);
2479/// }
2480///
2481/// #[php_module]
2482/// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
2483///     module
2484///         .function(wrap_function!(take_object))
2485/// }
2486/// # fn main() {}
2487/// ```
2488///
2489/// ## Enums
2490///
2491/// When used on an enum, the `FromZval` implementation will treat the enum as a
2492/// tagged union with a mixed datatype. This allows you to accept multiple types
2493/// in a parameter, for example, a string and an integer.
2494///
2495/// The enum variants must not have named fields, and each variant must have
2496/// exactly one field (the type to extract from the zval). Optionally, the enum
2497/// may have one default variant with no data contained, which will be used when
2498/// the rest of the variants could not be extracted from the zval.
2499///
2500/// The ordering of the variants in the enum is important, as the `FromZval`
2501/// implementation will attempt to parse the zval data in order. For example, if
2502/// you put a `String` variant before an integer variant, the integer would be
2503/// converted to a string and passed as the string variant. Because an `f64` or
2504/// `f32` variant also accepts an integer, the integer variants must come before
2505/// it.
2506///
2507/// ### Examples
2508///
2509/// Basic example showing the importance of variant ordering and default field:
2510///
2511/// ```rust,no_run,ignore
2512/// # #![cfg_attr(windows, feature(abi_vectorcall))]
2513/// # extern crate ext_php_rs;
2514/// use ext_php_rs::prelude::*;
2515///
2516/// #[derive(Debug, ZvalConvert)]
2517/// pub enum UnionExample<'a> {
2518///     Long(u64), // Long
2519///     ProperStr(&'a str), // Actual string - not a converted value
2520///     ParsedStr(String), // Potentially parsed string, i.e. a double
2521///     None // Zval did not contain anything that could be parsed above
2522/// }
2523///
2524/// #[php_function]
2525/// pub fn test_union(val: UnionExample) {
2526///     dbg!(val);
2527/// }
2528///
2529/// #[php_function]
2530/// pub fn give_union() -> UnionExample<'static> {
2531///     UnionExample::Long(5)
2532/// }
2533///
2534/// #[php_module]
2535/// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
2536///     module
2537///         .function(wrap_function!(test_union))
2538///         .function(wrap_function!(give_union))
2539/// }
2540/// # fn main() {}
2541/// ```
2542///
2543/// Use in PHP:
2544///
2545/// ```php
2546/// test_union(5); // UnionExample::Long(5)
2547/// test_union("Hello, world!"); // UnionExample::ProperStr("Hello, world!")
2548/// test_union(5.66666); // UnionExample::ParsedStr("5.6666")
2549/// test_union(null); // UnionExample::None
2550/// var_dump(give_union()); // int(5)
2551/// ```
2552// END DOCS FROM zval_convert.md
2553#[proc_macro_derive(ZvalConvert, attributes(php))]
2554pub fn zval_convert_derive(input: TokenStream) -> TokenStream {
2555    zval_convert_derive_internal(input.into()).into()
2556}
2557
2558fn zval_convert_derive_internal(input: TokenStream2) -> TokenStream2 {
2559    let input = parse_macro_input2!(input as DeriveInput);
2560
2561    zval::parser(input).unwrap_or_else(|e| e.to_compile_error())
2562}
2563
2564/// Defines an `extern` function with the Zend fastcall convention based on
2565/// operating system.
2566///
2567/// On Windows, Zend fastcall functions use the vector calling convention, while
2568/// on all other operating systems no fastcall convention is used (just the
2569/// regular C calling convention).
2570///
2571/// This macro wraps a function and applies the correct calling convention.
2572///
2573/// ## Examples
2574///
2575/// ```rust,ignore
2576/// # #![cfg_attr(windows, feature(abi_vectorcall))]
2577/// use ext_php_rs::zend_fastcall;
2578///
2579/// zend_fastcall! {
2580///     pub extern fn test_hello_world(a: i32, b: i32) -> i32 {
2581///         a + b
2582///     }
2583/// }
2584/// ```
2585///
2586/// On Windows, this function will have the signature `pub extern "vectorcall"
2587/// fn(i32, i32) -> i32`, while on macOS/Linux the function will have the
2588/// signature `pub extern "C" fn(i32, i32) -> i32`.
2589///
2590/// ## Support
2591///
2592/// The `vectorcall` ABI is currently only supported on Windows with nightly
2593/// Rust and the `abi_vectorcall` feature enabled.
2594#[proc_macro]
2595pub fn zend_fastcall(input: TokenStream) -> TokenStream {
2596    zend_fastcall_internal(input.into()).into()
2597}
2598
2599fn zend_fastcall_internal(input: TokenStream2) -> TokenStream2 {
2600    let input = parse_macro_input2!(input as ItemFn);
2601
2602    fastcall::parser(input)
2603}
2604
2605/// Wraps a function to be used in the [`Module::function`] method.
2606#[proc_macro]
2607pub fn wrap_function(input: TokenStream) -> TokenStream {
2608    wrap_function_internal(input.into()).into()
2609}
2610
2611fn wrap_function_internal(input: TokenStream2) -> TokenStream2 {
2612    let input = parse_macro_input2!(input as syn::Path);
2613
2614    match function::wrap(&input) {
2615        Ok(parsed) => parsed,
2616        Err(e) => e.to_compile_error(),
2617    }
2618}
2619
2620/// Wraps a constant to be used in the [`ModuleBuilder::constant`] method.
2621#[proc_macro]
2622pub fn wrap_constant(input: TokenStream) -> TokenStream {
2623    wrap_constant_internal(input.into()).into()
2624}
2625
2626fn wrap_constant_internal(input: TokenStream2) -> TokenStream2 {
2627    let input = parse_macro_input2!(input as syn::Path);
2628
2629    match constant::wrap(&input) {
2630        Ok(parsed) => parsed,
2631        Err(e) => e.to_compile_error(),
2632    }
2633}
2634
2635const PHP_ATTR_HINT: &str = "Use `#[php(<args>)]` on the item instead.";
2636const NO_ARGS_HINT: &str = "It takes no arguments.";
2637
2638fn reject_args(args: &TokenStream2, macro_name: &str, hint: &str) -> Result<(), syn::Error> {
2639    if args.is_empty() {
2640        return Ok(());
2641    }
2642    Err(err!(args => "`#[{macro_name}(<args>)]` args are not supported. {hint}"))
2643}
2644
2645macro_rules! parse_macro_input2 {
2646    ($tokenstream:ident as $ty:ty) => {
2647        match syn::parse2::<$ty>($tokenstream) {
2648            Ok(data) => data,
2649            Err(err) => {
2650                return proc_macro2::TokenStream::from(err.to_compile_error());
2651            }
2652        }
2653    };
2654    ($tokenstream:ident) => {
2655        $crate::parse_macro_input!($tokenstream as _)
2656    };
2657}
2658
2659pub(crate) use parse_macro_input2;
2660
2661macro_rules! err {
2662    ($span:expr => $($msg:tt)*) => {
2663        ::syn::Error::new(::syn::spanned::Spanned::span(&$span), format!($($msg)*))
2664    };
2665    ($($msg:tt)*) => {
2666        ::syn::Error::new(::proc_macro2::Span::call_site(), format!($($msg)*))
2667    };
2668}
2669
2670/// Bails out of a function with a syn error.
2671macro_rules! bail {
2672    ($span:expr => $($msg:tt)*) => {
2673        return Err($crate::err!($span => $($msg)*))
2674    };
2675    ($($msg:tt)*) => {
2676        return Err($crate::err!($($msg)*))
2677    };
2678}
2679
2680pub(crate) use bail;
2681pub(crate) use err;
2682
2683pub(crate) mod prelude {
2684    pub(crate) trait OptionTokens {
2685        fn option_tokens(&self) -> proc_macro2::TokenStream;
2686    }
2687
2688    impl<T: quote::ToTokens> OptionTokens for Option<T> {
2689        fn option_tokens(&self) -> proc_macro2::TokenStream {
2690            if let Some(token) = self {
2691                quote::quote! { ::std::option::Option::Some(#token) }
2692            } else {
2693                quote::quote! { ::std::option::Option::None }
2694            }
2695        }
2696    }
2697
2698    pub(crate) use crate::{bail, err};
2699    pub(crate) type Result<T> = std::result::Result<T, syn::Error>;
2700}
2701
2702#[cfg(test)]
2703mod tests {
2704    use super::*;
2705    use std::path::PathBuf;
2706
2707    type AttributeFn =
2708        fn(proc_macro2::TokenStream, proc_macro2::TokenStream) -> proc_macro2::TokenStream;
2709    type FunctionLikeFn = fn(proc_macro2::TokenStream) -> proc_macro2::TokenStream;
2710
2711    #[test]
2712    pub fn test_expand() {
2713        for entry in glob::glob("tests/expand/*.rs").expect("Failed to read expand tests glob") {
2714            let entry = entry.expect("Failed to read expand test file");
2715            runtime_expand_attr(&entry);
2716            runtime_expand_func(&entry);
2717            runtime_expand_derive(&entry);
2718        }
2719    }
2720
2721    fn runtime_expand_attr(path: &PathBuf) {
2722        let file = std::fs::File::open(path).expect("Failed to open expand test file");
2723        runtime_macros::emulate_attributelike_macro_expansion(
2724            file,
2725            &[
2726                ("php_class", php_class_internal as AttributeFn),
2727                ("php_const", php_const_internal as AttributeFn),
2728                ("php_enum", php_enum_internal as AttributeFn),
2729                ("php_interface", php_interface_internal as AttributeFn),
2730                ("php_extern", php_extern_internal as AttributeFn),
2731                ("php_function", php_function_internal as AttributeFn),
2732                ("php_impl", php_impl_internal as AttributeFn),
2733                (
2734                    "php_impl_interface",
2735                    php_impl_interface_internal as AttributeFn,
2736                ),
2737                ("php_module", php_module_internal as AttributeFn),
2738            ],
2739        )
2740        .expect("Failed to expand attribute macros in test file");
2741    }
2742
2743    fn runtime_expand_func(path: &PathBuf) {
2744        let file = std::fs::File::open(path).expect("Failed to open expand test file");
2745        runtime_macros::emulate_functionlike_macro_expansion(
2746            file,
2747            &[
2748                ("zend_fastcall", zend_fastcall_internal as FunctionLikeFn),
2749                ("wrap_function", wrap_function_internal as FunctionLikeFn),
2750                ("wrap_constant", wrap_constant_internal as FunctionLikeFn),
2751            ],
2752        )
2753        .expect("Failed to expand function-like macros in test file");
2754    }
2755
2756    fn runtime_expand_derive(path: &PathBuf) {
2757        let file = std::fs::File::open(path).expect("Failed to open expand test file");
2758        runtime_macros::emulate_derive_macro_expansion(
2759            file,
2760            &[("ZvalConvert", zval_convert_derive_internal)],
2761        )
2762        .expect("Failed to expand derive macros in test file");
2763    }
2764}