- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: php-best-practices/avoid-reassigning-parameters
Language: PHP
Severity: Warning
Category: Error Prone
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.
<?php
function sum($a, $b) {
$a = 2;
return $a + $b
}
<?php
function sum($a, $b) {
return $a + $b
}
|
|
For more information, please read the Code Analysis documentation
Identify code vulnerabilities directly in yourVS Code editor
Identify code vulnerabilities directly inJetBrains products