kalessil / phpinspectionsea

A Static Code Analyzer for PHP (a PhpStorm/Idea Plugin)
https://plugins.jetbrains.com/plugin/7622?pr=phpStorm
Other
1.44k stars 118 forks source link

[FR] string library patterns #1461

Open orklah opened 4 years ago

orklah commented 4 years ago

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

kalessil commented 4 years ago

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?

orklah commented 4 years ago

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