Define a constant instead of duplicating this literal "imagecolorize" 3 times. Open
$icon5 = $loader->imagecreatescaledcolourizedfromfile($iconFileName, 0, 0, $colour, 'imagecolorize');
- 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.
Define a constant instead of duplicating this literal "imagefilter" 3 times. Open
$icon7 = $loader->imagecreatescaledcolourizedfromfile($iconFileName, 0, 0, $colour, 'imagefilter');
- 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.
Identical sub-expressions on both sides of operator "==" Open
if (1 == 1) {
- Read upRead up
- Exclude checks
Using the same value on either side of a binary operator is almost always a mistake. In the case of logical operators, it is either a copy/paste error and therefore a bug, or it is simply wasted code, and should be simplified. In the case of bitwise operators and most binary mathematical operators, having the same value on both sides of an operator yields predictable results, and should be simplified.
This rule ignores *
, +
, and =
.
Noncompliant Code Example
if ( $a == $a ) { // always true doZ(); } if ( $a != $a ) { // always false doY(); } if ( $a == $b && $a == $b ) { // if the first one is true, the second one is too doX(); } if ( $a == $b || $a == $b ) { // if the first one is true, the second one is too doW(); } $j = 5 / 5; //always 1 $k = 5 - 5; //always 0
Exceptions
Left-shifting 1 onto 1 is common in the construction of bit masks, and is ignored.
$i = 1 << 1; // Compliant $j = $a << $a; // Noncompliant
See
- 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
- {rule:php:S1656} - Implements a check on
=
.
Identical sub-expressions on both sides of operator "==" Open
if (1 == 1) {
- Read upRead up
- Exclude checks
Using the same value on either side of a binary operator is almost always a mistake. In the case of logical operators, it is either a copy/paste error and therefore a bug, or it is simply wasted code, and should be simplified. In the case of bitwise operators and most binary mathematical operators, having the same value on both sides of an operator yields predictable results, and should be simplified.
This rule ignores *
, +
, and =
.
Noncompliant Code Example
if ( $a == $a ) { // always true doZ(); } if ( $a != $a ) { // always false doY(); } if ( $a == $b && $a == $b ) { // if the first one is true, the second one is too doX(); } if ( $a == $b || $a == $b ) { // if the first one is true, the second one is too doW(); } $j = 5 / 5; //always 1 $k = 5 - 5; //always 0
Exceptions
Left-shifting 1 onto 1 is common in the construction of bit masks, and is ignored.
$i = 1 << 1; // Compliant $j = $a << $a; // Noncompliant
See
- 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
- {rule:php:S1656} - Implements a check on
=
.