1.2.63. Avoid Large Array Assignation¶
Avoid setting large arrays to local variables. Such operation is done every time the function is called, and it wastes time.
This rule applies to constant arrays: when the arrays are dynamically build, with variables or properties, they are not reported here.
There are different ways to avoid this : inject the array, build the array once, use a constant or a global variable.
The effect on small arrays (less than 10 elements) is not significant. Arrays with 10 elements or more are reported here. The effect is also more important on functions that are called often, or within loops.
<?php
// with constants, for functions
const ARRAY = array(1,2,3,4,5,6,7,8,9,10,11);
function foo() {
$array = ARRAY;
//more code
}
// with class constants, for methods
class x {
const ARRAY = array(1,2,3,4,5,6,7,8,9,10,11);
function foo() {
$array = self::ARRAY;
//more code
}
}
// with properties, for methods
class x {
private $array = array(1,2,3,4,5,6,7,8,9,10,11);
function foo() {
$array = $this->array;
//more code
}
}
// injection, leveraging default values
function foo($array = array(1,2,3,4,5,6,7,8,9,10,11)) {
//more code
}
// local cache with static
function foo() {
static $array;
if ($array === null) {
$array = array(1,2,3,4,5,6,7,8,9,10,11);
}
//more code
}
// Avoid creating the same array all the time in a function
class x {
function foo() {
// assign to non local variable is OK.
// Here, to a property, though it may be better in a __construct or as default values
$this->s = array(1,2,3,4,5,6,7,8,9,10,11);
// This is wasting resources, as it is done each time.
$array = array(1,2,3,4,5,6,7,8,9,10,11);
}
}
?>
1.2.63.1. Connex PHP features¶
1.2.63.1.1. Suggestions¶
Make the literal a global constant or a class constant
Make the literal an argument, so it can be injected
Make the literal an property, with the array as default value
Make the literal an static variable, with the array as default value
1.2.63.1.2. Specs¶
Short name |
Structures/NoAssignationInFunction |
Rulesets |
|
Exakat since |
0.9.7 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Slow (1 hour) |
Precision |
High |
Available in |