Skip to content

Commit

Permalink
Revert "Revert PHP7.4 - Add \"str_split\" => \"mb_str_split\" mapping"
Browse files Browse the repository at this point in the history
This reverts commit d35c431.
  • Loading branch information
keradus authored and SpacePossum committed Oct 9, 2019
1 parent de9b7c8 commit 3fea39f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
20 changes: 19 additions & 1 deletion src/Fixer/Alias/MbStrFunctionsFixer.php
Expand Up @@ -28,6 +28,7 @@ final class MbStrFunctionsFixer extends AbstractFunctionReferenceFixer
* @var array the list of the string-related function names and their mb_ equivalent
*/
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]],
Expand All @@ -42,6 +43,23 @@ final class MbStrFunctionsFixer extends AbstractFunctionReferenceFixer
'substr_count' => ['alternativeName' => 'mb_substr_count', 'argumentCount' => [2, 3, 4]],
];

/**
* @var array<string, array>
*/
private $functions;

public function __construct()
{
parent::__construct();

$this->functions = array_filter(
self::$functionsMap,
static function (array $mapping) {
return \function_exists($mapping['alternativeName']);
}
);
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -86,7 +104,7 @@ public function isCandidate(Tokens $tokens)
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
{
$argumentsAnalyzer = new ArgumentsAnalyzer();
foreach (self::$functionsMap as $functionIdentity => $functionReplacement) {
foreach ($this->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;
}
}

0 comments on commit 3fea39f

Please sign in to comment.