Skip to main content

larust_convert/
controllers.rs

1//! `app/Http/Controllers/*.php` → enriches Phase 1's already-generated
2//! `todo!()` controller stubs (`convert.rs`'s `generate_controller_stubs`,
3//! driven by `routes::referenced_controllers`) with each stubbed method's
4//! **original PHP body preserved as a comment** - no logic translation,
5//! no smarter parameter typing (both explicitly out of scope, natural
6//! later enhancements). If the real source controller file doesn't exist,
7//! or a specific stubbed method isn't found in it, that method just gets
8//! Phase 1's existing bare stub - this module only ever adds a comment,
9//! never removes Phase 1's own guarantee that every referenced method
10//! compiles.
11
12use crate::php;
13
14pub struct ConvertedController {
15    pub content: String,
16}
17
18/// `methods` is the exact `(controller, [methods])` list Phase 1 already
19/// computed (`routes::referenced_controllers`) - this module doesn't
20/// re-derive which methods matter, only enriches their stub bodies.
21pub fn convert(
22    source: &str,
23    class_name: &str,
24    methods: &[String],
25) -> Result<ConvertedController, String> {
26    let tree = php::parse(source).map_err(|error| error.to_string())?;
27    if php::has_syntax_error(&tree) {
28        return Err(format!(
29            "{source_len} bytes read, but the file has a syntax error the parser couldn't recover from",
30            source_len = source.len()
31        ));
32    }
33    if php::find_class(&tree, source, class_name).is_none() {
34        return Err(format!("class `{class_name}` not found in this file"));
35    }
36
37    let mut out = format!("pub struct {class_name};\n\nimpl {class_name} {{\n");
38    for method in methods {
39        if let Some(body) = php::find_method(&tree, source, class_name, method)
40            .and_then(|m| m.child_by_field_name("body"))
41        {
42            out.push_str(&php::body_as_comment(body, source));
43        }
44        out.push_str(&format!(
45            "    pub async fn {method}() -> &'static str {{\n        todo!()\n    }}\n\n"
46        ));
47    }
48    out.push_str("}\n");
49    Ok(ConvertedController { content: out })
50}
51
52/// The original PHP method body, verbatim, as a comment block directly
53/// above the generated stub - a reference for whoever ports the real
54/// logic by hand, never translated itself.
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn preserves_a_method_body_as_a_comment_above_the_stub() {
61        let source = "<?php\nclass PostController extends Controller\n{\n    public function index()\n    {\n        return view('posts.index', ['posts' => Post::all()]);\n    }\n}\n";
62        let result = convert(source, "PostController", &["index".to_string()]).unwrap();
63        assert!(result.content.contains("// Original Laravel method body"));
64        assert!(result.content.contains("// return view('posts.index'"));
65        assert!(result
66            .content
67            .contains("pub async fn index() -> &'static str {"));
68        assert!(result.content.contains("todo!()"));
69    }
70
71    #[test]
72    fn a_stubbed_method_not_found_in_source_still_gets_a_bare_stub() {
73        let source = "<?php\nclass PostController extends Controller\n{\n    public function index() {}\n}\n";
74        let result = convert(
75            source,
76            "PostController",
77            &["index".to_string(), "show".to_string()],
78        )
79        .unwrap();
80        assert!(result.content.contains("pub async fn index()"));
81        assert!(result.content.contains("pub async fn show()"));
82    }
83
84    #[test]
85    fn rejects_when_the_class_is_not_found_in_the_file() {
86        let source = "<?php\nclass UserController extends Controller {}\n";
87        assert!(convert(source, "PostController", &["index".to_string()]).is_err());
88    }
89
90    #[test]
91    fn preserves_bodies_for_multiple_methods_independently() {
92        let source = "<?php\nclass PostController extends Controller\n{\n    public function index()\n    {\n        return Post::all();\n    }\n\n    public function show(Post $post)\n    {\n        return $post;\n    }\n}\n";
93        let result = convert(
94            source,
95            "PostController",
96            &["index".to_string(), "show".to_string()],
97        )
98        .unwrap();
99        assert!(result.content.contains("// return Post::all();"));
100        assert!(result.content.contains("// return $post;"));
101    }
102}