Skip to content

Commit ccd4a94

Browse files
committed
document arg + return value modifiers
1 parent 85f8354 commit ccd4a94

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

phper-doc/doc/_06_module/_02_register_functions/index.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,43 @@ pub fn get_module() -> Module {
8080
Here, the argument is registered as
8181
[`Argument::by_ref`](phper::functions::Argument::by_ref). Therefore, the type of
8282
the `count` parameter is no longer long, but a reference.
83+
84+
## Argument and return type modifiers
85+
86+
Arguments can have type-hints, nullability and default values applied. Here we define a function that accepts
87+
a nullable class (in this case, an interface), and a string with a default value:
88+
89+
```rust,no-run
90+
use phper::{modules::Module, php_get_module, functions::Argument, echo};
91+
92+
#[php_get_module]
93+
pub fn get_module() -> Module {
94+
let mut module = Module::new(
95+
env!("CARGO_CRATE_NAME"),
96+
env!("CARGO_PKG_VERSION"),
97+
env!("CARGO_PKG_AUTHORS"),
98+
);
99+
100+
module.add_function("my_function", |_| -> phper::Result<()> {
101+
Ok(())
102+
})
103+
.argument(Argument::by_val("a_class").with_type_hint(ArgumentTypeHint::ClassEntry(String::from(r"\MyNamespace\MyInterface"))).allow_null())
104+
.argument(Argument::by_val("name").with_type_hint(ArgumentTypeHint::String).with_default_value(CString::new("'my_default'").unwrap()))
105+
.argument(Argument::by_val("optional_bool").with_type_hint(ArgumentTypeHint::Bool).optional());
106+
107+
module
108+
}
109+
```
110+
111+
The output of `php --re` for this function would look like:
112+
113+
```txt
114+
Function [ <internal:integration> function my_function ] {
115+
116+
- Parameters [3] {
117+
Parameter #0 [ <required> ?class_name $a_class ]
118+
Parameter #1 [ <optional> string $name = 'my_default' ]
119+
Parameter #2 [ <optional> bool $optional_bool = <default> ]
120+
}
121+
}
122+
```

phper-doc/doc/_06_module/_06_register_class/index.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,28 @@ foo.add_static_method(
102102
).argument(Argument::by_val("name"));
103103
```
104104

105+
## Argument and return type modifiers
106+
107+
Methods may add argument and return typehints as per functions. For example:
108+
109+
```rust,no-run
110+
use phper::classes::{ClassEntity, ClassEntry, Visibility};
111+
use phper::functions::Argument;
112+
use phper::types::{ArgumentTypeHint, ReturnTypeHint},
113+
114+
let mut foo = ClassEntity::new("Foo");
115+
foo.add_method(
116+
"test",
117+
Visibility::Public,
118+
|_this, _arguments| {
119+
Ok(())
120+
},
121+
)
122+
.argument(Argument::by_val("a_string").with_type_hint(ArgumentTypeHint::String))
123+
.argument(Argument::by_val("an_interface").with_type_hint(ArgumentTypeHint::ClassEntry(String::from(r"\MyNamespace\MyInterface")))))
124+
.return_type(ReturnType::by_val(ReturnTypeHint::Bool).allow_null());
125+
```
126+
105127
## Add constants
106128
Interfaces can have public constants. Value can be string|int|bool|float|null.
107129

phper/src/functions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,10 +540,10 @@ impl Argument {
540540
self
541541
}
542542

543-
/// Argument default value (always a String)
543+
/// Argument default value. Example: "'a-string'", "A_CONST", "42", "[0=>'zero']"
544544
pub fn with_default_value(mut self, default_value: CString) -> Self {
545545
self.default_value = Some(default_value);
546-
self.required = false; // important!
546+
self.required = false; // arg with default value does not count towards required arg count
547547
self
548548
}
549549
}

0 commit comments

Comments
 (0)