phpinspectionsea icon indicating copy to clipboard operation
phpinspectionsea copied to clipboard

[FR] string library patterns

Open orklah opened this issue 4 years ago • 2 comments

Description (screenshot):

Hi,

We already match non-optimal patterns with substr. Here is a new one:

$var = 'déjà';
echo substr($var, 1, strlen($var)); // useless third param
echo mb_substr($var, 1, mb_strlen($var)); // useless third param

Furthermore, I saw this pattern at work today:

$var = 'déjà';
echo mb_substr($var, 1, strlen($var)); // string library mismatch
echo substr($var, 1, mb_strlen($var)); // string library mismatch

The risk here is to count with different library and to end up with weird strings in the end: https://3v4l.org/salLv

Thanks

orklah avatar Dec 09 '19 16:12 orklah

Hm, perhaps we should introduce a rule similar to Php Code Fixer's 'mb_str_functions': like here - https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/src/Fixer/Alias/MbStrFunctionsFixer.php, or it makes sense to target substring/strpos finctions only?

kalessil avatar Dec 10 '19 09:12 kalessil

Yeah, it would be cool to help migrate everything :)

orklah avatar Dec 10 '19 16:12 orklah