1use crate::ast::Access;
2use crate::ast::Argument;
3use crate::ast::ArrayElement;
4use crate::ast::Block;
5use crate::ast::Call;
6use crate::ast::ClassLikeConstantSelector;
7use crate::ast::ClassLikeMemberSelector;
8use crate::ast::Construct;
9use crate::ast::Expression;
10use crate::ast::ForBody;
11use crate::ast::ForeachBody;
12use crate::ast::IfBody;
13use crate::ast::MatchArm;
14use crate::ast::PartialApplication;
15use crate::ast::Return;
16use crate::ast::Statement;
17use crate::ast::SwitchBody;
18use crate::ast::SwitchCase;
19use crate::ast::WhileBody;
20use crate::utils::control_flow::ControlFlow;
21
22pub mod assignment;
23pub mod condition;
24pub mod control_flow;
25pub mod definition;
26pub mod reference;
27
28#[inline]
29#[must_use]
30pub fn find_returns_in_block<'ast, 'arena>(block: &'ast Block<'arena>) -> Vec<&'ast Return<'arena>> {
31 let mut returns = vec![];
32 for control_flow in control_flow::find_control_flows_in_block(block) {
33 if let ControlFlow::Return(r#return) = control_flow {
34 returns.push(r#return);
35 }
36 }
37
38 returns
39}
40
41#[inline]
42#[must_use]
43pub fn find_returns_in_statement<'ast, 'arena>(statement: &'ast Statement<'arena>) -> Vec<&'ast Return<'arena>> {
44 let mut returns = vec![];
45 for control_flow in control_flow::find_control_flows_in_statement(statement) {
46 if let ControlFlow::Return(r#return) = control_flow {
47 returns.push(r#return);
48 }
49 }
50
51 returns
52}
53
54#[inline]
55#[must_use]
56pub fn block_has_throws(block: &Block<'_>) -> bool {
57 for control_flow in control_flow::find_control_flows_in_block(block) {
58 if let ControlFlow::Throw(_) = control_flow {
59 return true;
60 }
61 }
62
63 false
64}
65
66#[inline]
67#[must_use]
68pub fn statement_has_throws(statement: &Statement<'_>) -> bool {
69 for control_flow in control_flow::find_control_flows_in_statement(statement) {
70 if let ControlFlow::Throw(_) = control_flow {
71 return true;
72 }
73 }
74
75 false
76}
77
78#[inline]
79#[must_use]
80pub fn expression_has_throws(expression: &Expression<'_>) -> bool {
81 for control_flow in control_flow::find_control_flows_in_expression(expression) {
82 if let ControlFlow::Throw(_) = control_flow {
83 return true;
84 }
85 }
86
87 false
88}
89
90#[inline]
91#[must_use]
92pub fn block_has_yield(block: &Block) -> bool {
93 for statement in &block.statements {
94 if statement_has_yield(statement) {
95 return true;
96 }
97 }
98
99 false
100}
101
102#[inline]
103pub fn statement_has_yield(statement: &Statement) -> bool {
104 match statement {
105 Statement::Namespace(namespace) => {
106 for statement in namespace.statements() {
107 if statement_has_yield(statement) {
108 return true;
109 }
110 }
111
112 false
113 }
114 Statement::Block(block) => block_has_yield(block),
115 Statement::Try(r#try) => {
116 if r#try.block.statements.iter().any(statement_has_yield)
117 || r#try.catch_clauses.iter().any(|catch| block_has_yield(&catch.block))
118 {
119 return true;
120 }
121
122 for catch in &r#try.catch_clauses {
123 if block_has_yield(&catch.block) {
124 return true;
125 }
126 }
127
128 if let Some(finally) = &r#try.finally_clause
129 && block_has_yield(&finally.block)
130 {
131 return true;
132 }
133
134 false
135 }
136 Statement::Foreach(foreach) => match &foreach.body {
137 ForeachBody::Statement(statement) => statement_has_yield(statement),
138 ForeachBody::ColonDelimited(foreach_colon_delimited_body) => {
139 foreach_colon_delimited_body.statements.iter().any(statement_has_yield)
140 }
141 },
142 Statement::For(r#for) => match &r#for.body {
143 ForBody::Statement(statement) => statement_has_yield(statement),
144 ForBody::ColonDelimited(foreach_colon_delimited_body) => {
145 foreach_colon_delimited_body.statements.iter().any(statement_has_yield)
146 }
147 },
148 Statement::While(r#while) => match &r#while.body {
149 WhileBody::Statement(statement) => statement_has_yield(statement),
150 WhileBody::ColonDelimited(foreach_colon_delimited_body) => {
151 foreach_colon_delimited_body.statements.iter().any(statement_has_yield)
152 }
153 },
154 Statement::DoWhile(do_while) => statement_has_yield(do_while.statement),
155 Statement::Switch(switch) => {
156 let cases = match &switch.body {
157 SwitchBody::BraceDelimited(switch_brace_delimited_body) => &switch_brace_delimited_body.cases,
158 SwitchBody::ColonDelimited(switch_colon_delimited_body) => &switch_colon_delimited_body.cases,
159 };
160
161 for case in cases {
162 match &case {
163 SwitchCase::Expression(switch_expression_case) => {
164 for statement in &switch_expression_case.statements {
165 if statement_has_yield(statement) {
166 return true;
167 }
168 }
169 }
170 SwitchCase::Default(switch_default_case) => {
171 for statement in &switch_default_case.statements {
172 if statement_has_yield(statement) {
173 return true;
174 }
175 }
176 }
177 }
178 }
179
180 false
181 }
182 Statement::If(r#if) => match &r#if.body {
183 IfBody::Statement(if_statement_body) => {
184 if statement_has_yield(if_statement_body.statement) {
185 return true;
186 }
187
188 for else_if in &if_statement_body.else_if_clauses {
189 if statement_has_yield(else_if.statement) {
190 return true;
191 }
192 }
193
194 if let Some(else_clause) = &if_statement_body.else_clause
195 && statement_has_yield(else_clause.statement)
196 {
197 return true;
198 }
199
200 false
201 }
202 IfBody::ColonDelimited(if_colon_delimited_body) => {
203 if if_colon_delimited_body.statements.iter().any(statement_has_yield) {
204 return true;
205 }
206
207 for else_if in &if_colon_delimited_body.else_if_clauses {
208 if else_if.statements.iter().any(statement_has_yield) {
209 return true;
210 }
211 }
212
213 if let Some(else_clause) = &if_colon_delimited_body.else_clause
214 && else_clause.statements.iter().any(statement_has_yield)
215 {
216 return true;
217 }
218
219 false
220 }
221 },
222 Statement::Expression(expression) => expression_has_yield(expression.expression),
223 _ => false,
224 }
225}
226
227#[inline]
228pub fn expression_has_yield(expression: &Expression) -> bool {
229 match &expression {
230 Expression::Parenthesized(parenthesized) => expression_has_yield(parenthesized.expression),
231 Expression::Literal(_) => false,
232 Expression::CompositeString(_) => false,
233 Expression::Binary(operation) => expression_has_yield(operation.lhs) || expression_has_yield(operation.rhs),
234 Expression::UnaryPrefix(operation) => expression_has_yield(operation.operand),
235 Expression::UnaryPostfix(operation) => expression_has_yield(operation.operand),
236 Expression::Assignment(assignment_operation) => {
237 expression_has_yield(assignment_operation.lhs) || expression_has_yield(assignment_operation.rhs)
238 }
239 Expression::Conditional(conditional) => {
240 expression_has_yield(conditional.condition)
241 || conditional.then.as_ref().is_some_and(|e| expression_has_yield(e))
242 || expression_has_yield(conditional.r#else)
243 }
244 Expression::Array(array) => array.elements.iter().any(|element| match element {
245 ArrayElement::KeyValue(key_value_array_element) => {
246 expression_has_yield(key_value_array_element.key) || expression_has_yield(key_value_array_element.value)
247 }
248 ArrayElement::Value(value_array_element) => expression_has_yield(value_array_element.value),
249 ArrayElement::Variadic(variadic_array_element) => expression_has_yield(variadic_array_element.value),
250 _ => false,
251 }),
252 Expression::LegacyArray(legacy_array) => legacy_array.elements.iter().any(|element| match element {
253 ArrayElement::KeyValue(key_value_array_element) => {
254 expression_has_yield(key_value_array_element.key) || expression_has_yield(key_value_array_element.value)
255 }
256 ArrayElement::Value(value_array_element) => expression_has_yield(value_array_element.value),
257 ArrayElement::Variadic(variadic_array_element) => expression_has_yield(variadic_array_element.value),
258 _ => false,
259 }),
260 Expression::List(list) => list.elements.iter().any(|element| match element {
261 ArrayElement::KeyValue(key_value_array_element) => {
262 expression_has_yield(key_value_array_element.key) || expression_has_yield(key_value_array_element.value)
263 }
264 ArrayElement::Value(value_array_element) => expression_has_yield(value_array_element.value),
265 ArrayElement::Variadic(variadic_array_element) => expression_has_yield(variadic_array_element.value),
266 _ => false,
267 }),
268 Expression::ArrayAccess(array_access) => {
269 expression_has_yield(array_access.array) || expression_has_yield(array_access.index)
270 }
271 Expression::ArrayAppend(array_append) => expression_has_yield(array_append.array),
272 Expression::Match(r#match) => {
273 expression_has_yield(r#match.expression)
274 || r#match.arms.iter().any(|arm| match arm {
275 MatchArm::Expression(match_expression_arm) => {
276 match_expression_arm.conditions.iter().any(expression_has_yield)
277 || expression_has_yield(match_expression_arm.expression)
278 }
279 MatchArm::Default(match_default_arm) => expression_has_yield(match_default_arm.expression),
280 })
281 }
282 Expression::Construct(construct) => match construct {
283 Construct::Isset(isset_construct) => isset_construct.values.iter().any(expression_has_yield),
284 Construct::Empty(empty_construct) => expression_has_yield(empty_construct.value),
285 Construct::Eval(eval_construct) => expression_has_yield(eval_construct.value),
286 Construct::Include(include_construct) => expression_has_yield(include_construct.value),
287 Construct::IncludeOnce(include_once_construct) => expression_has_yield(include_once_construct.value),
288 Construct::Require(require_construct) => expression_has_yield(require_construct.value),
289 Construct::RequireOnce(require_once_construct) => expression_has_yield(require_once_construct.value),
290 Construct::Print(print_construct) => expression_has_yield(print_construct.value),
291 Construct::Exit(exit_construct) => exit_construct.arguments.as_ref().is_some_and(|arguments| {
292 arguments.arguments.iter().any(|argument| match argument {
293 Argument::Positional(positional_argument) => expression_has_yield(&positional_argument.value),
294 Argument::Named(named_argument) => expression_has_yield(&named_argument.value),
295 })
296 }),
297 Construct::Die(die_construct) => die_construct.arguments.as_ref().is_some_and(|arguments| {
298 arguments.arguments.iter().any(|argument| match argument {
299 Argument::Positional(positional_argument) => expression_has_yield(&positional_argument.value),
300 Argument::Named(named_argument) => expression_has_yield(&named_argument.value),
301 })
302 }),
303 },
304 Expression::Throw(throw) => expression_has_yield(throw.exception),
305 Expression::Clone(clone) => expression_has_yield(clone.object),
306 Expression::Call(call) => match call {
307 Call::Function(function_call) => {
308 expression_has_yield(function_call.function)
309 || function_call.argument_list.arguments.iter().any(|argument| match argument {
310 Argument::Positional(positional_argument) => expression_has_yield(&positional_argument.value),
311 Argument::Named(named_argument) => expression_has_yield(&named_argument.value),
312 })
313 }
314 Call::Method(method_call) => {
315 expression_has_yield(method_call.object)
316 || matches!(&method_call.method, ClassLikeMemberSelector::Expression(selector) if expression_has_yield(selector.expression))
317 || method_call.argument_list.arguments.iter().any(|argument| match argument {
318 Argument::Positional(positional_argument) => expression_has_yield(&positional_argument.value),
319 Argument::Named(named_argument) => expression_has_yield(&named_argument.value),
320 })
321 }
322 Call::NullSafeMethod(null_safe_method_call) => {
323 expression_has_yield(null_safe_method_call.object)
324 || matches!(&null_safe_method_call.method, ClassLikeMemberSelector::Expression(selector) if expression_has_yield(selector.expression))
325 || null_safe_method_call.argument_list.arguments.iter().any(|argument| match argument {
326 Argument::Positional(positional_argument) => expression_has_yield(&positional_argument.value),
327 Argument::Named(named_argument) => expression_has_yield(&named_argument.value),
328 })
329 }
330 Call::StaticMethod(static_method_call) => {
331 expression_has_yield(static_method_call.class)
332 || matches!(&static_method_call.method, ClassLikeMemberSelector::Expression(selector) if expression_has_yield(selector.expression))
333 || static_method_call.argument_list.arguments.iter().any(|argument| match argument {
334 Argument::Positional(positional_argument) => expression_has_yield(&positional_argument.value),
335 Argument::Named(named_argument) => expression_has_yield(&named_argument.value),
336 })
337 }
338 },
339 Expression::Access(access) => match access {
340 Access::Property(property_access) => {
341 expression_has_yield(property_access.object)
342 || matches!(&property_access.property, ClassLikeMemberSelector::Expression(selector) if expression_has_yield(selector.expression))
343 }
344 Access::NullSafeProperty(null_safe_property_access) => {
345 expression_has_yield(null_safe_property_access.object)
346 || matches!(&null_safe_property_access.property, ClassLikeMemberSelector::Expression(selector) if expression_has_yield(selector.expression))
347 }
348 Access::StaticProperty(static_property_access) => expression_has_yield(static_property_access.class),
349 Access::ClassConstant(class_constant_access) => {
350 expression_has_yield(class_constant_access.class)
351 || matches!(&class_constant_access.constant, ClassLikeConstantSelector::Expression(selector) if expression_has_yield(selector.expression))
352 }
353 },
354 Expression::PartialApplication(partial_application) => match partial_application {
355 PartialApplication::Function(function_partial_application) => {
356 expression_has_yield(function_partial_application.function)
357 }
358 PartialApplication::Method(method_partial_application) => {
359 expression_has_yield(method_partial_application.object)
360 || matches!(&method_partial_application.method, ClassLikeMemberSelector::Expression(selector) if expression_has_yield(selector.expression))
361 }
362 PartialApplication::StaticMethod(static_method_partial_application) => {
363 expression_has_yield(static_method_partial_application.class)
364 || matches!(&static_method_partial_application.method, ClassLikeMemberSelector::Expression(selector) if expression_has_yield(selector.expression))
365 }
366 },
367 Expression::Instantiation(instantiation) => {
368 expression_has_yield(instantiation.class)
369 || instantiation.argument_list.as_ref().is_some_and(|arguments| {
370 arguments.arguments.iter().any(|argument| match argument {
371 Argument::Positional(positional_argument) => expression_has_yield(&positional_argument.value),
372 Argument::Named(named_argument) => expression_has_yield(&named_argument.value),
373 })
374 })
375 }
376 Expression::Yield(_) => true,
377 _ => false,
378 }
379}