Skip to content

Commit 95eaf6f

Browse files
authored
[Php74] Fix MbStrrposEncodingArgumentPositionRector treating mixed $offset as encoding (#8913)
* [Php74] Fix MbStrrposEncodingArgumentPositionRector treating mixed $offset as encoding * use check * extract fixtures
1 parent 2a02e37 commit 95eaf6f

5 files changed

Lines changed: 91 additions & 9 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php74\Rector\FuncCall\MbStrrposEncodingArgumentPositionRector\Fixture;
4+
5+
class SkipAlreadySetEncodingNamedArg
6+
{
7+
public function run(string $text, string $encoding)
8+
{
9+
return mb_strrpos($text, 'abc', encoding: $encoding);
10+
}
11+
12+
public function runWithOffset(string $text, int $offset, string $encoding)
13+
{
14+
return mb_strrpos($text, 'abc', $offset, encoding: $encoding);
15+
}
16+
17+
public function runWithNamedOffset(string $text, int $offset, string $encoding)
18+
{
19+
return mb_strrpos($text, 'abc', encoding: $encoding, offset: $offset);
20+
}
21+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php74\Rector\FuncCall\MbStrrposEncodingArgumentPositionRector\Fixture;
4+
5+
class SkipMixedOffset
6+
{
7+
public function run($string, $needle, $offset = 0)
8+
{
9+
return mb_strrpos($string, $needle, $offset);
10+
}
11+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php74\Rector\FuncCall\MbStrrposEncodingArgumentPositionRector\Fixture;
4+
5+
class SkipSpreadOrDocblock
6+
{
7+
public function runSpread(array $args)
8+
{
9+
return mb_strrpos(...$args);
10+
}
11+
12+
public function runFirstClassCallable()
13+
{
14+
return mb_strrpos(...);
15+
}
16+
17+
/**
18+
* @param string $encoding
19+
*/
20+
public function runDocblock($text, $encoding)
21+
{
22+
return mb_strrpos($text, 'abc', $encoding);
23+
}
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php74\Rector\FuncCall\MbStrrposEncodingArgumentPositionRector\Fixture;
4+
5+
class StringEncodingVariable
6+
{
7+
public function run($text, string $encoding)
8+
{
9+
return mb_strrpos($text, 'abc', $encoding);
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\Php74\Rector\FuncCall\MbStrrposEncodingArgumentPositionRector\Fixture;
18+
19+
class StringEncodingVariable
20+
{
21+
public function run($text, string $encoding)
22+
{
23+
return mb_strrpos($text, 'abc', 0, $encoding);
24+
}
25+
}
26+
27+
?>

‎rules/Php74/Rector/FuncCall/MbStrrposEncodingArgumentPositionRector.php‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,23 @@ public function refactor(Node $node): ?Node
4949
return null;
5050
}
5151

52-
if (! isset($node->args[2])) {
52+
// encoding already passed, positionally or by name
53+
if ($node->getArg('encoding', 3) instanceof Arg) {
5354
return null;
5455
}
5556

56-
if (isset($node->args[3])) {
57+
$offsetArg = $node->getArg('offset', 2);
58+
if (! $offsetArg instanceof Arg) {
5759
return null;
5860
}
5961

60-
if (! $node->args[2] instanceof Arg) {
62+
// only string can be an encoding; mixed/unknown can be the int $offset
63+
$offsetArgType = $this->nodeTypeResolver->getNativeType($offsetArg->value);
64+
if (! $offsetArgType->isString()->yes()) {
6165
return null;
6266
}
6367

64-
$secondArgType = $this->getType($node->args[2]->value);
65-
if ($secondArgType->isInteger()->yes()) {
66-
return null;
67-
}
68-
69-
$node->args[3] = $node->args[2];
68+
$node->args[3] = $offsetArg;
7069
$node->args[2] = new Arg(new Int_(0));
7170

7271
return $node;

0 commit comments

Comments
 (0)