- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: php-code-style/bad-null-guard
Language: PHP
Severity: Error
Category: Error Prone
This rule checks for improper null guard conditions in PHP code. A null guard is a conditional statement that checks if a variable is null before proceeding with an operation. This is important to prevent null pointer dereferences, which occur when the program tries to access a memory location through a null pointer. Null pointer dereferences can lead to unexpected behaviors and crashes in your application.
In non-compliant code, the logical AND (&&
) and OR (||
) operators are used incorrectly in null guard conditions. This can lead to situations where a method is called on a null object, causing a null pointer dereference.
To avoid violating this rule, always use the correct logical operator in your null guard conditions. If you want to ensure that a method is only called when a variable is not null, use the AND operator (&&
). If you want to ensure that a method is called when a variable is null or the method returns true, use the OR operator (||
). This way, you can prevent null pointer dereferences and improve the robustness of your code.
<?php
if ($var == null && $var->method()) {
echo "method is true";
}
if ($var != null || $var->method()) {
echo "method is true";
}
<?php
if ($var == null || $var->method()) {
echo "method is true";
}
if ($var != null && $var->method()) {
echo "method is true";
}
|
|
For more information, please read the Code Analysis documentation
Identify code vulnerabilities directly in yourVS Code editor
Identify code vulnerabilities directly inJetBrains products