--- title: "Building Phanalist: A Rust-Powered Static Analyzer for PHP" author: "Denzyl" published: "2026-09-06T01:54:39.502Z" updated: "2026-09-06T13:54:26.625Z" url: "https://zyvop.com/write-your-own-static-analyzer-for-php-2eegf" canonical: "https://dev.to/denzyldick/the-beginning-of-my-php-static-analyzer-in-rust-5bp8" tags: ["Rust","php"] --- # Building Phanalist: A Rust-Powered Static Analyzer for PHP ![Cover image]() I keep reading everywhere that rust is fast, has memory safety guardrails, and is 1 of the most loved languages lately. After reading the book and dreaming about understanding the language, I decided to build a static analyzer for the language I've been using for over a decade. I will be naming it [Phanalist]() and the code will also be available on [https://github.com/denzyldick/phanalist](). To see if this idea would work, I made a list of 4 common bad practices/mistakes I often see. As you can see in the code below, we can all agree that the class doesn't make you happy. ```php fake_variable = 'hellworld'; } function test($a){ return 1; }} ``` Enter fullscreen mode Exit fullscreen mode To keep it easy, I will first find these four common mistakes that are easy to spot. This list will continue to grow in the future. - Misplaced opening tag. - Class name that starts in lowercase - Lowercase constants - Defining parameters without a type. The static analyzer should be beginner friendly. Instead of making the developer's life harder, it should be able to explain the error in a way that the developer should understand. But first, let us focus on the first steps. I will use `PHP-Parser` a rust library that can parse PHP code and generate an AST(abstract syntax tree). The parser's output for the PHP code above will be a vector(`Vec project.opening_tag(tag.span, file), } } ``` Enter fullscreen mode Exit fullscreen mode As you can see in the AST, the information we need is stored in the struct `Span`. ```javascript FullOpeningTag( Span { line: 1, column: 1, position: 0, }, ), ``` Enter fullscreen mode Exit fullscreen mode We only need to check if the field line and column are higher than 0. If they are, it means the opening tag is not in the correct position, and we will `push` a suggestion into the field `suggestion` of the `file` parameter that is being passed to the `opening_tag` function. ```rust pub fn opening_tag(&mut self, span: Span, file: &mut File) -> &mut Project { if span.line > 1 { file.suggestions.push( Suggestion::from( "The opening tag 1 { file.suggestions.push(Suggestion::from( format!( "The opening tag doesn't start at the right column: {}.", t.column ) .to_string(), span, )); } self } ``` Enter fullscreen mode Exit fullscreen mode _I won't explain how to navigate to the correct statement in the AST for the rest of the mistakes._ ### Class name that starts in lowercase. ```typescript class uTesting extends FakeClass ``` Enter fullscreen mode Exit fullscreen mode When you do this, it's harder to distinguish a class between a variable and a method. I need to find out if the first letter of the name of the class is capitalized. The `String` type has a method `chars` that can convert the string into an iterator containing all the letters. You can grab the first character with the `next()` function. The `char` type has some valuable methods. The one we need is `is_uppercase()`. ```rust pub fn has_capitalized_name(name: String, span: Span) -> Option { if !name.chars().next().unwrap().is_uppercase() { Some(Suggestion::from( format!("The class name {} is not capitalized. The first letter of the name of the class should be in uppercase.", name).to_string(), span )); } None} ``` Enter fullscreen mode Exit fullscreen mode ### Lowercase constants ```javascript const I_ = null; const hello = null; ``` Enter fullscreen mode Exit fullscreen mode Similar to the capitalized class name, it's easier to distinguish a normal variable from a constant if the constant is in uppercase. I assume the constant is already upper cased, so I initialize the variable `is_uppercase = true`. When iterating through all of the letters, if I see a letter that is not in uppercase, I set the `is_uppercase = false`. ```javascript pub fn uppercased_constant_name(entry: ConstantEntry) -> bool { match entry { ConstantEntry { name, equals, value, } => { let mut is_uppercase = true; for l in name.value.to_string().chars() { if l.is_uppercase() == false && l.is_alphabetic() { is_uppercase = l.is_uppercase() } } return is_uppercase; } }} ``` Enter fullscreen mode Exit fullscreen mode ### Defining parameters without a type. I strongly advocate always defining the type for every parameter you declare. You need to define the type to avoid opening the gates to a bad interpretation of your code and introducing unnecessary bugs. In the AST, the `FunctionParameter` contains the parameter. We can pattern match the `data_type` field and the arm that has the `None` will need to return true. We will return the false for the `Some(_)` arm. ```javascript pub fn function_parameter_without_type(parameter: FunctionParameter) -> bool { match parameter { FunctionParameter { comments, name, attributes, data_type, ellipsis, default, ampersand, } => match data_type { None => return true, Some(_) => return false, }, }} ``` Enter fullscreen mode Exit fullscreen mode # Conclusion In part [2](), I will show how to calculate the cyclomatic complexity of an example code. If you are a PHP developer, you know that a developer can make many more mistakes in PHP. In the future, this list will continue to grow into something more useful. Thanks for reading! Contribution is always welcome if you have a mistake you would like to add to [phanalist]().