use tree_sitter::Node;
use super::PhpFile;
use crate::inlinable::{PHP_IDENT, PHP_UNITS};
use crate::scope_model;
const VETO_KINDS: &[&str] = &[
"if_statement",
"while_statement",
"do_statement",
"for_statement",
"foreach_statement",
"switch_statement",
"try_statement",
"catch_clause",
"match_expression",
];
const OWNER_KINDS: &[&str] = &["function_definition", "method_declaration"];
static PHP_SEMANTICS: scope_model::Semantics = scope_model::Semantics {
pure: pure,
unit_kinds: PHP_UNITS,
ident_kind: PHP_IDENT,
veto: VETO_KINDS,
owners: OWNER_KINDS,
include_root_scope: false,
exempt_bindings: false,
};
pub fn used_once_offenses(fm: &PhpFile) -> Vec<crate::used_once::UsedOnceOffense> {
scope_model::used_once_offenses(
fm.tree.root_node(),
fm.src,
&|byte| fm.line_col(byte),
&fm.scopes,
&PHP_SEMANTICS,
)
}
pub fn never_used_offenses(fm: &PhpFile) -> Vec<crate::never_used::NeverUsedOffense> {
scope_model::never_used_offenses(
fm.tree.root_node(),
fm.src,
&|byte| fm.line_col(byte),
&fm.scopes,
&PHP_SEMANTICS,
)
}
fn pure(n: Node) -> bool {
match n.kind() {
"integer" | "float" | "string" | "encapsed_string" => children_pure(n),
"boolean" | "null" => true,
"array_creation_expression"
| "array_element_initializer"
| "parenthesized_expression"
| "binary_expression"
| "unary_op_expression" => children_pure(n),
_ => false,
}
}
fn children_pure(n: Node) -> bool {
n.children(&mut n.walk())
.filter(|ch| ch.is_named())
.all(|ch| pure(ch))
}