Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP7.4 - Add "str_split" => "mb_str_split" mapping. #4380

Merged
merged 1 commit into from Apr 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/Fixer/Alias/MbStrFunctionsFixer.php
Expand Up @@ -27,18 +27,19 @@ final class MbStrFunctionsFixer extends AbstractFunctionReferenceFixer
/**
* @var array the list of the string-related function names and their mb_ equivalent
*/
private static $functions = [
private static $functionsMap = [
'str_split' => ['alternativeName' => 'mb_str_split', 'argumentCount' => [1, 2, 3]],
'stripos' => ['alternativeName' => 'mb_stripos', 'argumentCount' => [2, 3]],
'stristr' => ['alternativeName' => 'mb_stristr', 'argumentCount' => [2, 3]],
'strlen' => ['alternativeName' => 'mb_strlen', 'argumentCount' => [1]],
'strpos' => ['alternativeName' => 'mb_strpos', 'argumentCount' => [2, 3]],
'strrchr' => ['alternativeName' => 'mb_strrchr', 'argumentCount' => [2]],
'strripos' => ['alternativeName' => 'mb_strripos', 'argumentCount' => [2, 3]],
'strrpos' => ['alternativeName' => 'mb_strrpos', 'argumentCount' => [2, 3]],
'substr' => ['alternativeName' => 'mb_substr', 'argumentCount' => [2, 3]],
'strstr' => ['alternativeName' => 'mb_strstr', 'argumentCount' => [2, 3]],
'strtolower' => ['alternativeName' => 'mb_strtolower', 'argumentCount' => [1]],
'strtoupper' => ['alternativeName' => 'mb_strtoupper', 'argumentCount' => [1]],
'stripos' => ['alternativeName' => 'mb_stripos', 'argumentCount' => [2, 3]],
'strripos' => ['alternativeName' => 'mb_strripos', 'argumentCount' => [2, 3]],
'strstr' => ['alternativeName' => 'mb_strstr', 'argumentCount' => [2, 3]],
'stristr' => ['alternativeName' => 'mb_stristr', 'argumentCount' => [2, 3]],
'strrchr' => ['alternativeName' => 'mb_strrchr', 'argumentCount' => [2]],
'substr' => ['alternativeName' => 'mb_substr', 'argumentCount' => [2, 3]],
'substr_count' => ['alternativeName' => 'mb_substr_count', 'argumentCount' => [2, 3, 4]],
];

Expand Down Expand Up @@ -85,8 +86,15 @@ public function isCandidate(Tokens $tokens)
*/
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
{
$functions = array_filter(
self::$functionsMap,
static function ($mapping) {
return \function_exists($mapping['alternativeName']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was causing the issue. when we execute this, it's always truth, as we have the mb polyfill on execution time...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ref #4529

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
);

$argumentsAnalyzer = new ArgumentsAnalyzer();
foreach (self::$functions as $functionIdentity => $functionReplacement) {
foreach ($functions as $functionIdentity => $functionReplacement) {
$currIndex = 0;
while (null !== $currIndex) {
// try getting function reference and translate boundaries for humans
Expand Down
15 changes: 14 additions & 1 deletion tests/Fixer/Alias/MbStrFunctionsFixerTest.php
Expand Up @@ -37,7 +37,7 @@ public function testFix($expected, $input = null)

public function provideFixCases()
{
return [
$cases = [
['<?php $x = "strlen";'],
['<?php $x = Foo::strlen("bar");'],
['<?php $x = new strlen("bar");'],
Expand All @@ -62,5 +62,18 @@ public function strtolower($a);
}',
],
];

if (\function_exists('mb_str_split')) {
$cases[] = [
'<?php $a = mb_str_split($a);',
'<?php $a = str_split($a);',
];
} else {
$cases[] = [
'<?php $a = str_split($a);',
];
}

return $cases;
}
}