Showing 248 of 248 total issues
TODO found Open
// TODO: this whole function could probably be replaced with a single regex
- Exclude checks
TODO found Open
// TODO: write this, GitHub Issue #5
- Exclude checks
Define a constant instead of duplicating this literal "" 5 times. Open
print '</div>';
- 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.
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.
Variable $this
is undeclared Open
<a href="<?= $this->router->getHome(); ?>">Play 8 Queens</a>
- Exclude checks
Invalid operator: left operand of -
is array
(expected number) Open
while ($bitShift < ($board - $checkRow)) {
- Exclude checks
Cannot assign void return value Open
$start = startTimer();
- Exclude checks
Either remove this useless object instantiation of class "EightQueens" or use it Open
new EightQueens();
- Read upRead up
- Exclude checks
There is no good reason to create a new object to not do anything with it. Most of the time, this is due to a missing piece of code and so could lead to an unexpected behavior in production.
If it was done on purpose because the constructor has side-effects, then that side-effect code should be moved into a separate, static method and called directly.
Noncompliant Code Example
if ($x < 0) { new foo; // Noncompliant }
Compliant Solution
$var = NULL; if ($x < 0) { $var = new foo; }
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; }
Variable $this
is undeclared Open
| <a href="<?= $this->router->getHome(); ?>solutions/">Finding solutions in PHP</a>
- Exclude checks
Variable $this
is undeclared Open
| <a href="<?= $this->router->getHome(); ?>92/">The 92 solutions</a>
- 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
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
Variable $this
is undeclared Open
| <a href="<?= $this->router->getHome(); ?>about/">About the 8 Queens Puzzle</a>
- Exclude checks
Cannot assign void return value Open
$start = startTimer();
- 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
Variable $result
was undeclared, but array fields are being added to it. Open
$result[$name] = $value + $arrayB[$name];
- Exclude checks
Define a constant instead of duplicating this literal "<div style="border:1px solid black;padding:10px;height:300px;overflow:auto;">" 3 times.</div> Open
print '<div style="border:1px solid black;padding:10px;height:300px;overflow:auto;">';
- 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.
Refactor this function to reduce its Cognitive Complexity from 20 to the 15 allowed. Open
function queens_under_attack($board)
- 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
Returning type void
but numtoalpha()
is declared to return string
Open
return;
- Exclude checks