Showing 44 of 44 total issues
Avoid assigning values to variables in if clauses and the like (line '76', column '17'). Open
public function toArray(array $except = [], int $depth = 512)
{
if ($depth < 1) {
return false;
}
- Read upRead up
- Exclude checks
IfStatementAssignment
Since: 2.7.0
Assignments in if clauses and the like are considered a code smell. Assignments in PHP return the right operand as their result. In many cases, this is an expected behavior, but can lead to many difficult to spot bugs, especially when the right operand could result in zero, null or an empty string and the like.
Example
class Foo
{
public function bar($flag)
{
if ($foo = 'bar') { // possible typo
// ...
}
if ($baz = 0) { // always false
// ...
}
}
}
Source http://phpmd.org/rules/cleancode.html#ifstatementassignment
The method filter() has an NPath complexity of 217. The configured NPath complexity threshold is 200. Open
public static function filter(?string $value, array $options = []): ?\DateTime
{
static::convertConstants($options);
try {
- 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
Avoid unused local variables such as '$value'. Open
foreach (get_class_vars(get_class($this)) as $key => $value) {
- Read upRead up
- Exclude checks
UnusedLocalVariable
Since: 0.2
Detects when a local variable is declared and/or assigned, but not used.
Example
class Foo {
public function doSomething()
{
$i = 5; // Unused
}
}
Source https://phpmd.org/rules/unusedcode.html#unusedlocalvariable
The method filter() has a Cyclomatic Complexity of 12. The configured cyclomatic complexity threshold is 10. Open
public static function filter(?string $value, array $options = []): ?\DateTime
{
static::convertConstants($options);
try {
- 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
Multi-line function call not indented correctly; expected 12 spaces but found 16 Open
)
- Exclude checks
Opening brace should be on a new line Open
private function _getCustomSerializerMethod(string $property) {
- Exclude checks
Multi-line function call not indented correctly; expected 16 spaces but found 20 Open
array_map(function (ValidationException $exception): string {
- Exclude checks
Multi-line function call not indented correctly; expected 16 spaces but found 20 Open
"\n * ",
- Exclude checks
Opening brace should be on a new line Open
private function _getSerializedValue($value, int $depth, array $except) {
- Exclude checks
Closing parenthesis of a multi-line function call must be on a line by itself Open
is_object($providedValue) ? get_class($providedValue) : gettype($providedValue)),
- Exclude checks
Multi-line function call not indented correctly; expected 12 spaces but found 16 Open
is_object($providedValue) ? get_class($providedValue) : gettype($providedValue)),
- Exclude checks
The variable $_customSerializer is not named in camelCase. Open
private function _getCustomSerializerMethod(string $property) {
if (isset(self::$_customSerializer[$property])) {
return self::$_customSerializer[$property];
}
- Read upRead up
- Exclude checks
CamelCaseVariableName
Since: 0.2
It is considered best practice to use the camelCase notation to name variables.
Example
class ClassName {
public function doSomething() {
$data_module = new DataModule();
}
}
Source
The variable $_customSerializer is not named in camelCase. Open
private function _getCustomSerializerMethod(string $property) {
if (isset(self::$_customSerializer[$property])) {
return self::$_customSerializer[$property];
}
- Read upRead up
- Exclude checks
CamelCaseVariableName
Since: 0.2
It is considered best practice to use the camelCase notation to name variables.
Example
class ClassName {
public function doSomething() {
$data_module = new DataModule();
}
}
Source
The variable $_customSerializer is not named in camelCase. Open
private function _getCustomSerializerMethod(string $property) {
if (isset(self::$_customSerializer[$property])) {
return self::$_customSerializer[$property];
}
- Read upRead up
- Exclude checks
CamelCaseVariableName
Since: 0.2
It is considered best practice to use the camelCase notation to name variables.
Example
class ClassName {
public function doSomething() {
$data_module = new DataModule();
}
}
Source
The method _getSerializedValue is not named in camelCase. Open
private function _getSerializedValue($value, int $depth, array $except) {
if (is_array($value)) {
$subData = [];
foreach ($value as $subKey => $element) {
$subData[$subKey] = $this->evaluateAttribute($element, $depth, $except);
- Read upRead up
- Exclude checks
CamelCaseMethodName
Since: 0.2
It is considered best practice to use the camelCase notation to name methods.
Example
class ClassName {
public function get_name() {
}
}
Source
The method _getCustomSerializerMethod is not named in camelCase. Open
private function _getCustomSerializerMethod(string $property) {
if (isset(self::$_customSerializer[$property])) {
return self::$_customSerializer[$property];
}
- Read upRead up
- Exclude checks
CamelCaseMethodName
Since: 0.2
It is considered best practice to use the camelCase notation to name methods.
Example
class ClassName {
public function get_name() {
}
}
Source
Remove the unused function parameter "$except". Open
protected function resolveSerializationHook(array $data, int $depth, array $except): array
- Read upRead up
- Exclude checks
Unused parameters are misleading. Whatever the value passed to such parameters is, the behavior will be the same.
Noncompliant Code Example
function doSomething($a, $b) { // "$a" is unused return compute($b); }
Compliant Solution
function doSomething($b) { return compute($b); }
Exceptions
Functions in classes that override a class or implement interfaces are ignored.
class C extends B { function doSomething($a, $b) { // no issue reported on $b compute($a); } }
See
- MISRA C++:2008, 0-1-11 - There shall be no unused parameters (named or unnamed) in nonvirtual functions.
- MISRA C:2012, 2.7 - There should be no unused parameters in functions
- CERT, MSC12-C. - Detect and remove code that has no effect or is never executed
- CERT, MSC12-CPP. - Detect and remove code that has no effect
Define a constant instead of duplicating this literal "createFromFormat" 4 times. Open
if (($options['createFromFormat'] ?? false) && $value !== null) {
- Read upRead up
- Exclude checks
Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.
On the other hand, constants can be referenced from many places, but only need to be updated in a single place.
Noncompliant Code Example
With the default threshold of 3:
function run() { prepare('action1'); // Non-Compliant - 'action1' is duplicated 3 times execute('action1'); release('action1'); }
Compliant Solution
ACTION_1 = 'action1'; function run() { prepare(ACTION_1); execute(ACTION_1); release(ACTION_1); }
Exceptions
To prevent generating some false-positives, literals having less than 5 characters are excluded.
Remove the unused function parameter "$depth". Open
protected function resolveSerializationHook(array $data, int $depth, array $except): array
- Read upRead up
- Exclude checks
Unused parameters are misleading. Whatever the value passed to such parameters is, the behavior will be the same.
Noncompliant Code Example
function doSomething($a, $b) { // "$a" is unused return compute($b); }
Compliant Solution
function doSomething($b) { return compute($b); }
Exceptions
Functions in classes that override a class or implement interfaces are ignored.
class C extends B { function doSomething($a, $b) { // no issue reported on $b compute($a); } }
See
- MISRA C++:2008, 0-1-11 - There shall be no unused parameters (named or unnamed) in nonvirtual functions.
- MISRA C:2012, 2.7 - There should be no unused parameters in functions
- CERT, MSC12-C. - Detect and remove code that has no effect or is never executed
- CERT, MSC12-CPP. - Detect and remove code that has no effect
Reduce the number of returns of this function 4, down to the maximum allowed 3. Open
public static function filter(?string $value, array $options = []): ?\DateTime
- Read upRead up
- Exclude checks
Having too many return statements in a function increases the function's essential complexity because the flow of execution is broken each time a return statement is encountered. This makes it harder to read and understand the logic of the function.
Noncompliant Code Example
With the default threshold of 3:
function myFunction(){ // Noncompliant as there are 4 return statements if (condition1) { return true; } else { if (condition2) { return false; } else { return true; } } return false; }