2023-05-03 11:06:20 +02:00
|
|
|
== Why is this an issue?
|
|
|
|
|
2023-05-12 16:12:19 +02:00
|
|
|
In PHP, nested functions are functions, which are defined inside other functions.
|
|
|
|
They have access to the variables of the enclosing functions.
|
|
|
|
Once the parent function declares the nested function, the nested function becomes available to the global scope.
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2023-05-12 16:12:19 +02:00
|
|
|
Code that uses nested functions can become difficult to read, refactor and to maintain and should therefore be avoided.
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Noncompliant code example
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2023-05-12 16:12:19 +02:00
|
|
|
With the default threshold of 3 level:
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2023-09-12 15:20:48 +02:00
|
|
|
[source,php]
|
2021-04-28 16:49:39 +02:00
|
|
|
----
|
|
|
|
function f () {
|
|
|
|
function f_inner () {
|
|
|
|
function f_inner_inner() {
|
|
|
|
function f_inner_inner_inner() { // Noncompliant
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-05-12 16:12:19 +02:00
|
|
|
== Resources
|
|
|
|
|
|
|
|
=== Documentation
|
|
|
|
|
|
|
|
* https://www.php.net/manual/en/functions.user-defined.php[PHP Manual - User defined functions]
|
|
|
|
|
2021-09-20 15:38:42 +02:00
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
|
|
|
|
'''
|
2023-05-12 16:12:19 +02:00
|
|
|
|
2021-09-20 15:38:42 +02:00
|
|
|
== Implementation Specification
|
2023-05-12 16:12:19 +02:00
|
|
|
|
2021-09-20 15:38:42 +02:00
|
|
|
(visible only on this page)
|
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
=== Message
|
|
|
|
|
|
|
|
Refactor this code to not nest functions more than {n} levels deep.
|
|
|
|
|
|
|
|
|
|
|
|
=== Parameters
|
|
|
|
|
|
|
|
.max
|
|
|
|
****
|
|
|
|
|
|
|
|
----
|
|
|
|
3
|
|
|
|
----
|
|
|
|
|
|
|
|
Maximum allowed number of nested functions
|
|
|
|
****
|
2021-09-20 15:38:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
endif::env-github,rspecator-view[]
|