Cette page n'est pas encore disponible en français, sa traduction est en cours. Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.
This rule is a simple yet crucial guideline in PHP development. This rule states that once a parameter is passed into a function, its value should not be reassigned within the function. This practice is discouraged because it can lead to confusion and unexpected behavior, increasing the likelihood of bugs in the code.
The importance of this rule lies in its ability to enhance code readability and maintainability. When a parameter is reassigned, it can confuse other developers who may expect the parameter to retain its original value throughout the function. This can make the code more difficult to understand and debug, especially in complex codebases.
To adhere to this rule, developers should create new variables inside the function instead of reassigning the parameters’ values. If a function needs to modify a parameter’s value, it should do so by returning a new value rather than changing the parameter itself. For example, instead of writing function sum($a, $b) { $a = 2; return $a + $b; }, you can write function sum($a, $b) { $newA = 2; return $newA + $b; }. This makes the function’s behavior more predictable and the code easier to read and maintain.
Non-Compliant Code Examples
<?phpfunctionsum($a,$b){$a=2;return$a+$b}
Compliant Code Examples
<?phpfunctionsum($a,$b){return$a+$b}
Seamless integrations. Try Datadog Code Analysis
Datadog Code Analysis
Try this rule and analyze your code with Datadog Code Analysis
How to use this rule
1
2
rulesets:- php-best-practices # Rules to enforce PHP best practices.
Create a static-analysis.datadog.yml with the content above at the root of your repository
Use our free IDE Plugins or add Code Analysis scans to your CI pipelines