mago-source 0.26.1

Manages PHP source files by providing functions for loading, reading, and referencing PHP code, making it easier to work with file-based inputs.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
use std::path::PathBuf;
use std::sync::Arc;

use ahash::HashMap;
use parking_lot::RwLock;
use serde::Deserialize;
use serde::Serialize;

use mago_interner::StringIdentifier;
use mago_interner::ThreadedInterner;

use crate::error::SourceError;

pub mod error;

/// Represents the category of the source for a PHP construct.
///
/// This enum categorizes the origin of a source, based on where it is are defined.
/// The categories are useful for distinguishing between user-written code, vendor-provided libraries,
/// and built-in PHP features.
///
/// # Variants
///
/// - `BuiltIn`: Represents a construct that is part of PHP's core or extension libraries.
/// - `External`: Represents a construct defined in a vendor-provided or third-party library.
/// - `UserDefined`: Represents a construct written by the user or part of the current project.
#[derive(Default, Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
pub enum SourceCategory {
    /// Represents a PHP construct that is part of the PHP core or extension libraries.
    BuiltIn,

    /// Represents a PHP construct defined in vendor-provided or third-party libraries.
    External,

    /// Represents a PHP construct written by the user or part of the current project.
    #[default]
    UserDefined,
}

/// A unique identifier for a source.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
#[repr(C)]
pub struct SourceIdentifier(pub StringIdentifier, pub SourceCategory);

/// Represents a source file.
#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
pub struct Source {
    pub identifier: SourceIdentifier,
    pub path: Option<PathBuf>,
    pub content: StringIdentifier,
    pub size: usize,
    pub lines: Vec<usize>,
}

/// Trait for items that have a source.
pub trait HasSource {
    fn source(&self) -> SourceIdentifier;
}

/// Internal structure to store source information before full loading.
#[derive(Debug)]
struct SourceEntry {
    /// The file path (if any).
    path: Option<PathBuf>,
    /// The content, if already loaded, plus its size and line-start positions.
    content: Option<(StringIdentifier, usize, Vec<usize>)>,
}

/// Internal container for our maps. We keep two maps:
///  - one from SourceIdentifier → SourceEntry
///  - an auxiliary index from interned name → SourceIdentifier
#[derive(Debug)]
struct SourceManagerInner {
    sources: HashMap<SourceIdentifier, SourceEntry>,
    sources_by_name: HashMap<StringIdentifier, SourceIdentifier>,
}

/// A manager for sources.
///
/// This version replaces DashMap with a single inner structure protected by a
/// high-performance `RwLock` (from the parking_lot crate) and uses AHashMap for speed.
#[derive(Clone, Debug)]
pub struct SourceManager {
    /// The interner used for source names and content.
    interner: ThreadedInterner,
    /// Inner maps protected by a lock.
    inner: Arc<RwLock<SourceManagerInner>>,
}

/// Methods for SourceCategory.
impl SourceCategory {
    #[inline(always)]
    pub const fn is_built_in(&self) -> bool {
        matches!(self, Self::BuiltIn)
    }

    #[inline(always)]
    pub const fn is_external(&self) -> bool {
        matches!(self, Self::External)
    }

    #[inline(always)]
    pub const fn is_user_defined(&self) -> bool {
        matches!(self, Self::UserDefined)
    }
}

/// Methods for SourceIdentifier.
impl SourceIdentifier {
    #[inline(always)]
    pub fn dummy() -> Self {
        Self(StringIdentifier::empty(), SourceCategory::UserDefined)
    }

    /// Returns the interned string identifier.
    #[inline(always)]
    pub const fn value(&self) -> StringIdentifier {
        self.0
    }

    /// Returns the source category.
    #[inline(always)]
    pub const fn category(&self) -> SourceCategory {
        self.1
    }
}
/// Methods for Source.
impl Source {
    /// Creates a [`Source`] from a single piece of `content` without needing
    /// a full [`SourceManager`].
    ///
    /// This is particularly useful for quick parsing or one-off analyses
    /// where you do not need to manage multiple sources.
    ///
    /// # Arguments
    ///
    /// * `interner` - A reference to a [`ThreadedInterner`] used to intern
    ///   the `content` and store string identifiers.
    /// * `name` - A logical identifier for this source, such as `"inline"`
    ///   or `"my_script.php"`.
    /// * `content` - The actual PHP (or other) code string.
    #[inline(always)]
    pub fn standalone(interner: &ThreadedInterner, name: &str, content: &str) -> Self {
        let lines: Vec<_> = line_starts(content).collect();
        let size = content.len();
        let content_id = interner.intern(content);

        Self {
            identifier: SourceIdentifier(interner.intern(name), SourceCategory::UserDefined),
            path: None,
            content: content_id,
            size,
            lines,
        }
    }

    /// Retrieve the line number for the given byte offset.
    ///
    /// # Parameters
    ///
    /// - `offset`: The byte offset to retrieve the line number for.
    ///
    /// # Returns
    ///
    /// The line number for the given byte offset (0-based index).
    #[inline(always)]
    pub fn line_number(&self, offset: usize) -> usize {
        self.lines.binary_search(&offset).unwrap_or_else(|next_line| next_line - 1)
    }

    /// Retrieve the byte offset for the start of the given line.
    ///
    /// # Parameters
    ///
    /// - `line`: The line number to retrieve the start offset for.
    ///
    /// # Returns
    ///
    /// The byte offset for the start of the given line (0-based index).
    pub fn get_line_start_offset(&self, line: usize) -> Option<usize> {
        self.lines.get(line).copied()
    }

    /// Retrieve the byte offset for the end of the given line.
    ///
    /// # Parameters
    ///
    /// - `line`: The line number to retrieve the end offset for.
    ///
    /// # Returns
    ///
    /// The byte offset for the end of the given line (0-based index).
    pub fn get_line_end_offset(&self, line: usize) -> Option<usize> {
        match self.lines.get(line + 1) {
            Some(&end) => Some(end - 1),
            None if line == self.lines.len() - 1 => Some(self.size),
            _ => None,
        }
    }

    /// Retrieve the column number for the given byte offset.
    ///
    /// # Parameters
    ///
    /// - `offset`: The byte offset to retrieve the column number for.
    ///
    /// # Returns
    ///
    /// The column number for the given byte offset (0-based index).
    #[inline(always)]
    pub fn column_number(&self, offset: usize) -> usize {
        let line_start = self.lines.binary_search(&offset).unwrap_or_else(|next_line| self.lines[next_line - 1]);

        offset - line_start
    }
}

impl SourceManager {
    /// Creates a new source manager.
    #[inline(always)]
    pub fn new(interner: ThreadedInterner) -> Self {
        Self {
            interner,
            inner: Arc::new(RwLock::new(SourceManagerInner {
                sources: HashMap::default(),
                sources_by_name: HashMap::default(),
            })),
        }
    }

    /// Inserts a source with the given name and file path.
    #[inline(always)]
    pub fn insert_path(&self, name: impl AsRef<str>, path: PathBuf, category: SourceCategory) -> SourceIdentifier {
        let name_str = name.as_ref();
        let name_id = self.interner.intern(name_str);
        let source_id = SourceIdentifier(name_id, category);

        {
            let inner = self.inner.read();
            if inner.sources.contains_key(&source_id) {
                return source_id;
            }
        }

        let mut inner = self.inner.write();
        // Double-check to avoid duplicate insertion.
        if inner.sources.contains_key(&source_id) {
            return source_id;
        }
        inner.sources.insert(source_id, SourceEntry { path: Some(path), content: None });
        inner.sources_by_name.insert(name_id, source_id);
        source_id
    }

    /// Inserts a source with the given name and content.
    #[inline(always)]
    pub fn insert_content(
        &self,
        name: impl AsRef<str>,
        content: impl AsRef<str>,
        category: SourceCategory,
    ) -> SourceIdentifier {
        let name_str = name.as_ref();
        let content_str = content.as_ref();
        let name_id = self.interner.intern(name_str);

        {
            let inner = self.inner.read();
            if let Some(&source_id) = inner.sources_by_name.get(&name_id) {
                return source_id;
            }
        }

        let lines: Vec<_> = line_starts(content_str).collect();
        let size = content_str.len();
        let content_id = self.interner.intern(content_str);
        let source_id = SourceIdentifier(name_id, category);

        let mut inner = self.inner.write();
        if let Some(&existing) = inner.sources_by_name.get(&name_id) {
            return existing;
        }
        inner.sources.insert(source_id, SourceEntry { path: None, content: Some((content_id, size, lines)) });
        inner.sources_by_name.insert(name_id, source_id);
        source_id
    }

    /// Returns whether the manager contains a source with the given identifier.
    #[inline(always)]
    pub fn contains(&self, source_id: &SourceIdentifier) -> bool {
        let inner = self.inner.read();
        inner.sources.contains_key(source_id)
    }

    /// Returns all source identifiers.
    #[inline(always)]
    pub fn source_ids(&self) -> Vec<SourceIdentifier> {
        let inner = self.inner.read();
        inner.sources.keys().cloned().collect()
    }

    /// Returns source identifiers for the given category.
    #[inline(always)]
    pub fn source_ids_for_category(&self, category: SourceCategory) -> Vec<SourceIdentifier> {
        let inner = self.inner.read();
        inner.sources.keys().filter(|id| id.category() == category).cloned().collect()
    }

    /// Returns source identifiers for categories other than the given one.
    #[inline(always)]
    pub fn source_ids_except_category(&self, category: SourceCategory) -> Vec<SourceIdentifier> {
        let inner = self.inner.read();
        inner.sources.keys().filter(|id| id.category() != category).cloned().collect()
    }

    /// Loads the source for the given identifier.
    ///
    /// If the source content is already loaded, it is returned immediately.
    /// Otherwise the file is read from disk, processed, and cached.
    #[inline(always)]
    pub fn load(&self, source_id: &SourceIdentifier) -> Result<Source, SourceError> {
        let path = {
            let inner = self.inner.read();
            let entry = inner.sources.get(source_id).ok_or(SourceError::UnavailableSource(*source_id))?;

            // First, try to read without locking for update.
            if let Some((content, size, ref lines)) = entry.content {
                return Ok(Source {
                    identifier: *source_id,
                    path: entry.path.clone(),
                    content,
                    size,
                    lines: lines.clone(),
                });
            }

            // Retrieve the file path (must exist if content is not loaded).
            entry.path.clone().ok_or(SourceError::UnavailableSource(*source_id))?
        };

        // Perform file I/O outside the lock.
        let bytes = std::fs::read(&path).map_err(SourceError::IOError)?;
        let content_str = match String::from_utf8(bytes) {
            Ok(s) => s,
            Err(err) => {
                let s = err.into_bytes();
                let s = String::from_utf8_lossy(&s).into_owned();
                if source_id.category().is_user_defined() {
                    tracing::debug!(
                        "Source '{}' contains invalid UTF-8 sequence; behavior is undefined.",
                        path.display()
                    );
                } else {
                    tracing::info!(
                        "Source '{}' contains invalid UTF-8 sequence; behavior is undefined.",
                        path.display()
                    );
                }

                s
            }
        };
        let lines: Vec<_> = line_starts(&content_str).collect();
        let size = content_str.len();
        let content_id = self.interner.intern(&content_str);

        // Update the entry under a write lock.
        {
            let mut inner = self.inner.write();
            if let Some(entry) = inner.sources.get_mut(source_id) {
                // Check again in case another thread updated it meanwhile.
                if entry.content.is_none() {
                    entry.content = Some((content_id, size, lines.clone()));
                }
                Ok(Source { identifier: *source_id, path: entry.path.clone(), content: content_id, size, lines })
            } else {
                Err(SourceError::UnavailableSource(*source_id))
            }
        }
    }

    /// Writes updated content for the source with the given identifier.
    #[inline(always)]
    pub fn write(&self, source_id: SourceIdentifier, new_content: impl AsRef<str>) -> Result<(), SourceError> {
        let new_content_str = new_content.as_ref();
        let new_content_id = self.interner.intern(new_content_str);
        let new_lines: Vec<_> = line_starts(new_content_str).collect();
        let new_size = new_content_str.len();

        let path_opt = {
            let mut inner = self.inner.write();
            let entry = inner.sources.get_mut(&source_id).ok_or(SourceError::UnavailableSource(source_id))?;
            if let Some((old_content, _, _)) = entry.content
                && old_content == new_content_id
            {
                return Ok(());
            }
            entry.content = Some((new_content_id, new_size, new_lines));
            entry.path.clone()
        };

        // If the source has an associated file, update it on disk.
        if let Some(ref path) = path_opt {
            std::fs::write(path, self.interner.lookup(&new_content_id)).map_err(SourceError::IOError)?;
        }

        Ok(())
    }

    /// Returns the number of sources.
    #[inline(always)]
    pub fn len(&self) -> usize {
        let inner = self.inner.read();
        inner.sources.len()
    }

    /// Returns true if there are no sources.
    #[inline(always)]
    pub fn is_empty(&self) -> bool {
        let inner = self.inner.read();
        inner.sources.is_empty()
    }
}

impl<T: HasSource> HasSource for Box<T> {
    #[inline(always)]
    fn source(&self) -> SourceIdentifier {
        self.as_ref().source()
    }
}

/// Returns an iterator over the starting byte offsets of each line in `source`.
#[inline(always)]
fn line_starts(source: &str) -> impl Iterator<Item = usize> + '_ {
    let bytes = source.as_bytes();

    std::iter::once(0)
        .chain(memchr::memchr_iter(b'\n', bytes).map(|i| if i > 0 && bytes[i - 1] == b'\r' { i } else { i + 1 }))
}