1.2.445. Failed Substr() Comparison¶
The extracted string must be of the size of the compared string.
This is also true for negative lengths. This rule raise a false positive when the variable is already smaller than the expected substr() results.
This rule doesn’t apply to mb_substr() and iconv_substr() : those functions use the character size, not the byte size.
<?php
// Possible comparison : strings and substr results are the same
if (substr($a, 0, 3) === 'abc') { }
if (substr($b, 4, 3) === 'abc') { }
// Always failing : substr will probably provide a longer string
if (substr($a, 0, 3) === 'ab') { }
if (substr($a, 3, -3) === 'ab') { }
// Omitted in this analysis
if (substr($a, 0, 3) !== 'ab') { }
?>
1.2.445.1. Connex PHP features¶
1.2.445.1.1. Suggestions¶
Fix the string
Fix the length of the string
Put the string in a constant, and use strlen() or mb_strlen()
Put the string in a constant, and use strlen() or mb_strlen()
1.2.445.1.2. Specs¶
Short name |
Structures/FailingSubstrComparison |
Rulesets |
|
Exakat since |
0.8.4 |
PHP Version |
All |
Severity |
Major |
Time To Fix |
Instant (5 mins) |
Precision |
Very high |
Examples |
|
Available in |