isHttps accesses the super-global variable $_SERVER. Open
public static function isHttps(): bool
{
if (!isset(self::$httpsCache)) {
self::$httpsCache = (!empty($_SERVER['HTTPS']) && 'off' !== strtolower($_SERVER['HTTPS']))
|| (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && 'https' === strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']));
- Read upRead up
- Exclude checks
Superglobals
Since: 0.2
Accessing a super-global variable directly is considered a bad practice. These variables should be encapsulated in objects that are provided by a framework, for instance.
Example
class Foo {
public function bar() {
$name = $_POST['foo'];
}
}
Source
requestId accesses the super-global variable $_SERVER. Open
public static function requestId(): string
{
if (empty(self::$requestId)) {
self::$requestId = sprintf('%08x', abs(crc32($_SERVER['REMOTE_ADDR'] . $_SERVER['REQUEST_TIME_FLOAT'] . $_SERVER['REMOTE_PORT'])));
}
- Read upRead up
- Exclude checks
Superglobals
Since: 0.2
Accessing a super-global variable directly is considered a bad practice. These variables should be encapsulated in objects that are provided by a framework, for instance.
Example
class Foo {
public function bar() {
$name = $_POST['foo'];
}
}
Source
isHttps accesses the super-global variable $_SERVER. Open
public static function isHttps(): bool
{
if (!isset(self::$httpsCache)) {
self::$httpsCache = (!empty($_SERVER['HTTPS']) && 'off' !== strtolower($_SERVER['HTTPS']))
|| (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && 'https' === strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']));
- Read upRead up
- Exclude checks
Superglobals
Since: 0.2
Accessing a super-global variable directly is considered a bad practice. These variables should be encapsulated in objects that are provided by a framework, for instance.
Example
class Foo {
public function bar() {
$name = $_POST['foo'];
}
}
Source
getBrowserInfo accesses the super-global variable $_SERVER. Open
public static function getBrowserInfo(): object
{
if (empty(self::$browserCache)) {
$browserAgent = strtolower(\App\Request::_getServer('HTTP_USER_AGENT', ''));
- Read upRead up
- Exclude checks
Superglobals
Since: 0.2
Accessing a super-global variable directly is considered a bad practice. These variables should be encapsulated in objects that are provided by a framework, for instance.
Example
class Foo {
public function bar() {
$name = $_POST['foo'];
}
}
Source
isHttps accesses the super-global variable $_SERVER. Open
public static function isHttps(): bool
{
if (!isset(self::$httpsCache)) {
self::$httpsCache = (!empty($_SERVER['HTTPS']) && 'off' !== strtolower($_SERVER['HTTPS']))
|| (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && 'https' === strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']));
- Read upRead up
- Exclude checks
Superglobals
Since: 0.2
Accessing a super-global variable directly is considered a bad practice. These variables should be encapsulated in objects that are provided by a framework, for instance.
Example
class Foo {
public function bar() {
$name = $_POST['foo'];
}
}
Source
isHttps accesses the super-global variable $_SERVER. Open
public static function isHttps(): bool
{
if (!isset(self::$httpsCache)) {
self::$httpsCache = (!empty($_SERVER['HTTPS']) && 'off' !== strtolower($_SERVER['HTTPS']))
|| (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && 'https' === strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']));
- Read upRead up
- Exclude checks
Superglobals
Since: 0.2
Accessing a super-global variable directly is considered a bad practice. These variables should be encapsulated in objects that are provided by a framework, for instance.
Example
class Foo {
public function bar() {
$name = $_POST['foo'];
}
}
Source
getRemoteIP accesses the super-global variable $_SERVER. Open
public static function getRemoteIP($onlyIP = false)
{
$address = Request::_getServer('REMOTE_ADDR');
if ($onlyIP) {
return empty($address) ? '' : $address;
- Read upRead up
- Exclude checks
Superglobals
Since: 0.2
Accessing a super-global variable directly is considered a bad practice. These variables should be encapsulated in objects that are provided by a framework, for instance.
Example
class Foo {
public function bar() {
$name = $_POST['foo'];
}
}
Source
getBrowserInfo accesses the super-global variable $_SERVER. Open
public static function getBrowserInfo(): object
{
if (empty(self::$browserCache)) {
$browserAgent = strtolower(\App\Request::_getServer('HTTP_USER_AGENT', ''));
- Read upRead up
- Exclude checks
Superglobals
Since: 0.2
Accessing a super-global variable directly is considered a bad practice. These variables should be encapsulated in objects that are provided by a framework, for instance.
Example
class Foo {
public function bar() {
$name = $_POST['foo'];
}
}
Source
requestId accesses the super-global variable $_SERVER. Open
public static function requestId(): string
{
if (empty(self::$requestId)) {
self::$requestId = sprintf('%08x', abs(crc32($_SERVER['REMOTE_ADDR'] . $_SERVER['REQUEST_TIME_FLOAT'] . $_SERVER['REMOTE_PORT'])));
}
- Read upRead up
- Exclude checks
Superglobals
Since: 0.2
Accessing a super-global variable directly is considered a bad practice. These variables should be encapsulated in objects that are provided by a framework, for instance.
Example
class Foo {
public function bar() {
$name = $_POST['foo'];
}
}
Source
requestId accesses the super-global variable $_SERVER. Open
public static function requestId(): string
{
if (empty(self::$requestId)) {
self::$requestId = sprintf('%08x', abs(crc32($_SERVER['REMOTE_ADDR'] . $_SERVER['REQUEST_TIME_FLOAT'] . $_SERVER['REMOTE_PORT'])));
}
- Read upRead up
- Exclude checks
Superglobals
Since: 0.2
Accessing a super-global variable directly is considered a bad practice. These variables should be encapsulated in objects that are provided by a framework, for instance.
Example
class Foo {
public function bar() {
$name = $_POST['foo'];
}
}
Source
getRemoteIP accesses the super-global variable $_SERVER. Open
public static function getRemoteIP($onlyIP = false)
{
$address = Request::_getServer('REMOTE_ADDR');
if ($onlyIP) {
return empty($address) ? '' : $address;
- Read upRead up
- Exclude checks
Superglobals
Since: 0.2
Accessing a super-global variable directly is considered a bad practice. These variables should be encapsulated in objects that are provided by a framework, for instance.
Example
class Foo {
public function bar() {
$name = $_POST['foo'];
}
}
Source
Consider simplifying this complex logical expression. Open
if (empty(self::$browserCache)) {
$browserAgent = strtolower(\App\Request::_getServer('HTTP_USER_AGENT', ''));
$browser = new \stdClass();
$browser->ver = 0;
Function getBrowserInfo
has a Cognitive Complexity of 29 (exceeds 5 allowed). Consider refactoring. Open
public static function getBrowserInfo(): object
{
if (empty(self::$browserCache)) {
$browserAgent = strtolower(\App\Request::_getServer('HTTP_USER_AGENT', ''));
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
The class RequestUtil has an overall complexity of 55 which is very high. The configured complexity threshold is 50. Open
class RequestUtil
{
/** @var stdClass Browser cache variable. */
protected static $browserCache;
- Exclude checks
Method getBrowserInfo
has 62 lines of code (exceeds 25 allowed). Consider refactoring. Open
public static function getBrowserInfo(): object
{
if (empty(self::$browserCache)) {
$browserAgent = strtolower(\App\Request::_getServer('HTTP_USER_AGENT', ''));
Function getRemoteIP
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
public static function getRemoteIP($onlyIP = false)
{
$address = Request::_getServer('REMOTE_ADDR');
if ($onlyIP) {
return empty($address) ? '' : $address;
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
The method getBrowserInfo() has an NPath complexity of 1601. The configured NPath complexity threshold is 200. Open
public static function getBrowserInfo(): object
{
if (empty(self::$browserCache)) {
$browserAgent = strtolower(\App\Request::_getServer('HTTP_USER_AGENT', ''));
- Read upRead up
- Exclude checks
NPathComplexity
Since: 0.1
The NPath complexity of a method is the number of acyclic execution paths through that method. A threshold of 200 is generally considered the point where measures should be taken to reduce complexity.
Example
class Foo {
function bar() {
// lots of complicated code
}
}
Source https://phpmd.org/rules/codesize.html#npathcomplexity
The method getBrowserInfo() has a Cyclomatic Complexity of 33. The configured cyclomatic complexity threshold is 10. Open
public static function getBrowserInfo(): object
{
if (empty(self::$browserCache)) {
$browserAgent = strtolower(\App\Request::_getServer('HTTP_USER_AGENT', ''));
- Read upRead up
- Exclude checks
CyclomaticComplexity
Since: 0.1
Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
Example
// Cyclomatic Complexity = 11
class Foo {
1 public function example() {
2 if ($a == $b) {
3 if ($a1 == $b1) {
fiddle();
4 } elseif ($a2 == $b2) {
fiddle();
} else {
fiddle();
}
5 } elseif ($c == $d) {
6 while ($c == $d) {
fiddle();
}
7 } elseif ($e == $f) {
8 for ($n = 0; $n < $h; $n++) {
fiddle();
}
} else {
switch ($z) {
9 case 1:
fiddle();
break;
10 case 2:
fiddle();
break;
11 case 3:
fiddle();
break;
default:
fiddle();
break;
}
}
}
}
Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity
Refactor this function to reduce its Cognitive Complexity from 37 to the 15 allowed. Open
public static function getBrowserInfo(): object
- Read upRead up
- Exclude checks
Cognitive Complexity is a measure of how hard the control flow of a function is to understand. Functions with high Cognitive Complexity will be difficult to maintain.
See
Avoid using undefined variables such as '$remoteIp' which will lead to PHP notices. Open
$remoteIp[] = 'X-Real-IP: ' . Request::_getServer('HTTP_X_REAL_IP');
- Read upRead up
- Exclude checks
UndefinedVariable
Since: 2.8.0
Detects when a variable is used that has not been defined before.
Example
class Foo
{
private function bar()
{
// $message is undefined
echo $message;
}
}
Source https://phpmd.org/rules/cleancode.html#undefinedvariable
Avoid using undefined variables such as '$remoteIp' which will lead to PHP notices. Open
if (!empty($remoteIp)) {
- Read upRead up
- Exclude checks
UndefinedVariable
Since: 2.8.0
Detects when a variable is used that has not been defined before.
Example
class Foo
{
private function bar()
{
// $message is undefined
echo $message;
}
}
Source https://phpmd.org/rules/cleancode.html#undefinedvariable
Avoid using undefined variables such as '$remoteIp' which will lead to PHP notices. Open
$address .= '(' . implode(',', $remoteIp) . ')';
- Read upRead up
- Exclude checks
UndefinedVariable
Since: 2.8.0
Detects when a variable is used that has not been defined before.
Example
class Foo
{
private function bar()
{
// $message is undefined
echo $message;
}
}
Source https://phpmd.org/rules/cleancode.html#undefinedvariable
Avoid using undefined variables such as '$remoteIp' which will lead to PHP notices. Open
$remoteIp[] = "$key: " . Request::_getServer($key);
- Read upRead up
- Exclude checks
UndefinedVariable
Since: 2.8.0
Detects when a variable is used that has not been defined before.
Example
class Foo
{
private function bar()
{
// $message is undefined
echo $message;
}
}
Source https://phpmd.org/rules/cleancode.html#undefinedvariable
Missing class import via use statement (line '77', column '19'). Open
$browser = new \stdClass();
- Read upRead up
- Exclude checks
MissingImport
Since: 2.7.0
Importing all external classes in a file through use statements makes them clearly visible.
Example
function make() {
return new \stdClass();
}
Source http://phpmd.org/rules/cleancode.html#MissingImport
The method getRemoteIP has a boolean flag argument $onlyIP, which is a certain sign of a Single Responsibility Principle violation. Open
public static function getRemoteIP($onlyIP = false)
- Read upRead up
- Exclude checks
BooleanArgumentFlag
Since: 1.4.0
A boolean flag argument is a reliable indicator for a violation of the Single Responsibility Principle (SRP). You can fix this problem by extracting the logic in the boolean flag into its own class or method.
Example
class Foo {
public function bar($flag = true) {
}
}
Source https://phpmd.org/rules/cleancode.html#booleanargumentflag
The method getBrowserInfo uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$browser->lang = 'en';
}
- Read upRead up
- Exclude checks
ElseExpression
Since: 1.4.0
An if expression with an else branch is basically not necessary. You can rewrite the conditions in a way that the else clause is not necessary and the code becomes simpler to read. To achieve this, use early return statements, though you may need to split the code it several smaller methods. For very simple assignments you could also use the ternary operations.
Example
class Foo
{
public function bar($flag)
{
if ($flag) {
// one branch
} else {
// another branch
}
}
}
Source https://phpmd.org/rules/cleancode.html#elseexpression
Avoid using static access to class 'App\Request' in method 'getRemoteIP'. Open
$remoteIp[] = "$key: " . Request::_getServer($key);
- Read upRead up
- Exclude checks
StaticAccess
Since: 1.4.0
Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.
Example
class Foo
{
public function bar()
{
Bar::baz();
}
}
Source https://phpmd.org/rules/cleancode.html#staticaccess
Avoid using static access to class 'App\Request' in method 'getBrowserInfo'. Open
$dirPath = explode('/', Request::_getServer('SCRIPT_NAME'));
- Read upRead up
- Exclude checks
StaticAccess
Since: 1.4.0
Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.
Example
class Foo
{
public function bar()
{
Bar::baz();
}
}
Source https://phpmd.org/rules/cleancode.html#staticaccess
The method getBrowserInfo uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$browser->name = 'unknow';
}
- Read upRead up
- Exclude checks
ElseExpression
Since: 1.4.0
An if expression with an else branch is basically not necessary. You can rewrite the conditions in a way that the else clause is not necessary and the code becomes simpler to read. To achieve this, use early return statements, though you may need to split the code it several smaller methods. For very simple assignments you could also use the ternary operations.
Example
class Foo
{
public function bar($flag)
{
if ($flag) {
// one branch
} else {
// another branch
}
}
}
Source https://phpmd.org/rules/cleancode.html#elseexpression
Avoid using static access to class 'App\Request' in method 'getBrowserInfo'. Open
$host = Request::_getServer('HTTP_X_FORWARDED_HOST', Request::_getServer('HTTP_HOST', ''));
- Read upRead up
- Exclude checks
StaticAccess
Since: 1.4.0
Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.
Example
class Foo
{
public function bar()
{
Bar::baz();
}
}
Source https://phpmd.org/rules/cleancode.html#staticaccess
Avoid using static access to class 'App\Request' in method 'getBrowserInfo'. Open
$browser->requestUri = ltrim(Request::_getServer('REQUEST_URI'), '/');
- Read upRead up
- Exclude checks
StaticAccess
Since: 1.4.0
Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.
Example
class Foo
{
public function bar()
{
Bar::baz();
}
}
Source https://phpmd.org/rules/cleancode.html#staticaccess
Avoid using static access to class 'App\Request' in method 'getRemoteIP'. Open
$address = Request::_getServer('REMOTE_ADDR');
- Read upRead up
- Exclude checks
StaticAccess
Since: 1.4.0
Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.
Example
class Foo
{
public function bar()
{
Bar::baz();
}
}
Source https://phpmd.org/rules/cleancode.html#staticaccess
Avoid using static access to class 'App\Request' in method 'getRemoteIP'. Open
$remoteIp[] = 'X-Real-IP: ' . Request::_getServer('HTTP_X_REAL_IP');
- Read upRead up
- Exclude checks
StaticAccess
Since: 1.4.0
Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.
Example
class Foo
{
public function bar()
{
Bar::baz();
}
}
Source https://phpmd.org/rules/cleancode.html#staticaccess
Avoid using static access to class 'App\Request' in method 'getBrowserInfo'. Open
$browser->url = $protocol . '://' . $host . Request::_getServer('REQUEST_URI');
- Read upRead up
- Exclude checks
StaticAccess
Since: 1.4.0
Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.
Example
class Foo
{
public function bar()
{
Bar::baz();
}
}
Source https://phpmd.org/rules/cleancode.html#staticaccess
Avoid using static access to class '\App\Cache' in method 'getIpByName'. Open
return \App\Cache::get(__METHOD__, $name);
- Read upRead up
- Exclude checks
StaticAccess
Since: 1.4.0
Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.
Example
class Foo
{
public function bar()
{
Bar::baz();
}
}
Source https://phpmd.org/rules/cleancode.html#staticaccess
Avoid using static access to class '\App\Cache' in method 'getIpByName'. Open
if (\App\Cache::has(__METHOD__, $name)) {
- Read upRead up
- Exclude checks
StaticAccess
Since: 1.4.0
Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.
Example
class Foo
{
public function bar()
{
Bar::baz();
}
}
Source https://phpmd.org/rules/cleancode.html#staticaccess
Avoid using static access to class '\App\Request' in method 'getBrowserInfo'. Open
$browserAgent = strtolower(\App\Request::_getServer('HTTP_USER_AGENT', ''));
- Read upRead up
- Exclude checks
StaticAccess
Since: 1.4.0
Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.
Example
class Foo
{
public function bar()
{
Bar::baz();
}
}
Source https://phpmd.org/rules/cleancode.html#staticaccess
Avoid using static access to class 'App\Request' in method 'getBrowserInfo'. Open
$host = $host ?? Request::_getServer('SERVER_NAME') . $port;
- Read upRead up
- Exclude checks
StaticAccess
Since: 1.4.0
Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.
Example
class Foo
{
public function bar()
{
Bar::baz();
}
}
Source https://phpmd.org/rules/cleancode.html#staticaccess
Avoid using static access to class '\App\Cache' in method 'getIpByName'. Open
return \App\Cache::save(__METHOD__, $name, $ip);
- Read upRead up
- Exclude checks
StaticAccess
Since: 1.4.0
Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.
Example
class Foo
{
public function bar()
{
Bar::baz();
}
}
Source https://phpmd.org/rules/cleancode.html#staticaccess
Avoid using static access to class 'App\Request' in method 'getBrowserInfo'. Open
$sp = strtolower(Request::_getServer('SERVER_PROTOCOL'));
- Read upRead up
- Exclude checks
StaticAccess
Since: 1.4.0
Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.
Example
class Foo
{
public function bar()
{
Bar::baz();
}
}
Source https://phpmd.org/rules/cleancode.html#staticaccess
Avoid using static access to class 'App\Request' in method 'getBrowserInfo'. Open
$host = Request::_getServer('HTTP_X_FORWARDED_HOST', Request::_getServer('HTTP_HOST', ''));
- Read upRead up
- Exclude checks
StaticAccess
Since: 1.4.0
Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.
Example
class Foo
{
public function bar()
{
Bar::baz();
}
}
Source https://phpmd.org/rules/cleancode.html#staticaccess
Avoid using static access to class '\App\Config' in method 'isNetConnection'. Open
if (!\App\Config::performance('ACCESS_TO_INTERNET')) {
- Read upRead up
- Exclude checks
StaticAccess
Since: 1.4.0
Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.
Example
class Foo
{
public function bar()
{
Bar::baz();
}
}
Source https://phpmd.org/rules/cleancode.html#staticaccess
Variable $remoteIp
was undeclared, but array fields are being added to it. Open
$remoteIp[] = 'X-Real-IP: ' . Request::_getServer('HTTP_X_REAL_IP');
- Exclude checks
Property \App\RequestUtil::$browserCache
has undeclared type \App\stdClass
(Did you mean class \stdClass) Open
protected static $browserCache;
- Exclude checks
Assigning \stdClass
to property but \App\RequestUtil::$browserCache
is \App\stdClass
Open
self::$browserCache = $browser;
- Exclude checks
Returning type bool
but getIpByName()
is declared to return string
Open
return \App\Cache::save(__METHOD__, $name, $ip);
- Exclude checks
Returning type false
but getIpByName()
is declared to return string
Open
return false;
- Exclude checks
This branch's code block is the same as the block for the branch on line 91. Open
} elseif (false !== strpos($browserAgent, 'trident')) { //For Supporting IE 11
$browser->name = 'Internet explorer';
} elseif (false !== strpos($browserAgent, 'firefox')) {
- Read upRead up
- Exclude checks
Having two cases
in a switch
statement or two branches in an if
chain with the same implementation is at
best duplicate code, and at worst a coding error. If the same logic is truly needed for both instances, then in an if
chain they should
be combined, or for a switch
, one should fall through to the other.
Noncompliant Code Example
switch ($i) { case 1: doSomething(); break; case 2: doSomethingDifferent(); break; case 3: // Noncompliant; duplicates case 1's implementation doSomething(); break; default: doTheRest(); } if ($a >= 0 && $a < 10) { doTheThing(); else if ($a >= 10 && $a < 20) { doTheOtherThing(); } else if ($a >= 20 && $a < 50) { doTheThing(); // Noncompliant; duplicates first condition } else { doTheRest(); } if ($b == 0) { doOneMoreThing(); } else { doOneMoreThing(); // Noncompliant; duplicates then-branch } var b = a ? 12 > 4 : 4; // Noncompliant; always results in the same value
Compliant Solution
switch ($i) { case 1: case 3: doSomething(); break; case 2: doSomethingDifferent(); break; default: doTheRest(); } if (($a >= 0 && $a < 10) || ($a >= 20 && $a < 50)) { doTheThing(); else if ($a >= 10 && $a < 20) { doTheOtherThing(); } else { doTheRest(); } doOneMoreThing(); b = 4;
or
switch ($i) { case 1: doSomething(); break; case 2: doSomethingDifferent(); break; case 3: doThirdThing(); break; default: doTheRest(); } if ($a >= 0 && $a < 10) { doTheThing(); else if ($a >= 10 && $a < 20) { doTheOtherThing(); } else if ($a >= 20 && $a < 50) { doTheThirdThing(); } else { doTheRest(); } if ($b == 0) { doOneMoreThing(); } else { doTheRest(); } int b = a ? 12 > 4 : 8;
Exceptions
Blocks in an if
chain that contain a single line of code are ignored, as are blocks in a switch
statement that contain a
single line of code with or without a following break
.
Avoid variables with short names like $ip. Configured minimum length is 3. Open
$ip = gethostbyname($name);
- Read upRead up
- Exclude checks
ShortVariable
Since: 0.2
Detects when a field, local, or parameter has a very short name.
Example
class Something {
private $q = 15; // VIOLATION - Field
public static function main( array $as ) { // VIOLATION - Formal
$r = 20 + $this->q; // VIOLATION - Local
for (int $i = 0; $i < 10; $i++) { // Not a Violation (inside FOR)
$r += $this->q;
}
}
}
Source https://phpmd.org/rules/naming.html#shortvariable
Avoid variables with short names like $sp. Configured minimum length is 3. Open
$sp = strtolower(Request::_getServer('SERVER_PROTOCOL'));
- Read upRead up
- Exclude checks
ShortVariable
Since: 0.2
Detects when a field, local, or parameter has a very short name.
Example
class Something {
private $q = 15; // VIOLATION - Field
public static function main( array $as ) { // VIOLATION - Formal
$r = 20 + $this->q; // VIOLATION - Local
for (int $i = 0; $i < 10; $i++) { // Not a Violation (inside FOR)
$r += $this->q;
}
}
}
Source https://phpmd.org/rules/naming.html#shortvariable
Spaces must be used to indent lines; tabs are not allowed Open
/**
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @var string[]
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
/** @var string Cache request id variable. */
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
*/
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
/** @var bool Net connection cache. */
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
*
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
/** @var stdClass Browser cache variable. */
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* IP fields names variable.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
/** @var bool Cache https check variable. */
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
protected static $requestId;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
protected static $httpsCache;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
protected static $connectionCache;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
protected static $browserCache;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'HTTP_CF_CONNECTING_IP',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browser->unix = false !== strpos($browserAgent, 'unix');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browser->name = 'Mozilla Firefox';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
} elseif (preg_match('/(chrome|msie|version|khtml)(\s*|\/)([0-9.]+)/', $browserAgent, $regs)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browser->lang = $regs[1];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browser->url = $protocol . '://' . $host . Request::_getServer('REQUEST_URI');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public static function isNetConnection(): bool
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
*
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @return bool
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public static function isHttps(): bool
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @param string $name
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @return string
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$ip = gethostbyname($name);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public static function getRemoteIP($onlyIP = false)
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
// append the NGINX X-Real-IP header, if set
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browser->chrome = !$browser->opera && false !== strpos($browserAgent, 'chrome');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (preg_match('/ ([a-z]{2})-([a-z]{2})/', $browserAgent, $regs)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$host = $host ?? Request::_getServer('SERVER_NAME') . $port;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browser->siteUrl = $protocol . '://' . $host . $dirPath . '/';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* Check net connection.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (isset(self::$connectionCache)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
/**
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
*/
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browser->webkit = false !== strpos($browserAgent, 'applewebkit');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
} elseif (false !== strpos($browserAgent, 'opera')) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browser->name = 'Safari';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return false;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
*
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browser->name = 'Google Chrome';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($browser->opera) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browser->requestUri = ltrim(Request::_getServer('REQUEST_URI'), '/');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
/**
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* Get the IP address corresponding to a given Internet host name.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
*
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return false;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return empty($address) ? '' : $address;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* Get browser details.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @return object
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
*/
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
} elseif (false !== strpos($browserAgent, 'firefox')) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$dirPath = implode('/', $dirPath);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($ip === $name) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'HTTP_X_FORWARDED',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'HTTP_FORWARDED_FOR',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browser->linux = false !== strpos($browserAgent, 'linux');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browser->safari = !$browser->opera && !$browser->chrome && ($browser->webkit || false !== strpos($browserAgent, 'safari'));
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
self::$browserCache = $browser;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
*
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
{
- Exclude checks
Line exceeds 120 characters; contains 143 characters Open
self::$requestId = sprintf('%08x', abs(crc32($_SERVER['REMOTE_ADDR'] . $_SERVER['REQUEST_TIME_FLOAT'] . $_SERVER['REMOTE_PORT'])));
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return empty($address) ? '' : $address;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browser->ns = false !== strpos($browserAgent, 'netscape');
- Exclude checks
Line exceeds 120 characters; contains 148 characters Open
$browser->ie = !$browser->opera && (false !== strpos($browserAgent, 'compatible; msie') || false !== strpos($browserAgent, 'trident/'));
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browser->name = 'Internet explorer';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
} else {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
} else {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$port = isset($_SERVER['SERVER_PORT']) ? (int) $_SERVER['SERVER_PORT'] : 0;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return self::$browserCache;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @return bool
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public static function getIpByName(string $name): string
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($onlyIP) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach (self::$ipFields as $key) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public static function getBrowserInfo(): object
- Exclude checks
Line exceeds 120 characters; contains 136 characters Open
$browser->safari = !$browser->opera && !$browser->chrome && ($browser->webkit || false !== strpos($browserAgent, 'safari'));
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
} elseif (false !== strpos($browserAgent, 'safari')) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browser->ver = (float) $regs[3];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
/**
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (\App\Cache::has(__METHOD__, $name)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (!empty($_SERVER['HTTP_X_REAL_IP'])) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (empty(self::$browserCache)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
*/
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
|| (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && 'https' === strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']));
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$remoteIp[] = 'X-Real-IP: ' . Request::_getServer('HTTP_X_REAL_IP');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return \App\Cache::get(__METHOD__, $name);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
*
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
} elseif (false !== strpos($browserAgent, 'chrome')) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browser->ver = (float) $regs[2];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return self::$connectionCache;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @return string
- Exclude checks
Line exceeds 120 characters; contains 136 characters Open
$browser->opera = false !== strpos($browserAgent, 'opera') || ($browser->webkit && false !== strpos($browserAgent, 'opr/'));
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browser->mz = !$browser->ie && !$browser->safari && !$browser->chrome && !$browser->ns && !$browser->opera && false !== strpos($browserAgent, 'mozilla');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browser->name = 'Internet explorer';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* Check that the connection is https.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (!isset(self::$httpsCache)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return self::$httpsCache;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* Get request id.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
*/
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'HTTP_X_CLUSTER_CLIENT_IP',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
/**
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browser->win = false !== strpos($browserAgent, 'win');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browser->opera = false !== strpos($browserAgent, 'opera') || ($browser->webkit && false !== strpos($browserAgent, 'opr/'));
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$protocol = substr($sp, 0, strpos($sp, '/')) . (($browser->https) ? 's' : '');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (!\App\Config::performance('ACCESS_TO_INTERNET')) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
*/
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (!self::isNetConnection()) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'HTTP_X_FORWARDED_FOR',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$address = Request::_getServer('REMOTE_ADDR');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (!empty($remoteIp)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$address .= '(' . implode(',', $remoteIp) . ')';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browserAgent = strtolower(\App\Request::_getServer('HTTP_USER_AGENT', ''));
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browser->mac = false !== strpos($browserAgent, 'mac');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
} elseif (false !== strpos($browserAgent, 'trident')) { //For Supporting IE 11
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (preg_match('/(opera|opr)\/([0-9.]+)/', $browserAgent, $regs)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browser->https = self::isHttps();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$host = Request::_getServer('HTTP_X_FORWARDED_HOST', Request::_getServer('HTTP_HOST', ''));
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$dirPath = explode('/', Request::_getServer('SCRIPT_NAME'));
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
*
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'HTTP_CLIENT_IP',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$remoteIp[] = "$key: " . Request::_getServer($key);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browser->ver = 0;
- Exclude checks
Line exceeds 120 characters; contains 166 characters Open
$browser->mz = !$browser->ie && !$browser->safari && !$browser->chrome && !$browser->ns && !$browser->opera && false !== strpos($browserAgent, 'mozilla');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browser->name = 'Opera Mini';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browser->name = 'unknow';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
array_pop($dirPath);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
/**
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public static function requestId(): string
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
protected static $ipFields = [
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (isset($_SERVER[$key])) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$sp = strtolower(Request::_getServer('SERVER_PROTOCOL'));
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
self::$httpsCache = (!empty($_SERVER['HTTPS']) && 'off' !== strtolower($_SERVER['HTTPS']))
- Exclude checks
Line exceeds 120 characters; contains 126 characters Open
|| (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && 'https' === strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']));
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'HTTP_FORWARDED',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browser = new \stdClass();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browser->ie = !$browser->opera && (false !== strpos($browserAgent, 'compatible; msie') || false !== strpos($browserAgent, 'trident/'));
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (false !== strpos($browserAgent, 'msie')) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
} elseif (false !== strpos($browserAgent, 'opera mini')) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browser->name = 'Opera';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
} elseif (preg_match('/rv:([0-9.]+)/', $browserAgent, $regs)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browser->ver = (float) $regs[1];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$browser->lang = 'en';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$port = ((!$browser->https && 80 === $port) || ($browser->https && 443 === $port)) ? '' : ':' . $port;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return self::$connectionCache = 'www.google.com' !== gethostbyname('www.google.com');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$ip = '';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return \App\Cache::save(__METHOD__, $name, $ip);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (empty(self::$requestId)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return self::$requestId;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
self::$requestId = sprintf('%08x', abs(crc32($_SERVER['REMOTE_ADDR'] . $_SERVER['REQUEST_TIME_FLOAT'] . $_SERVER['REMOTE_PORT'])));
- Exclude checks