Skip to content

Commit c36ba6b

Browse files
committed
feat: Add safety documentation to get_case method in Enum
1 parent 5e5ace5 commit c36ba6b

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

phper/src/enums.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,14 @@ impl Enum {
190190
///
191191
/// A reference to ZObj representing the enum case, or an error if the case
192192
/// doesn't exist
193+
///
194+
/// # Safety
195+
///
196+
/// This function is marked as unsafe because the underlying `zend_enum_get_case`
197+
/// function may cause a SIGSEGV (segmentation fault) when the case doesn't exist.
198+
/// Even though this method attempts to check for null and return an error instead,
199+
/// there might still be scenarios where the PHP internal function behaves unpredictably.
200+
/// Callers must ensure the enum and case name are valid before calling this function.
193201
pub fn get_case<'a>(&self, case_name: impl AsRef<str>) -> crate::Result<&'a ZObj> {
194202
unsafe {
195203
let ce = self.as_class_entry().as_ptr() as *mut _;
@@ -225,6 +233,14 @@ impl Enum {
225233
///
226234
/// A mutable reference to ZObj representing the enum case, or an error if
227235
/// the case doesn't exist
236+
///
237+
/// # Safety
238+
///
239+
/// This function is marked as unsafe because the underlying `zend_enum_get_case`
240+
/// function may cause a SIGSEGV (segmentation fault) when the case doesn't exist.
241+
/// Even though this method attempts to check for null and return an error instead,
242+
/// there might still be scenarios where the PHP internal function behaves unpredictably.
243+
/// Callers must ensure the enum and case name are valid before calling this function.
228244
pub fn get_mut_case<'a>(&mut self, case_name: impl AsRef<str>) -> crate::Result<&'a mut ZObj> {
229245
unsafe {
230246
let ce = self.as_class_entry().as_ptr() as *mut _;

tests/integration/src/enums.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,4 @@ fn test_enum_from_name(module: &mut Module) {
112112

113113
Ok::<_, phper::Error>(zobj)
114114
});
115-
116-
// Test getting a non-existent case
117-
module.add_function("test_enum_get_invalid_case", |_args| {
118-
let pure_enum = Enum::from_name("IntegrationTest\\PureEnum");
119-
120-
// Try to get a non-existent enum case
121-
phper::ok(match pure_enum.get_case("NONEXISTENT") {
122-
Ok(_) => false,
123-
Err(phper::Error::EnumCaseNotFound(_)) => true,
124-
Err(_) => false,
125-
})
126-
});
127115
}

tests/integration/tests/php/enums.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,3 @@
7171
assert_eq($red_case->name, 'RED', 'Should be the RED case');
7272
assert_eq($red_case->value, 'FF0000', 'RED value should be FF0000');
7373
assert_eq($red_case, IntegrationTest\StringEnum::RED, 'Should be equal to the enum case');
74-
75-
// Test error handling when retrieving invalid case
76-
assert_true(test_enum_get_invalid_case(), 'Should return an error for non-existent case');

0 commit comments

Comments
 (0)