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

davidcole1340 / ext-php-rs / 15788182852

20 Jun 2025 09:32PM UTC coverage: 22.034%. Remained the same
15788182852

Pull #460

github

web-flow
Merge d4fc01bb5 into 660f308c0
Pull Request #460: chore(clippy): flowing lifetimes warning and clippy::mut_from_ref

6 of 56 new or added lines in 13 files covered. (10.71%)

3 existing lines in 2 files now uncovered.

871 of 3953 relevant lines covered (22.03%)

2.35 hits per line

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

31.82
/src/zend/class.rs
1
//! Builder and objects for creating classes in the PHP world.
2

3
use crate::ffi::instanceof_function_slow;
4
use crate::types::{ZendIterator, Zval};
5
use crate::{
6
    boxed::ZBox,
7
    ffi::zend_class_entry,
8
    flags::ClassFlags,
9
    types::{ZendObject, ZendStr},
10
    zend::ExecutorGlobals,
11
};
12
use std::ptr;
13
use std::{convert::TryInto, fmt::Debug};
14

15
/// A PHP class entry.
16
///
17
/// Represents a class registered with the PHP interpreter.
18
pub type ClassEntry = zend_class_entry;
19

20
impl ClassEntry {
21
    /// Attempts to find a reference to a class in the global class table.
22
    ///
23
    /// Returns a reference to the class if found, or [`None`] if the class
24
    /// could not be found or the class table has not been initialized.
25
    #[must_use]
26
    pub fn try_find(name: &str) -> Option<&'static Self> {
×
27
        ExecutorGlobals::get().class_table()?;
×
28
        let mut name = ZendStr::new(name, false);
×
29

30
        unsafe {
NEW
31
            crate::ffi::zend_lookup_class_ex(&raw mut *name, std::ptr::null_mut(), 0).as_ref()
×
32
        }
33
    }
34

35
    /// Creates a new [`ZendObject`], returned inside an [`ZBox<ZendObject>`]
36
    /// wrapper.
37
    ///
38
    /// # Panics
39
    ///
40
    /// Panics when allocating memory for the new object fails.
41
    #[allow(clippy::new_ret_no_self)]
42
    #[must_use]
43
    pub fn new(&self) -> ZBox<ZendObject> {
×
44
        ZendObject::new(self)
×
45
    }
46

47
    /// Returns the class flags.
48
    #[must_use]
49
    pub fn flags(&self) -> ClassFlags {
4✔
50
        ClassFlags::from_bits_truncate(self.ce_flags)
4✔
51
    }
52

53
    /// Returns `true` if the class entry is an interface, and `false`
54
    /// otherwise.
55
    #[must_use]
56
    pub fn is_interface(&self) -> bool {
×
57
        self.flags().contains(ClassFlags::Interface)
×
58
    }
59

60
    /// Checks if the class is an instance of another class or interface.
61
    ///
62
    /// # Parameters
63
    ///
64
    /// * `other` - The inherited class entry to check.
65
    #[must_use]
66
    pub fn instance_of(&self, other: &ClassEntry) -> bool {
4✔
67
        if self == other {
4✔
68
            return true;
×
69
        }
70

71
        unsafe { instanceof_function_slow(ptr::from_ref(self), ptr::from_ref(other)) }
4✔
72
    }
73

74
    /// Returns an iterator of all the interfaces that the class implements.
75
    ///
76
    /// Returns [`None`] if the interfaces have not been resolved on the
77
    /// class.
78
    ///
79
    /// # Panics
80
    ///
81
    /// Panics if the number of interfaces exceeds `isize::MAX`.
82
    #[must_use]
83
    pub fn interfaces(&self) -> Option<impl Iterator<Item = &ClassEntry>> {
×
84
        self.flags()
×
85
            .contains(ClassFlags::ResolvedInterfaces)
×
86
            .then(|| unsafe {
×
87
                (0..self.num_interfaces)
×
88
                    .map(move |i| {
×
89
                        *self
×
90
                            .__bindgen_anon_3
×
91
                            .interfaces
×
92
                            .offset(isize::try_from(i).expect("index exceeds isize"))
×
93
                    })
94
                    .filter_map(|ptr| ptr.as_ref())
×
95
            })
96
    }
97

98
    /// Returns the parent of the class.
99
    ///
100
    /// If the parent of the class has not been resolved, it attempts to find
101
    /// the parent by name. Returns [`None`] if the parent was not resolved
102
    /// and the parent was not able to be found by name.
103
    #[must_use]
104
    pub fn parent(&self) -> Option<&Self> {
×
105
        if self.flags().contains(ClassFlags::ResolvedParent) {
×
106
            unsafe { self.__bindgen_anon_1.parent.as_ref() }
×
107
        } else {
108
            let name = unsafe { self.__bindgen_anon_1.parent_name.as_ref()? };
×
109
            Self::try_find(name.as_str().ok()?)
×
110
        }
111
    }
112

113
    /// Returns the iterator for the class for a specific instance
114
    ///
115
    /// Returns [`None`] if there is no associated iterator for the class.
116
    #[must_use]
117
    pub fn get_iterator<'a>(&self, zval: &'a Zval, by_ref: bool) -> Option<&'a mut ZendIterator> {
2✔
118
        let ptr: *const Self = self;
2✔
119
        let zval_ptr: *const Zval = zval;
2✔
120

121
        let iterator =
2✔
122
            unsafe { (*ptr).get_iterator?(ptr.cast_mut(), zval_ptr.cast_mut(), i32::from(by_ref)) };
2✔
123

124
        unsafe { iterator.as_mut() }
×
125
    }
126

127
    /// Gets the name of the class.
128
    #[must_use]
129
    pub fn name(&self) -> Option<&str> {
19✔
130
        unsafe { self.name.as_ref().and_then(|s| s.as_str().ok()) }
57✔
131
    }
132
}
133

134
impl PartialEq for ClassEntry {
135
    fn eq(&self, other: &Self) -> bool {
9✔
136
        std::ptr::eq(self, other)
9✔
137
    }
138
}
139

140
impl Debug for ClassEntry {
141
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
×
142
        let name: String = unsafe { self.name.as_ref() }
×
143
            .and_then(|s| s.try_into().ok())
×
144
            .ok_or(std::fmt::Error)?;
×
145

146
        f.debug_struct("ClassEntry")
147
            .field("name", &name)
148
            .field("flags", &self.flags())
149
            .field("is_interface", &self.is_interface())
150
            .field(
151
                "interfaces",
152
                &self.interfaces().map(Iterator::collect::<Vec<_>>),
153
            )
154
            .field("parent", &self.parent())
155
            .finish()
156
    }
157
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc