Method findRotation
has 29 lines of code (exceeds 25 allowed). Consider refactoring. Open
function findRotation($row, $board, $solutions)
{
$tmp = rotateBoard($row, $board); // Rotated 90
if (!in_array($tmp, $solutions)) {
$solutions[] = $tmp;
Avoid too many return
statements within this method. Open
return 'd';
Avoid too many return
statements within this method. Open
return 'f';
Avoid too many return
statements within this method. Open
return 'g';
Avoid too many return
statements within this method. Open
return 'h';
Avoid too many return
statements within this method. Open
return 'e';
Reduce the number of returns of this function 9, down to the maximum allowed 3. Open
function numtoalpha($number)
- 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; }
Avoid using undefined variables such as '$tmp' which will lead to PHP notices. Open
$tmp[strlen(decbin($row[$checkRow])) - 1] = 1 << ($board - $checkRow - 1);
- 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
Remove error control operator '@' on line 142. Open
function nextPermutation($row)
{
$size = count($row) - 1;
// slide down the array looking for where we're smaller than the next guy
for ($i = $size - 1; @$row[$i] >= @$row[$i+1]; --$i) {
- Read upRead up
- Exclude checks
ErrorControlOperator
Error suppression should be avoided if possible as it doesn't just suppress the error, that you are trying to stop, but will also suppress errors that you didn't predict would ever occur. Consider changing error_reporting() level and/or setting up your own error handler.
Example
function foo($filePath) {
$file = @fopen($filPath); // hides exceptions
$key = @$array[$notExistingKey]; // assigns null to $key
}
Source http://phpmd.org/rules/cleancode.html#errorcontroloperator
Remove error control operator '@' on line 142. Open
function nextPermutation($row)
{
$size = count($row) - 1;
// slide down the array looking for where we're smaller than the next guy
for ($i = $size - 1; @$row[$i] >= @$row[$i+1]; --$i) {
- Read upRead up
- Exclude checks
ErrorControlOperator
Error suppression should be avoided if possible as it doesn't just suppress the error, that you are trying to stop, but will also suppress errors that you didn't predict would ever occur. Consider changing error_reporting() level and/or setting up your own error handler.
Example
function foo($filePath) {
$file = @fopen($filPath); // hides exceptions
$key = @$array[$notExistingKey]; // assigns null to $key
}
Source http://phpmd.org/rules/cleancode.html#errorcontroloperator
Move this "case default" clause to the end of this "switch" statement. Open
default:
- Read upRead up
- Exclude checks
The requirement for a final case default
clause is defensive programming. The clause should either take appropriate action, or contain
a suitable comment as to why no action is taken. Even when the switch
covers all current values of an enum
, a default case
should still be used because there is no guarantee that the enum
won't be extended.
Noncompliant Code Example
switch ($param) { //missing default clause case 0: do_something(); break; case 1: do_something_else(); break; } switch ($param) { default: // default clause should be the last one error(); break; case 0: do_something(); break; case 1: do_something_else(); break; }
Compliant Solution
switch ($param) { case 0: do_something(); break; case 1: do_something_else(); break; default: error(); break; }
See
- MISRA C:2004, 15.0 - The MISRA C switch syntax shall be used.
- MISRA C:2004, 15.3 - The final clause of a switch statement shall be the default clause
- MISRA C++:2008, 6-4-3 - A switch statement shall be a well-formed switch statement.
- MISRA C++:2008, 6-4-6 - The final clause of a switch statement shall be the default-clause
- MISRA C:2012, 16.1 - All switch statements shall be well-formed
- MISRA C:2012, 16.4 - Every switch statement shall have a default label
- MISRA C:2012, 16.5 - A default label shall appear as either the first or the last switch label of a switch statement
- MITRE, CWE-478 - Missing Default Case in Switch Statement
- CERT, MSC01-C. - Strive for logical completeness
- CERT, MSC01-CPP. - Strive for logical completeness
Merge this if statement with the enclosing one. Open
if (!in_array($row, $solutions)) {
- Read upRead up
- Exclude checks
Merging collapsible if
statements increases the code's readability.
Noncompliant Code Example
if (condition1) { if (condition2) { ... } }
Compliant Solution
if (condition1 && condition2) { ... }
Variable $row
was undeclared, but array fields are being added to it. Open
$row[$x] = 1 << $x;
- Exclude checks
Either remove or fill this block of code. Open
for ($i = $size - 1; @$row[$i] >= @$row[$i+1]; --$i) {
}
- Read upRead up
- Exclude checks
Most of the time a block of code is empty when a piece of code is really missing. So such empty block must be either filled or removed.
Noncompliant Code Example
for ($i = 0; $i < 42; $i++){} // Empty on purpose or missing piece of code ?
Exceptions
When a block contains a comment, this block is not considered to be empty.
Invalid operator: left operand of >>
is mixed
(expected int) Open
$ycheck = $row[$checkRow + $bitShift] >> $bitShift; // shift row[ checkRow + bitShift ], bitShift bits right
- Exclude checks
Invalid operator: left operand of <<
is mixed
(expected int) Open
$xcheck = $row[$checkRow + $bitShift] << $bitShift; // shift row[ checkRow + bitShift ], bitShift bits left
- Exclude checks
Returning type void
but numtoalpha()
is declared to return string
Open
return;
- Exclude checks
Variable $tmp
was undeclared, but array fields are being added to it. Open
$tmp[strlen(decbin($row[$checkRow])) - 1] = 1 << ($board - $checkRow - 1);
- Exclude checks
Argument 2 (board)
is 8
but \checkBoard()
takes array
defined at /code/src/solve8queens_2.php:171
Open
if (checkBoard($row, $board)) {
- Exclude checks
Invalid operator: left operand of -
is array
(expected number) Open
while ($bitShift < ($board - $checkRow)) {
- Exclude checks
Either remove or fill this block of code. Open
for ($j = $size; $row[$j] <= $row[$i]; --$j) {
}
- Read upRead up
- Exclude checks
Most of the time a block of code is empty when a piece of code is really missing. So such empty block must be either filled or removed.
Noncompliant Code Example
for ($i = 0; $i < 42; $i++){} // Empty on purpose or missing piece of code ?
Exceptions
When a block contains a comment, this block is not considered to be empty.