Skip to content

Commit 5d1f802

Browse files
authored
allow static interface methods (#198)
1 parent ba3e1d4 commit 5d1f802

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ interface Foo extends ArrayAccess, Iterator {}
5858

5959
## Add methods
6060

61-
Interface can add public abstract methods.
61+
Interface can add public abstract methods, and public static abstract methods.
6262

6363
```rust,no_run
6464
use phper::classes::{InterfaceEntity, ClassEntry, Visibility};
@@ -68,6 +68,7 @@ use phper::values::ZVal;
6868
6969
let mut foo = InterfaceEntity::new("Foo");
7070
foo.add_method("doSomethings").argument(Argument::new("name"));
71+
foo.add_static_method("test");
7172
```
7273

7374
Note that abstract has no method body, so you don't need to add the handler to the method.

phper/src/classes.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,15 @@ impl InterfaceEntity {
837837
self.method_entities.last_mut().unwrap()
838838
}
839839

840+
/// Add static member method to interface.
841+
pub fn add_static_method(&mut self, name: impl Into<String>) -> &mut MethodEntity {
842+
let mut entity = MethodEntity::new(name, None, Visibility::Public);
843+
entity.set_vis_abstract();
844+
entity.set_vis_static();
845+
self.method_entities.push(entity);
846+
self.method_entities.last_mut().unwrap()
847+
}
848+
840849
/// Add constant to interface
841850
pub fn add_constant(&mut self, name: impl Into<String>, value: impl Into<Scalar>) {
842851
let constant = ConstantEntity::new(name, value);

tests/integration/src/classes.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ fn integrate_i_bar(module: &mut Module) {
181181
.add_method("doSomethings")
182182
.argument(Argument::new("job_name"));
183183

184+
interface.add_static_method("myStaticMethod");
185+
184186
module.add_interface(interface);
185187
}
186188

tests/integration/tests/php/classes.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@
6565
$doSomethings = $interface->getMethod("doSomethings");
6666
assert_true($doSomethings->isPublic());
6767
assert_true($doSomethings->isAbstract());
68+
assert_false($doSomethings->isStatic());
69+
70+
$myStaticMethod = $interface->getMethod("myStaticMethod");
71+
assert_true($myStaticMethod->isPublic());
72+
assert_true($myStaticMethod->isAbstract());
73+
assert_true($myStaticMethod->isStatic());
6874

6975
// Test get or set static properties.
7076
assert_eq(IntegrationTest\PropsHolder::$foo, "bar");

0 commit comments

Comments
 (0)